Subversion Repositories eFlore/Applications.cel

Rev

Rev 2062 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2062 Rev 2148
1
package org.tela_botanica.client.util;
1
package org.tela_botanica.client.util;
2
 
2
 
3
import java.util.HashMap;
3
import java.util.HashMap;
4
 
4
 
5
import com.google.gwt.json.client.JSONArray;
5
import com.google.gwt.json.client.JSONArray;
6
import com.google.gwt.json.client.JSONObject;
6
import com.google.gwt.json.client.JSONObject;
7
import com.google.gwt.user.client.Window;
7
import com.google.gwt.user.client.Window;
8
import com.gwtext.client.data.Node;
8
import com.gwtext.client.data.Node;
9
import com.gwtext.client.data.Tree;
9
import com.gwtext.client.data.Tree;
10
import com.gwtext.client.widgets.tree.TreeNode;
10
import com.gwtext.client.widgets.tree.TreeNode;
11
 
11
 
12
public class MotsClesUtilitaire {
12
public class MotsClesUtilitaire {
13
	
13
	
14
	//TODO rassembler tout ce qui construit les arbres de mots clés ici
14
	//TODO rassembler tout ce qui construit les arbres de mots clés ici
15
	private static String[] caracteresInterdits = {"\\", "/", ","};
15
	private static String[] caracteresInterdits = {"\\", "/", ","};
16
	
16
	
17
	public static String obtenirCheminParent(String cheminMotCle, String motCle) {
17
	public static String obtenirCheminParent(String cheminMotCle, String motCle) {
18
		return cheminMotCle.substring(0, cheminMotCle.length() - motCle.length() -1);
18
		return cheminMotCle.substring(0, cheminMotCle.length() - motCle.length() -1).toLowerCase();
19
	}
19
	}
20
 
20
 
21
	public static Object[] construireArbre(String NomRacine, String idRacine,
21
	public static Object[] construireArbre(String NomRacine, String idRacine,
22
			JSONArray arbreJson, boolean afficherCheckbox) {
22
			JSONArray arbreJson, boolean afficherCheckbox) {
23
		
23
		
24
		Tree arbreMotsCles = new Tree() ;	
24
		Tree arbreMotsCles = new Tree() ;	
25
		HashMap<String, String> motsCles = new HashMap<String, String>(0);
25
		HashMap<String, String> motsCles = new HashMap<String, String>(0);
26
		HashMap<String, String> parentes = new HashMap<String, String>();
26
		HashMap<String, String> parentes = new HashMap<String, String>();
27
		
27
		
28
		final int taillemax = arbreJson.size();
28
		final int taillemax = arbreJson.size();
29
 
29
 
30
		// on crée un arbre vide
30
		// on crée un arbre vide
31
		TreeNode root = new TreeNode();
31
		TreeNode root = new TreeNode();
32
		root.setId(idRacine);
32
		root.setId(idRacine);
33
		root.setText(NomRacine);
33
		root.setText(NomRacine);
34
		String[] usObjRacine = {NomRacine, idRacine};
34
		String[] usObjRacine = {NomRacine, idRacine};
35
		root.setUserObject(usObjRacine);
35
		root.setUserObject(usObjRacine);
36
		arbreMotsCles.setRootNode(root);
36
		arbreMotsCles.setRootNode(root);
37
		
37
		
38
		// le mot clé racine possède le chemin racine ("/")
38
		// le mot clé racine possède le chemin racine ("/")
39
		parentes.put("/", idRacine);
39
		parentes.put("/", idRacine);
40
 
40
 
41
		// pour chacun des élements du tableau
41
		// pour chacun des élements du tableau
42
		for (int j = 0; j < taillemax; j++) {
42
		for (int j = 0; j < taillemax; j++) {
43
			// on extrait les élements du tableau
43
			// on extrait les élements du tableau
44
			if (arbreJson.get(j).isObject() != null) {
44
			if (arbreJson.get(j).isObject() != null) {
45
				JSONObject noeud = (JSONObject) arbreJson.get(j);
45
				JSONObject noeud = (JSONObject) arbreJson.get(j);
46
 
46
 
47
				String idMotCle = noeud.get("id_mot_cle")
47
				String idMotCle = noeud.get("id_mot_cle")
48
						.isString().stringValue();
48
						.isString().stringValue();
49
				String motCle = noeud.get("mot_cle").isString()
49
				String motCle = noeud.get("mot_cle").isString()
50
						.stringValue();
50
						.stringValue();
-
 
51
				
-
 
52
				// le chemin est mis en minuscule afin de fusionner toutes 
-
 
53
				// la variantes minuscules majuscule d'un mot clé
-
 
54
				// qui peuvent poser problème
51
				String chemin = noeud.get("chemin").isString()
55
				String chemin = noeud.get("chemin").isString()
52
						.stringValue();
56
						.stringValue().toLowerCase();
53
				
57
				
54
				String cheminParent = MotsClesUtilitaire.obtenirCheminParent(chemin, motCle);
58
				String cheminParent = MotsClesUtilitaire.obtenirCheminParent(chemin, motCle);
55
				String parent = parentes.get(cheminParent);
59
				String parent = parentes.get(cheminParent);
56
				
60
				
57
				String[] usObj = {motCle, idMotCle};
61
				String[] usObj = {motCle, idMotCle};
58
 
62
 
59
				// et on construit l'arbre en ajoutant les noeuds un à un (qui sont renvoyés
63
				// et on construit l'arbre en ajoutant les noeuds un à un (qui sont renvoyés
60
				// dans l'ordre hierarchique de leur niveau
64
				// dans l'ordre hierarchique de leur niveau
61
				// ce qui permet de les traiter séquentiellement)
65
				// ce qui permet de les traiter séquentiellement)
62
				TreeNode node = new TreeNode();
66
				TreeNode node = new TreeNode();
63
				node.setId(idMotCle);
67
				node.setId(idMotCle);
64
				node.setText(motCle);
68
				node.setText(motCle);
65
				if(afficherCheckbox) {
69
				if(afficherCheckbox) {
66
					node.setChecked(false);
70
					node.setChecked(false);
67
				}
71
				}
-
 
72
 
68
				Node parentNode = arbreMotsCles.getNodeById(parent);
73
				Node parentNode = arbreMotsCles.getNodeById(parent);
69
				node.setUserObject(usObj);
74
				node.setUserObject(usObj);
70
				parentNode.appendChild(node);
75
				parentNode.appendChild(node);
71
				
76
				
72
				parentes.put(chemin, idMotCle);
77
				parentes.put(chemin, idMotCle);
73
				motsCles.put(idMotCle, motCle);
78
				motsCles.put(idMotCle, motCle);
74
			}
79
			}
75
		}
80
		}
76
		Object[] retour = {arbreMotsCles, motsCles};
81
		Object[] retour = {arbreMotsCles, motsCles};
77
		return retour;
82
		return retour;
78
	}
83
	}
79
 
84
 
80
	/**
85
	/**
81
	 * Fonction récursive qui prend deux noeuds d'arbre en paramètre et crée un
86
	 * Fonction récursive qui prend deux noeuds d'arbre en paramètre et crée un
82
	 * copie du sous arbre du premier noeud, qu'elle concatène au deuxième
87
	 * copie du sous arbre du premier noeud, qu'elle concatène au deuxième
83
	 * 
88
	 * 
84
	 * @param ndPereOriginal
89
	 * @param ndPereOriginal
85
	 *            le père des noeuds de l'arbre original
90
	 *            le père des noeuds de l'arbre original
86
	 * @param ndPereCopie
91
	 * @param ndPereCopie
87
	 *            le père qui va recevoir les copies
92
	 *            le père qui va recevoir les copies
88
	 */
93
	 */
89
	public static void copierFilsNoeud(Node ndPereOriginal, TreeNode ndPereCopie, boolean afficherCheckbox) {
94
	public static void copierFilsNoeud(Node ndPereOriginal, TreeNode ndPereCopie, boolean afficherCheckbox) {
90
		if (ndPereCopie != null && ndPereOriginal != null) {
95
		if (ndPereCopie != null && ndPereOriginal != null) {
91
			Node[] ndNodeFils = ndPereOriginal.getChildNodes();
96
			Node[] ndNodeFils = ndPereOriginal.getChildNodes();
92
			
97
			
93
			for (int i = 0; i < ndNodeFils.length; i++) {
98
			for (int i = 0; i < ndNodeFils.length; i++) {
94
 
99
 
95
				String[] usObj = (String[]) ndNodeFils[i].getUserObject();
100
				String[] usObj = (String[]) ndNodeFils[i].getUserObject();
96
				TreeNode child = new TreeNode(usObj[0]);
101
				TreeNode child = new TreeNode(usObj[0]);
97
				child.setId(usObj[1]);
102
				child.setId(usObj[1]);
98
				if(afficherCheckbox) {
103
				if(afficherCheckbox) {
99
					child.setChecked(false);
104
					child.setChecked(false);
100
				}
105
				}
101
				child.setUserObject(usObj);
106
				child.setUserObject(usObj);
102
				ndPereCopie.appendChild(child);
107
				ndPereCopie.appendChild(child);
103
 
108
 
104
				if (!ndNodeFils[i].isLeaf()) {
109
				if (!ndNodeFils[i].isLeaf()) {
105
					copierFilsNoeud(ndNodeFils[i], child, afficherCheckbox);
110
					copierFilsNoeud(ndNodeFils[i], child, afficherCheckbox);
106
				}
111
				}
107
			}
112
			}
108
		}
113
		}
109
	}
114
	}
110
	
115
	
111
	public static TreeNode ajouterNoeud(TreeNode parent, boolean afficherCheckbox) {
116
	public static TreeNode ajouterNoeud(TreeNode parent, boolean afficherCheckbox) {
112
		// on crée un nouveau noeud vide
117
		// on crée un nouveau noeud vide
113
		TreeNode nd = new TreeNode("");
118
		TreeNode nd = new TreeNode("");
114
		nd.setCls("x-view-treenode-keyword");
119
		nd.setCls("x-view-treenode-keyword");
115
		
120
		
116
		if(afficherCheckbox) {
121
		if(afficherCheckbox) {
117
			nd.setChecked(true);
122
			nd.setChecked(true);
118
		}
123
		}
119
		
124
		
120
		// on associe un objet au noeud qui contient des infos
125
		// on associe un objet au noeud qui contient des infos
121
		String[] usObject = new String[2];
126
		String[] usObject = new String[2];
122
		// l'objet contient le nom du noeud
127
		// l'objet contient le nom du noeud
123
		usObject[0] = "";
128
		usObject[0] = "";
124
		usObject[1] = "";
129
		usObject[1] = "";
125
		nd.setUserObject(usObject);
130
		nd.setUserObject(usObject);
126
		
131
		
127
		String cheminTemporaireAjout = parent.getPath()+"/"+nd.getId();
132
		String cheminTemporaireAjout = parent.getPath()+"/"+nd.getId();
128
		nd.setId(cheminTemporaireAjout);
133
		nd.setId(cheminTemporaireAjout);
129
				
134
				
130
		return nd;
135
		return nd;
131
	}
136
	}
132
	
137
	
133
	public static String getChaineCaracteresInterdits() {
138
	public static String getChaineCaracteresInterdits() {
134
		String interdits = "";
139
		String interdits = "";
135
		for (int i = 0; i < MotsClesUtilitaire.caracteresInterdits.length; i++) {
140
		for (int i = 0; i < MotsClesUtilitaire.caracteresInterdits.length; i++) {
136
			interdits += MotsClesUtilitaire.caracteresInterdits[i]+" ";
141
			interdits += MotsClesUtilitaire.caracteresInterdits[i]+" ";
137
		}
142
		}
138
		return interdits;
143
		return interdits;
139
	}
144
	}
140
	
145
	
141
	public static boolean estUnMotCleAutorise(String motCle) {
146
	public static boolean estUnMotCleAutorise(String motCle) {
142
		boolean valide = !motCle.trim().isEmpty();
147
		boolean valide = !motCle.trim().isEmpty();
143
		for (int i = 0; i < MotsClesUtilitaire.caracteresInterdits.length; i++) {
148
		for (int i = 0; i < MotsClesUtilitaire.caracteresInterdits.length; i++) {
144
			if(motCle.indexOf(MotsClesUtilitaire.caracteresInterdits[i]) != -1) {
149
			if(motCle.indexOf(MotsClesUtilitaire.caracteresInterdits[i]) != -1) {
145
				valide = false;
150
				valide = false;
146
				break;
151
				break;
147
			}
152
			}
148
		}		
153
		}		
149
		return valide;
154
		return valide;
150
	}
155
	}
151
}
156
}