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.Tools;
32 import org.collectionspace.services.common.context.MultipartServiceContext;
33 import org.collectionspace.services.common.context.ServiceContext;
34 import org.collectionspace.services.common.document.DocumentWrapper;
35 import org.collectionspace.services.common.vocabulary.AuthorityJAXBSchema;
36 import org.collectionspace.services.config.service.ObjectPartType;
38 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
39 import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl;
40 import org.nuxeo.ecm.core.api.ClientException;
41 import org.nuxeo.ecm.core.api.DocumentModel;
42 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
45 * AuthorityDocumentModelHandler
47 * $LastChangedRevision: $
50 public abstract class AuthorityDocumentModelHandler<AuthCommon>
51 extends DocHandlerBase<AuthCommon> {
53 private String authorityCommonSchemaName;
55 public AuthorityDocumentModelHandler(String authorityCommonSchemaName) {
56 this.authorityCommonSchemaName = authorityCommonSchemaName;
60 * Non standard injection of CSID into common part, since caller may access through
61 * shortId, and not know the CSID.
62 * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#extractPart(org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, org.collectionspace.services.common.service.ObjectPartType)
65 protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
67 Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
69 // Add the CSID to the common part
70 if (partMeta.getLabel().equalsIgnoreCase(authorityCommonSchemaName)) {
71 String csid = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
72 unQObjectProperties.put("csid", csid);
75 return unQObjectProperties;
79 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
80 super.handleCreate(wrapDoc);
82 // Uncomment once debugged and App layer is read to integrate
83 // Experimenting with this uncommented now ...
84 handleDisplayNameAsShortIdentifier(wrapDoc.getWrappedObject(), authorityCommonSchemaName);
85 updateRefnameForAuthority(wrapDoc, authorityCommonSchemaName);//CSPACE-3178
89 * If no short identifier was provided in the input payload,
90 * generate a short identifier from the display name.
92 private void handleDisplayNameAsShortIdentifier(DocumentModel docModel, String schemaName) throws Exception {
93 String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
94 String displayName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.DISPLAY_NAME);
95 String shortDisplayName = "";
96 if (Tools.isEmpty(shortIdentifier)) {
97 String generatedShortIdentifier = AuthorityIdentifierUtils.generateShortIdentifierFromDisplayName(displayName, shortDisplayName);
98 docModel.setProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER, generatedShortIdentifier);
103 * Generate a refName for the authority from the short identifier
106 * All refNames for authorities are generated. If a client supplies
107 * a refName, it will be overwritten during create (per this method)
108 * or discarded during update (per filterReadOnlyPropertiesForPart).
110 * @see #filterReadOnlyPropertiesForPart(Map<String, Object>, org.collectionspace.services.common.service.ObjectPartType)
113 protected void updateRefnameForAuthority(DocumentWrapper<DocumentModel> wrapDoc, String schemaName) throws Exception {
114 DocumentModel docModel = wrapDoc.getWrappedObject();
115 String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
116 String displayName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.DISPLAY_NAME);
117 MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
118 RefName.Authority authority = RefName.buildAuthority(ctx.getTenantName(),
119 ctx.getServiceName(),
122 String refName = authority.toString();
123 docModel.setProperty(schemaName, AuthorityJAXBSchema.REF_NAME, refName);
126 public String getShortIdentifier(String authCSID, String schemaName) throws Exception {
127 String shortIdentifier = null;
128 RepositoryInstance repoSession = null;
130 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
131 RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(ctx);
133 repoSession = nuxeoRepoClient.getRepositorySession();
134 DocumentWrapper<DocumentModel> wrapDoc = nuxeoRepoClient.getDocFromCsid(ctx, repoSession, authCSID);
135 DocumentModel docModel = wrapDoc.getWrappedObject();
136 shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
137 } catch (ClientException ce) {
138 throw new RuntimeException("AuthorityDocHandler Internal Error: cannot get shortId!", ce);
140 if (repoSession != null) {
141 nuxeoRepoClient.releaseRepositorySession(repoSession);
145 return shortIdentifier;
149 * Filters out selected values supplied in an update request.
151 * @param objectProps the properties filtered out from the update payload
152 * @param partMeta metadata for the object to fill
155 public void filterReadOnlyPropertiesForPart(
156 Map<String, Object> objectProps, ObjectPartType partMeta) {
157 super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
158 String commonPartLabel = getServiceContext().getCommonPartLabel();
159 if (partMeta.getLabel().equalsIgnoreCase(commonPartLabel)) {
160 objectProps.remove(AuthorityJAXBSchema.CSID);
161 objectProps.remove(AuthorityJAXBSchema.SHORT_IDENTIFIER);
162 objectProps.remove(AuthorityJAXBSchema.REF_NAME);