]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d3cb257af4bfec4a6f61222f90588ca9132069ec
[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     /**
97      * Check the logic around the parent pointer. Note that we only need do this on
98      * create, since we have logic to make this read-only on update. 
99      * 
100      * @param docModel
101      * 
102      * @throws Exception the exception
103      */
104     private void handleInAuthority(DocumentModel docModel) throws Exception {
105         String commonPartLabel = getServiceContext().getCommonPartLabel("contacts");
106         docModel.setProperty(commonPartLabel,
107                 ContactJAXBSchema.IN_AUTHORITY, inAuthority);
108         docModel.setProperty(commonPartLabel,
109                 ContactJAXBSchema.IN_ITEM, inItem);
110     }
111
112     private void handleDisplayNames(DocumentModel docModel) throws Exception {
113         String commonPartLabel = getServiceContext().getCommonPartLabel("contacts");
114         String email = getStringValueInPrimaryRepeatingComplexProperty(
115                 docModel, commonPartLabel, ContactJAXBSchema.EMAIL_GROUP_LIST,
116                 ContactJAXBSchema.EMAIL);
117         String telephoneNumber = getStringValueInPrimaryRepeatingComplexProperty(
118                 docModel, commonPartLabel, ContactJAXBSchema.TELEPHONE_NUMBER_GROUP_LIST,
119                 ContactJAXBSchema.TELEPHONE_NUMBER);
120         String addressPlace1 = getStringValueInPrimaryRepeatingComplexProperty(
121                 docModel, commonPartLabel, ContactJAXBSchema.ADDRESS_GROUP_LIST,
122                 ContactJAXBSchema.ADDRESS_PLACE_1);
123         String displayName = prepareDefaultDisplayName(email, telephoneNumber, addressPlace1);
124         docModel.setProperty(commonPartLabel, ContactJAXBSchema.DISPLAY_NAME,
125                     displayName);
126     }
127
128     /**
129      * Produces a default displayName from the basic name and foundingPlace fields.
130      * @see OrgAuthorityClientUtils.prepareDefaultDisplayName() which
131      * duplicates this logic, until we define a service-general utils package
132      * that is neither client nor service specific.
133      * @param shortName
134      * @param foundingPlace
135      * @return
136      * @throws Exception
137      */
138     private static String prepareDefaultDisplayName(String email,
139             String telephoneNumber, String addressPlace1) throws Exception {
140         
141         final int MAX_DISPLAY_NAME_LENGTH = 30;
142         
143         StringBuilder newStr = new StringBuilder("");
144         final String sep = " ";
145         boolean firstAdded = false;
146         
147         if (! (email == null || email.isEmpty()) ) {
148             newStr.append(email);
149             firstAdded = true;
150         }
151         
152         if (! (telephoneNumber == null || telephoneNumber.isEmpty()) ) {
153             if (newStr.length() <= MAX_DISPLAY_NAME_LENGTH) {
154                 if (firstAdded) {
155                     newStr.append(sep);
156                 } else {
157                     firstAdded = true;
158                 }
159                 newStr.append(telephoneNumber);
160             }
161         }
162         
163         if (! (addressPlace1 == null || addressPlace1.isEmpty()) ) {
164             if (newStr.length() <= MAX_DISPLAY_NAME_LENGTH) {
165                 if (firstAdded) {
166                     newStr.append(sep);
167                 }
168                 newStr.append(addressPlace1);
169             }
170         }
171         
172         String displayName = newStr.toString();
173              
174         if (displayName.length() > MAX_DISPLAY_NAME_LENGTH) {
175            return displayName.substring(0, MAX_DISPLAY_NAME_LENGTH) + "...";
176         } else {
177            return displayName;
178         }
179     }
180
181     /**
182      * Filters out ContactJAXBSchema.IN_AUTHORITY, and IN_ITEM, to ensure that
183      * the parent links remains untouched.
184      * Also remove the display name, as this is always computed.
185      * @param objectProps the properties parsed from the update payload
186      * @param partMeta metadata for the object to fill
187      */
188     @Override
189     public void filterReadOnlyPropertiesForPart(
190             Map<String, Object> objectProps, ObjectPartType partMeta) {
191         super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
192         objectProps.remove(ContactJAXBSchema.IN_AUTHORITY);
193         objectProps.remove(ContactJAXBSchema.IN_ITEM);
194         objectProps.remove(ContactJAXBSchema.DISPLAY_NAME);
195     }
196 }