]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
eb12ee287ba83fbe5a6fe2102505a331a1673d18
[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 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;
35
36 import org.nuxeo.ecm.core.api.DocumentModel;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * The Class ContactDocumentModelHandler.
42  */
43 public class ContactDocumentModelHandler
44         extends NuxeoDocumentModelHandler<ContactsCommon> {
45
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;
50
51     /**
52      * Gets the in authority.
53      *
54      * @return the in authority
55      */
56     public String getInAuthority() {
57         return inAuthority;
58     }
59
60     /**
61      * Sets the in authority.
62      *
63      * @param inAuthority the new in item
64      */
65     public void setInAuthority(String inAuthority) {
66         this.inAuthority = inAuthority;
67     }
68
69     /**
70      * Gets the in item.
71      *
72      * @return the in item
73      */
74     public String getInItem() {
75         return inItem;
76     }
77
78     /**
79      * Sets the in item.
80      *
81      * @param inItem the new in item
82      */
83     public void setInItem(String inItem) {
84         this.inItem = inItem;
85     }
86
87     /* (non-Javadoc)
88      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
89      */
90     @Override
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());
96     }
97
98     /* (non-Javadoc)
99      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
100      */
101     @Override
102     public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
103         super.handleUpdate(wrapDoc);
104         handleDisplayNames(wrapDoc.getWrappedObject());
105     }
106
107     /**
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. 
110      * 
111      * @param docModel
112      * 
113      * @throws Exception the exception
114      */
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);
121     }
122
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,
136                 displayName);
137     }
138
139     /**
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.
144      * @param shortName
145      * @param foundingPlace
146      * @return
147      * @throws Exception
148      */
149     private static String prepareDefaultDisplayName(String email,
150             String telephoneNumber, String addressPlace1) throws Exception {
151
152         final int MAX_DISPLAY_NAME_LENGTH = 30;
153
154         StringBuilder newStr = new StringBuilder("");
155         final String sep = " ";
156         boolean firstAdded = false;
157
158         if (!(email == null || email.isEmpty())) {
159             newStr.append(email);
160             firstAdded = true;
161         }
162
163         if (!(telephoneNumber == null || telephoneNumber.isEmpty())) {
164             if (newStr.length() <= MAX_DISPLAY_NAME_LENGTH) {
165                 if (firstAdded) {
166                     newStr.append(sep);
167                 } else {
168                     firstAdded = true;
169                 }
170                 newStr.append(telephoneNumber);
171             }
172         }
173
174         if (!(addressPlace1 == null || addressPlace1.isEmpty())) {
175             if (newStr.length() <= MAX_DISPLAY_NAME_LENGTH) {
176                 if (firstAdded) {
177                     newStr.append(sep);
178                 }
179                 newStr.append(addressPlace1);
180             }
181         }
182
183         String displayName = newStr.toString();
184
185         if (displayName.length() > MAX_DISPLAY_NAME_LENGTH) {
186             return displayName.substring(0, MAX_DISPLAY_NAME_LENGTH) + "...";
187         } else {
188             return displayName;
189         }
190     }
191
192     @Override
193     public String getUri(DocumentModel docModel) {
194         String uri = "";
195         UriInfo ui = getServiceContext().getUriInfo();
196         if (ui != null) {
197             uri = '/' + getAuthorityPathComponent(ui) + '/' + inAuthority
198                     + '/' + AuthorityClient.ITEMS + '/' + inItem
199                     + getServiceContextPath() + getCsid(docModel);
200             // uri = "/" + ui.getPath() + "/" + getCsid(docModel);
201         } else {
202             uri = super.getUri(docModel);
203         }
204         return uri;
205     }
206
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();
211     }
212
213     /**
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
219      */
220     @Override
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);
228     }
229 }