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 University of California at Berkeley
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.common.vocabulary.nuxeo;
28 import org.collectionspace.services.client.PoxPayloadIn;
29 import org.collectionspace.services.client.PoxPayloadOut;
30 import org.collectionspace.services.common.api.RefName;
31 import org.collectionspace.services.common.api.RefName.Authority;
32 import org.collectionspace.services.common.api.Tools;
33 import org.collectionspace.services.common.context.ServiceContext;
34 import org.collectionspace.services.common.document.DocumentException;
35 import org.collectionspace.services.common.document.DocumentNotFoundException;
36 import org.collectionspace.services.common.document.DocumentWrapper;
37 import org.collectionspace.services.common.vocabulary.AuthorityJAXBSchema;
38 import org.collectionspace.services.config.service.ObjectPartType;
39 import org.collectionspace.services.nuxeo.client.java.NuxeoDocumentModelHandler;
40 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
41 import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl;
42 import org.nuxeo.ecm.core.api.ClientException;
43 import org.nuxeo.ecm.core.api.DocumentModel;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * AuthorityDocumentModelHandler
50 * $LastChangedRevision: $
53 public abstract class AuthorityDocumentModelHandler<AuthCommon>
54 extends NuxeoDocumentModelHandler<AuthCommon> {
56 private final Logger logger = LoggerFactory.getLogger(AuthorityDocumentModelHandler.class);
57 private String authorityCommonSchemaName;
59 public AuthorityDocumentModelHandler(String authorityCommonSchemaName) {
60 this.authorityCommonSchemaName = authorityCommonSchemaName;
64 * Non standard injection of CSID into common part, since caller may access through
65 * shortId, and not know the CSID.
66 * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#extractPart(org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, org.collectionspace.services.common.service.ObjectPartType)
69 protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
71 Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
73 // Add the CSID to the common part
74 if (partMeta.getLabel().equalsIgnoreCase(authorityCommonSchemaName)) {
75 String csid = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
76 unQObjectProperties.put("csid", csid);
79 return unQObjectProperties;
83 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
84 super.handleCreate(wrapDoc);
86 // Uncomment once debugged and App layer is read to integrate
87 // Experimenting with this uncommented now ...
88 handleDisplayNameAsShortIdentifier(wrapDoc.getWrappedObject(), authorityCommonSchemaName);
89 updateRefnameForAuthority(wrapDoc, authorityCommonSchemaName);//CSPACE-3178
92 protected String buildWhereForShortId(String name) {
93 return authorityCommonSchemaName
94 + ":" + AuthorityJAXBSchema.SHORT_IDENTIFIER
98 private boolean isUnique(DocumentModel docModel, String schemaName) throws DocumentException {
102 private boolean temp_isUnique(DocumentModel docModel, String schemaName) throws DocumentException {
103 boolean result = true;
105 ServiceContext ctx = this.getServiceContext();
106 String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
107 String nxqlWhereClause = buildWhereForShortId(shortIdentifier);
109 DocumentWrapper<DocumentModel> searchResultWrapper = getRepositoryClient(ctx).findDoc(ctx, nxqlWhereClause);
110 if (searchResultWrapper != null) {
112 if (logger.isInfoEnabled() == true) {
113 DocumentModel searchResult = searchResultWrapper.getWrappedObject();
114 String debugMsg = String.format("Could not create a new authority with a short identifier of '%s', because one already exists with the same short identifer: CSID = '%s'",
115 shortIdentifier, searchResult.getName());
116 logger.trace(debugMsg);
119 } catch (DocumentNotFoundException e) {
120 // Not a problem, just means we couldn't find another authority with that short ID
127 * If no short identifier was provided in the input payload,
128 * generate a short identifier from the display name. Either way though,
129 * the short identifier needs to be unique.
131 private void handleDisplayNameAsShortIdentifier(DocumentModel docModel, String schemaName) throws Exception {
132 String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
133 String displayName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.DISPLAY_NAME);
134 String shortDisplayName = "";
135 String generateShortIdentifier = null;
136 if (Tools.isEmpty(shortIdentifier)) {
137 generateShortIdentifier = AuthorityIdentifierUtils.generateShortIdentifierFromDisplayName(displayName, shortDisplayName);
138 docModel.setProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER, shortIdentifier);
141 if (isUnique(docModel, schemaName) == false) {
142 String shortId = generateShortIdentifier == null ? shortIdentifier : generateShortIdentifier;
143 String errMsgVerb = generateShortIdentifier == null ? "supplied" : "generated";
144 String errMsg = String.format("The %s short identifier '%s' was not unique, so the new authority could not be created.",
145 errMsgVerb, shortId);
146 throw new DocumentException(errMsg);
151 * Generate a refName for the authority from the short identifier
154 * All refNames for authorities are generated. If a client supplies
155 * a refName, it will be overwritten during create (per this method)
156 * or discarded during update (per filterReadOnlyPropertiesForPart).
158 * @see #filterReadOnlyPropertiesForPart(Map<String, Object>, org.collectionspace.services.common.service.ObjectPartType)
161 protected void updateRefnameForAuthority(DocumentWrapper<DocumentModel> wrapDoc, String schemaName) throws Exception {
162 DocumentModel docModel = wrapDoc.getWrappedObject();
163 RefName.Authority authority = (Authority) getRefName(getServiceContext(), docModel);
164 String refName = authority.toString();
165 docModel.setProperty(schemaName, AuthorityJAXBSchema.REF_NAME, refName);
169 public RefName.RefNameInterface getRefName(ServiceContext ctx,
170 DocumentModel docModel) {
171 RefName.RefNameInterface refname = null;
174 String shortIdentifier = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
175 String displayName = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.DISPLAY_NAME);
176 RefName.Authority authority = RefName.Authority.buildAuthority(ctx.getTenantName(),
177 ctx.getServiceName(),
178 null, // Only use shortId form!!!
182 } catch (Exception e) {
183 logger.error(e.getMessage(), e);
190 protected String getRefnameDisplayName(DocumentWrapper<DocumentModel> docWrapper) {
191 String result = null;
193 DocumentModel docModel = docWrapper.getWrappedObject();
194 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
195 RefName.Authority refname = (RefName.Authority)getRefName(ctx, docModel);
196 result = refname.getDisplayName();
201 public String getShortIdentifier(String authCSID, String schemaName) throws Exception {
202 String shortIdentifier = null;
203 CoreSessionInterface repoSession = null;
205 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
206 RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(ctx);
208 repoSession = nuxeoRepoClient.getRepositorySession(ctx);
209 DocumentWrapper<DocumentModel> wrapDoc = nuxeoRepoClient.getDocFromCsid(ctx, repoSession, authCSID);
210 DocumentModel docModel = wrapDoc.getWrappedObject();
211 shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
212 } catch (ClientException ce) {
213 throw new RuntimeException("AuthorityDocHandler Internal Error: cannot get shortId!", ce);
215 if (repoSession != null) {
216 nuxeoRepoClient.releaseRepositorySession(ctx, repoSession);
220 return shortIdentifier;
224 * Filters out selected values supplied in an update request.
226 * @param objectProps the properties filtered out from the update payload
227 * @param partMeta metadata for the object to fill
230 public void filterReadOnlyPropertiesForPart(
231 Map<String, Object> objectProps, ObjectPartType partMeta) {
232 super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
233 String commonPartLabel = getServiceContext().getCommonPartLabel();
234 if (partMeta.getLabel().equalsIgnoreCase(commonPartLabel)) {
235 objectProps.remove(AuthorityJAXBSchema.CSID);
236 objectProps.remove(AuthorityJAXBSchema.SHORT_IDENTIFIER);
237 objectProps.remove(AuthorityJAXBSchema.REF_NAME);