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 {
99 boolean result = true;
101 ServiceContext ctx = this.getServiceContext();
102 String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
103 String nxqlWhereClause = buildWhereForShortId(shortIdentifier);
105 DocumentWrapper<DocumentModel> searchResultWrapper = getRepositoryClient(ctx).findDoc(ctx, nxqlWhereClause);
106 if (searchResultWrapper != null) {
108 if (logger.isInfoEnabled() == true) {
109 DocumentModel searchResult = searchResultWrapper.getWrappedObject();
110 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'",
111 shortIdentifier, searchResult.getName());
112 logger.trace(debugMsg);
115 } catch (DocumentNotFoundException e) {
116 // Not a problem, just means we couldn't find another authority with that short ID
123 * If no short identifier was provided in the input payload,
124 * generate a short identifier from the display name. Either way though,
125 * the short identifier needs to be unique.
127 private void handleDisplayNameAsShortIdentifier(DocumentModel docModel, String schemaName) throws Exception {
128 String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
129 String displayName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.DISPLAY_NAME);
130 String shortDisplayName = "";
131 String generateShortIdentifier = null;
132 if (Tools.isEmpty(shortIdentifier)) {
133 generateShortIdentifier = AuthorityIdentifierUtils.generateShortIdentifierFromDisplayName(displayName, shortDisplayName);
134 docModel.setProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER, shortIdentifier);
137 if (isUnique(docModel, schemaName) == false) {
138 String shortId = generateShortIdentifier == null ? shortIdentifier : generateShortIdentifier;
139 String errMsgVerb = generateShortIdentifier == null ? "supplied" : "generated";
140 String errMsg = String.format("The %s short identifier '%s' was not unique, so the new authority could not be created.",
141 errMsgVerb, shortId);
142 throw new DocumentException(errMsg);
147 * Generate a refName for the authority from the short identifier
150 * All refNames for authorities are generated. If a client supplies
151 * a refName, it will be overwritten during create (per this method)
152 * or discarded during update (per filterReadOnlyPropertiesForPart).
154 * @see #filterReadOnlyPropertiesForPart(Map<String, Object>, org.collectionspace.services.common.service.ObjectPartType)
157 protected void updateRefnameForAuthority(DocumentWrapper<DocumentModel> wrapDoc, String schemaName) throws Exception {
158 DocumentModel docModel = wrapDoc.getWrappedObject();
159 RefName.Authority authority = (Authority) getRefName(getServiceContext(), docModel);
160 String refName = authority.toString();
161 docModel.setProperty(schemaName, AuthorityJAXBSchema.REF_NAME, refName);
165 public RefName.RefNameInterface getRefName(ServiceContext ctx,
166 DocumentModel docModel) {
167 RefName.RefNameInterface refname = null;
170 String shortIdentifier = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
171 String displayName = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.DISPLAY_NAME);
172 RefName.Authority authority = RefName.Authority.buildAuthority(ctx.getTenantName(),
173 ctx.getServiceName(),
174 null, // Only use shortId form!!!
178 } catch (Exception e) {
179 logger.error(e.getMessage(), e);
186 protected String getRefnameDisplayName(DocumentWrapper<DocumentModel> docWrapper) {
187 String result = null;
189 DocumentModel docModel = docWrapper.getWrappedObject();
190 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
191 RefName.Authority refname = (RefName.Authority)getRefName(ctx, docModel);
192 result = refname.getDisplayName();
197 public String getShortIdentifier(String authCSID, String schemaName) throws Exception {
198 String shortIdentifier = null;
199 CoreSessionInterface repoSession = null;
201 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
202 RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(ctx);
204 repoSession = nuxeoRepoClient.getRepositorySession(ctx);
205 DocumentWrapper<DocumentModel> wrapDoc = nuxeoRepoClient.getDocFromCsid(ctx, repoSession, authCSID);
206 DocumentModel docModel = wrapDoc.getWrappedObject();
207 shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
208 } catch (ClientException ce) {
209 throw new RuntimeException("AuthorityDocHandler Internal Error: cannot get shortId!", ce);
211 if (repoSession != null) {
212 nuxeoRepoClient.releaseRepositorySession(ctx, repoSession);
216 return shortIdentifier;
220 * Filters out selected values supplied in an update request.
222 * @param objectProps the properties filtered out from the update payload
223 * @param partMeta metadata for the object to fill
226 public void filterReadOnlyPropertiesForPart(
227 Map<String, Object> objectProps, ObjectPartType partMeta) {
228 super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
229 String commonPartLabel = getServiceContext().getCommonPartLabel();
230 if (partMeta.getLabel().equalsIgnoreCase(commonPartLabel)) {
231 objectProps.remove(AuthorityJAXBSchema.CSID);
232 objectProps.remove(AuthorityJAXBSchema.SHORT_IDENTIFIER);
233 objectProps.remove(AuthorityJAXBSchema.REF_NAME);