]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
44d14ceaf48060c2228e17c5958124ed9663a7b2
[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;
25
26 import java.util.List;
27 import java.util.Map;
28
29 import javax.ws.rs.Consumes;
30 import javax.ws.rs.DELETE;
31 import javax.ws.rs.GET;
32 import javax.ws.rs.POST;
33 import javax.ws.rs.PUT;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.core.Context;
38 import javax.ws.rs.core.MultivaluedMap;
39 import javax.ws.rs.core.Response;
40 import javax.ws.rs.core.UriBuilder;
41 import javax.ws.rs.core.UriInfo;
42
43 import org.collectionspace.services.client.*;
44 import org.collectionspace.services.common.CSWebApplicationException;
45 import org.collectionspace.services.common.StoredValuesUriTemplate;
46 import org.collectionspace.services.common.UriTemplateFactory;
47 import org.collectionspace.services.common.UriTemplateRegistryKey;
48 import org.collectionspace.services.common.vocabulary.AuthorityResource;
49 import org.collectionspace.services.common.context.ServiceContext;
50 import org.collectionspace.services.common.document.DocumentFilter;
51 import org.collectionspace.services.common.document.DocumentHandler;
52 import org.collectionspace.services.contact.ContactResource;
53 import org.collectionspace.services.contact.ContactsCommon;
54 import org.collectionspace.services.contact.ContactJAXBSchema;
55 import org.collectionspace.services.contact.nuxeo.ContactConstants;
56 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
57 import org.collectionspace.services.jaxb.AbstractCommonList;
58 import org.jboss.resteasy.util.HttpResponseCodes;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62 /**
63  * The Class AuthorityResourceWithContacts.
64  */
65 @Consumes("application/xml")
66 @Produces("application/xml")
67 public abstract class AuthorityResourceWithContacts<AuthCommon, AuthItemHandler> extends //FIXME: REM - Why is this resource in this package instead of somewhere in 'common'?
68         AuthorityResource<AuthCommon, AuthItemHandler> {
69
70     private ContactResource contactResource = new ContactResource(); // Warning: ContactResource is a singleton.
71     final Logger logger = LoggerFactory.getLogger(AuthorityResourceWithContacts.class);
72
73     public AuthorityResourceWithContacts(
74             Class<AuthCommon> authCommonClass, Class<?> resourceClass,
75             String authorityCommonSchemaName, String authorityItemCommonSchemaName) {
76         super(authCommonClass, resourceClass,
77                 authorityCommonSchemaName, authorityItemCommonSchemaName);
78     }
79
80     public abstract String getItemServiceName();
81
82     public String getContactServiceName() {
83         return contactResource.getServiceName();
84     }
85     
86     private DocumentHandler createContactDocumentHandler(
87             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String inAuthority,
88             String inItem) throws Exception {
89         UriInfo ui = null;
90         return createContactDocumentHandler(ctx, inAuthority, inItem, ui);
91     }
92
93     private DocumentHandler createContactDocumentHandler(
94             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String inAuthority,
95             String inItem, UriInfo ui) throws Exception {
96         ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler) createDocumentHandler(
97                 ctx,
98                 ctx.getCommonPartLabel(getContactServiceName()),
99                 ContactsCommon.class);
100         docHandler.setInAuthority(inAuthority);
101         docHandler.setInItem(inItem);
102         docHandler.getServiceContext().setUriInfo(ui);
103         return docHandler;
104     }
105
106     /*************************************************************************
107      * Contact parts - this is a sub-resource of the AuthorityItem
108      * @param parentspecifier either a CSID or one of the urn forms
109      * @param itemspecifier either a CSID or one of the urn forms
110      * @return contact
111      *************************************************************************/
112     @POST
113     @Path("{parentcsid}/items/{itemcsid}/contacts")
114     public Response createContact(
115             @PathParam("parentcsid") String parentspecifier,
116             @PathParam("itemcsid") String itemspecifier,
117             String xmlPayload,
118             @Context UriInfo ui) {
119         try {
120             PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
121             String parentcsid = lookupParentCSID(parentspecifier, "createContact(authority)", "CREATE_ITEM_CONTACT", null);
122
123             ServiceContext itemCtx = createServiceContext(getItemServiceName());
124             String itemcsid = lookupItemCSID(itemCtx, itemspecifier, parentcsid, "createContact(item)", "CREATE_ITEM_CONTACT");
125
126             // Note that we have to create the service context and document
127             // handler for the Contact service, not the main service.
128             ServiceContext ctx = createServiceContext(getContactServiceName(), input);
129             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid, ui);
130             String csid = getRepositoryClient(ctx).create(ctx, handler);
131
132             UriBuilder path = UriBuilder.fromResource(resourceClass);
133             path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
134             Response response = Response.created(path.build()).build();
135             return response;
136         } catch (Exception e) {
137             throw bigReThrow(e,
138                     "Create Contact failed; one of the requested specifiers for authority:"
139                     + parentspecifier + ": and item:" + itemspecifier + ": was not found.",
140                     itemspecifier);
141         }
142     }
143     
144     public String createContact(ServiceContext existingCtx, String parentCsid, String itemCsid, PoxPayloadIn input,
145             UriInfo ui) throws Exception {
146         ServiceContext ctx = createServiceContext(getContactServiceName(), input);
147         if (existingCtx != null) {
148             Object repoSession = existingCtx.getCurrentRepositorySession();
149             if (repoSession != null) {
150                 ctx.setCurrentRepositorySession(repoSession);
151                 ctx.setProperties(existingCtx.getProperties());
152             }
153         }
154         
155         DocumentHandler handler = createContactDocumentHandler(ctx, parentCsid, itemCsid, ui);
156         String csid = getRepositoryClient(ctx).create(ctx, handler);
157
158         return csid;
159     }
160
161     /**
162      * Gets the contact list.
163      * 
164      * @param parentspecifier either a CSID or one of the urn forms
165      * @param itemspecifier either a CSID or one of the urn forms
166      * @param ui the ui
167      * 
168      * @return the contact list
169      */
170     @GET
171     @Produces({"application/xml"})
172     @Path("{parentcsid}/items/{itemcsid}/contacts/")
173     public AbstractCommonList getContactList(
174             @PathParam("parentcsid") String parentspecifier,
175             @PathParam("itemcsid") String itemspecifier,
176             @Context UriInfo uriInfo) {
177         AbstractCommonList contactObjectList = new AbstractCommonList();
178
179         try {
180             ServiceContext ctx = createServiceContext(getContactServiceName(), uriInfo);
181             MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
182                 
183             String parentcsid = lookupParentCSID(parentspecifier, "getContactList(parent)", "GET_CONTACT_LIST", null);
184             ServiceContext itemCtx = createServiceContext(getItemServiceName());
185             String itemcsid = lookupItemCSID(itemCtx, itemspecifier, parentcsid, "getContactList(item)", "GET_CONTACT_LIST");
186
187             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid, uriInfo);
188             DocumentFilter myFilter = handler.getDocumentFilter(); //new DocumentFilter();
189             myFilter.appendWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":"
190                     + ContactJAXBSchema.IN_AUTHORITY
191                     + "='" + parentcsid + "'"
192                     + IQueryManager.SEARCH_QUALIFIER_AND
193                     + ContactJAXBSchema.CONTACTS_COMMON + ":"
194                     + ContactJAXBSchema.IN_ITEM
195                     + "='" + itemcsid + "'",
196                     IQueryManager.SEARCH_QUALIFIER_AND);  // "AND" this clause to any existing
197             getRepositoryClient(ctx).getFiltered(ctx, handler);
198             contactObjectList = (AbstractCommonList) handler.getCommonPartList();
199         } catch (Exception e) {
200             throw bigReThrow(e,
201                     "Get ContactList failed; one of the requested specifiers for authority:"
202                     + parentspecifier + ": and item:" + itemspecifier + ": was not found.",
203                     itemspecifier);
204         }
205         
206         return contactObjectList;
207     }
208
209     /**
210      * Gets the contact.
211      * 
212      * @param parentspecifier either a CSID or one of the urn forms
213      * @param itemspecifier either a CSID or one of the urn forms
214      * @param csid the csid
215      * 
216      * @return the contact
217      */
218     @GET
219     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
220     public String getContact(
221             @PathParam("parentcsid") String parentspecifier,
222             @PathParam("itemcsid") String itemspecifier,
223             @PathParam("csid") String csid) {
224         PoxPayloadOut result = null;
225         try {
226             String parentcsid = lookupParentCSID(parentspecifier, "getContact(parent)", "GET_ITEM_CONTACT", null);
227
228             ServiceContext<PoxPayloadIn, PoxPayloadOut> itemCtx = createServiceContext(getItemServiceName());
229             String itemcsid = lookupItemCSID(itemCtx, itemspecifier, parentcsid, "getContact(item)", "GET_ITEM_CONTACT");
230
231             // Note that we have to create the service context and document handler for the Contact service, not the main service.
232             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getContactServiceName());
233             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
234             getRepositoryClient(ctx).get(ctx, csid, handler);
235             result = (PoxPayloadOut) ctx.getOutput();
236         } catch (Exception e) {
237             throw bigReThrow(e, "Get failed, the requested Contact CSID:" + csid
238                     + ": or one of the specifiers for authority:" + parentspecifier
239                     + ": and item:" + itemspecifier + ": was not found.",
240                     csid);
241         }
242         if (result == null) {
243             Response response = Response.status(Response.Status.NOT_FOUND).entity("Get failed, the requested Contact CSID:" + csid + ": was not found.").type("text/plain").build();
244             throw new CSWebApplicationException(response);
245         }
246         return result.toXML();
247     }
248
249     /**
250      * Update contact.
251      * 
252      * @param parentspecifier either a CSID or one of the urn forms
253      * @param itemspecifier either a CSID or one of the urn forms
254      * @param csid the csid
255      *
256      * @return the multipart output
257      */
258     @PUT
259     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
260     public String updateContact(
261             @PathParam("parentcsid") String parentspecifier,
262             @PathParam("itemcsid") String itemspecifier,
263             @PathParam("csid") String csid,
264             String xmlPayload) {
265         PoxPayloadOut result = null;
266         try {
267             PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
268             String parentcsid = lookupParentCSID(parentspecifier, "updateContact(authority)", "UPDATE_CONTACT", null);
269
270             ServiceContext<PoxPayloadIn, PoxPayloadOut> itemCtx = createServiceContext(getItemServiceName());
271             String itemcsid = lookupItemCSID(itemCtx, itemspecifier, parentcsid, "updateContact(item)", "UPDATE_CONTACT");
272
273             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
274             // Note that we have to create the service context and document handler for the Contact service, not the main service.
275             ctx = createServiceContext(getContactServiceName(), theUpdate);
276             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
277             getRepositoryClient(ctx).update(ctx, csid, handler);
278             result = (PoxPayloadOut) ctx.getOutput();
279         } catch (Exception e) {
280             throw bigReThrow(e, "Update failed, the requested Contact CSID:" + csid
281                     + ": or one of the specifiers for authority:" + parentspecifier
282                     + ": and item:" + itemspecifier + ": was not found.",
283                     csid);
284         }
285         return result.toXML();
286     }
287
288     /**
289      * Delete contact.
290      * 
291      * @param parentspecifier either a CSID or one of the urn forms
292      * @param itemspecifier either a CSID or one of the urn forms
293      * @param csid the csid
294      * 
295      * @return the response
296      */
297     @DELETE
298     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
299     public Response deleteContact(
300             @PathParam("parentcsid") String parentspecifier,
301             @PathParam("itemcsid") String itemspecifier,
302             @PathParam("csid") String csid) {
303         try {
304             String parentcsid = lookupParentCSID(parentspecifier, "deleteContact(authority)", "DELETE_CONTACT", null);
305
306             ServiceContext<PoxPayloadIn, PoxPayloadOut> itemCtx = createServiceContext(getItemServiceName());
307             String itemcsid = lookupItemCSID(itemCtx, itemspecifier, parentcsid, "deleteContact(item)", "DELETE_CONTACT");
308             //NOTE: itemcsid is not used below.  Leaving the above call in for possible side effects???       CSPACE-3175
309
310             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
311             // Note that we have to create the service context for the Contact service, not the main service.
312             ctx = createServiceContext(getContactServiceName());
313             DocumentHandler handler = createDocumentHandler(ctx);
314             getRepositoryClient(ctx).delete(ctx, csid, handler);
315             return Response.status(HttpResponseCodes.SC_OK).build();
316         } catch (Exception e) {
317             throw bigReThrow(e, "DELETE failed, the requested Contact CSID:" + csid
318                     + ": or one of the specifiers for authority:" + parentspecifier
319                     + ": and item:" + itemspecifier + ": was not found.", csid);
320         }
321     }
322     
323     protected String getContactDocType() {
324         return ContactConstants.NUXEO_DOCTYPE;
325     }
326     
327     /**
328      * Returns a UriRegistry entry: a map of tenant-qualified URI templates
329      * for the current resource, for all tenants
330      * 
331      * @return a map of URI templates for the current resource, for all tenants
332      */
333     @Override
334     public Map<UriTemplateRegistryKey,StoredValuesUriTemplate> getUriRegistryEntries() {
335         Map<UriTemplateRegistryKey,StoredValuesUriTemplate> uriRegistryEntriesMap =
336                 super.getUriRegistryEntries();
337         List<String> tenantIds = getTenantBindingsReader().getTenantIds();
338         for (String tenantId : tenantIds) {
339                 uriRegistryEntriesMap.putAll(getUriRegistryEntries(tenantId, getContactDocType(), UriTemplateFactory.CONTACT));
340         }
341         return uriRegistryEntriesMap;
342     }
343
344 }