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 javax.ws.rs.core.MultivaluedMap;
30 import org.collectionspace.services.client.IQueryManager;
31 import org.collectionspace.services.client.PoxPayloadIn;
32 import org.collectionspace.services.client.PoxPayloadOut;
33 import org.collectionspace.services.common.api.RefName;
34 import org.collectionspace.services.common.api.RefName.Authority;
35 import org.collectionspace.services.common.api.Tools;
36 import org.collectionspace.services.common.context.MultipartServiceContext;
37 import org.collectionspace.services.common.context.ServiceContext;
38 import org.collectionspace.services.common.document.DocumentWrapper;
39 import org.collectionspace.services.common.vocabulary.AuthorityJAXBSchema;
40 import org.collectionspace.services.config.service.ObjectPartType;
42 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
43 import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl;
44 import org.nuxeo.ecm.core.api.ClientException;
45 import org.nuxeo.ecm.core.api.DocumentModel;
46 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * AuthorityDocumentModelHandler
53 * $LastChangedRevision: $
56 public abstract class AuthorityDocumentModelHandler<AuthCommon>
57 extends DocHandlerBase<AuthCommon> {
59 private final Logger logger = LoggerFactory.getLogger(AuthorityDocumentModelHandler.class);
60 private String authorityCommonSchemaName;
62 public AuthorityDocumentModelHandler(String authorityCommonSchemaName) {
63 this.authorityCommonSchemaName = authorityCommonSchemaName;
67 * Non standard injection of CSID into common part, since caller may access through
68 * shortId, and not know the CSID.
69 * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#extractPart(org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, org.collectionspace.services.common.service.ObjectPartType)
72 protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
74 Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
76 // Add the CSID to the common part
77 if (partMeta.getLabel().equalsIgnoreCase(authorityCommonSchemaName)) {
78 String csid = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
79 unQObjectProperties.put("csid", csid);
82 return unQObjectProperties;
86 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
87 super.handleCreate(wrapDoc);
89 // Uncomment once debugged and App layer is read to integrate
90 // Experimenting with this uncommented now ...
91 handleDisplayNameAsShortIdentifier(wrapDoc.getWrappedObject(), authorityCommonSchemaName);
92 updateRefnameForAuthority(wrapDoc, authorityCommonSchemaName);//CSPACE-3178
96 * If no short identifier was provided in the input payload,
97 * generate a short identifier from the display name.
99 private void handleDisplayNameAsShortIdentifier(DocumentModel docModel, String schemaName) throws Exception {
100 String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
101 String displayName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.DISPLAY_NAME);
102 String shortDisplayName = "";
103 if (Tools.isEmpty(shortIdentifier)) {
104 String generatedShortIdentifier = AuthorityIdentifierUtils.generateShortIdentifierFromDisplayName(displayName, shortDisplayName);
105 docModel.setProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER, generatedShortIdentifier);
110 * Generate a refName for the authority from the short identifier
113 * All refNames for authorities are generated. If a client supplies
114 * a refName, it will be overwritten during create (per this method)
115 * or discarded during update (per filterReadOnlyPropertiesForPart).
117 * @see #filterReadOnlyPropertiesForPart(Map<String, Object>, org.collectionspace.services.common.service.ObjectPartType)
120 protected void updateRefnameForAuthority(DocumentWrapper<DocumentModel> wrapDoc, String schemaName) throws Exception {
121 DocumentModel docModel = wrapDoc.getWrappedObject();
122 RefName.Authority authority = (Authority) getRefName(getServiceContext(), docModel);
123 String refName = authority.toString();
124 docModel.setProperty(schemaName, AuthorityJAXBSchema.REF_NAME, refName);
128 public RefName.RefNameInterface getRefName(ServiceContext ctx,
129 DocumentModel docModel) {
130 RefName.RefNameInterface refname = null;
133 String shortIdentifier = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
134 String displayName = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.DISPLAY_NAME);
135 RefName.Authority authority = RefName.Authority.buildAuthority(ctx.getTenantName(),
136 ctx.getServiceName(),
137 null, // Only use shortId form!!!
141 } catch (Exception e) {
142 logger.error(e.getMessage(), e);
149 protected String getRefnameDisplayName(DocumentWrapper<DocumentModel> docWrapper) {
150 String result = null;
152 DocumentModel docModel = docWrapper.getWrappedObject();
153 ServiceContext ctx = this.getServiceContext();
154 RefName.Authority refname = (RefName.Authority)getRefName(ctx, docModel);
155 result = refname.getDisplayName();
160 public String getShortIdentifier(String authCSID, String schemaName) throws Exception {
161 String shortIdentifier = null;
162 RepositoryInstance repoSession = null;
164 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
165 RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(ctx);
167 repoSession = nuxeoRepoClient.getRepositorySession(ctx);
168 DocumentWrapper<DocumentModel> wrapDoc = nuxeoRepoClient.getDocFromCsid(ctx, repoSession, authCSID);
169 DocumentModel docModel = wrapDoc.getWrappedObject();
170 shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
171 } catch (ClientException ce) {
172 throw new RuntimeException("AuthorityDocHandler Internal Error: cannot get shortId!", ce);
174 if (repoSession != null) {
175 nuxeoRepoClient.releaseRepositorySession(ctx, repoSession);
179 return shortIdentifier;
183 * Filters out selected values supplied in an update request.
185 * @param objectProps the properties filtered out from the update payload
186 * @param partMeta metadata for the object to fill
189 public void filterReadOnlyPropertiesForPart(
190 Map<String, Object> objectProps, ObjectPartType partMeta) {
191 super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
192 String commonPartLabel = getServiceContext().getCommonPartLabel();
193 if (partMeta.getLabel().equalsIgnoreCase(commonPartLabel)) {
194 objectProps.remove(AuthorityJAXBSchema.CSID);
195 objectProps.remove(AuthorityJAXBSchema.SHORT_IDENTIFIER);
196 objectProps.remove(AuthorityJAXBSchema.REF_NAME);