]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0887cf267c766c85aabf0afb431a91238f1deddc
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.common.vocabulary.nuxeo;
25
26 import java.util.Map;
27
28 import javax.ws.rs.core.MultivaluedMap;
29
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;
41 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
42 import org.collectionspace.services.nuxeo.client.java.RepositoryInstanceInterface;
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;
49
50 /**
51  * AuthorityDocumentModelHandler
52  *
53  * $LastChangedRevision: $
54  * $LastChangedDate: $
55  */
56 public abstract class AuthorityDocumentModelHandler<AuthCommon>
57         extends DocHandlerBase<AuthCommon> {
58
59     private final Logger logger = LoggerFactory.getLogger(AuthorityDocumentModelHandler.class); 
60     private String authorityCommonSchemaName;
61
62     public AuthorityDocumentModelHandler(String authorityCommonSchemaName) {
63         this.authorityCommonSchemaName = authorityCommonSchemaName;
64     }
65
66     /*
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)
70      */
71     @Override
72     protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
73             throws Exception {
74         Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
75
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);
80         }
81
82         return unQObjectProperties;
83     }
84
85     @Override
86     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
87         super.handleCreate(wrapDoc);
88         // CSPACE-3178:
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
93     }
94
95     /**
96      * If no short identifier was provided in the input payload,
97      * generate a short identifier from the display name.
98      */
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);
106         }
107     }
108  
109     /**
110      * Generate a refName for the authority from the short identifier
111      * and display name.
112      * 
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).
116      * 
117      * @see #filterReadOnlyPropertiesForPart(Map<String, Object>, org.collectionspace.services.common.service.ObjectPartType)
118      * 
119      */
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);
125     }
126     
127     @Override
128     public RefName.RefNameInterface getRefName(ServiceContext ctx,
129                 DocumentModel docModel) {
130         RefName.RefNameInterface refname = null;
131
132         try {
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!!!
138                         shortIdentifier,
139                         displayName);
140                 refname = authority;
141         } catch (Exception e) {
142                 logger.error(e.getMessage(), e);
143         }
144         
145         return refname;
146     }
147     
148     @Override
149     protected String getRefnameDisplayName(DocumentWrapper<DocumentModel> docWrapper) {
150         String result = null;
151         
152         DocumentModel docModel = docWrapper.getWrappedObject();
153         ServiceContext ctx = this.getServiceContext();
154         RefName.Authority refname = (RefName.Authority)getRefName(ctx, docModel);
155         result = refname.getDisplayName();
156         
157         return result;
158     }    
159     
160     public String getShortIdentifier(String authCSID, String schemaName) throws Exception {
161         String shortIdentifier = null;
162         RepositoryInstanceInterface repoSession = null;
163
164         ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
165         RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(ctx);
166         try {
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);
173         } finally {
174                 if (repoSession != null) {
175                         nuxeoRepoClient.releaseRepositorySession(ctx, repoSession);
176                 }
177         }
178         
179         return shortIdentifier;
180     }
181
182     /**
183      * Filters out selected values supplied in an update request.
184      * 
185      * @param objectProps the properties filtered out from the update payload
186      * @param partMeta metadata for the object to fill
187      */
188     @Override
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);
197         }
198     }    
199 }