| 935 | 
           jpm | 
           1 | 
           package org.tela_botanica.client.modeles.collection;
  | 
        
        
           | 463 | 
           jp_milcent | 
           2 | 
              | 
        
        
           | 1041 | 
           gduche | 
           3 | 
           import java.util.HashMap;
  | 
        
        
            | 
            | 
           4 | 
              | 
        
        
           | 871 | 
           jpm | 
           5 | 
           import org.tela_botanica.client.Mediateur;
  | 
        
        
           | 463 | 
           jp_milcent | 
           6 | 
           import org.tela_botanica.client.RegistreId;
  | 
        
        
            | 
            | 
           7 | 
           import org.tela_botanica.client.http.JsonRestRequestBuilder;
  | 
        
        
            | 
            | 
           8 | 
           import org.tela_botanica.client.http.JsonRestRequestCallback;
  | 
        
        
            | 
            | 
           9 | 
           import org.tela_botanica.client.interfaces.Rafraichissable;
  | 
        
        
           | 935 | 
           jpm | 
           10 | 
           import org.tela_botanica.client.modeles.Information;
  | 
        
        
           | 1367 | 
           cyprien | 
           11 | 
           import org.tela_botanica.client.synchronisation.Reponse;
  | 
        
        
           | 968 | 
           jpm | 
           12 | 
           import org.tela_botanica.client.util.Debug;
  | 
        
        
           | 871 | 
           jpm | 
           13 | 
           import org.tela_botanica.client.util.UtilDAO;
  | 
        
        
           | 463 | 
           jp_milcent | 
           14 | 
              | 
        
        
            | 
            | 
           15 | 
           import com.extjs.gxt.ui.client.Registry;
  | 
        
        
            | 
            | 
           16 | 
           import com.google.gwt.core.client.GWT;
  | 
        
        
           | 871 | 
           jpm | 
           17 | 
           import com.google.gwt.http.client.URL;
  | 
        
        
           | 463 | 
           jp_milcent | 
           18 | 
           import com.google.gwt.json.client.JSONArray;
  | 
        
        
           | 1041 | 
           gduche | 
           19 | 
           import com.google.gwt.json.client.JSONNumber;
  | 
        
        
           | 463 | 
           jp_milcent | 
           20 | 
           import com.google.gwt.json.client.JSONObject;
  | 
        
        
            | 
            | 
           21 | 
           import com.google.gwt.json.client.JSONValue;
  | 
        
        
           | 1613 | 
           aurelien | 
           22 | 
           import com.google.gwt.user.client.Window;
  | 
        
        
           | 463 | 
           jp_milcent | 
           23 | 
              | 
        
        
            | 
            | 
           24 | 
           public class CollectionAsyncDao {
  | 
        
        
            | 
            | 
           25 | 
              | 
        
        
            | 
            | 
           26 | 
           	public static final String SERVICE_NOM = "CoelCollection";
  | 
        
        
           | 1762 | 
           mathias | 
           27 | 
           	public static String tri = null;
  | 
        
        
           | 871 | 
           jpm | 
           28 | 
           	private String utilisateurId = null;
  | 
        
        
            | 
            | 
           29 | 
           	private Rafraichissable vueARafraichir = null;
  | 
        
        
           | 463 | 
           jp_milcent | 
           30 | 
              | 
        
        
           | 871 | 
           jpm | 
           31 | 
           	public CollectionAsyncDao(Rafraichissable vueARafraichirCourrante) {
  | 
        
        
           | 1633 | 
           aurelien | 
           32 | 
           		//if (Mediateur.DEBUG) System.out.println("|| CollectionAsyncDao > vueARafraichir = "+vueARafraichirCourrante.getClass().toString());
  | 
        
        
           | 871 | 
           jpm | 
           33 | 
           		vueARafraichir = vueARafraichirCourrante;
  | 
        
        
            | 
            | 
           34 | 
           		utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId();
  | 
        
        
            | 
            | 
           35 | 
           	}
  | 
        
        
            | 
            | 
           36 | 
              | 
        
        
           | 1329 | 
           cyprien | 
           37 | 
           	/**
  | 
        
        
            | 
            | 
           38 | 
           	 *
  | 
        
        
            | 
            | 
           39 | 
           	 * @param paginationProgressive : définit le mode de consultation de la base de données
  | 
        
        
            | 
            | 
           40 | 
           	 * 			- True :	la consultation des données est progressive, ce qui signifie que la liste est chargée (paginée) au
  | 
        
        
            | 
            | 
           41 | 
           	 * 						fur et à mesure de la consultation des données par l'utilisateur.
  | 
        
        
            | 
            | 
           42 | 
           	 * 			- False :	la consultation des données est classique : un seul appel à la base de données est effectué, le retour
  | 
        
        
            | 
            | 
           43 | 
           	 * 						est renvoyé à l'appelant
  | 
        
        
            | 
            | 
           44 | 
           	 * 			// FIXME : si la taille de la liste est supérieure à la limite du JREST (150), ce deuxieme mode ne fonctionne pas
  | 
        
        
            | 
            | 
           45 | 
           	 */
  | 
        
        
           | 1764 | 
           aurelien | 
           46 | 
           	public void selectionner(final boolean paginationProgressive, final String collectionId, final String nomCollection, final int start, final int nbElements, final Integer seqId) {
  | 
        
        
           | 1329 | 
           cyprien | 
           47 | 
              | 
        
        
           | 463 | 
           jp_milcent | 
           48 | 
           		// Ajout des paramètres et données à selectionner dans l'URL
  | 
        
        
           | 1329 | 
           cyprien | 
           49 | 
           		String nom = (nomCollection == null) ? "%" : nomCollection+"%";
  | 
        
        
           | 1764 | 
           aurelien | 
           50 | 
           		String[] parametres = {collectionId, nom};
  | 
        
        
           | 1329 | 
           cyprien | 
           51 | 
              | 
        
        
           | 1041 | 
           gduche | 
           52 | 
           		HashMap<String, String> restrictions = new HashMap<String, String>();
  | 
        
        
           | 1329 | 
           cyprien | 
           53 | 
              | 
        
        
           | 1041 | 
           gduche | 
           54 | 
           		if (nbElements != -1)	{
  | 
        
        
            | 
            | 
           55 | 
           			restrictions.put("limit", String.valueOf(nbElements));
  | 
        
        
            | 
            | 
           56 | 
           		}
  | 
        
        
           | 1329 | 
           cyprien | 
           57 | 
              | 
        
        
           | 1762 | 
           mathias | 
           58 | 
           		if(tri != null) {
  | 
        
        
            | 
            | 
           59 | 
           			restrictions.put("orderby", tri);
  | 
        
        
            | 
            | 
           60 | 
           		}
  | 
        
        
            | 
            | 
           61 | 
              | 
        
        
           | 1329 | 
           cyprien | 
           62 | 
           		/** GESTION DE LA REQUETE dans le cas d'une liste paginée progressive **/
  | 
        
        
            | 
            | 
           63 | 
           		if (paginationProgressive) {
  | 
        
        
            | 
            | 
           64 | 
              | 
        
        
            | 
            | 
           65 | 
           			/** DEFINITION DU TUPLE DE DEPART **/
  | 
        
        
            | 
            | 
           66 | 
           			restrictions.put("start", String.valueOf(start));
  | 
        
        
            | 
            | 
           67 | 
              | 
        
        
            | 
            | 
           68 | 
           			/** CONSTRUCTION DE LA REQUETE **/
  | 
        
        
            | 
            | 
           69 | 
               		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
               		/** ENVOI DE LA REQUETE **/
  | 
        
        
            | 
            | 
           72 | 
               		rb.envoyerRequete(null, new JsonRestRequestCallback()
  | 
        
        
            | 
            | 
           73 | 
               		{
  | 
        
        
            | 
            | 
           74 | 
               			/** RECEPTION DE LA REPONSE **/
  | 
        
        
            | 
            | 
           75 | 
               			public void surReponse(JSONValue responseValue)
  | 
        
        
            | 
            | 
           76 | 
               			{
  | 
        
        
            | 
            | 
           77 | 
               				/** Dans le cas d'une liste paginée, vueARafraichir est un objet Proxy.
  | 
        
        
            | 
            | 
           78 | 
               				 * On retourne l'objet JSON au proxy afin que ce soit lui qui le traite **/
  | 
        
        
           | 1428 | 
           cyprien | 
           79 | 
              | 
        
        
           | 1367 | 
           cyprien | 
           80 | 
           					if (seqId != null)	{
  | 
        
        
            | 
            | 
           81 | 
           						if (Mediateur.DEBUG) System.out.println("<-- CollectionAsyncDao > Liste paginée, retour au sequenceur");
  | 
        
        
            | 
            | 
           82 | 
           						Reponse reponseRequete = new Reponse(responseValue, seqId);
  | 
        
        
            | 
            | 
           83 | 
           						vueARafraichir.rafraichir(reponseRequete);
  | 
        
        
            | 
            | 
           84 | 
           					}
  | 
        
        
            | 
            | 
           85 | 
           					else	{
  | 
        
        
            | 
            | 
           86 | 
           						if (Mediateur.DEBUG) System.out.println("<-- CollectionAsyncDao > Liste paginée, retour à "+vueARafraichir.getClass().toString());
  | 
        
        
            | 
            | 
           87 | 
           						vueARafraichir.rafraichir(responseValue);
  | 
        
        
            | 
            | 
           88 | 
           					}
  | 
        
        
           | 1329 | 
           cyprien | 
           89 | 
               			}
  | 
        
        
            | 
            | 
           90 | 
               		});
  | 
        
        
            | 
            | 
           91 | 
           		}
  | 
        
        
            | 
            | 
           92 | 
           		/** GESTION DE LA REQUETE dans le cas d'une liste NON paginée progressive **/
  | 
        
        
            | 
            | 
           93 | 
           		else {
  | 
        
        
           | 1428 | 
           cyprien | 
           94 | 
              | 
        
        
           | 1329 | 
           cyprien | 
           95 | 
           			/** DEFINITION DU TUPLE DE DEPART **/
  | 
        
        
            | 
            | 
           96 | 
           			restrictions.put("start", String.valueOf(start*nbElements));
  | 
        
        
            | 
            | 
           97 | 
              | 
        
        
            | 
            | 
           98 | 
           			final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
  | 
        
        
            | 
            | 
           99 | 
              | 
        
        
            | 
            | 
           100 | 
           			rb.envoyerRequete(null, new JsonRestRequestCallback() {
  | 
        
        
            | 
            | 
           101 | 
           				@Override
  | 
        
        
            | 
            | 
           102 | 
           				public void surReponse(JSONValue responseValue) {
  | 
        
        
            | 
            | 
           103 | 
           					if (responseValue != null) {
  | 
        
        
            | 
            | 
           104 | 
              | 
        
        
            | 
            | 
           105 | 
           						JSONObject responseObject = responseValue.isObject();
  | 
        
        
            | 
            | 
           106 | 
              | 
        
        
            | 
            | 
           107 | 
           						if (responseObject != null) {
  | 
        
        
            | 
            | 
           108 | 
           							// Si la réponse est un tableau, alors c'est une liste de collections qui a été retournée
  | 
        
        
            | 
            | 
           109 | 
           							if (responseObject.get("collections").isArray() != null) {
  | 
        
        
            | 
            | 
           110 | 
           								final JSONArray reponse = responseObject.get("collections").isArray();
  | 
        
        
            | 
            | 
           111 | 
           								CollectionListe collections = new CollectionListe(reponse, responseObject.get("nbElements").isNumber(), vueARafraichir);
  | 
        
        
            | 
            | 
           112 | 
           								collections.setTaillePage(nbElements);
  | 
        
        
            | 
            | 
           113 | 
           								collections.setPageCourante(start);
  | 
        
        
            | 
            | 
           114 | 
              | 
        
        
            | 
            | 
           115 | 
           								vueARafraichir.rafraichir(collections);
  | 
        
        
            | 
            | 
           116 | 
              | 
        
        
            | 
            | 
           117 | 
           							// Si la réponse est un objet, alors c'est une unique collection qui a été retournée
  | 
        
        
            | 
            | 
           118 | 
           							} else if (responseObject.get("collections").isObject() != null) {
  | 
        
        
            | 
            | 
           119 | 
           								final JSONObject reponse = responseObject.get("collections").isObject();
  | 
        
        
            | 
            | 
           120 | 
           								Collection collection = new Collection(reponse);
  | 
        
        
            | 
            | 
           121 | 
           								CollectionBotanique collectionBotanique = new CollectionBotanique(reponse);
  | 
        
        
            | 
            | 
           122 | 
           								collection.setBotanique(collectionBotanique);
  | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
           								Information info = new Information("selection_collection");
  | 
        
        
            | 
            | 
           125 | 
           								info.setDonnee(0, collection);
  | 
        
        
           | 1367 | 
           cyprien | 
           126 | 
              | 
        
        
            | 
            | 
           127 | 
           								// et on met à jour le demandeur des données
  | 
        
        
            | 
            | 
           128 | 
           								if (seqId != null)	{
  | 
        
        
            | 
            | 
           129 | 
           									if (Mediateur.DEBUG) System.out.println("<-- CollectionAsyncDao > Liste non paginée, retour au sequenceur");
  | 
        
        
            | 
            | 
           130 | 
           									Reponse reponseRequete = new Reponse(info, seqId);
  | 
        
        
            | 
            | 
           131 | 
           									vueARafraichir.rafraichir(reponseRequete);
  | 
        
        
           | 1513 | 
           jpm | 
           132 | 
           								}
  | 
        
        
            | 
            | 
           133 | 
           								else	{
  | 
        
        
           | 1428 | 
           cyprien | 
           134 | 
           									if (Mediateur.DEBUG) System.out.println("<-- CollectionAsyncDao > Liste non paginée, retour à "+vueARafraichir.getClass().toString());
  | 
        
        
           | 1367 | 
           cyprien | 
           135 | 
           									vueARafraichir.rafraichir(info);
  | 
        
        
            | 
            | 
           136 | 
           								}
  | 
        
        
           | 1329 | 
           cyprien | 
           137 | 
           							}
  | 
        
        
            | 
            | 
           138 | 
           						} else {
  | 
        
        
           | 1513 | 
           jpm | 
           139 | 
           							GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas un objet ou un talbeau JSON et vaut : "+responseValue.toString(), null);
  | 
        
        
           | 1329 | 
           cyprien | 
           140 | 
           						}
  | 
        
        
           | 463 | 
           jp_milcent | 
           141 | 
           					} else {
  | 
        
        
           | 1329 | 
           cyprien | 
           142 | 
           						// Dans le cas, où nous demandons toutes les institutions et qu'il n'y en a pas, nous retournons un objet vide
  | 
        
        
            | 
            | 
           143 | 
           						if (collectionId == null) {
  | 
        
        
            | 
            | 
           144 | 
           							CollectionListe collections = new CollectionListe(0);
  | 
        
        
            | 
            | 
           145 | 
           							vueARafraichir.rafraichir(collections);
  | 
        
        
            | 
            | 
           146 | 
           						}
  | 
        
        
           | 463 | 
           jp_milcent | 
           147 | 
           					}
  | 
        
        
            | 
            | 
           148 | 
           				}
  | 
        
        
           | 1329 | 
           cyprien | 
           149 | 
           			});
  | 
        
        
            | 
            | 
           150 | 
           		}
  | 
        
        
           | 463 | 
           jp_milcent | 
           151 | 
           	}
  | 
        
        
           | 643 | 
           jp_milcent | 
           152 | 
              | 
        
        
           | 1262 | 
           cyprien | 
           153 | 
           	public void ajouter(Collection collection) {
  | 
        
        
           | 871 | 
           jpm | 
           154 | 
           		String postDonneesEncodees = construirePost(null, collection);
  | 
        
        
            | 
            | 
           155 | 
           		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM);
  | 
        
        
            | 
            | 
           156 | 
           		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
  | 
        
        
            | 
            | 
           157 | 
           			@Override
  | 
        
        
            | 
            | 
           158 | 
           			public void surReponse(JSONValue responseValue) {
  | 
        
        
            | 
            | 
           159 | 
           				if (responseValue.isString() != null) {
  | 
        
        
            | 
            | 
           160 | 
           					Information info = new Information("ajout_collection");
  | 
        
        
            | 
            | 
           161 | 
           					String structureIdOuMessage = responseValue.isString().stringValue();
  | 
        
        
            | 
            | 
           162 | 
           					if (structureIdOuMessage.matches("^[0-9]+$")) {
  | 
        
        
            | 
            | 
           163 | 
           						info.setDonnee(structureIdOuMessage);
  | 
        
        
            | 
            | 
           164 | 
           					} else {
  | 
        
        
            | 
            | 
           165 | 
           						info.setMessage(structureIdOuMessage);
  | 
        
        
            | 
            | 
           166 | 
           					}
  | 
        
        
            | 
            | 
           167 | 
           					vueARafraichir.rafraichir(info);
  | 
        
        
            | 
            | 
           168 | 
           				} else {
  | 
        
        
            | 
            | 
           169 | 
           					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
  | 
        
        
            | 
            | 
           170 | 
           				}
  | 
        
        
            | 
            | 
           171 | 
           			}
  | 
        
        
            | 
            | 
           172 | 
           		});
  | 
        
        
            | 
            | 
           173 | 
           	}
  | 
        
        
            | 
            | 
           174 | 
              | 
        
        
            | 
            | 
           175 | 
           	public void modifier(Collection collection) {
  | 
        
        
           | 1292 | 
           cyprien | 
           176 | 
           		String postDonneesEncodees = construirePost(collection.getId(), collection);
  | 
        
        
           | 871 | 
           jpm | 
           177 | 
           		String[] parametres = {collection.getId()};
  | 
        
        
            | 
            | 
           178 | 
           		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
  | 
        
        
            | 
            | 
           179 | 
           		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
  | 
        
        
           | 643 | 
           jp_milcent | 
           180 | 
           			@Override
  | 
        
        
           | 871 | 
           jpm | 
           181 | 
           			public void surReponse(JSONValue responseValue) {
  | 
        
        
           | 643 | 
           jp_milcent | 
           182 | 
           				// Si la requête est un succès, reception d'une chaine
  | 
        
        
           | 871 | 
           jpm | 
           183 | 
           				if (responseValue.isString() != null) {
  | 
        
        
            | 
            | 
           184 | 
           					Information info = new Information("modif_collection");
  | 
        
        
            | 
            | 
           185 | 
           					info.setMessage(responseValue.isString().stringValue());
  | 
        
        
            | 
            | 
           186 | 
           					vueARafraichir.rafraichir(info);
  | 
        
        
           | 643 | 
           jp_milcent | 
           187 | 
           				} else {
  | 
        
        
           | 871 | 
           jpm | 
           188 | 
           					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
  | 
        
        
           | 643 | 
           jp_milcent | 
           189 | 
           				}
  | 
        
        
            | 
            | 
           190 | 
           			}
  | 
        
        
            | 
            | 
           191 | 
           		});
  | 
        
        
            | 
            | 
           192 | 
           	}
  | 
        
        
           | 871 | 
           jpm | 
           193 | 
              | 
        
        
            | 
            | 
           194 | 
           	public void supprimer(String collectionsId) {
  | 
        
        
            | 
            | 
           195 | 
           		String[] parametres = {utilisateurId, collectionsId};
  | 
        
        
            | 
            | 
           196 | 
           		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
  | 
        
        
            | 
            | 
           197 | 
           		rb.envoyerRequeteSuppression(new JsonRestRequestCallback() {
  | 
        
        
            | 
            | 
           198 | 
           			@Override
  | 
        
        
            | 
            | 
           199 | 
           			public void surReponse(JSONValue responseValue) {
  | 
        
        
            | 
            | 
           200 | 
           				if (responseValue.isString() != null) {
  | 
        
        
            | 
            | 
           201 | 
           					Information info = new Information("suppression_collection");
  | 
        
        
            | 
            | 
           202 | 
           					info.setMessage(responseValue.isString().stringValue());
  | 
        
        
            | 
            | 
           203 | 
           					vueARafraichir.rafraichir(info);
  | 
        
        
            | 
            | 
           204 | 
           				} else {
  | 
        
        
            | 
            | 
           205 | 
           					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
  | 
        
        
            | 
            | 
           206 | 
           				}
  | 
        
        
            | 
            | 
           207 | 
           			}
  | 
        
        
            | 
            | 
           208 | 
           		});
  | 
        
        
            | 
            | 
           209 | 
           	}
  | 
        
        
            | 
            | 
           210 | 
              | 
        
        
           | 1262 | 
           cyprien | 
           211 | 
           	private String construirePost(String collectionId, Collection collection) {
  | 
        
        
           | 871 | 
           jpm | 
           212 | 
           		String postDonnees = "cmhl_ce_modifier_par=" + URL.encodeComponent(utilisateurId);
  | 
        
        
            | 
            | 
           213 | 
              | 
        
        
            | 
            | 
           214 | 
           		if (collection != null) {
  | 
        
        
            | 
            | 
           215 | 
           			if (collectionId != null) {
  | 
        
        
            | 
            | 
           216 | 
           				postDonnees += "&cc_id_collection=" + URL.encodeComponent(collectionId);
  | 
        
        
            | 
            | 
           217 | 
           			}
  | 
        
        
           | 1262 | 
           cyprien | 
           218 | 
              | 
        
        
           | 1173 | 
           jpm | 
           219 | 
           			postDonnees += "&" + collection.obtenirChainePOST();
  | 
        
        
           | 1613 | 
           aurelien | 
           220 | 
              | 
        
        
            | 
            | 
           221 | 
           			if (collection.getBotanique() != null) {
  | 
        
        
            | 
            | 
           222 | 
           				if (collectionId != null) {
  | 
        
        
            | 
            | 
           223 | 
           					postDonnees += "&ccb_id_collection=" + URL.encodeComponent(collectionId);
  | 
        
        
            | 
            | 
           224 | 
           				}
  | 
        
        
            | 
            | 
           225 | 
           				CollectionBotanique collectionBotanique = collection.getBotanique();
  | 
        
        
            | 
            | 
           226 | 
           				postDonnees += "&" + collectionBotanique.obtenirChainePOST();
  | 
        
        
           | 956 | 
           jpm | 
           227 | 
           			}
  | 
        
        
           | 871 | 
           jpm | 
           228 | 
           		}
  | 
        
        
            | 
            | 
           229 | 
              | 
        
        
            | 
            | 
           230 | 
           		return postDonnees;
  | 
        
        
            | 
            | 
           231 | 
           	}
  | 
        
        
           | 463 | 
           jp_milcent | 
           232 | 
           }
  |