]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
1570184da218ad700da029ef8365c02e99f652ea
[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.acquisition.nuxeo;
25
26 import java.util.Iterator;
27 import java.util.List;
28
29 import org.collectionspace.services.AcquisitionJAXBSchema;
30 import org.collectionspace.services.common.repository.DocumentWrapper;
31 import org.collectionspace.services.acquisition.Acquisition;
32 import org.collectionspace.services.acquisition.AcquisitionList;
33 import org.collectionspace.services.acquisition.AcquisitionList.AcquisitionListItem;
34 import org.collectionspace.services.nuxeo.client.java.DocumentModelHandler;
35 import org.collectionspace.services.acquisition.nuxeo.AcquisitionConstants;
36
37 import org.nuxeo.ecm.core.api.DocumentModel;
38 import org.nuxeo.ecm.core.api.DocumentModelList;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * AcquisitionDocumentModelHandler
44  *
45  * $LastChangedRevision: $
46  * $LastChangedDate: $
47  */
48 public class AcquisitionDocumentModelHandler
49         extends DocumentModelHandler<Acquisition, AcquisitionList> {
50
51     private final Logger logger = LoggerFactory.getLogger(AcquisitionDocumentModelHandler.class);
52     /**
53      * acquisition is used to stash JAXB object to use when handle is called
54      * for Action.CREATE, Action.UPDATE or Action.GET
55      */
56     private Acquisition acquisition;
57     /**
58      * acquisitionList is stashed when handle is called
59      * for ACTION.GET_ALL
60      */
61     private AcquisitionList acquisitionList;
62
63     @Override
64     public void prepare(Action action) throws Exception {
65         //no specific action needed
66     }
67
68     /**
69      * getCommonObject get associated acquisition
70      * @return
71      */
72     @Override
73     public Acquisition getCommonObject() {
74         return acquisition;
75     }
76
77     /**
78      * setCommonObject set associated acquisition
79      * @param acquisition
80      */
81     @Override
82     public void setCommonObject(Acquisition acquisition) {
83         this.acquisition = acquisition;
84     }
85
86     /**
87      * getAcquisitionList get associated acquisition (for index/GET_ALL)
88      * @return
89      */
90     @Override
91     public AcquisitionList getCommonObjectList() {
92         return acquisitionList;
93     }
94
95     @Override
96     public void setCommonObjectList(AcquisitionList acquisitionList) {
97         this.acquisitionList = acquisitionList;
98     }
99
100     @Override
101     public Acquisition extractCommonObject(DocumentWrapper wrapDoc)
102             throws Exception {
103         DocumentModel docModel = (DocumentModel) wrapDoc.getWrappedObject();
104         Acquisition acquisitionObject = new Acquisition();
105
106         //FIXME property get should be dynamically set using schema inspection
107         //so it does not require hard coding
108
109         // acquisition core values
110         acquisitionObject.setAccessiondate((String)docModel.getPropertyValue(
111                 getQProperty(AcquisitionJAXBSchema.ACCESSIONDATE)));
112
113         return acquisitionObject;
114     }
115
116     @Override
117     public void fillCommonObject(Acquisition acquisitionObject, DocumentWrapper wrapDoc) throws Exception {
118         DocumentModel docModel = (DocumentModel) wrapDoc.getWrappedObject();
119         //FIXME property setter should be dynamically set using schema inspection
120         //so it does not require hard coding
121
122         // a default title for the Dublin Core schema
123         docModel.setPropertyValue("dublincore:title", AcquisitionConstants.NUXEO_DC_TITLE);
124
125         // acquisition core values
126         if(acquisitionObject.getAccessiondate() != null){
127             docModel.setPropertyValue(getQProperty(
128                     AcquisitionJAXBSchema.ACCESSIONDATE), acquisitionObject.getAccessiondate());
129         }
130     }
131
132     @Override
133     public AcquisitionList extractCommonObjectList(DocumentWrapper wrapDoc) throws Exception {
134         DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject();
135
136         AcquisitionList coList = new AcquisitionList();
137         List<AcquisitionList.AcquisitionListItem> list = coList.getAcquisitionListItem();
138
139         //FIXME: iterating over a long list of documents is not a long term
140         //strategy...need to change to more efficient iterating in future
141         Iterator<DocumentModel> iter = docList.iterator();
142         while(iter.hasNext()){
143             DocumentModel docModel = iter.next();
144             AcquisitionListItem listItem = new AcquisitionListItem();
145             listItem.setAccessiondate((String)docModel.getPropertyValue(
146                     getQProperty(AcquisitionJAXBSchema.ACCESSIONDATE)));
147             //need fully qualified context for URI
148             String id = docModel.getId();
149             listItem.setUri("/acquisitions/" + id);
150             listItem.setCsid(id);
151             list.add(listItem);
152         }
153
154         return coList;
155     }
156
157     @Override
158     public void fillCommonObjectList(AcquisitionList obj, DocumentWrapper wrapDoc) throws Exception {
159         throw new UnsupportedOperationException();
160     }
161
162     /* (non-Javadoc)
163      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#getDocumentType()
164      */
165     public String getDocumentType() {
166         return AcquisitionConstants.NUXEO_DOCTYPE;
167     }
168     
169     /**
170      * getQProperty converts the given property to qualified schema property
171      * @param prop
172      * @return
173      */
174     private String getQProperty(String prop) {
175         return AcquisitionConstants.NUXEO_SCHEMA_NAME + ":" + prop;
176     }
177 }
178