]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e95ff1bc335c5704246848db524992bb3927bea8
[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 org.collectionspace.services.client.OrgAuthorityClient;
27 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityItemDocumentModelHandler;
28 import org.collectionspace.services.OrganizationJAXBSchema;
29 import org.collectionspace.services.common.document.DocumentWrapper;
30 import org.collectionspace.services.organization.OrganizationsCommon;
31 import org.nuxeo.ecm.core.api.DocumentModel;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * OrganizationDocumentModelHandler
37  *
38  * $LastChangedRevision$
39  * $LastChangedDate$
40  */
41 public class OrganizationDocumentModelHandler
42                 extends AuthorityItemDocumentModelHandler<OrganizationsCommon> {
43
44     /** The logger. */
45     private final Logger logger = LoggerFactory.getLogger(OrganizationDocumentModelHandler.class);
46     /**
47      * Common part schema label
48      */
49     private static final String COMMON_PART_LABEL = "organizations_common";   
50     
51     public OrganizationDocumentModelHandler() {
52         super(COMMON_PART_LABEL);
53     }
54
55     @Override
56     public String getAuthorityServicePath(){
57         return OrgAuthorityClient.SERVICE_PATH_COMPONENT;    //  CSPACE-3932
58     }
59         
60     /* (non-Javadoc)
61      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
62      */
63     @Override
64     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
65         // first fill all the parts of the document
66         super.handleCreate(wrapDoc);            
67         handleDisplayNames(wrapDoc.getWrappedObject());
68     }
69     
70     /* (non-Javadoc)
71      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
72      */
73     @Override
74     public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
75         super.handleUpdate(wrapDoc);
76         handleDisplayNames(wrapDoc.getWrappedObject());
77     }
78
79     /**
80      * Check the logic around the computed displayName
81      * 
82      * @param docModel
83      * 
84      * @throws Exception the exception
85      */
86     private void handleDisplayNames(DocumentModel docModel) throws Exception {
87         String commonPartLabel = getServiceContext().getCommonPartLabel("organizations");
88         Boolean displayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
89                         OrganizationJAXBSchema.DISPLAY_NAME_COMPUTED);
90         Boolean shortDisplayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
91                         OrganizationJAXBSchema.SHORT_DISPLAY_NAME_COMPUTED);
92         if(displayNameComputed==null)
93                 displayNameComputed = Boolean.TRUE;
94         if(shortDisplayNameComputed==null)
95                 shortDisplayNameComputed = Boolean.TRUE;
96         if (displayNameComputed.booleanValue() || shortDisplayNameComputed.booleanValue()) {
97                 String shortName = getStringValueInPrimaryRepeatingComplexProperty(
98                                 docModel, commonPartLabel, OrganizationJAXBSchema.MAIN_BODY_GROUP_LIST, 
99                                 OrganizationJAXBSchema.SHORT_NAME);
100             // FIXME: Determine how to handle cases where primary short name is null or empty.
101                 if(shortDisplayNameComputed.booleanValue()) {
102                         String displayName = prepareDefaultDisplayName(shortName, null);
103                         docModel.setProperty(commonPartLabel, OrganizationJAXBSchema.SHORT_DISPLAY_NAME,
104                                         displayName);
105                 }
106                 if(displayNameComputed.booleanValue()) {
107                 String foundingPlace = (String) docModel.getProperty(commonPartLabel,
108                                                 OrganizationJAXBSchema.FOUNDING_PLACE);
109                         String displayName = prepareDefaultDisplayName(shortName, foundingPlace);
110                                 docModel.setProperty(commonPartLabel, OrganizationJAXBSchema.DISPLAY_NAME,
111                                                         displayName);
112                 }
113         }
114     }
115
116     /**
117      * Produces a default displayName from the basic name and foundingPlace fields.
118      * @see OrgAuthorityClientUtils.prepareDefaultDisplayName() which
119      * duplicates this logic, until we define a service-general utils package
120      * that is neither client nor service specific.
121      * @param shortName
122      * @param foundingPlace
123      * @return
124      * @throws Exception
125      */
126     private static String prepareDefaultDisplayName(
127                 String shortName, String foundingPlace ) throws Exception {
128         StringBuilder newStr = new StringBuilder();
129                 final String sep = " ";
130                 boolean firstAdded = false;
131                 if(null != shortName ) {
132                         newStr.append(shortName);
133                         firstAdded = true;
134                 }
135         // Now we add the place
136                 if(null != foundingPlace ) {
137                         if(firstAdded) {
138                                 newStr.append(sep);
139                         }
140                         newStr.append(foundingPlace);
141                 }
142                 return newStr.toString();
143     }
144     
145     /**
146      * getQProperty converts the given property to qualified schema property
147      * @param prop
148      * @return
149      */
150     @Override
151     public String getQProperty(String prop) {
152         return OrganizationConstants.NUXEO_SCHEMA_NAME + ":" + prop;
153     }
154
155 }
156