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 javax.ws.rs.core.UriInfo;
29 import org.collectionspace.services.client.AuthorityClient;
30 import org.collectionspace.services.config.service.ObjectPartType;
31 import org.collectionspace.services.contact.ContactJAXBSchema;
32 import org.collectionspace.services.common.document.DocumentWrapper;
33 import org.collectionspace.services.nuxeo.client.java.NuxeoDocumentModelHandler;
34 import org.collectionspace.services.contact.ContactsCommon;
36 import org.nuxeo.ecm.core.api.DocumentModel;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The Class ContactDocumentModelHandler.
43 public class ContactDocumentModelHandler
44 extends NuxeoDocumentModelHandler<ContactsCommon> {
46 private final Logger logger = LoggerFactory.getLogger(ContactDocumentModelHandler.class);
47 private static final String COMMON_PART_LABEL = "contacts_common";
48 private String inAuthority;
49 private String inItem;
52 * Gets the in authority.
54 * @return the in authority
56 public String getInAuthority() {
61 * Sets the in authority.
63 * @param inAuthority the new in item
65 public void setInAuthority(String inAuthority) {
66 this.inAuthority = inAuthority;
74 public String getInItem() {
81 * @param inItem the new in item
83 public void setInItem(String inItem) {
88 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
91 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
92 // first fill all the parts of the document
93 super.handleCreate(wrapDoc);
94 handleInAuthority(wrapDoc.getWrappedObject());
95 handleDisplayNames(wrapDoc.getWrappedObject());
99 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
102 public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
103 super.handleUpdate(wrapDoc);
104 handleDisplayNames(wrapDoc.getWrappedObject());
108 * Check the logic around the parent pointer. Note that we only need do this on
109 * create, since we have logic to make this read-only on update.
113 * @throws Exception the exception
115 private void handleInAuthority(DocumentModel docModel) throws Exception {
116 String commonPartLabel = getServiceContext().getCommonPartLabel("contacts");
117 docModel.setProperty(commonPartLabel,
118 ContactJAXBSchema.IN_AUTHORITY, inAuthority);
119 docModel.setProperty(commonPartLabel,
120 ContactJAXBSchema.IN_ITEM, inItem);
123 private void handleDisplayNames(DocumentModel docModel) throws Exception {
124 String commonPartLabel = getServiceContext().getCommonPartLabel("contacts");
125 String email = getStringValueInPrimaryRepeatingComplexProperty(
126 docModel, commonPartLabel, ContactJAXBSchema.EMAIL_GROUP_LIST,
127 ContactJAXBSchema.EMAIL);
128 String telephoneNumber = getStringValueInPrimaryRepeatingComplexProperty(
129 docModel, commonPartLabel, ContactJAXBSchema.TELEPHONE_NUMBER_GROUP_LIST,
130 ContactJAXBSchema.TELEPHONE_NUMBER);
131 String addressPlace1 = getStringValueInPrimaryRepeatingComplexProperty(
132 docModel, commonPartLabel, ContactJAXBSchema.ADDRESS_GROUP_LIST,
133 ContactJAXBSchema.ADDRESS_PLACE_1);
134 String displayName = prepareDefaultDisplayName(email, telephoneNumber, addressPlace1);
135 docModel.setProperty(commonPartLabel, ContactJAXBSchema.DISPLAY_NAME,
140 * Produces a default displayName from the basic name and foundingPlace fields.
141 * @see OrgAuthorityClientUtils.prepareDefaultDisplayName() which
142 * duplicates this logic, until we define a service-general utils package
143 * that is neither client nor service specific.
145 * @param foundingPlace
149 private static String prepareDefaultDisplayName(String email,
150 String telephoneNumber, String addressPlace1) throws Exception {
152 final int MAX_DISPLAY_NAME_LENGTH = 30;
154 StringBuilder newStr = new StringBuilder("");
155 final String sep = " ";
156 boolean firstAdded = false;
158 if (!(email == null || email.isEmpty())) {
159 newStr.append(email);
163 if (!(telephoneNumber == null || telephoneNumber.isEmpty())) {
164 if (newStr.length() <= MAX_DISPLAY_NAME_LENGTH) {
170 newStr.append(telephoneNumber);
174 if (!(addressPlace1 == null || addressPlace1.isEmpty())) {
175 if (newStr.length() <= MAX_DISPLAY_NAME_LENGTH) {
179 newStr.append(addressPlace1);
183 String displayName = newStr.toString();
185 if (displayName.length() > MAX_DISPLAY_NAME_LENGTH) {
186 return displayName.substring(0, MAX_DISPLAY_NAME_LENGTH) + "...";
193 public String getUri(DocumentModel docModel) {
195 UriInfo ui = getServiceContext().getUriInfo();
197 uri = '/' + getAuthorityPathComponent(ui) + '/' + inAuthority
198 + '/' + AuthorityClient.ITEMS + '/' + inItem
199 + getServiceContextPath() + getCsid(docModel);
200 // uri = "/" + ui.getPath() + "/" + getCsid(docModel);
202 uri = super.getUri(docModel);
207 // Assumes the initial path component in the URI, following the base URI,
208 // identifies the relevant authority resource
209 private String getAuthorityPathComponent(UriInfo ui) {
210 return ui.getPathSegments().get(0).toString();
214 * Filters out ContactJAXBSchema.IN_AUTHORITY, and IN_ITEM, to ensure that
215 * the parent links remains untouched.
216 * Also remove the display name, as this is always computed.
217 * @param objectProps the properties parsed from the update payload
218 * @param partMeta metadata for the object to fill
221 public void filterReadOnlyPropertiesForPart(
222 Map<String, Object> objectProps, ObjectPartType partMeta) {
223 super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
224 objectProps.remove(ContactJAXBSchema.IN_AUTHORITY);
225 objectProps.remove(ContactJAXBSchema.IN_ITEM);
226 objectProps.remove(ContactJAXBSchema.URI);
227 objectProps.remove(ContactJAXBSchema.DISPLAY_NAME);