--- /dev/null
+package com.pentila.entSavoie.dashboard.widgets.pronote;
+
+import com.index_education.accueilent.eleve.TypeAbsence;
+import com.index_education.accueilent.eleve.TypeDevoir;
+import com.index_education.accueilent.eleve.TypeEleve;
+import com.index_education.accueilent.eleve.TypeObservation;
+import com.index_education.accueilent.eleve.TypePageReleveDeNotes;
+import com.index_education.accueilent.eleve.TypePageVieScolaire;
+import com.index_education.accueilent.eleve.TypePassageInfirmerie;
+import com.index_education.accueilent.eleve.TypePunition;
+import com.index_education.accueilent.eleve.TypeRetard;
+import com.index_education.accueilent.eleve.TypeSanction;
+import com.liferay.portal.kernel.json.JSONArray;
+import com.liferay.portal.kernel.json.JSONFactoryUtil;
+import com.liferay.portal.kernel.json.JSONObject;
+import com.liferay.portal.kernel.log.Log;
+import com.liferay.portal.kernel.log.LogFactoryUtil;
+
+public class JSONProxyStudentUtil {
+
+ private static Log logger = LogFactoryUtil.getLog(JSONProxyStudentUtil.class);
+
+ public static JSONObject convertTypeEleveToJSON(TypeEleve student) {
+
+ // Json structure is {
+ // "evaluations" : [ {"note": "17", "subject": "math", "date": "yyyy-MM-dd", "scale": "20" } ],
+ // "absences": [ {"startDate": "2017-01-15", "endDate: "2017-01-18", "isOpened": true, "isJustified": true, "reason": "Malade"} ],
+ // "lateness": [ {"date": "2017-01-22", "isJustified": true, "reason", "bus"} ],
+ // "infirmary": [ {"date": "2017-01-03"} ],
+ // "punishments": [ {"date": "2017-01-10", "nature", "lignes", "subject": "maths", "reason": "blabla", "circumstances", "dsdv"} ],
+ // "sanctions": [ {"date": "2017-01-10", "nature", "lignes", "reason": "blabla", "circumstances", "dsdv", "length": "3"} ],
+ // "observations": [ {"date": "2017-01-12", "origin": "Mr Lacoste", "observation": "dsdsvvd", "subject": "maths"} ],
+ // }
+
+ JSONObject studentData = JSONFactoryUtil.createJSONObject();
+ if (student == null) {
+ studentData.put("success", false);
+ return studentData;
+ }
+ studentData.put("success", true);
+
+ // Evaluations
+ JSONArray evaluations = JSONFactoryUtil.createJSONArray();
+ TypePageReleveDeNotes releve = student.getPageReleveDeNotes();
+ if (releve != null) {
+ for (TypeDevoir devoir : releve.getDevoir()) {
+ JSONObject eval = convertEval(devoir);
+ evaluations.put(eval);
+ }
+ }
+ studentData.put("evaluations", evaluations);
+ logger.debug("Converted "+evaluations.length()+" evaluations");
+
+ // Vie scolaire
+ TypePageVieScolaire schoolLife = student.getPageVieScolaire();
+ if (schoolLife != null) {
+
+ // Absences
+ JSONArray absences = JSONFactoryUtil.createJSONArray();
+ if (schoolLife.getAbsence() != null) {
+
+ for (TypeAbsence absence : schoolLife.getAbsence()) {
+ JSONObject absenceJson = convertAbsence(absence);
+ absences.put(absenceJson);
+ }
+ }
+ studentData.put("absences", absences);
+ logger.debug("Converted "+absences.length()+" absences");
+
+ // Lateness
+ JSONArray latenessArray = JSONFactoryUtil.createJSONArray();
+ if (schoolLife.getRetard() != null) {
+
+ for (TypeRetard retard : schoolLife.getRetard()) {
+ JSONObject latenessJson = convertRetard(retard);
+ latenessArray.put(latenessJson);
+ }
+ }
+ studentData.put("lateness", latenessArray);
+ logger.debug("Converted "+latenessArray.length()+" lateness");
+
+ // Infirmary
+ JSONArray infirmaryArray = JSONFactoryUtil.createJSONArray();
+ if (schoolLife.getPassageInfirmerie() != null) {
+
+ for (TypePassageInfirmerie infirmary : schoolLife.getPassageInfirmerie()) {
+ JSONObject infirmaryJson = convertInfirmary(infirmary);
+ infirmaryArray.put(infirmaryJson);
+ }
+ }
+ studentData.put("infirmary", infirmaryArray);
+ logger.debug("Converted "+infirmaryArray.length()+" infirmary");
+
+ // Punishment
+ JSONArray punishmentArray = JSONFactoryUtil.createJSONArray();
+ if (schoolLife.getPunition() != null) {
+
+ for (TypePunition punishment : schoolLife.getPunition()) {
+ JSONObject punishmentJson = convertPunishment(punishment);
+ punishmentArray.put(punishmentJson);
+ }
+ }
+ studentData.put("punishments", punishmentArray);
+ logger.debug("Converted "+punishmentArray.length()+" punishments");
+
+ // Sanctions
+ JSONArray sanctionsArray = JSONFactoryUtil.createJSONArray();
+ if (schoolLife.getSanction() != null) {
+
+ for (TypeSanction sanction : schoolLife.getSanction()) {
+ JSONObject sanctionJson = convertSanction(sanction);
+ sanctionsArray.put(sanctionJson);
+ }
+ }
+ studentData.put("sanctions", sanctionsArray);
+ logger.debug("Converted "+sanctionsArray.length()+" sanctions");
+
+ // Observations
+ JSONArray observationsArray = JSONFactoryUtil.createJSONArray();
+ if (schoolLife.getObservation() != null) {
+
+ for (TypeObservation observation : schoolLife.getObservation()) {
+ JSONObject observationJson = convertObservation(observation);
+ observationsArray.put(observationJson);
+ }
+ }
+ studentData.put("observations", observationsArray);
+ logger.debug("Converted "+observationsArray.length()+" observations");
+
+ }
+
+ return studentData;
+ }
+
+ private static JSONObject convertEval (TypeDevoir devoir) {
+ JSONObject eval = JSONFactoryUtil.createJSONObject();
+ eval.put("note", PronoteUtil.shortenNote(devoir.getNote()));
+ eval.put("subject", devoir.getMatiere());
+ eval.put("date", devoir.getDate().toXMLFormat());
+ eval.put("scale", devoir.getBareme());
+ eval.put("color", PronoteUtil.getEvaluationColor(devoir.getNote(), devoir.getBareme()));
+ return eval;
+ }
+
+ private static JSONObject convertAbsence (TypeAbsence absence) {
+ JSONObject absenceJson = JSONFactoryUtil.createJSONObject();
+ absenceJson.put("startDate", absence.getDateDebut().toString());
+ absenceJson.put("endDate", absence.getDateFin().toString());
+ absenceJson.put("isOpened", absence.isEstOuverte());
+ absenceJson.put("isJustified", absence.isJustifie());
+ absenceJson.put("reason", absence.getMotif());
+ return absenceJson;
+ }
+
+ private static JSONObject convertRetard (TypeRetard retard) {
+ JSONObject lateJson = JSONFactoryUtil.createJSONObject();
+ lateJson.put("date", retard.getDate().toString());
+ lateJson.put("reason", retard.getMotif());
+ lateJson.put("isJustified", retard.isJustifie());
+ return lateJson;
+ }
+
+ private static JSONObject convertInfirmary (TypePassageInfirmerie infirmary) {
+ JSONObject infirmaryJson = JSONFactoryUtil.createJSONObject();
+ infirmaryJson.put("date", infirmary.getDate().toString());
+ return infirmaryJson;
+ }
+
+ private static JSONObject convertPunishment (TypePunition punishment) {
+ JSONObject punishmentJson = JSONFactoryUtil.createJSONObject();
+ punishmentJson.put("date", punishment.getDate().toString());
+ punishmentJson.put("nature", punishment.getNature());
+ punishmentJson.put("subject", punishment.getMatiere());
+ punishmentJson.put("reason", punishment.getMotif());
+ punishmentJson.put("circumstances", punishment.getCirconstances());
+ return punishmentJson;
+ }
+
+ private static JSONObject convertSanction (TypeSanction sanction) {
+ JSONObject sanctionJson = JSONFactoryUtil.createJSONObject();
+ sanctionJson.put("date", sanction.getDate().toString());
+ sanctionJson.put("nature", sanction.getNature());
+ sanctionJson.put("length", sanction.getDuree().toString());
+ sanctionJson.put("reason", sanction.getMotif());
+ sanctionJson.put("circumstances", sanction.getCirconstances());
+ return sanctionJson;
+ }
+
+ private static JSONObject convertObservation (TypeObservation observation) {
+ JSONObject observationJson = JSONFactoryUtil.createJSONObject();
+ observationJson.put("date", observation.getDate().toString());
+ observationJson.put("origin", observation.getDemandeur());
+ observationJson.put("subject", observation.getMatiere());
+ observationJson.put("observation", observation.getObservation());
+ return observationJson;
+ }
+
+
+
+}