]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d11dc16b5e81a22759dd0bf52ba42337a2f95cd7
[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.organization.nuxeo;
25
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityItemDocumentModelHandler;
31 import org.collectionspace.services.OrganizationJAXBSchema;
32 import org.collectionspace.services.common.document.DocumentWrapper;
33 import org.collectionspace.services.common.service.ObjectPartType;
34 import org.collectionspace.services.jaxb.AbstractCommonList;
35 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
36 import org.collectionspace.services.organization.OrganizationsCommon;
37 import org.collectionspace.services.organization.OrganizationsCommonList;
38 import org.collectionspace.services.organization.OrganizationsCommonList.OrganizationListItem;
39 import org.nuxeo.ecm.core.api.DocumentModel;
40 import org.nuxeo.ecm.core.api.DocumentModelList;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * OrganizationDocumentModelHandler
46  *
47  * $LastChangedRevision: $
48  * $LastChangedDate: $
49  */
50 public class OrganizationDocumentModelHandler
51                 extends AuthorityItemDocumentModelHandler<OrganizationsCommon, OrganizationsCommonList> {
52
53     /** The logger. */
54     private final Logger logger = LoggerFactory.getLogger(OrganizationDocumentModelHandler.class);
55     /**
56      * Common part schema label
57      */
58     private static final String COMMON_PART_LABEL = "organizations_common";   
59     
60     public OrganizationDocumentModelHandler() {
61         super(COMMON_PART_LABEL);
62     }
63         
64     /* (non-Javadoc)
65      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
66      */
67     @Override
68     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
69         // first fill all the parts of the document
70         super.handleCreate(wrapDoc);            
71         handleDisplayName(wrapDoc.getWrappedObject());
72     }
73     
74     /* (non-Javadoc)
75      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
76      */
77     @Override
78     public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
79         super.handleUpdate(wrapDoc);
80         handleDisplayName(wrapDoc.getWrappedObject());
81     }
82
83     /**
84      * Check the logic around the computed displayName
85      * 
86      * @param docModel
87      * 
88      * @throws Exception the exception
89      */
90     private void handleDisplayName(DocumentModel docModel) throws Exception {
91         String commonPartLabel = getServiceContext().getCommonPartLabel("organizations");
92         Boolean displayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
93                         OrganizationJAXBSchema.DISPLAY_NAME_COMPUTED);
94         if (displayNameComputed) {
95                 String displayName = prepareDefaultDisplayName(
96                                 (String) docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.SHORT_NAME),
97                                 (String) docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.FOUNDING_PLACE));
98                         docModel.setProperty(commonPartLabel, OrganizationJAXBSchema.DISPLAY_NAME,
99                                                 displayName);
100         }
101     }
102
103     /**
104      * Produces a default displayName from the basic name and foundingPlace fields.
105      * @see OrgAuthorityClientUtils.prepareDefaultDisplayName() which
106      * duplicates this logic, until we define a service-general utils package
107      * that is neither client nor service specific.
108      * @param shortName
109      * @param foundingPlace
110      * @return
111      * @throws Exception
112      */
113     private static String prepareDefaultDisplayName(
114                 String shortName, String foundingPlace ) throws Exception {
115         StringBuilder newStr = new StringBuilder();
116                 final String sep = " ";
117                 boolean firstAdded = false;
118                 if(null != shortName ) {
119                         newStr.append(shortName);
120                         firstAdded = true;
121                 }
122         // Now we add the place
123                 if(null != foundingPlace ) {
124                         if(firstAdded) {
125                                 newStr.append(sep);
126                         }
127                         newStr.append(foundingPlace);
128                 }
129                 return newStr.toString();
130     }
131     
132     /* (non-Javadoc)
133      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
134      */
135     @Override
136     public OrganizationsCommonList extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) 
137                 throws Exception {
138         OrganizationsCommonList coList = this.extractPagingInfo(new OrganizationsCommonList(), wrapDoc);
139         AbstractCommonList commonList = (AbstractCommonList) coList;
140         commonList.setFieldsReturned("displayName|refName|uri|csid");
141         List<OrganizationsCommonList.OrganizationListItem> list = coList.getOrganizationListItem();
142         Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
143         String commonPartLabel = getServiceContext().getCommonPartLabel("organizations");
144         while(iter.hasNext()){
145             DocumentModel docModel = iter.next();
146             OrganizationListItem ilistItem = new OrganizationListItem();
147             ilistItem.setDisplayName((String)
148                         docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.DISPLAY_NAME ));
149                         ilistItem.setRefName((String) 
150                                         docModel.getProperty(commonPartLabel, OrganizationJAXBSchema.REF_NAME));
151                         String id = NuxeoUtils.extractId(docModel.getPathAsString());
152             ilistItem.setUri("/orgauthorities/" + this.inAuthority + "/items/" + id);
153             ilistItem.setCsid(id);
154             list.add(ilistItem);
155         }
156
157         return coList;
158     }
159
160     /**
161      * getQProperty converts the given property to qualified schema property
162      * @param prop
163      * @return
164      */
165     @Override
166     public String getQProperty(String prop) {
167         return OrganizationConstants.NUXEO_SCHEMA_NAME + ":" + prop;
168     }
169 }
170