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.document.DocumentHandler.Action;
38 import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
39 import org.collectionspace.services.common.vocabulary.AuthorityJAXBSchema;
40 import org.collectionspace.services.config.service.ObjectPartType;
41 import org.collectionspace.services.nuxeo.client.java.NuxeoDocumentModelHandler;
42 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
43 import org.collectionspace.services.nuxeo.client.java.RepositoryClientImpl;
44 import org.nuxeo.ecm.core.api.ClientException;
45 import org.nuxeo.ecm.core.api.DocumentModel;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * AuthorityDocumentModelHandler
52 * $LastChangedRevision: $
55 public abstract class AuthorityDocumentModelHandler<AuthCommon>
56 extends NuxeoDocumentModelHandler<AuthCommon> {
58 private final Logger logger = LoggerFactory.getLogger(AuthorityDocumentModelHandler.class);
59 private String authorityCommonSchemaName;
61 public AuthorityDocumentModelHandler(String authorityCommonSchemaName) {
62 this.authorityCommonSchemaName = authorityCommonSchemaName;
66 * Non standard injection of CSID into common part, since caller may access through
67 * shortId, and not know the CSID.
68 * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#extractPart(org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, org.collectionspace.services.common.service.ObjectPartType)
71 protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
73 Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
75 // Add the CSID to the common part
76 if (partMeta.getLabel().equalsIgnoreCase(authorityCommonSchemaName)) {
77 String csid = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
78 unQObjectProperties.put("csid", csid);
81 return unQObjectProperties;
84 public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
85 super.fillAllParts(wrapDoc, action);
87 // Update the record's revision number on both CREATE and UPDATE actions
89 updateRevNumbers(wrapDoc);
92 protected void updateRevNumbers(DocumentWrapper<DocumentModel> wrapDoc) {
93 DocumentModel documentModel = wrapDoc.getWrappedObject();
94 Long rev = (Long)documentModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.REV);
100 documentModel.setProperty(authorityCommonSchemaName, AuthorityJAXBSchema.REV, rev);
104 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
105 super.handleCreate(wrapDoc);
107 // Uncomment once debugged and App layer is read to integrate
108 // Experimenting with this uncommented now ...
109 handleDisplayNameAsShortIdentifier(wrapDoc.getWrappedObject(), authorityCommonSchemaName);
110 updateRefnameForAuthority(wrapDoc, authorityCommonSchemaName);//CSPACE-3178
113 protected String buildWhereForShortId(String name) {
114 return authorityCommonSchemaName
115 + ":" + AuthorityJAXBSchema.SHORT_IDENTIFIER
119 private boolean isUnique(DocumentModel docModel, String schemaName) throws DocumentException {
123 private boolean temp_isUnique(DocumentModel docModel, String schemaName) throws DocumentException {
124 boolean result = true;
126 ServiceContext ctx = this.getServiceContext();
127 String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
128 String nxqlWhereClause = buildWhereForShortId(shortIdentifier);
130 DocumentWrapper<DocumentModel> searchResultWrapper = getRepositoryClient(ctx).findDoc(ctx, nxqlWhereClause);
131 if (searchResultWrapper != null) {
133 if (logger.isInfoEnabled() == true) {
134 DocumentModel searchResult = searchResultWrapper.getWrappedObject();
135 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'",
136 shortIdentifier, searchResult.getName());
137 logger.trace(debugMsg);
140 } catch (DocumentNotFoundException e) {
141 // Not a problem, just means we couldn't find another authority with that short ID
148 * If no short identifier was provided in the input payload,
149 * generate a short identifier from the display name. Either way though,
150 * the short identifier needs to be unique.
152 private void handleDisplayNameAsShortIdentifier(DocumentModel docModel, String schemaName) throws Exception {
153 String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
154 String displayName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.DISPLAY_NAME);
155 String shortDisplayName = "";
156 String generateShortIdentifier = null;
157 if (Tools.isEmpty(shortIdentifier)) {
158 generateShortIdentifier = AuthorityIdentifierUtils.generateShortIdentifierFromDisplayName(displayName, shortDisplayName);
159 docModel.setProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER, shortIdentifier);
162 if (isUnique(docModel, schemaName) == false) {
163 String shortId = generateShortIdentifier == null ? shortIdentifier : generateShortIdentifier;
164 String errMsgVerb = generateShortIdentifier == null ? "supplied" : "generated";
165 String errMsg = String.format("The %s short identifier '%s' was not unique, so the new authority could not be created.",
166 errMsgVerb, shortId);
167 throw new DocumentException(errMsg);
172 * Generate a refName for the authority from the short identifier
175 * All refNames for authorities are generated. If a client supplies
176 * a refName, it will be overwritten during create (per this method)
177 * or discarded during update (per filterReadOnlyPropertiesForPart).
179 * @see #filterReadOnlyPropertiesForPart(Map<String, Object>, org.collectionspace.services.common.service.ObjectPartType)
182 protected void updateRefnameForAuthority(DocumentWrapper<DocumentModel> wrapDoc, String schemaName) throws Exception {
183 DocumentModel docModel = wrapDoc.getWrappedObject();
184 RefName.Authority authority = (Authority) getRefName(getServiceContext(), docModel);
185 String refName = authority.toString();
186 docModel.setProperty(schemaName, AuthorityJAXBSchema.REF_NAME, refName);
190 public RefName.RefNameInterface getRefName(ServiceContext ctx,
191 DocumentModel docModel) {
192 RefName.RefNameInterface refname = null;
195 String shortIdentifier = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
196 String displayName = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.DISPLAY_NAME);
197 RefName.Authority authority = RefName.Authority.buildAuthority(ctx.getTenantName(),
198 ctx.getServiceName(),
199 null, // Only use shortId form!!!
203 } catch (Exception e) {
204 logger.error(e.getMessage(), e);
211 protected String getRefnameDisplayName(DocumentWrapper<DocumentModel> docWrapper) {
212 String result = null;
214 DocumentModel docModel = docWrapper.getWrappedObject();
215 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
216 RefName.Authority refname = (RefName.Authority)getRefName(ctx, docModel);
217 result = refname.getDisplayName();
222 public String getShortIdentifier(String authCSID, String schemaName) throws Exception {
223 String shortIdentifier = null;
224 CoreSessionInterface repoSession = null;
226 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
227 RepositoryClientImpl nuxeoRepoClient = (RepositoryClientImpl)this.getRepositoryClient(ctx);
229 repoSession = nuxeoRepoClient.getRepositorySession(ctx);
230 DocumentWrapper<DocumentModel> wrapDoc = nuxeoRepoClient.getDocFromCsid(ctx, repoSession, authCSID);
231 DocumentModel docModel = wrapDoc.getWrappedObject();
232 shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
233 } catch (ClientException ce) {
234 throw new RuntimeException("AuthorityDocHandler Internal Error: cannot get shortId!", ce);
236 if (repoSession != null) {
237 nuxeoRepoClient.releaseRepositorySession(ctx, repoSession);
241 return shortIdentifier;
245 * Filters out selected values supplied in an update request.
247 * @param objectProps the properties filtered out from the update payload
248 * @param partMeta metadata for the object to fill
251 public void filterReadOnlyPropertiesForPart(
252 Map<String, Object> objectProps, ObjectPartType partMeta) {
253 super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
254 String commonPartLabel = getServiceContext().getCommonPartLabel();
255 if (partMeta.getLabel().equalsIgnoreCase(commonPartLabel)) {
256 objectProps.remove(AuthorityJAXBSchema.CSID);
257 objectProps.remove(AuthorityJAXBSchema.SHORT_IDENTIFIER);
258 objectProps.remove(AuthorityJAXBSchema.REF_NAME);