| 935 | jpm | 1 | package org.tela_botanica.client.modeles.projet;
 | 
        
           | 147 | gduche | 2 |   | 
        
           | 1045 | gduche | 3 | import java.util.HashMap;
 | 
        
           |  |  | 4 |   | 
        
           | 890 | aurelien | 5 | import org.tela_botanica.client.Mediateur;
 | 
        
           |  |  | 6 | import org.tela_botanica.client.RegistreId;
 | 
        
           | 751 | jpm | 7 | import org.tela_botanica.client.http.JsonRestRequestBuilder;
 | 
        
           |  |  | 8 | import org.tela_botanica.client.http.JsonRestRequestCallback;
 | 
        
           | 147 | gduche | 9 | import org.tela_botanica.client.interfaces.Rafraichissable;
 | 
        
           | 935 | jpm | 10 | import org.tela_botanica.client.modeles.Information;
 | 
        
           | 1045 | gduche | 11 | import org.tela_botanica.client.modeles.structure.StructureListe;
 | 
        
           | 1319 | gduche | 12 | import org.tela_botanica.client.synchronisation.Reponse;
 | 
        
           | 147 | gduche | 13 | import org.tela_botanica.client.util.UtilDAO;
 | 
        
           |  |  | 14 |   | 
        
           | 890 | aurelien | 15 | import com.extjs.gxt.ui.client.Registry;
 | 
        
           | 1080 | gduche | 16 | import com.extjs.gxt.ui.client.widget.Info;
 | 
        
           | 929 | jpm | 17 | import com.google.gwt.core.client.GWT;
 | 
        
           | 147 | gduche | 18 | import com.google.gwt.json.client.JSONArray;
 | 
        
           | 879 | aurelien | 19 | import com.google.gwt.json.client.JSONObject;
 | 
        
           | 147 | gduche | 20 | import com.google.gwt.json.client.JSONValue;
 | 
        
           |  |  | 21 |   | 
        
           | 268 | jp_milcent | 22 | public class ProjetAsyncDao {
 | 
        
           |  |  | 23 | 	private static final String SERVICE_NOM = "CoelProjet";
 | 
        
           | 147 | gduche | 24 |   | 
        
           | 890 | aurelien | 25 | 	String utilisateurId = null;
 | 
        
           | 751 | jpm | 26 | 	private Rafraichissable vueARafraichir = null;
 | 
        
           | 147 | gduche | 27 |   | 
        
           | 751 | jpm | 28 | 	public ProjetAsyncDao(Rafraichissable vueARafraichirCourrante) {
 | 
        
           | 1367 | cyprien | 29 | 		if (Mediateur.DEBUG) System.out.println("|| ProjetAsyncDao > vueARafraichir = "+vueARafraichirCourrante.getClass().toString());
 | 
        
           | 751 | jpm | 30 | 		vueARafraichir = vueARafraichirCourrante;
 | 
        
           | 890 | aurelien | 31 | 		utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId();
 | 
        
           | 147 | gduche | 32 | 	}
 | 
        
           | 1329 | cyprien | 33 |   | 
        
           | 147 | gduche | 34 |   | 
        
           | 1329 | cyprien | 35 | 	/**
 | 
        
           |  |  | 36 | 	 * @param paginationProgressive : définit le mode de consultation de la base de données
 | 
        
           |  |  | 37 | 	 * 			- True :	la consultation des données est progressive, ce qui signifie que la liste est chargée (paginée) au
 | 
        
           |  |  | 38 | 	 * 						fur et à mesure de la consultation des données par l'utilisateur.
 | 
        
           |  |  | 39 | 	 * 			- False :	la consultation des données est classique : un seul appel à la base de données est effectué, le retour
 | 
        
           |  |  | 40 | 	 * 						est renvoyé à l'appelant
 | 
        
           |  |  | 41 | 	 * 			// FIXME : si la taille de la liste est supérieure à la limite du JREST (150), ce deuxieme mode ne fonctionne pas
 | 
        
           |  |  | 42 | 	 */
 | 
        
           |  |  | 43 | 	public void selectionner(final boolean paginationProgressive, final String projetId, final String nomProjet, final int start, final int nbElements, final Integer seqId) {
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 | 		String nom = (nomProjet == null) ? "%" : nomProjet+"%";
 | 
        
           | 1045 | gduche | 46 | 		String[] param = {projetId, nom};
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 | 		HashMap<String, String> restrictions = new HashMap<String, String>();
 | 
        
           | 1329 | cyprien | 49 |   | 
        
           | 1045 | gduche | 50 | 		if (nbElements != -1)	{
 | 
        
           |  |  | 51 | 			restrictions.put("limit", String.valueOf(nbElements));
 | 
        
           |  |  | 52 | 		}
 | 
        
           |  |  | 53 |   | 
        
           | 1329 | cyprien | 54 | 		/** GESTION DE LA REQUETE dans le cas d'une liste paginée progressive **/
 | 
        
           |  |  | 55 | 		if (paginationProgressive) {
 | 
        
           |  |  | 56 |   | 
        
           |  |  | 57 | 			/** DEFINITION DU TUPLE DE DEPART **/
 | 
        
           |  |  | 58 | 			restrictions.put("start", String.valueOf(start));
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 | 			/** CONSTRUCTION DE LA REQUETE **/
 | 
        
           |  |  | 61 |     		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, param, restrictions);
 | 
        
           |  |  | 62 |   | 
        
           |  |  | 63 |     		/** ENVOI DE LA REQUETE **/
 | 
        
           |  |  | 64 |     		rb.envoyerRequete(null, new JsonRestRequestCallback()
 | 
        
           |  |  | 65 |     		{
 | 
        
           |  |  | 66 |     			/** RECEPTION DE LA REPONSE **/
 | 
        
           |  |  | 67 |     			public void surReponse(JSONValue responseValue)
 | 
        
           |  |  | 68 |     			{
 | 
        
           |  |  | 69 |     				/** Dans le cas d'une liste paginée, vueARafraichir est un objet Proxy.
 | 
        
           |  |  | 70 |     				 * On retourne l'objet JSON au proxy afin que ce soit lui qui le traite **/
 | 
        
           | 1367 | cyprien | 71 | 					if (seqId != null)	{
 | 
        
           |  |  | 72 | 						Reponse reponseRequete = new Reponse(responseValue, seqId);
 | 
        
           |  |  | 73 | 						vueARafraichir.rafraichir(reponseRequete);
 | 
        
           |  |  | 74 | 					}
 | 
        
           |  |  | 75 | 					else	{
 | 
        
           |  |  | 76 | 						vueARafraichir.rafraichir(responseValue);
 | 
        
           |  |  | 77 | 					}
 | 
        
           | 1329 | cyprien | 78 |     			}
 | 
        
           |  |  | 79 |     		});
 | 
        
           |  |  | 80 | 		}
 | 
        
           |  |  | 81 | 		/** GESTION DE LA REQUETE dans le cas d'une liste NON paginée progressive **/
 | 
        
           |  |  | 82 | 		else {
 | 
        
           |  |  | 83 |   | 
        
           |  |  | 84 | 			/** DEFINITION DU TUPLE DE DEPART **/
 | 
        
           |  |  | 85 | 			restrictions.put("start", String.valueOf(start*nbElements));
 | 
        
           |  |  | 86 |   | 
        
           |  |  | 87 | 			final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, param, restrictions);
 | 
        
           |  |  | 88 |   | 
        
           |  |  | 89 | 			rb.envoyerRequete(null, new JsonRestRequestCallback() {
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 | 				public void surReponse(JSONValue responseValue) {
 | 
        
           |  |  | 92 |   | 
        
           |  |  | 93 | 					if (responseValue != null) {
 | 
        
           | 929 | jpm | 94 |   | 
        
           | 1329 | cyprien | 95 | 						JSONObject responseObject = responseValue.isObject();
 | 
        
           | 1045 | gduche | 96 |   | 
        
           | 1329 | cyprien | 97 | 						if (responseObject != null) {
 | 
        
           |  |  | 98 |   | 
        
           |  |  | 99 | 							// Si la réponse est un tableau, alors c'est une liste de projets qui a été retournée
 | 
        
           |  |  | 100 | 							if (responseObject.get("projets").isArray() != null) {
 | 
        
           |  |  | 101 |   | 
        
           |  |  | 102 | 								JSONArray reponse = responseObject.get("projets").isArray();
 | 
        
           |  |  | 103 | 								ProjetListe projets;
 | 
        
           |  |  | 104 | 								projets = new ProjetListe(reponse, responseObject.get("nbElements").isNumber(), vueARafraichir);
 | 
        
           |  |  | 105 | 								projets.setTaillePage(nbElements);
 | 
        
           |  |  | 106 | 								projets.setPageCourante(start);
 | 
        
           |  |  | 107 |   | 
        
           |  |  | 108 | 								if (seqId != null)	{
 | 
        
           |  |  | 109 | 									Reponse retourRequete = new Reponse(projets, seqId);
 | 
        
           |  |  | 110 | 									vueARafraichir.rafraichir(retourRequete);
 | 
        
           |  |  | 111 | 								} else {
 | 
        
           |  |  | 112 | 									vueARafraichir.rafraichir(projets);
 | 
        
           |  |  | 113 | 								}
 | 
        
           |  |  | 114 |   | 
        
           |  |  | 115 | 							// Si la réponse est un objet, alors c'est un unique projet qui a été retournée
 | 
        
           |  |  | 116 | 							} else if (responseObject.get("projets").isObject() != null) {
 | 
        
           |  |  | 117 |   | 
        
           |  |  | 118 | 								JSONObject reponse = responseObject.get("projets").isObject();
 | 
        
           |  |  | 119 | 								Projet projet = new Projet(reponse);
 | 
        
           |  |  | 120 |   | 
        
           |  |  | 121 | 								Information info = new Information("selection_projet");
 | 
        
           |  |  | 122 | 								info.setDonnee(0, projet);
 | 
        
           | 1367 | cyprien | 123 |   | 
        
           |  |  | 124 | 								if (seqId != null)	{
 | 
        
           |  |  | 125 | 									Reponse reponseRequete = new Reponse(info, seqId);
 | 
        
           |  |  | 126 | 									vueARafraichir.rafraichir(reponseRequete);
 | 
        
           |  |  | 127 | 								}
 | 
        
           |  |  | 128 | 								else	{
 | 
        
           |  |  | 129 | 									vueARafraichir.rafraichir(info);
 | 
        
           |  |  | 130 | 								}
 | 
        
           | 1329 | cyprien | 131 | 							}
 | 
        
           | 1319 | gduche | 132 | 						} else {
 | 
        
           | 1329 | cyprien | 133 | 							GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas un objet ou un tableau JSON et vaut : "+responseValue.toString(), null);
 | 
        
           | 1319 | gduche | 134 | 						}
 | 
        
           | 929 | jpm | 135 | 					} else {
 | 
        
           | 1329 | cyprien | 136 | 						// Dans le cas, où nous demandons tous les projets et qu'il n'y en a pas, nous retournons un objet vide
 | 
        
           |  |  | 137 | 						if (projetId == null) {
 | 
        
           |  |  | 138 | 							ProjetListe projets = new ProjetListe(0);
 | 
        
           | 1367 | cyprien | 139 | 							if (seqId != null)	{
 | 
        
           |  |  | 140 | 								Reponse reponseRequete = new Reponse(projets, seqId);
 | 
        
           |  |  | 141 | 								vueARafraichir.rafraichir(reponseRequete);
 | 
        
           |  |  | 142 | 							}
 | 
        
           |  |  | 143 | 							else	{
 | 
        
           |  |  | 144 | 								vueARafraichir.rafraichir(projets);
 | 
        
           |  |  | 145 | 							}
 | 
        
           | 1329 | cyprien | 146 | 						}
 | 
        
           | 929 | jpm | 147 | 					}
 | 
        
           | 147 | gduche | 148 | 				}
 | 
        
           | 1329 | cyprien | 149 | 			});
 | 
        
           |  |  | 150 | 		}
 | 
        
           | 879 | aurelien | 151 | 	}
 | 
        
           | 890 | aurelien | 152 |   | 
        
           |  |  | 153 | 	public void ajouter(Projet projet) {
 | 
        
           |  |  | 154 | 		String postDonneesEncodees = projet.obtenirChainePOST()+"&cmhl_ce_modifier_par="+utilisateurId;
 | 
        
           |  |  | 155 |   | 
        
           |  |  | 156 | 		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM);
 | 
        
           |  |  | 157 | 		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
 | 
        
           |  |  | 158 | 			@Override
 | 
        
           |  |  | 159 | 			public void surReponse(JSONValue reponseValeur) {
 | 
        
           | 929 | jpm | 160 | 				traiterReponse(reponseValeur, "ajout_projet");
 | 
        
           | 890 | aurelien | 161 | 			}
 | 
        
           |  |  | 162 | 		}) ;
 | 
        
           |  |  | 163 | 	}
 | 
        
           |  |  | 164 |   | 
        
           |  |  | 165 | 	public void modifier(Projet projet) {
 | 
        
           |  |  | 166 | 		String[] parametres = {projet.getId()};
 | 
        
           |  |  | 167 | 		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
 | 
        
           |  |  | 168 |   | 
        
           |  |  | 169 | 		String postDonneesEncodees = projet.obtenirChainePOST()+"&cmhl_ce_modifier_par="+utilisateurId;
 | 
        
           |  |  | 170 |   | 
        
           |  |  | 171 | 		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
 | 
        
           |  |  | 172 | 			@Override
 | 
        
           |  |  | 173 | 			public void surReponse(JSONValue reponseValeur) {
 | 
        
           | 929 | jpm | 174 | 				traiterReponse(reponseValeur, "modif_projet");
 | 
        
           | 890 | aurelien | 175 | 			}
 | 
        
           |  |  | 176 | 		});
 | 
        
           |  |  | 177 | 	}
 | 
        
           |  |  | 178 |   | 
        
           |  |  | 179 | 	public void supprimer(String projetsId) {
 | 
        
           |  |  | 180 | 		String[] parametres = {utilisateurId, projetsId};
 | 
        
           |  |  | 181 | 		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
 | 
        
           |  |  | 182 | 		rb.envoyerRequeteSuppression(new JsonRestRequestCallback() {
 | 
        
           |  |  | 183 | 			@Override
 | 
        
           |  |  | 184 | 			public void surReponse(JSONValue reponseValeur) {
 | 
        
           |  |  | 185 | 				traiterReponse(reponseValeur, "suppression_projet");
 | 
        
           |  |  | 186 | 			}
 | 
        
           |  |  | 187 | 		});
 | 
        
           |  |  | 188 | 	}
 | 
        
           |  |  | 189 |   | 
        
           |  |  | 190 | 	private void traiterReponse(JSONValue reponseValeur, String type) {
 | 
        
           |  |  | 191 | 		Information info = new Information(type);
 | 
        
           | 1080 | gduche | 192 | 		String idsNonSuppr = "";
 | 
        
           |  |  | 193 |   | 
        
           | 890 | aurelien | 194 | 		// Si la requête est un succès, réception d'une chaîne
 | 
        
           | 1080 | gduche | 195 | 		if (type.equals("suppression_projet") && reponseValeur.isArray() != null)	{
 | 
        
           |  |  | 196 | 			JSONArray tableauInfo = reponseValeur.isArray();
 | 
        
           |  |  | 197 | 			idsNonSuppr = tableauInfo.get(0).isString().stringValue();
 | 
        
           |  |  | 198 | 		} else if (reponseValeur.isString() != null) {
 | 
        
           | 1103 | jpm | 199 | 			String idOuMessage = reponseValeur.isString().stringValue();
 | 
        
           |  |  | 200 | 			if (idOuMessage.matches("^[0-9]+$")) {
 | 
        
           |  |  | 201 | 				info.setDonnee(idOuMessage);
 | 
        
           |  |  | 202 | 			} else {
 | 
        
           |  |  | 203 | 				info.setDonnee("");
 | 
        
           |  |  | 204 | 				info.setMessage(idOuMessage);
 | 
        
           |  |  | 205 | 			}
 | 
        
           | 890 | aurelien | 206 | 		} else {
 | 
        
           |  |  | 207 | 			info.setDeboguage("La réponse n'est pas une chaine JSON.");
 | 
        
           |  |  | 208 | 		}
 | 
        
           | 1080 | gduche | 209 | 		info.setDonnee(1, idsNonSuppr);
 | 
        
           | 890 | aurelien | 210 | 		vueARafraichir.rafraichir(info);
 | 
        
           |  |  | 211 | 	}
 | 
        
           | 151 | jpm | 212 | }
 |