1 | package com.medias.xml.metadata; |
---|
2 | |
---|
3 | |
---|
4 | import com.medias.ConversionException; |
---|
5 | import com.medias.utils.Entity; |
---|
6 | import com.medias.xml.XMLBuilder; |
---|
7 | import com.medias.xml.hierarchie.Hierarchie; |
---|
8 | import org.jdom.Element; |
---|
9 | import org.jdom.JDOMException; |
---|
10 | |
---|
11 | import java.io.IOException; |
---|
12 | import java.util.Iterator; |
---|
13 | |
---|
14 | |
---|
15 | /** |
---|
16 | * @author combaz |
---|
17 | * Created on 10 févr. 2005 |
---|
18 | * |
---|
19 | */ |
---|
20 | public class Metadata implements Entity |
---|
21 | { |
---|
22 | |
---|
23 | /** |
---|
24 | * |
---|
25 | */ |
---|
26 | private static final long serialVersionUID = 1L; |
---|
27 | public static final int CONTACT_OK = 0; |
---|
28 | public static final int CONTACT_PI = 1; |
---|
29 | public static final int CONTACT_TECH = 2; |
---|
30 | public static final int CONTACT_WRONG = 3; |
---|
31 | |
---|
32 | private String absolutePath; |
---|
33 | private String relativePath; |
---|
34 | private String xmlFileName; |
---|
35 | |
---|
36 | private Presentation pres; |
---|
37 | private Description desc; |
---|
38 | private Contenu contenuPrevu; |
---|
39 | private Integration integ; |
---|
40 | // private Contenu contenuActuel; |
---|
41 | |
---|
42 | public Metadata() { |
---|
43 | super(); |
---|
44 | this.absolutePath = null; |
---|
45 | this.relativePath = null; |
---|
46 | this.xmlFileName = null; |
---|
47 | this.pres = new Presentation(); |
---|
48 | this.desc = new Description(); |
---|
49 | this.contenuPrevu = new Contenu(); |
---|
50 | this.integ = new Integration(); |
---|
51 | // this.contenuActuel = new Contenu(); |
---|
52 | } |
---|
53 | |
---|
54 | public Metadata(String path) throws IOException, JDOMException, ConversionException { |
---|
55 | this.pres = new Presentation(); |
---|
56 | this.desc = new Description(); |
---|
57 | this.contenuPrevu = new Contenu(); |
---|
58 | this.integ = new Integration(); |
---|
59 | // this.contenuActuel = new Contenu(); |
---|
60 | |
---|
61 | this.setAbsolutePath(path.substring(0,path.lastIndexOf("/"))); |
---|
62 | |
---|
63 | XMLBuilder builder = new XMLBuilder(); |
---|
64 | builder.readXml(path); |
---|
65 | this.fromXmlElement(builder.getRoot()); |
---|
66 | this.setRelativePath(this.getPres().getCat() + "/" + this.getPres().getNom()); |
---|
67 | this.setXmlFileName(this.pres.getNom() + "_metadata.com.medias.xml"); |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | /** |
---|
72 | * @return Returns the contenuActuel. |
---|
73 | */ |
---|
74 | // public Contenu getContenuActuel() { |
---|
75 | // return contenuActuel; |
---|
76 | // } |
---|
77 | // /** |
---|
78 | // * @param contenuActuel The contenuActuel to set. |
---|
79 | // */ |
---|
80 | // public void setContenuActuel(Contenu contenuActuel) { |
---|
81 | // this.contenuActuel = contenuActuel; |
---|
82 | // } |
---|
83 | |
---|
84 | /** |
---|
85 | * @return Returns the contenuPrevu. |
---|
86 | */ |
---|
87 | public Contenu getContenuPrevu() { |
---|
88 | return contenuPrevu; |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * @param contenuPrevu The contenuPrevu to set. |
---|
93 | */ |
---|
94 | public void setContenuPrevu(Contenu contenuPrevu) { |
---|
95 | this.contenuPrevu = contenuPrevu; |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * @return Returns the desc. |
---|
100 | */ |
---|
101 | public Description getDesc() { |
---|
102 | return desc; |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | * @param desc The desc to set. |
---|
107 | */ |
---|
108 | public void setDesc(Description desc) { |
---|
109 | this.desc = desc; |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * @return Returns the integ. |
---|
114 | */ |
---|
115 | public Integration getInteg() { |
---|
116 | return integ; |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | * @param integ The integ to set. |
---|
121 | */ |
---|
122 | public void setInteg(Integration integ) { |
---|
123 | this.integ = integ; |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * @return Returns the pres. |
---|
128 | */ |
---|
129 | public Presentation getPres() { |
---|
130 | return pres; |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * @param pres The pres to set. |
---|
135 | */ |
---|
136 | public void setPres(Presentation pres) { |
---|
137 | this.pres = pres; |
---|
138 | this.updateContacts(); |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | * @return Returns the absolutePath. |
---|
143 | */ |
---|
144 | public String getAbsolutePath() { |
---|
145 | return absolutePath; |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | * @param absolutePath The absolutePath to set. |
---|
150 | */ |
---|
151 | public void setAbsolutePath(String absolutePath) { |
---|
152 | this.absolutePath = absolutePath; |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | * @return Returns the relativePath. |
---|
157 | */ |
---|
158 | public String getRelativePath() { |
---|
159 | this.relativePath = this.pres.getCat() + "/" + this.pres.getNom(); |
---|
160 | return relativePath; |
---|
161 | } |
---|
162 | /** |
---|
163 | * @param relativePath The relativePath to set. |
---|
164 | */ |
---|
165 | public void setRelativePath(String relativePath) { |
---|
166 | this.relativePath = relativePath; |
---|
167 | } |
---|
168 | /** |
---|
169 | * @return Returns the xmlFileName. |
---|
170 | */ |
---|
171 | public String getXmlFileName() { |
---|
172 | return xmlFileName; |
---|
173 | } |
---|
174 | /** |
---|
175 | * @param xmlFileName The xmlFileName to set. |
---|
176 | */ |
---|
177 | public void setXmlFileName(String xmlFileName) { |
---|
178 | this.xmlFileName = xmlFileName; |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * Sets the path of the com.medias.xml file where metadata are written |
---|
183 | * @param uploadroot The root directory where data and metadata are saved. |
---|
184 | */ |
---|
185 | public void setXmlFile(String uploadroot) { |
---|
186 | this.setRelativePath(this.pres.getCat() + "/" + this.pres.getNom()); |
---|
187 | this.setAbsolutePath(uploadroot + "/" + this.relativePath); |
---|
188 | this.setXmlFileName(this.pres.getNom() + "_metadata.com.medias.xml"); |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | * Permet de créer un nouveau jeu de données : |
---|
193 | * <ul> |
---|
194 | * <li>definit l'endroit ou le fichier com.medias.xml de metadonnes sera stocke</li> |
---|
195 | * <li>met a jour le fichier de description de l'arborescence</li> |
---|
196 | * <li>cree le dossier correspondant au nouveau jeu dans le site de depot</li> |
---|
197 | * <li>cree le fichier com.medias.xml de metadonnees et le sauve au bon endroit.</li> |
---|
198 | * <li>met à jour la vue représentant l'arborescence.</li> |
---|
199 | * </ul> |
---|
200 | * @param tree |
---|
201 | * @throws IOException |
---|
202 | * @throws JDOMException |
---|
203 | */ |
---|
204 | public void createNewSet(Hierarchie tree) throws Exception { |
---|
205 | this.setXmlFile(tree.getDataRoot()); |
---|
206 | tree.saveMetadata(this.relativePath,this.xmlFileName); |
---|
207 | this.createXmlFile(); |
---|
208 | tree.createTreeView(); |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | * Cree le fichier com.medias.xml de metadonnees et le sauve au bon endroit. |
---|
213 | * @throws IOException |
---|
214 | */ |
---|
215 | public void createXmlFile() throws IOException { |
---|
216 | Element root = this.toXmlElement("metadata"); |
---|
217 | XMLBuilder builder = new XMLBuilder(root); |
---|
218 | builder.writeXml(this.getAbsolutePath() + "/" + this.getXmlFileName()); |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * Synchronise la liste de contacts (enregistrée dans la partie "présentation") avec les responsables |
---|
223 | * thématique et technique, enregistrés dans la partie "description". |
---|
224 | * @return |
---|
225 | */ |
---|
226 | public int updateContacts() { |
---|
227 | boolean delete_pi = true; |
---|
228 | boolean delete_tech = true; |
---|
229 | |
---|
230 | int returnValue = CONTACT_OK; |
---|
231 | |
---|
232 | Iterator<Contact> it = this.getPres().getContactsList().iterator(); |
---|
233 | while(it.hasNext()){ |
---|
234 | Contact pers = it.next(); |
---|
235 | pers.formToModel(); |
---|
236 | if(pers.getRole().equals("pi")) { |
---|
237 | if (delete_pi == true) { |
---|
238 | this.desc.setPi(pers); |
---|
239 | delete_pi = false; |
---|
240 | } |
---|
241 | else { |
---|
242 | // on retire de la présentation le contact en double PI / Tech |
---|
243 | // it.remove (); |
---|
244 | returnValue = CONTACT_PI; |
---|
245 | break; |
---|
246 | } |
---|
247 | } |
---|
248 | else if(pers.getRole().equals("tech")) { |
---|
249 | if (delete_tech == true) { |
---|
250 | this.desc.setTech(pers); |
---|
251 | delete_tech = false; |
---|
252 | } |
---|
253 | else { |
---|
254 | // on retire de la présentation le contact en double PI / Tech |
---|
255 | // it.remove (); |
---|
256 | returnValue = CONTACT_TECH; |
---|
257 | break; |
---|
258 | } |
---|
259 | } |
---|
260 | // |
---|
261 | if (!pers.getMail().equals("") && pers.getMdp().equals("")) { |
---|
262 | returnValue = CONTACT_WRONG; |
---|
263 | break; |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | if (delete_pi) |
---|
268 | this.desc.setPi(new Contact()); |
---|
269 | if (delete_tech) |
---|
270 | this.desc.setTech(new Contact()); |
---|
271 | return returnValue; |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | /** |
---|
276 | * Crée un élément JDOM représentant la structure de la métadonnée. |
---|
277 | * @param title Le nom à donner à l'élément retourné |
---|
278 | * @return Retourne l'élément JDOM représentant la structure de la métadonnée. |
---|
279 | */ |
---|
280 | public Element toXmlElement (String title) { |
---|
281 | Element p = this.pres.toXmlElement ("presentation"); |
---|
282 | Element d = this.desc.toXmlElement ("description"); |
---|
283 | Element cp = this.contenuPrevu.toXmlElement ("contenu_prevu"); |
---|
284 | Element i = this.integ.toXmlElement ("integration"); |
---|
285 | // Element ca = this.contenuActuel.toXmlElement("contenu_actuel"); |
---|
286 | |
---|
287 | Element metadata = new Element (title); |
---|
288 | metadata.addContent (p); |
---|
289 | metadata.addContent (d); |
---|
290 | metadata.addContent (cp); |
---|
291 | metadata.addContent (i); |
---|
292 | // metadata.addContent(ca); |
---|
293 | return metadata; |
---|
294 | } |
---|
295 | |
---|
296 | /** |
---|
297 | * Met à jour tous les champs à partir de l'élément JDOM passé en paramÚtre. |
---|
298 | */ |
---|
299 | public void fromXmlElement (Element el) throws ConversionException { |
---|
300 | Element p = el.getChild ("presentation"); |
---|
301 | Element d = el.getChild ("description"); |
---|
302 | Element cp = el.getChild ("contenu_prevu"); |
---|
303 | Element i = el.getChild ("integration"); |
---|
304 | // Element ca = el.getChild("contenu_actuel"); |
---|
305 | |
---|
306 | if((p == null) || (d == null) || (cp == null) || (i == null)) |
---|
307 | //||(ca==null)) |
---|
308 | throw new ConversionException (ConversionException.ERROR_fromXml + "Metadata !"); |
---|
309 | |
---|
310 | else { |
---|
311 | this.pres.fromXmlElement (p); |
---|
312 | this.desc.fromXmlElement (d); |
---|
313 | this.contenuPrevu.fromXmlElement (cp); |
---|
314 | this.integ.fromXmlElement (i); |
---|
315 | // this.contenuActuel.fromXmlElement(ca); |
---|
316 | } |
---|
317 | } |
---|
318 | } |
---|