| 935 | jpm | 1 | package org.tela_botanica.client.modeles.personne;
 | 
        
           | 351 | gduche | 2 |   | 
        
           | 593 | gduche | 3 | import java.util.HashMap;
 | 
        
           |  |  | 4 |   | 
        
           | 317 | gduche | 5 | import org.tela_botanica.client.Mediateur;
 | 
        
           | 126 | gduche | 6 | import org.tela_botanica.client.RegistreId;
 | 
        
           | 748 | jpm | 7 | import org.tela_botanica.client.http.JsonRestRequestBuilder;
 | 
        
           |  |  | 8 | import org.tela_botanica.client.http.JsonRestRequestCallback;
 | 
        
           | 126 | gduche | 9 | import org.tela_botanica.client.interfaces.Rafraichissable;
 | 
        
           | 935 | jpm | 10 | import org.tela_botanica.client.modeles.Information;
 | 
        
           | 1319 | gduche | 11 | import org.tela_botanica.client.synchronisation.Reponse;
 | 
        
           |  |  | 12 | import org.tela_botanica.client.synchronisation.Sequenceur;
 | 
        
           | 1231 | cyprien | 13 | import org.tela_botanica.client.util.Debug;
 | 
        
           | 146 | gduche | 14 | import org.tela_botanica.client.util.UtilDAO;
 | 
        
           | 126 | gduche | 15 | import com.extjs.gxt.ui.client.Registry;
 | 
        
           |  |  | 16 | import com.google.gwt.core.client.GWT;
 | 
        
           |  |  | 17 | import com.google.gwt.json.client.JSONArray;
 | 
        
           | 1329 | cyprien | 18 | import com.google.gwt.json.client.JSONObject;
 | 
        
           | 126 | gduche | 19 | import com.google.gwt.json.client.JSONValue;
 | 
        
           |  |  | 20 |   | 
        
           | 268 | jp_milcent | 21 | public class PersonneAsyncDao {
 | 
        
           |  |  | 22 | 	private static final String SERVICE_NOM = "CoelPersonne";
 | 
        
           | 126 | gduche | 23 |   | 
        
           | 748 | jpm | 24 | 	private String utilisateurId = null;
 | 
        
           | 277 | jp_milcent | 25 | 	private Rafraichissable vueARafraichir = null;
 | 
        
           | 126 | gduche | 26 |   | 
        
           | 277 | jp_milcent | 27 | 	public PersonneAsyncDao(Rafraichissable vue) {
 | 
        
           | 1367 | cyprien | 28 | 		if (Mediateur.DEBUG) System.out.println("|| PersonneAsyncDao > vueARafraichir = "+vue.getClass().toString());
 | 
        
           | 277 | jp_milcent | 29 | 		vueARafraichir = vue;
 | 
        
           | 748 | jpm | 30 | 		utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId();
 | 
        
           | 1329 | cyprien | 31 | 	}
 | 
        
           |  |  | 32 |   | 
        
           |  |  | 33 | 	/**
 | 
        
           |  |  | 34 | 	 * @param paginationProgressive : définit le mode de consultation de la base de données
 | 
        
           |  |  | 35 | 	 * 			- True :	la consultation des données est progressive, ce qui signifie que la liste est chargée (paginée) au
 | 
        
           |  |  | 36 | 	 * 						fur et à mesure de la consultation des données par l'utilisateur.
 | 
        
           |  |  | 37 | 	 * 			- False :	la consultation des données est classique : un seul appel à la base de données est effectué, le retour
 | 
        
           |  |  | 38 | 	 * 						est renvoyé à l'appelant
 | 
        
           |  |  | 39 | 	 * 			// FIXME : si la taille de la liste est supérieure à la limite du JREST (150), ce deuxieme mode ne fonctionne pas
 | 
        
           |  |  | 40 | 	 */
 | 
        
           |  |  | 41 | 	public void selectionner(final boolean paginationProgressive, String personneId, String projetId, String nomComplet, final int start, final int nbElements, final Integer seqId) {
 | 
        
           | 268 | jp_milcent | 42 |   | 
        
           | 1329 | cyprien | 43 | 		String nom = (nomComplet == null) ? "%" : nomComplet+"%";
 | 
        
           |  |  | 44 | 		String[] parametres = {personneId, projetId, nom};
 | 
        
           | 415 | gduche | 45 |   | 
        
           | 748 | jpm | 46 | 		HashMap<String, String> restrictions = new HashMap<String, String>();
 | 
        
           | 1231 | cyprien | 47 |   | 
        
           | 593 | gduche | 48 | 		if (nbElements != -1)	{
 | 
        
           | 748 | jpm | 49 | 			restrictions.put("limit", String.valueOf(nbElements));
 | 
        
           | 593 | gduche | 50 | 		}
 | 
        
           |  |  | 51 |   | 
        
           | 1329 | cyprien | 52 | 		/** GESTION DE LA REQUETE dans le cas d'une liste paginée progressive **/
 | 
        
           |  |  | 53 | 		if (paginationProgressive) {
 | 
        
           |  |  | 54 |   | 
        
           |  |  | 55 | 			/** DEFINITION DU TUPLE DE DEPART **/
 | 
        
           |  |  | 56 | 			restrictions.put("start", String.valueOf(start));
 | 
        
           |  |  | 57 |   | 
        
           |  |  | 58 | 			/** CONSTRUCTION DE LA REQUETE **/
 | 
        
           |  |  | 59 |     		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 |     		/** ENVOI DE LA REQUETE **/
 | 
        
           |  |  | 62 |     		rb.envoyerRequete(null, new JsonRestRequestCallback()
 | 
        
           |  |  | 63 |     		{
 | 
        
           |  |  | 64 |     			/** RECEPTION DE LA REPONSE **/
 | 
        
           |  |  | 65 |     			public void surReponse(JSONValue responseValue)
 | 
        
           |  |  | 66 |     			{
 | 
        
           |  |  | 67 |     				/** Dans le cas d'une liste paginée, vueARafraichir est un objet Proxy.
 | 
        
           |  |  | 68 |     				 * On retourne l'objet JSON au proxy afin que ce soit lui qui le traite **/
 | 
        
           | 1367 | cyprien | 69 |   | 
        
           |  |  | 70 | 					if (seqId != null)	{
 | 
        
           |  |  | 71 | 						if (Mediateur.DEBUG) System.out.println("<-- PersonneAsyncDao > Liste paginée, retour au sequenceur");
 | 
        
           |  |  | 72 | 						Reponse reponseRequete = new Reponse(responseValue, seqId);
 | 
        
           |  |  | 73 | 						vueARafraichir.rafraichir(reponseRequete);
 | 
        
           |  |  | 74 | 					}
 | 
        
           |  |  | 75 | 					else	{
 | 
        
           |  |  | 76 | 						if (Mediateur.DEBUG) System.out.println("<-- PersonneAsyncDao > Liste paginée, retour à "+vueARafraichir.getClass().toString());
 | 
        
           |  |  | 77 | 						vueARafraichir.rafraichir(responseValue);
 | 
        
           |  |  | 78 | 					}
 | 
        
           | 1329 | cyprien | 79 |     			}
 | 
        
           |  |  | 80 |     		});
 | 
        
           |  |  | 81 | 		}
 | 
        
           |  |  | 82 | 		/** GESTION DE LA REQUETE dans le cas d'une liste NON paginée progressive **/
 | 
        
           |  |  | 83 | 		else {
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 | 			/** DEFINITION DU TUPLE DE DEPART **/
 | 
        
           |  |  | 86 | 			restrictions.put("start", String.valueOf(start*nbElements));
 | 
        
           |  |  | 87 |   | 
        
           |  |  | 88 | 			final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
 | 
        
           |  |  | 89 | 			rb.envoyerRequete(null, new JsonRestRequestCallback() {
 | 
        
           |  |  | 90 | 				@Override
 | 
        
           |  |  | 91 | 				public void surReponse(JSONValue responseValue) {
 | 
        
           | 748 | jpm | 92 |   | 
        
           | 1329 | cyprien | 93 | 					if (responseValue != null) {
 | 
        
           |  |  | 94 | 						JSONObject reponseObject = responseValue.isObject();
 | 
        
           |  |  | 95 |   | 
        
           |  |  | 96 | 						if (reponseObject.get("personnes").isArray() != null) {
 | 
        
           |  |  | 97 | 							JSONArray reponse = responseValue.isArray();
 | 
        
           |  |  | 98 |   | 
        
           |  |  | 99 | 							// Transformation du tableau JSON réponse en ListePersonne
 | 
        
           |  |  | 100 | 							Information info = new Information("liste_personne");
 | 
        
           |  |  | 101 | 							PersonneListe personnes;
 | 
        
           |  |  | 102 | 							personnes = new PersonneListe(reponseObject.get("personnes").isArray(), reponseObject.get("nbElements").isNumber(), vueARafraichir);
 | 
        
           |  |  | 103 | 							personnes.setTaillePage(nbElements);
 | 
        
           |  |  | 104 | 							personnes.setPageCourante(start);
 | 
        
           |  |  | 105 | 							info.setDonnee(0, personnes);
 | 
        
           |  |  | 106 |   | 
        
           |  |  | 107 | 							// et on met à jour le demandeur des données
 | 
        
           |  |  | 108 | 							if (seqId != null)	{
 | 
        
           | 1367 | cyprien | 109 | 								if (Mediateur.DEBUG) System.out.println("<-- PersonneAsyncDao > Liste non paginée, retour au sequenceur");
 | 
        
           | 1329 | cyprien | 110 | 								Reponse reponseRequete = new Reponse(info, seqId);
 | 
        
           |  |  | 111 | 								vueARafraichir.rafraichir(reponseRequete);
 | 
        
           |  |  | 112 | 							}
 | 
        
           |  |  | 113 | 							else	{
 | 
        
           | 1367 | cyprien | 114 | 								if (Mediateur.DEBUG) System.out.println("<-- PersonneAsyncDao > Liste non paginée, retour à "+vueARafraichir.getClass().toString());
 | 
        
           | 1329 | cyprien | 115 | 								vueARafraichir.rafraichir(info);
 | 
        
           |  |  | 116 | 							}
 | 
        
           |  |  | 117 | 						}
 | 
        
           | 241 | jp_milcent | 118 | 					} else {
 | 
        
           | 1329 | cyprien | 119 | 						GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas un tableau JSON et vaut : "+responseValue.toString(), null);
 | 
        
           | 1319 | gduche | 120 | 					}
 | 
        
           | 241 | jp_milcent | 121 | 				}
 | 
        
           | 1329 | cyprien | 122 | 			});
 | 
        
           |  |  | 123 | 		}
 | 
        
           | 126 | gduche | 124 | 	}
 | 
        
           | 241 | jp_milcent | 125 |   | 
        
           | 1292 | cyprien | 126 | 	public void ajouter(Personne personne) {
 | 
        
           | 748 | jpm | 127 | 		String postDonneesEncodees = personne.obtenirChainePOST();
 | 
        
           | 1292 | cyprien | 128 | 		postDonneesEncodees += "&cmhl_ce_modifier_par=" + utilisateurId;
 | 
        
           | 748 | jpm | 129 | 		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM);
 | 
        
           |  |  | 130 | 		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
 | 
        
           |  |  | 131 | 			@Override
 | 
        
           |  |  | 132 | 			public void surReponse(JSONValue responseValue) {
 | 
        
           |  |  | 133 | 				if (responseValue.isString() != null) {
 | 
        
           | 772 | jpm | 134 | 					Information info = new Information("ajout_personne");
 | 
        
           | 884 | jpm | 135 | 					String structureIdOuMessage = responseValue.isString().stringValue();
 | 
        
           |  |  | 136 | 					if (structureIdOuMessage.matches("^[0-9]+$")) {
 | 
        
           |  |  | 137 | 						info.setDonnee(structureIdOuMessage);
 | 
        
           |  |  | 138 | 					} else {
 | 
        
           |  |  | 139 | 						info.setMessage(structureIdOuMessage);
 | 
        
           |  |  | 140 | 					}
 | 
        
           | 748 | jpm | 141 | 					vueARafraichir.rafraichir(info);
 | 
        
           |  |  | 142 | 				} else {
 | 
        
           |  |  | 143 | 					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
 | 
        
           | 432 | gduche | 144 | 				}
 | 
        
           | 748 | jpm | 145 | 			}
 | 
        
           | 1292 | cyprien | 146 | 		});
 | 
        
           | 432 | gduche | 147 | 	}
 | 
        
           |  |  | 148 |   | 
        
           | 748 | jpm | 149 | 	public void modifier(Personne personne) {
 | 
        
           |  |  | 150 | 		String postDonneesEncodees = personne.obtenirChainePOST();
 | 
        
           |  |  | 151 | 		postDonneesEncodees += "&cmhl_ce_modifier_par=" + utilisateurId;
 | 
        
           | 775 | jpm | 152 | 		GWT.log(postDonneesEncodees, null);
 | 
        
           | 748 | jpm | 153 | 		String[] parametres = {personne.getId()};
 | 
        
           | 755 | aurelien | 154 | 		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
 | 
        
           | 748 | jpm | 155 | 		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
 | 
        
           |  |  | 156 | 			@Override
 | 
        
           |  |  | 157 | 			public void surReponse(JSONValue responseValue) {
 | 
        
           |  |  | 158 | 				// Si la requête est un succès, reception d'une chaine
 | 
        
           |  |  | 159 | 				if (responseValue.isString() != null) {
 | 
        
           | 772 | jpm | 160 | 					Information info = new Information("modification_personne");
 | 
        
           |  |  | 161 | 					info.setMessage(responseValue.isString().stringValue());
 | 
        
           | 748 | jpm | 162 | 					vueARafraichir.rafraichir(info);
 | 
        
           |  |  | 163 | 				} else {
 | 
        
           |  |  | 164 | 					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
 | 
        
           | 387 | gduche | 165 | 				}
 | 
        
           | 748 | jpm | 166 | 			}
 | 
        
           |  |  | 167 | 		});
 | 
        
           | 387 | gduche | 168 | 	}
 | 
        
           | 751 | jpm | 169 |   | 
        
           | 1231 | cyprien | 170 | 	public void supprimer(String personnesId) {
 | 
        
           | 772 | jpm | 171 | 		String[] parametres = {utilisateurId, personnesId};
 | 
        
           | 755 | aurelien | 172 | 		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
 | 
        
           | 751 | jpm | 173 | 		rb.envoyerRequeteSuppression(new JsonRestRequestCallback() {
 | 
        
           |  |  | 174 | 			@Override
 | 
        
           |  |  | 175 | 			public void surReponse(JSONValue responseValue) {
 | 
        
           |  |  | 176 | 				if (responseValue.isString() != null) {
 | 
        
           | 772 | jpm | 177 | 					Information info = new Information("suppression_personne");
 | 
        
           |  |  | 178 | 					info.setMessage(responseValue.isString().stringValue());
 | 
        
           | 751 | jpm | 179 | 					vueARafraichir.rafraichir(info);
 | 
        
           |  |  | 180 | 				} else {
 | 
        
           |  |  | 181 | 					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
 | 
        
           |  |  | 182 | 				}
 | 
        
           |  |  | 183 | 			}
 | 
        
           |  |  | 184 | 		});
 | 
        
           |  |  | 185 | 	}
 | 
        
           | 126 | gduche | 186 |   | 
        
           |  |  | 187 | }
 |