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