]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0cb6d71fa1f84f844c86be7e8cc5c8f493a602e9
[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 Regents of the University of California
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.contact.nuxeo;
25
26 import java.util.Map;
27
28 import org.collectionspace.services.contact.ContactJAXBSchema;
29 import org.collectionspace.services.common.document.DocumentWrapper;
30 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
31 import org.collectionspace.services.common.service.ObjectPartType;
32 import org.collectionspace.services.contact.ContactsCommon;
33
34 import org.nuxeo.ecm.core.api.DocumentModel;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * The Class ContactDocumentModelHandler.
40  */
41 public class ContactDocumentModelHandler
42         extends DocHandlerBase<ContactsCommon> {
43
44     private final Logger logger = LoggerFactory.getLogger(ContactDocumentModelHandler.class);
45     private static final String COMMON_PART_LABEL = "contacts_common";
46     private String inAuthority;
47     private String inItem;
48     
49      /**
50      * Gets the in authority.
51      *
52      * @return the in authority
53      */
54     public String getInAuthority() {
55         return inAuthority;
56     }
57
58     /**
59      * Sets the in authority.
60      *
61      * @param inAuthority the new in item
62      */
63     public void setInAuthority(String inAuthority) {
64         this.inAuthority = inAuthority;
65     }
66
67     /**
68      * Gets the in item.
69      *
70      * @return the in item
71      */
72     public String getInItem() {
73         return inItem;
74     }
75
76     /**
77      * Sets the in item.
78      *
79      * @param inItem the new in item
80      */
81     public void setInItem(String inItem) {
82         this.inItem = inItem;
83     }
84
85     /* (non-Javadoc)
86      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
87      */
88     @Override
89     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
90         // first fill all the parts of the document
91         super.handleCreate(wrapDoc);
92         handleInAuthority(wrapDoc.getWrappedObject());
93         handleDisplayNames(wrapDoc.getWrappedObject());
94     }
95     
96     /* (non-Javadoc)
97      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
98      */
99     @Override
100     public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
101         super.handleUpdate(wrapDoc);
102         handleDisplayNames(wrapDoc.getWrappedObject());
103     }
104
105     /**
106      * Check the logic around the parent pointer. Note that we only need do this on
107      * create, since we have logic to make this read-only on update. 
108      * 
109      * @param docModel
110      * 
111      * @throws Exception the exception
112      */
113     private void handleInAuthority(DocumentModel docModel) throws Exception {
114         String commonPartLabel = getServiceContext().getCommonPartLabel("contacts");
115         docModel.setProperty(commonPartLabel,
116                 ContactJAXBSchema.IN_AUTHORITY, inAuthority);
117         docModel.setProperty(commonPartLabel,
118                 ContactJAXBSchema.IN_ITEM, inItem);
119     }
120
121     private void handleDisplayNames(DocumentModel docModel) throws Exception {
122         String commonPartLabel = getServiceContext().getCommonPartLabel("contacts");
123         String email = getStringValueInPrimaryRepeatingComplexProperty(
124                 docModel, commonPartLabel, ContactJAXBSchema.EMAIL_GROUP_LIST,
125                 ContactJAXBSchema.EMAIL);
126         String telephoneNumber = getStringValueInPrimaryRepeatingComplexProperty(
127                 docModel, commonPartLabel, ContactJAXBSchema.TELEPHONE_NUMBER_GROUP_LIST,
128                 ContactJAXBSchema.TELEPHONE_NUMBER);
129         String addressPlace1 = getStringValueInPrimaryRepeatingComplexProperty(
130                 docModel, commonPartLabel, ContactJAXBSchema.ADDRESS_GROUP_LIST,
131                 ContactJAXBSchema.ADDRESS_PLACE_1);
132         String displayName = prepareDefaultDisplayName(email, telephoneNumber, addressPlace1);
133         docModel.setProperty(commonPartLabel, ContactJAXBSchema.DISPLAY_NAME,
134                     displayName);
135     }
136
137     /**
138      * Produces a default displayName from the basic name and foundingPlace fields.
139      * @see OrgAuthorityClientUtils.prepareDefaultDisplayName() which
140      * duplicates this logic, until we define a service-general utils package
141      * that is neither client nor service specific.
142      * @param shortName
143      * @param foundingPlace
144      * @return
145      * @throws Exception
146      */
147     private static String prepareDefaultDisplayName(String email,
148             String telephoneNumber, String addressPlace1) throws Exception {
149         
150         final int MAX_DISPLAY_NAME_LENGTH = 30;
151         
152         StringBuilder newStr = new StringBuilder("");
153         final String sep = " ";
154         boolean firstAdded = false;
155         
156         if (! (email == null || email.isEmpty()) ) {
157             newStr.append(email);
158             firstAdded = true;
159         }
160         
161         if (! (telephoneNumber == null || telephoneNumber.isEmpty()) ) {
162             if (newStr.length() <= MAX_DISPLAY_NAME_LENGTH) {
163                 if (firstAdded) {
164                     newStr.append(sep);
165                 } else {
166                     firstAdded = true;
167                 }
168                 newStr.append(telephoneNumber);
169             }
170         }
171         
172         if (! (addressPlace1 == null || addressPlace1.isEmpty()) ) {
173             if (newStr.length() <= MAX_DISPLAY_NAME_LENGTH) {
174                 if (firstAdded) {
175                     newStr.append(sep);
176                 }
177                 newStr.append(addressPlace1);
178             }
179         }
180         
181         String displayName = newStr.toString();
182              
183         if (displayName.length() > MAX_DISPLAY_NAME_LENGTH) {
184            return displayName.substring(0, MAX_DISPLAY_NAME_LENGTH) + "...";
185         } else {
186            return displayName;
187         }
188     }
189
190     /**
191      * Filters out ContactJAXBSchema.IN_AUTHORITY, and IN_ITEM, to ensure that
192      * the parent links remains untouched.
193      * Also remove the display name, as this is always computed.
194      * @param objectProps the properties parsed from the update payload
195      * @param partMeta metadata for the object to fill
196      */
197     @Override
198     public void filterReadOnlyPropertiesForPart(
199             Map<String, Object> objectProps, ObjectPartType partMeta) {
200         super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
201         objectProps.remove(ContactJAXBSchema.IN_AUTHORITY);
202         objectProps.remove(ContactJAXBSchema.IN_ITEM);
203         objectProps.remove(ContactJAXBSchema.DISPLAY_NAME);
204     }
205 }