]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
54bb6cee1912a348a6f5893ed457c94db4ca07f0
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.collectionobject.nuxeo;
25
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.List;
29
30 //import org.collectionspace.services.jaxb.AbstractCommonList;
31
32 import org.collectionspace.services.CollectionObjectListItemJAXBSchema;
33 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
34 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList;
35 import org.collectionspace.services.collectionobject.CollectionobjectsCommonList.CollectionObjectListItem;
36 import org.collectionspace.services.common.document.DocumentUtils;
37 import org.collectionspace.services.common.document.DocumentWrapper;
38 import org.collectionspace.services.jaxb.AbstractCommonList;
39 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
40 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
41 import org.nuxeo.ecm.core.api.DocumentModel;
42 import org.nuxeo.ecm.core.api.DocumentModelList;
43 import org.nuxeo.ecm.core.schema.types.ComplexType;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * CollectionObjectDocumentModelHandler
49  *
50  * $LastChangedRevision: $
51  * $LastChangedDate: $
52  */
53 public class CollectionObjectDocumentModelHandler
54         extends RemoteDocumentModelHandlerImpl<CollectionobjectsCommon, CollectionobjectsCommonList> {
55
56     /** The logger. */
57     private final Logger logger = LoggerFactory.getLogger(CollectionObjectDocumentModelHandler.class);
58     /**
59      * collectionObject is used to stash JAXB object to use when handle is called
60      * for Action.CREATE, Action.UPDATE or Action.GET
61      */
62     private CollectionobjectsCommon collectionObject;
63     /**
64      * collectionObjectList is stashed when handle is called
65      * for ACTION.GET_ALL
66      */
67     private CollectionobjectsCommonList collectionObjectList;
68
69     /**
70      * getCommonPart get associated CollectionobjectsCommon
71      * @return
72      */
73     @Override
74     public CollectionobjectsCommon getCommonPart() {
75         return collectionObject;
76     }
77
78     /**
79      * setCommonPart set associated collectionobject
80      * @param collectionObject
81      */
82     @Override
83     public void setCommonPart(CollectionobjectsCommon collectionObject) {
84         this.collectionObject = collectionObject;
85     }
86
87     /**
88      * getCollectionobjectsCommonList get associated CollectionobjectsCommon (for index/GET_ALL)
89      * @return
90      */
91     @Override
92     public CollectionobjectsCommonList getCommonPartList() {
93         return collectionObjectList;
94     }
95
96     /* (non-Javadoc)
97      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#setCommonPartList(java.lang.Object)
98      */
99     @Override
100     public void setCommonPartList(CollectionobjectsCommonList collectionObjectList) {
101         this.collectionObjectList = collectionObjectList;
102     }
103
104     /* (non-Javadoc)
105      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
106      */
107     @Override
108     public CollectionobjectsCommon extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc)
109             throws Exception {
110         throw new UnsupportedOperationException();
111     }
112
113     /* (non-Javadoc)
114      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
115      */
116     @Override
117     public void fillCommonPart(CollectionobjectsCommon co, DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
118         throw new UnsupportedOperationException();
119     }
120
121     /* (non-Javadoc)
122      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
123      */
124     @Override
125     public CollectionobjectsCommonList extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
126         CollectionobjectsCommonList coList = this.extractPagingInfo(new CollectionobjectsCommonList(),
127                         wrapDoc);
128         AbstractCommonList commonList = (AbstractCommonList) coList;
129         commonList.setFieldsReturned("objectNumber|objectName|title|responsibleDepartment|uri|csid");
130         List<CollectionobjectsCommonList.CollectionObjectListItem> list = coList.getCollectionObjectListItem();
131         Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
132         String label = getServiceContext().getCommonPartLabel();
133         while(iter.hasNext()){
134             DocumentModel docModel = iter.next();
135             CollectionObjectListItem coListItem = new CollectionObjectListItem();
136             try {
137                     coListItem.setObjectNumber((String) docModel.getProperty(label,
138                                 CollectionObjectListItemJAXBSchema.OBJECT_NUMBER));
139                     List names = (List) docModel.getProperty(label,
140                                 CollectionObjectListItemJAXBSchema.OBJECT_NAME_LIST);
141                     if(names!=null && !names.isEmpty()) {
142                         HashMap<String,Object> firstNameInfo = (HashMap<String,Object>)names.get(0);
143                             coListItem.setObjectName((String)firstNameInfo.get(
144                                         CollectionObjectListItemJAXBSchema.OBJECT_NAME) );
145                     }
146                     coListItem.setTitle((String) docModel.getProperty(label,
147                                 CollectionObjectListItemJAXBSchema.TITLE));
148                     Object respDepts = docModel.getProperty(label,
149                                 CollectionObjectListItemJAXBSchema.RESPONSIBLE_DEPARTMENTS);
150                         coListItem.setResponsibleDepartment(DocumentUtils.getFirstString(respDepts));
151                     
152                     String id = NuxeoUtils.extractId(docModel.getPathAsString());
153                     coListItem.setUri(getServiceContextPath() + id);
154                     coListItem.setCsid(id);
155             } catch (ClassCastException cce) {
156                 throw new RuntimeException("Unexpected schema structure encountered", cce);
157             } catch (Exception e) {
158                 throw new RuntimeException("Problem encountered retrieving values", e);
159             }
160             list.add(coListItem);
161         }
162
163         return coList;
164     }
165
166     /* (non-Javadoc)
167      * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#fillAllParts(org.collectionspace.services.common.document.DocumentWrapper)
168      */
169     @Override
170     public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
171
172         super.fillAllParts(wrapDoc, action);
173         fillDublinCoreObject(wrapDoc); //dublincore might not be needed in future
174     }
175
176     /**
177      * Fill dublin core object.
178      *
179      * @param wrapDoc the wrap doc
180      * @throws Exception the exception
181      */
182     private void fillDublinCoreObject(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
183         DocumentModel docModel = wrapDoc.getWrappedObject();
184         //FIXME property setter should be dynamically set using schema inspection
185         //so it does not require hard coding
186         // a default title for the Dublin Core schema
187         docModel.setPropertyValue("dublincore:title", CollectionObjectConstants.NUXEO_DC_TITLE);
188     }
189
190     /* (non-Javadoc)
191      * @see org.collectionspace.services.common.document.AbstractMultipartDocumentHandlerImpl#getQProperty(java.lang.String)
192      */
193     @Override
194     public String getQProperty(String prop) {
195         return CollectionObjectConstants.NUXEO_SCHEMA_NAME + ":" + prop;
196     }
197 }
198