--- /dev/null
+package com.pentila.emawsconnector.dao;
+
+
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+import com.pentila.emawsconnector.utils.EmaWSConnector;
+import com.pentila.evalcomp.domain.definition.Referentiel;
+import com.pentila.evalcomp.domain.plan.Plan;
+import com.pentila.evalcomp.service.ws.WsErrors;
+
+
+public class PlanDAO {
+
+
+ /**
+ * Obtenir la liste des plans de formation
+ * @return retourne la liste des plans de formation existants.
+ */
+ public List<Plan> getPlans(){
+ List<Plan> result = new ArrayList<Plan>();
+ result = EmaWSConnector.read("plan/getAll?login=" + EmaWSConnector.getLogin(), result.getClass());
+ return result;
+ }
+
+ /**
+ * Obtenir un plan de formation existant.
+ * @param id identifiant du plan de formation
+ * @return retourne le plan
+ */
+ public Plan getPlan(Long id){
+ try{
+ Plan result = new Plan();
+ result = EmaWSConnector.read("plan/getPlan?id="+id+
+ "&login=" + EmaWSConnector.getLogin(), result.getClass());
+ return result;
+ }
+ catch (Exception e){
+ Integer error = EmaWSConnector.read("plan/getPlan?id="+id+
+ "&login=" + EmaWSConnector.getLogin(), Integer.class);
+ System.out.println("erreur : " + error);
+ return null;
+ }
+ }
+
+ /**
+ * Créer un nouveau plan de formation
+ * @param nom nom du plan de formation
+ * @param organisation organisation (ou établissement)
+ * @param ville ville dans laquelle est présente l'organisation
+ * @return retourne le plan de formation crée (null en cas d'erreur)
+ */
+ public Plan addPlan(String nom, String organisation, String ville){
+ try {
+ nom = java.net.URLEncoder.encode(nom, "UTF-8");
+ organisation = java.net.URLEncoder.encode(organisation, "UTF-8");
+ ville = java.net.URLEncoder.encode(ville, "UTF-8");
+ }catch (UnsupportedEncodingException e) {
+ //e.printStackTrace();
+ return null;
+ }
+
+ Long result = (long) -1;
+ result = EmaWSConnector.read("plan/addPlan?nom="+nom+
+ "&organisation="+organisation+
+ "&ville="+ville+
+ "&login=" + EmaWSConnector.getLogin(), result.getClass());
+
+ if(result != null && result >= 0){
+ return getPlan(result);
+ }
+ else{
+ return null;
+ }
+ }
+
+ /**
+ * Ajoute un référentiel d'un plan.
+ * @param plan le plan de formation
+ * @param referentiel le référentiel
+ * @return retourne le plan mis à jour, null en cas d'erreur
+ */
+ public Plan addReferentiel(Plan plan, Referentiel referentiel){
+
+ Integer result = EmaWSConnector.read("plan/updateReferentiel?plan="+plan.getId()+
+ "&referentiel="+referentiel.getId()+
+ "&action=add"+
+ "&login="+EmaWSConnector.getLogin(), Integer.class);
+ if(result != null && result == WsErrors.WS_OK){
+ return getPlan(plan.getId());
+ }
+ else{
+ return null;
+ }
+ }
+
+ /**
+ * supprime un référentiel d'un plan.
+ * @param plan le plan de formation
+ * @param referentiel le référentiel
+ * @return retourne le plan mis à jour, null en cas d'erreur
+ */
+ public Plan removeReferentiel(Plan plan, Referentiel referentiel){
+ Integer result = EmaWSConnector.read("plan/updateReferentiel?plan="+plan.getId()+
+ "&referentiel="+referentiel.getId()+
+ "&action=remove"+
+ "&login="+EmaWSConnector.getLogin(), Integer.class);
+ if(result != null && result == WsErrors.WS_OK){
+ return getPlan(plan.getId());
+ }
+ else{
+ return null;
+ }
+ }
+
+ public Plan updateEntities(Long planID, Set<String> logins,
+ String action, String role) {
+ String entities = "";
+
+ for(String s : logins){
+ entities += s;
+ entities += ",";
+ }
+
+ entities = entities.substring(0, entities.length()-1);
+
+ Integer result = EmaWSConnector.read("plan/updateEntities?plan="+planID+
+ "&entities="+entities+
+ "&role="+role+
+ "&action="+action+
+ "&login="+EmaWSConnector.getLogin(), Integer.class);
+
+ if(result != null && result == WsErrors.WS_OK){
+ return getPlan(planID);
+ }
+ else{
+ return null;
+ }
+
+ }
+
+}