]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
6a5656a23fee7659c3e1a2c5bb8d23d2becd0790
[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 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.DocumentWrapper;
35 import org.collectionspace.services.common.vocabulary.AuthorityJAXBSchema;
36 import org.collectionspace.services.config.service.ObjectPartType;
37 import org.collectionspace.services.nuxeo.client.java.NuxeoDocumentModelHandler;
38 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
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.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * AuthorityDocumentModelHandler
47  *
48  * $LastChangedRevision: $
49  * $LastChangedDate: $
50  */
51 public abstract class AuthorityDocumentModelHandler<AuthCommon>
52         extends NuxeoDocumentModelHandler<AuthCommon> {
53
54     private final Logger logger = LoggerFactory.getLogger(AuthorityDocumentModelHandler.class); 
55     private String authorityCommonSchemaName;
56
57     public AuthorityDocumentModelHandler(String authorityCommonSchemaName) {
58         this.authorityCommonSchemaName = authorityCommonSchemaName;
59     }
60
61     /*
62      * Non standard injection of CSID into common part, since caller may access through
63      * shortId, and not know the CSID.
64      * @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      */
66     @Override
67     protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
68             throws Exception {
69         Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
70
71         // Add the CSID to the common part
72         if (partMeta.getLabel().equalsIgnoreCase(authorityCommonSchemaName)) {
73             String csid = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
74             unQObjectProperties.put("csid", csid);
75         }
76
77         return unQObjectProperties;
78     }
79
80     @Override
81     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
82         super.handleCreate(wrapDoc);
83         // CSPACE-3178:
84         // Uncomment once debugged and App layer is read to integrate
85         // Experimenting with this uncommented now ...
86         handleDisplayNameAsShortIdentifier(wrapDoc.getWrappedObject(), authorityCommonSchemaName);
87         updateRefnameForAuthority(wrapDoc, authorityCommonSchemaName);//CSPACE-3178
88     }
89
90     /**
91      * If no short identifier was provided in the input payload,
92      * generate a short identifier from the display name.
93      */
94     private void handleDisplayNameAsShortIdentifier(DocumentModel docModel, String schemaName) throws Exception {
95         String shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
96         String displayName = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.DISPLAY_NAME);
97         String shortDisplayName = "";
98         if (Tools.isEmpty(shortIdentifier)) {
99             String generatedShortIdentifier = AuthorityIdentifierUtils.generateShortIdentifierFromDisplayName(displayName, shortDisplayName);
100             docModel.setProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER, generatedShortIdentifier);
101         }
102     }
103  
104     /**
105      * Generate a refName for the authority from the short identifier
106      * and display name.
107      * 
108      * All refNames for authorities are generated.  If a client supplies
109      * a refName, it will be overwritten during create (per this method) 
110      * or discarded during update (per filterReadOnlyPropertiesForPart).
111      * 
112      * @see #filterReadOnlyPropertiesForPart(Map<String, Object>, org.collectionspace.services.common.service.ObjectPartType)
113      * 
114      */
115     protected void updateRefnameForAuthority(DocumentWrapper<DocumentModel> wrapDoc, String schemaName) throws Exception {
116         DocumentModel docModel = wrapDoc.getWrappedObject();
117         RefName.Authority authority = (Authority) getRefName(getServiceContext(), docModel);
118         String refName = authority.toString();
119         docModel.setProperty(schemaName, AuthorityJAXBSchema.REF_NAME, refName);
120     }
121     
122     @Override
123     public RefName.RefNameInterface getRefName(ServiceContext ctx,
124                 DocumentModel docModel) {
125         RefName.RefNameInterface refname = null;
126
127         try {
128                 String shortIdentifier = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
129                 String displayName = (String) docModel.getProperty(authorityCommonSchemaName, AuthorityJAXBSchema.DISPLAY_NAME);
130                 RefName.Authority authority = RefName.Authority.buildAuthority(ctx.getTenantName(),
131                         ctx.getServiceName(),
132                         null,   // Only use shortId form!!!
133                         shortIdentifier,
134                         displayName);
135                 refname = authority;
136         } catch (Exception e) {
137                 logger.error(e.getMessage(), e);
138         }
139         
140         return refname;
141     }
142     
143     @Override
144     protected String getRefnameDisplayName(DocumentWrapper<DocumentModel> docWrapper) {
145         String result = null;
146         
147         DocumentModel docModel = docWrapper.getWrappedObject();
148         ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
149         RefName.Authority refname = (RefName.Authority)getRefName(ctx, docModel);
150         result = refname.getDisplayName();
151         
152         return result;
153     }    
154     
155     public String getShortIdentifier(String authCSID, String schemaName) throws Exception {
156         String shortIdentifier = null;
157         CoreSessionInterface repoSession = null;
158
159         ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
160         RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(ctx);
161         try {
162                 repoSession = nuxeoRepoClient.getRepositorySession(ctx);
163             DocumentWrapper<DocumentModel> wrapDoc = nuxeoRepoClient.getDocFromCsid(ctx, repoSession, authCSID);
164             DocumentModel docModel = wrapDoc.getWrappedObject();
165             shortIdentifier = (String) docModel.getProperty(schemaName, AuthorityJAXBSchema.SHORT_IDENTIFIER);
166         } catch (ClientException ce) {
167             throw new RuntimeException("AuthorityDocHandler Internal Error: cannot get shortId!", ce);
168         } finally {
169                 if (repoSession != null) {
170                         nuxeoRepoClient.releaseRepositorySession(ctx, repoSession);
171                 }
172         }
173         
174         return shortIdentifier;
175     }
176
177     /**
178      * Filters out selected values supplied in an update request.
179      * 
180      * @param objectProps the properties filtered out from the update payload
181      * @param partMeta metadata for the object to fill
182      */
183     @Override
184     public void filterReadOnlyPropertiesForPart(
185             Map<String, Object> objectProps, ObjectPartType partMeta) {
186         super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
187         String commonPartLabel = getServiceContext().getCommonPartLabel();
188         if (partMeta.getLabel().equalsIgnoreCase(commonPartLabel)) {
189             objectProps.remove(AuthorityJAXBSchema.CSID);
190             objectProps.remove(AuthorityJAXBSchema.SHORT_IDENTIFIER);
191             objectProps.remove(AuthorityJAXBSchema.REF_NAME);
192         }
193     }    
194 }