]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
93f7634ae15284df6ea328db483e169f215e7b1f
[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.location.nuxeo;
25
26 import java.util.Iterator;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.Map;
31
32 import org.collectionspace.services.LocationJAXBSchema;
33 import org.collectionspace.services.client.LocationAuthorityClient;
34 import org.collectionspace.services.common.document.DocumentFilter;
35 import org.collectionspace.services.common.document.DocumentWrapper;
36 import org.collectionspace.services.common.service.ObjectPartType;
37 import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
38 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityItemDocumentModelHandler;
39 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
40 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
41 import org.collectionspace.services.jaxb.AbstractCommonList;
42 import org.collectionspace.services.location.LocationsCommon;
43 import org.collectionspace.services.location.LocationsCommonList;
44 import org.collectionspace.services.location.LocationsCommonList.LocationListItem;
45 import org.nuxeo.ecm.core.api.DocumentModel;
46 import org.nuxeo.ecm.core.api.DocumentModelList;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * LocationDocumentModelHandler
52  *
53  * $LastChangedRevision: $
54  * $LastChangedDate: $
55  */
56 /**
57  * @author pschmitz
58  *
59  */
60 public class LocationDocumentModelHandler
61         extends AuthorityItemDocumentModelHandler<LocationsCommon, LocationsCommonList> {
62
63     /** The logger. */
64     private final Logger logger = LoggerFactory.getLogger(LocationDocumentModelHandler.class);
65     /**
66      * Common part schema label
67      */
68     private static final String COMMON_PART_LABEL = "locations_common";
69     
70     public LocationDocumentModelHandler() {
71         super(COMMON_PART_LABEL);
72     }
73
74     @Override
75     public String getAuthorityServicePath(){
76         return LocationAuthorityClient.SERVICE_PATH_COMPONENT;    //  CSPACE-3932
77     }
78         
79     /* (non-Javadoc)
80      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
81      */
82     @Override
83     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
84         // first fill all the parts of the document
85         super.handleCreate(wrapDoc);            
86         handleDisplayNames(wrapDoc.getWrappedObject());
87     }
88     
89     /* (non-Javadoc)
90      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
91      */
92     @Override
93     public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
94         super.handleUpdate(wrapDoc);
95         handleDisplayNames(wrapDoc.getWrappedObject());
96     }
97
98     /**
99      * Handle display name.
100      *
101      * @param docModel the doc model
102      * @throws Exception the exception
103      */
104     private void handleDisplayNames(DocumentModel docModel) throws Exception {
105         String commonPartLabel = getServiceContext().getCommonPartLabel("locations");
106         Boolean displayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
107                         LocationJAXBSchema.DISPLAY_NAME_COMPUTED);
108         Boolean shortDisplayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
109                         LocationJAXBSchema.SHORT_DISPLAY_NAME_COMPUTED);
110         if(displayNameComputed==null)
111                 displayNameComputed = true;
112         if(shortDisplayNameComputed==null)
113                 shortDisplayNameComputed = true;
114         if (displayNameComputed || shortDisplayNameComputed) {
115                 String displayName = prepareDefaultDisplayName(
116                         (String)docModel.getProperty(commonPartLabel, LocationJAXBSchema.NAME ));
117                 if (displayNameComputed) {
118                         docModel.setProperty(commonPartLabel, LocationJAXBSchema.DISPLAY_NAME,
119                                         displayName);
120                 }
121                 if (shortDisplayNameComputed) {
122                         docModel.setProperty(commonPartLabel, LocationJAXBSchema.SHORT_DISPLAY_NAME,
123                                         displayName);
124                 }
125         }
126     }
127         
128     /**
129      * Produces a default displayName from the basic name and dates fields.
130      * @see LocationAuthorityClientUtils.prepareDefaultDisplayName() which
131      * duplicates this logic, until we define a service-general utils package
132      * that is neither client nor service specific.
133      * @param foreName  
134      * @param middleName
135      * @param surName
136      * @param birthDate
137      * @param deathDate
138      * @return
139      * @throws Exception
140      */
141     private static String prepareDefaultDisplayName(
142                 String name ) throws Exception {
143         StringBuilder newStr = new StringBuilder();
144                         newStr.append(name);
145                         return newStr.toString();
146     }
147     
148     /* (non-Javadoc)
149      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
150      */
151     @Override
152         public LocationsCommonList extractCommonPartList(
153                         DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
154                 LocationsCommonList coList = extractPagingInfo(new LocationsCommonList(), wrapDoc);
155         AbstractCommonList commonList = (AbstractCommonList) coList;
156         commonList.setFieldsReturned("displayName|refName|shortIdentifier|uri|csid");
157                 List<LocationsCommonList.LocationListItem> list = coList.getLocationListItem();
158                 Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
159                 String commonPartLabel = getServiceContext().getCommonPartLabel(
160                                 "locations");
161                 while (iter.hasNext()) {
162                         DocumentModel docModel = iter.next();
163                         LocationListItem ilistItem = new LocationListItem();
164                         ilistItem.setDisplayName((String) docModel.getProperty(
165                                         commonPartLabel, AuthorityItemJAXBSchema.DISPLAY_NAME));
166                         ilistItem.setShortIdentifier((String) docModel.getProperty(commonPartLabel,
167                                         AuthorityItemJAXBSchema.SHORT_IDENTIFIER));
168                         ilistItem.setRefName((String) docModel.getProperty(commonPartLabel,
169                                         AuthorityItemJAXBSchema.REF_NAME));
170                         String id = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
171                         ilistItem.setUri("/locationauthorities/" + inAuthority + "/items/"
172                                         + id);
173                         ilistItem.setCsid(id);
174                         list.add(ilistItem);
175                 }
176
177                 return coList;
178         }
179
180     /**
181      * getQProperty converts the given property to qualified schema property
182      * @param prop
183      * @return
184      */
185     @Override
186     public String getQProperty(String prop) {
187         return LocationConstants.NUXEO_SCHEMA_NAME + ":" + prop;
188     }
189 }
190