]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
95f52bba13837034995cccc7ab6b65ab2a3ae64d
[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.TaxonJAXBSchema;
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.TaxonCommon;
42 import org.collectionspace.services.taxonomy.TaxonCommonList;
43 import org.collectionspace.services.taxonomy.TaxonCommonList.TaxonListItem;
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 TaxonDocumentModelHandler
60         extends AuthorityItemDocumentModelHandler<TaxonCommon, TaxonCommonList> {
61
62     /** The logger. */
63     private final Logger logger = LoggerFactory.getLogger(TaxonDocumentModelHandler.class);
64     /**
65      * Common part schema label
66      */
67     private static final String COMMON_PART_LABEL = "taxon_common";
68
69     public TaxonDocumentModelHandler() {
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("taxon");
100         Boolean displayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
101                 TaxonJAXBSchema.DISPLAY_NAME_COMPUTED);
102         Boolean shortDisplayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
103                 TaxonJAXBSchema.SHORT_DISPLAY_NAME_COMPUTED);
104         if (displayNameComputed == null) {
105             displayNameComputed = true;
106         }
107         if (shortDisplayNameComputed == null) {
108             shortDisplayNameComputed = true;
109         }
110         if (displayNameComputed || shortDisplayNameComputed) {
111             String displayName = prepareDefaultDisplayName(
112                     (String) docModel.getProperty(commonPartLabel, TaxonJAXBSchema.NAME));
113             if (displayNameComputed) {
114                 docModel.setProperty(commonPartLabel, TaxonJAXBSchema.DISPLAY_NAME,
115                         displayName);
116             }
117             if (shortDisplayNameComputed) {
118                 docModel.setProperty(commonPartLabel, TaxonJAXBSchema.SHORT_DISPLAY_NAME,
119                         displayName);
120             }
121         }
122     }
123
124     /**
125      * Produces a default displayName from the basic name and dates fields.
126      * @see TaxonomyAuthorityClientUtils.prepareDefaultDisplayName() which
127      * duplicates this logic, until we define a service-general utils package
128      * that is neither client nor service specific.
129      * @param name
130      * @return
131      * @throws Exception
132      */
133     private static String prepareDefaultDisplayName(
134             String name) throws Exception {
135         StringBuilder newStr = new StringBuilder();
136         newStr.append(name);
137         return newStr.toString();
138     }
139
140     /* (non-Javadoc)
141      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
142      */
143     @Override
144     public TaxonCommonList extractCommonPartList(
145             DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
146         TaxonCommonList coList = extractPagingInfo(new TaxonCommonList(), wrapDoc);
147         AbstractCommonList commonList = (AbstractCommonList) coList;
148         commonList.setFieldsReturned("displayName|refName|shortIdentifier|uri|csid");
149         List<TaxonCommonList.TaxonListItem> list = coList.getTaxonListItem();
150         Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
151         String commonPartLabel = getServiceContext().getCommonPartLabel("taxon");
152         while (iter.hasNext()) {
153             DocumentModel docModel = iter.next();
154             TaxonListItem ilistItem = new TaxonListItem();
155             ilistItem.setDisplayName((String) docModel.getProperty(
156                     commonPartLabel, AuthorityItemJAXBSchema.DISPLAY_NAME));
157             ilistItem.setShortIdentifier((String) docModel.getProperty(commonPartLabel,
158                     AuthorityItemJAXBSchema.SHORT_IDENTIFIER));
159             ilistItem.setRefName((String) docModel.getProperty(commonPartLabel,
160                     AuthorityItemJAXBSchema.REF_NAME));
161             String id = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
162             ilistItem.setUri("/taxonomyauthorities/" + inAuthority + "/items/"
163                     + id);
164             ilistItem.setCsid(id);
165             list.add(ilistItem);
166         }
167
168         return coList;
169     }
170
171     /**
172      * getQProperty converts the given property to qualified schema property
173      * @param prop
174      * @return
175      */
176     @Override
177     public String getQProperty(String prop) {
178         return TaxonomyConstants.NUXEO_SCHEMA_NAME + ":" + prop;
179     }
180 }
181