]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e3c6127b8d89aa69476d5a62f836b49dd078756f
[tmp/jakarta-migration.git] /
1 /**     \r
2  * RelationServiceNuxeoImpl.java\r
3  *\r
4  * {Purpose of This Class}\r
5  *\r
6  * {Other Notes Relating to This Class (Optional)}\r
7  *\r
8  * $LastChangedBy: $\r
9  * $LastChangedRevision: $\r
10  * $LastChangedDate: $\r
11  *\r
12  * This document is a part of the source code and related artifacts\r
13  * for CollectionSpace, an open source collections management system\r
14  * for museums and related institutions:\r
15  *\r
16  * http://www.collectionspace.org\r
17  * http://wiki.collectionspace.org\r
18  *\r
19  * Copyright © 2009 {Contributing Institution}\r
20  *\r
21  * Licensed under the Educational Community License (ECL), Version 2.0.\r
22  * You may not use this file except in compliance with this License.\r
23  *\r
24  * You may obtain a copy of the ECL 2.0 License at\r
25  * https://source.collectionspace.org/collection-space/LICENSE.txt\r
26  */\r
27 package org.collectionspace.services.relation.nuxeo;\r
28 \r
29 //import java.io.ByteArrayInputStream;\r
30 import java.io.IOException;\r
31 //import java.io.Serializable;\r
32 //import java.util.ArrayList;\r
33 //import java.util.Arrays;\r
34 //import java.util.HashMap;\r
35 import java.util.List;\r
36 //import java.util.Map;\r
37 //import java.util.Iterator;\r
38 \r
39 import org.collectionspace.services.common.repository.DocumentException;\r
40 //import org.collectionspace.services.nuxeo.NuxeoRESTClient;\r
41 import org.collectionspace.services.nuxeo.CollectionSpaceServiceNuxeoImpl;\r
42 import org.collectionspace.services.common.relation.nuxeo.RelationUtilsNuxeoImpl;\r
43 import org.collectionspace.services.relation.Relation;\r
44 import org.collectionspace.services.relation.RelationService;\r
45 //import org.collectionspace.services.relation.RelationList;\r
46 //import org.collectionspace.services.relation.RelationshipType;\r
47 //import org.collectionspace.services.RelationJAXBSchema;\r
48 import org.collectionspace.services.nuxeo.util.NuxeoUtils;\r
49 import org.collectionspace.services.common.relation.RelationsManager;\r
50 //import org.collectionspace.services.common.ServiceMain;\r
51 \r
52 import org.dom4j.Document;\r
53 //import org.dom4j.Element;\r
54 //import org.dom4j.dom.DOMDocument;\r
55 //import org.dom4j.DocumentException;\r
56 //import org.dom4j.io.SAXReader;\r
57 //import org.restlet.resource.Representation;\r
58 \r
59 import org.nuxeo.common.utils.IdUtils;\r
60 //import org.nuxeo.ecm.core.api.ClientException;\r
61 import org.nuxeo.ecm.core.api.IdRef;\r
62 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;\r
63 import org.nuxeo.ecm.core.api.DocumentModel;\r
64 //import org.nuxeo.ecm.core.api.DocumentModelList;\r
65 //import org.nuxeo.ecm.core.api.model.DocumentPart;\r
66 import org.nuxeo.ecm.core.api.DocumentRef;\r
67 \r
68 \r
69 \r
70 /**\r
71  * @author remillet\r
72  * \r
73  */\r
74 public class RelationServiceNuxeoImpl extends\r
75                 CollectionSpaceServiceNuxeoImpl implements RelationService {\r
76 \r
77         // replace WORKSPACE_UID for resource workspace\r
78         //      static String CS_RELATION_WORKSPACE_UID = "55f99358-5dbe-4462-8000-c5c3c2063919";\r
79 \r
80         /**\r
81          * Delete relation.\r
82          * \r
83          * @param csid the csid\r
84          * \r
85          * @return the document\r
86          * \r
87          * @throws DocumentException the document exception\r
88          * @throws IOException Signals that an I/O exception has occurred.\r
89          */\r
90         public Document deleteRelation(String csid) throws DocumentException {\r
91                 Document result = null;\r
92 \r
93                 RepositoryInstance repoSession = null;\r
94                 try {\r
95                         repoSession = getRepositorySession();\r
96                         result = deleteDocument(repoSession, csid);\r
97                 } catch (Exception e) {\r
98                         if (logger.isDebugEnabled()) {\r
99                                 logger.debug("Caught exception ", e);\r
100                         }\r
101                         throw new DocumentException(e);\r
102                 } finally {\r
103                         if (repoSession != null) {\r
104                                 releaseRepositorySession(repoSession);\r
105                         }\r
106                 }\r
107 \r
108                 return result;\r
109         }\r
110         \r
111         /**\r
112          * Gets the relation.\r
113          * \r
114          * @param csid the csid\r
115          * \r
116          * @return the relation\r
117          * \r
118          * @throws DocumentException the document exception\r
119          * @throws IOException Signals that an I/O exception has occurred.\r
120          */\r
121         public Document getRelation(String csid)\r
122                         throws DocumentException, IOException {\r
123                 Document result = null;\r
124                 RepositoryInstance repoSession = null;\r
125                 \r
126                 try {\r
127                         repoSession = getRepositorySession();\r
128                         result = NuxeoUtils.getDocument(repoSession, csid);\r
129                 } catch (Exception e) {\r
130                         if (logger.isDebugEnabled()) {\r
131                                 logger.debug("Caught exception ", e);\r
132                         }\r
133                         throw new DocumentException(e);\r
134                 } finally {\r
135                         if (repoSession != null) {\r
136                                 releaseRepositorySession(repoSession);\r
137                         }\r
138                 }\r
139                 \r
140                 // Dump out the contents of the result\r
141                 if (logger.isDebugEnabled() == true) {\r
142                         System.out.println(result.asXML());\r
143                 }\r
144                 \r
145                 return result;\r
146         }\r
147 \r
148         /**\r
149          * Gets the relation list.\r
150          * \r
151          * @return the relation list\r
152          * \r
153          * @throws DocumentException the document exception\r
154          * @throws IOException Signals that an I/O exception has occurred.\r
155          */\r
156         public Document getRelationList()\r
157                         throws DocumentException, IOException {\r
158                 Document result = null;\r
159                 RepositoryInstance repoSession = null;\r
160 \r
161                 try {\r
162                         repoSession = getRepositorySession();\r
163                         List<Relation> relationList = RelationsManager.getRelationships(repoSession);\r
164                         \r
165                         result = RelationUtilsNuxeoImpl.getDocument(relationList);\r
166                 } catch (Exception e) {\r
167                         if (logger.isDebugEnabled()) {\r
168                                 logger.debug("Caught exception ", e);\r
169                         }\r
170                         throw new DocumentException(e);\r
171                 } finally {\r
172                         if (repoSession != null) {\r
173                                 releaseRepositorySession(repoSession);\r
174                         }\r
175                 }\r
176                 \r
177                 if (logger.isDebugEnabled() == true) {\r
178                         System.out.println(result.asXML());\r
179                 }\r
180 \r
181                 return result;\r
182         }\r
183                 \r
184         /* (non-Javadoc)\r
185          * @see org.collectionspace.services.RelationService#getRelationList(java.lang.String, java.lang.String, java.lang.String)\r
186          */\r
187         public Document getRelationList(String subjectCsid,\r
188                         String predicate, String objectCsid)\r
189                                 throws DocumentException, IOException {\r
190                 Document result = null;\r
191                 RepositoryInstance repoSession = null;\r
192 \r
193                 try {\r
194                         repoSession = getRepositorySession();\r
195                         List<Relation> relationList = RelationsManager.getRelationships(repoSession,\r
196                                         subjectCsid, predicate, objectCsid);\r
197                         \r
198                         result = RelationUtilsNuxeoImpl.getDocument(relationList);\r
199                 } catch (Exception e) {\r
200                         if (logger.isDebugEnabled()) {\r
201                                 logger.debug("Caught exception ", e);\r
202                         }\r
203                         throw new DocumentException(e);\r
204                 } finally {\r
205                         if (repoSession != null) {\r
206                                 releaseRepositorySession(repoSession);\r
207                         }\r
208                 }\r
209                 \r
210                 if (logger.isDebugEnabled() == true) {\r
211                         System.out.println(result.asXML());\r
212                 }\r
213 \r
214                 return result;\r
215                 }\r
216 \r
217         // Create a new relation document\r
218         /**\r
219          * Post relation.\r
220          * \r
221          * @param co the co\r
222          * \r
223          * @return the document\r
224          * \r
225          * @throws DocumentException the document exception\r
226          * @throws IOException Signals that an I/O exception has occurred.\r
227          */\r
228         public Document postRelation(Relation co) throws DocumentException,\r
229                         IOException {\r
230                 Document result = null;\r
231                 RepositoryInstance repoSession = null;\r
232                 \r
233                 try {\r
234                         repoSession = getRepositorySession();\r
235             DocumentModel resultDocModel = RelationUtilsNuxeoImpl.createRelationship(repoSession, co);\r
236             repoSession.save();\r
237             result = NuxeoUtils.getDocument(repoSession, resultDocModel);\r
238                 } catch (Exception e) {\r
239                         if (logger.isDebugEnabled() == true) {\r
240                                 logger.debug("Caught exception ", e);\r
241                         }\r
242                         throw new DocumentException(e);\r
243                 } finally {\r
244                         if (repoSession != null) {\r
245                                 releaseRepositorySession(repoSession);\r
246                         }\r
247                 }\r
248 \r
249                 // Dump out the contents of the result\r
250                 if (logger.isDebugEnabled() == true) {\r
251                         System.out.println(result.asXML());\r
252                 }\r
253                 \r
254                 return result;\r
255         }\r
256         \r
257         /**\r
258          * Put relation.\r
259          * \r
260          * @param csid the csid\r
261          * @param theUpdate the the update\r
262          * \r
263          * @return the document\r
264          * \r
265          * @throws DocumentException the document exception\r
266          * @throws IOException Signals that an I/O exception has occurred.\r
267          */\r
268         public Document putRelation(String csid, Relation theUpdate)\r
269                         throws DocumentException, IOException {\r
270                 \r
271                 Document result = null;\r
272         RepositoryInstance repoSession = null;\r
273         try{\r
274             repoSession = getRepositorySession();\r
275             DocumentRef documentRef = new IdRef(csid);\r
276             DocumentModel documentModel = repoSession.getDocument(documentRef);\r
277             RelationUtilsNuxeoImpl.fillDocModelFromRelation(theUpdate, documentModel);\r
278             repoSession.saveDocument(documentModel);\r
279             repoSession.save();\r
280             result = NuxeoUtils.getDocument(repoSession, documentModel);\r
281         } catch(Exception e){\r
282                         if (logger.isDebugEnabled()) {\r
283                                 logger.debug("Caught exception ", e);\r
284                         }\r
285                         throw new DocumentException(e);\r
286         } finally{\r
287             if(repoSession != null){\r
288                 releaseRepositorySession(repoSession);\r
289             }\r
290         }\r
291                 \r
292                 return result;\r
293         }       \r
294 \r
295 }\r