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