1 package org.collectionspace.services.structureddate;
3 import java.math.BigInteger;
5 import javax.ws.rs.Consumes;
6 import javax.ws.rs.GET;
7 import javax.ws.rs.Path;
8 import javax.ws.rs.Produces;
9 import javax.ws.rs.core.Context;
10 import javax.ws.rs.core.MultivaluedMap;
11 import javax.ws.rs.core.Response;
12 import javax.ws.rs.core.UriInfo;
14 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
15 import org.collectionspace.services.common.CSWebApplicationException;
16 import org.collectionspace.services.common.ServiceMessages;
17 import org.collectionspace.services.common.api.Tools;
18 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
19 import org.collectionspace.services.common.context.ServiceContext;
20 import org.collectionspace.services.common.context.ServiceContextFactory;
22 @Path(StructuredDateClient.SERVICE_PATH)
23 @Produces({"application/xml"})
24 @Consumes({"application/xml"})
25 public class StructuredDateResource extends AbstractCollectionSpaceResourceImpl<StructureddateCommon, StructureddateCommon> {
28 public Class<?> getCommonPartClass() {
29 // TODO Auto-generated method stub
34 public String getServiceName() {
35 return StructuredDateClient.SERVICE_NAME;
39 protected String getVersionString() {
40 // TODO Auto-generated method stub
49 public StructureddateCommon get(@Context UriInfo ui) {
50 StructureddateCommon result = null;
53 ServiceContext<StructureddateCommon, StructureddateCommon> ctx = createServiceContext(getServiceName());
54 MultivaluedMap<String,String> queryParams = ui.getQueryParameters();
55 String dateToParse = queryParams.getFirst(StructuredDateClient.DATE_TO_PARSE_QP);
56 if (Tools.isEmpty(dateToParse) != true) {
57 StructuredDateInternal structuredDate = StructuredDateInternal.parse(dateToParse);
58 result = toStructureddateCommon(ctx.getTenantName(), structuredDate);
60 String msg = String.format("Use the '%s' query parameter to specify a date string you want parsed.",
61 StructuredDateClient.DATE_TO_PARSE_QP);
63 Response.status(Response.Status.BAD_REQUEST).entity(msg).type("text/plain").build();
64 throw new CSWebApplicationException(response);
66 } catch(StructuredDateFormatException fe) {
67 Response response = Response.status(Response.Status.BAD_REQUEST).entity(fe.getMessage()).type("text/plain").build();
68 throw new CSWebApplicationException(response);
69 } catch (Exception e) {
70 throw bigReThrow(e, ServiceMessages.GET_FAILED);
76 private StructureddateCommon toStructureddateCommon(String tenantDomain, StructuredDateInternal structuredDate) {
77 StructureddateCommon result = new StructureddateCommon();
79 String association = structuredDate.getAssociation();
80 if (!Tools.isEmpty(association)) {
81 result.setAssociation(association);
84 String displayDate = structuredDate.getDisplayDate();
85 if (!Tools.isEmpty(displayDate)) {
86 result.setDisplayDate(displayDate);
89 String earliestScalarDate = structuredDate.getEarliestScalarDate();
90 if (!Tools.isEmpty(earliestScalarDate)) {
91 result.setEarliestScalarDate(earliestScalarDate);
94 Date earliestSingleDate = structuredDate.getEarliestSingleDate();
95 if (earliestSingleDate != null) {
96 result.setEarliestSingleDate(toDateCommon(tenantDomain, earliestSingleDate));
99 result.setLatestDate(toDateCommon(tenantDomain, structuredDate.getLatestDate()));
100 Date latestDate = structuredDate.getLatestDate();
101 if (latestDate != null) {
102 result.setLatestDate(toDateCommon(tenantDomain, latestDate));
108 private DateCommon toDateCommon(String tenantDomain, org.collectionspace.services.structureddate.Date date) {
109 DateCommon result = null;
112 result = new DateCommon();
114 if (date.getCertainty() != null) {
115 result.setCertainty(date.getCertainty().toString());
118 if (date.getDay() != null) {
119 result.setDay(BigInteger.valueOf(date.getDay()));
122 if (date.getEra() != null) {
123 result.setEra(date.getEra().toString(tenantDomain));
126 if (date.getMonth() != null) {
127 result.setMonth(BigInteger.valueOf(date.getMonth()));
130 if (date.getQualifierType() != null) {
131 result.setQualifierType(date.getQualifierType().toString());
134 if (date.getQualifierUnit() != null) {
135 result.setQualifierUnit(date.getQualifierUnit().toString());
138 if (date.getQualifierValue() != null) {
139 result.setQualifierValue(date.getQualifierValue().toString());
142 if (date.getYear() != null) {
143 result.setYear(BigInteger.valueOf(date.getYear()));
151 public ServiceContextFactory<StructureddateCommon, StructureddateCommon> getServiceContextFactory() {
152 return (ServiceContextFactory<StructureddateCommon, StructureddateCommon>) RemoteServiceContextFactory.get();