]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
327a3a2f891b78c47ceaa2990fb693067ee95f85
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.structureddate;
2
3 import java.math.BigInteger;
4
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;
13
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;
21
22 @Path(StructuredDateClient.SERVICE_PATH)
23 @Produces({"application/xml"})
24 @Consumes({"application/xml"})
25 public class StructuredDateResource extends AbstractCollectionSpaceResourceImpl<StructureddateCommon, StructureddateCommon> {
26
27         @Override
28         public Class<?> getCommonPartClass() {
29                 // TODO Auto-generated method stub
30                 return null;
31         }
32
33         @Override
34         public String getServiceName() {
35                 return StructuredDateClient.SERVICE_NAME;
36         }
37
38         @Override
39         protected String getVersionString() {
40                 // TODO Auto-generated method stub
41                 return null;
42         }
43
44         //
45         // API Endpoints
46         //
47
48     @GET
49     public StructureddateCommon get(@Context UriInfo ui) {
50         StructureddateCommon result = null;
51
52         try {
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);
59                 } else {
60                         String msg = String.format("Use the '%s' query parameter to specify a date string you want parsed.",
61                                         StructuredDateClient.DATE_TO_PARSE_QP);
62                         Response response =
63                                 Response.status(Response.Status.BAD_REQUEST).entity(msg).type("text/plain").build();
64                         throw new CSWebApplicationException(response);
65                 }
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);
71         }
72
73         return result;
74     }
75
76                 private StructureddateCommon toStructureddateCommon(String tenantDomain, StructuredDateInternal structuredDate) {
77                         StructureddateCommon result = new StructureddateCommon();
78
79                         String association = structuredDate.getAssociation();
80
81                         if (!Tools.isEmpty(association)) {
82                                 result.setAssociation(association);
83                         }
84
85                         String displayDate = structuredDate.getDisplayDate();
86
87                         if (!Tools.isEmpty(displayDate)) {
88                                 result.setDisplayDate(displayDate);
89                         }
90
91                         String earliestScalarValue = structuredDate.getEarliestScalarValue();
92
93                         if (!Tools.isEmpty(earliestScalarValue)) {
94                                 result.setEarliestScalarValue(earliestScalarValue);
95                         }
96
97                         String latestScalarValue = structuredDate.getLatestScalarValue();
98
99                         if (!Tools.isEmpty(latestScalarValue)) {
100                                 result.setLatestScalarValue(latestScalarValue);
101                         }
102
103                         result.setScalarValuesComputed(structuredDate.areScalarValuesComputed());
104
105                         Date earliestSingleDate = structuredDate.getEarliestSingleDate();
106
107                         if (earliestSingleDate != null) {
108                                 result.setEarliestSingleDate(toDateCommon(tenantDomain, earliestSingleDate));
109                         }
110
111                         Date latestDate = structuredDate.getLatestDate();
112
113                         if (latestDate != null) {
114                                 result.setLatestDate(toDateCommon(tenantDomain, latestDate));
115                         }
116
117                         return result;
118                 }
119
120     private DateCommon toDateCommon(String tenantDomain, org.collectionspace.services.structureddate.Date date) {
121         DateCommon result = null;
122
123         if (date != null) {
124                 result = new DateCommon();
125
126                 if (date.getCertainty() != null) {
127                         result.setCertainty(date.getCertainty().toString());
128                 }
129
130                 if (date.getDay() != null) {
131                         result.setDay(BigInteger.valueOf(date.getDay()));
132                 }
133
134                 if (date.getEra() != null) {
135                         result.setEra(date.getEra().toString(tenantDomain));
136                 }
137
138                 if (date.getMonth() != null) {
139                         result.setMonth(BigInteger.valueOf(date.getMonth()));
140                 }
141
142                 if (date.getQualifierType() != null) {
143                         result.setQualifierType(date.getQualifierType().toString());
144                 }
145
146                 if (date.getQualifierUnit() != null) {
147                         result.setQualifierUnit(date.getQualifierUnit().toString());
148                 }
149
150                 if (date.getQualifierValue() != null) {
151                         result.setQualifierValue(date.getQualifierValue().toString());
152                 }
153
154                 if (date.getYear() != null) {
155                         result.setYear(BigInteger.valueOf(date.getYear()));
156                 }
157         }
158
159         return result;
160     }
161
162         @Override
163         public ServiceContextFactory<StructureddateCommon, StructureddateCommon> getServiceContextFactory() {
164         return (ServiceContextFactory<StructureddateCommon, StructureddateCommon>) RemoteServiceContextFactory.get();
165         }
166 }