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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.contact.nuxeo;
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;
34 import org.nuxeo.ecm.core.api.DocumentModel;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * The Class ContactDocumentModelHandler.
41 public class ContactDocumentModelHandler
42 extends DocHandlerBase<ContactsCommon> {
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;
50 * Gets the in authority.
52 * @return the in authority
54 public String getInAuthority() {
59 * Sets the in authority.
61 * @param inAuthority the new in item
63 public void setInAuthority(String inAuthority) {
64 this.inAuthority = inAuthority;
72 public String getInItem() {
79 * @param inItem the new in item
81 public void setInItem(String inItem) {
86 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
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());
97 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
100 public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
101 super.handleUpdate(wrapDoc);
102 handleDisplayNames(wrapDoc.getWrappedObject());
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.
111 * @throws Exception the exception
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);
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,
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.
143 * @param foundingPlace
147 private static String prepareDefaultDisplayName(String email,
148 String telephoneNumber, String addressPlace1) throws Exception {
150 final int MAX_DISPLAY_NAME_LENGTH = 30;
152 StringBuilder newStr = new StringBuilder("");
153 final String sep = " ";
154 boolean firstAdded = false;
156 if (! (email == null || email.isEmpty()) ) {
157 newStr.append(email);
161 if (! (telephoneNumber == null || telephoneNumber.isEmpty()) ) {
162 if (newStr.length() <= MAX_DISPLAY_NAME_LENGTH) {
168 newStr.append(telephoneNumber);
172 if (! (addressPlace1 == null || addressPlace1.isEmpty()) ) {
173 if (newStr.length() <= MAX_DISPLAY_NAME_LENGTH) {
177 newStr.append(addressPlace1);
181 String displayName = newStr.toString();
183 if (displayName.length() > MAX_DISPLAY_NAME_LENGTH) {
184 return displayName.substring(0, MAX_DISPLAY_NAME_LENGTH) + "...";
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
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);