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