]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a19e658fceb118b27396bbbc1e9161f44357ff46
[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         if (!Tools.isEmpty(association)) {
81                 result.setAssociation(association);
82         }
83         
84         String displayDate = structuredDate.getDisplayDate();
85         if (!Tools.isEmpty(displayDate)) {
86                 result.setDisplayDate(displayDate);
87         }       
88         
89         String earliestScalarDate = structuredDate.getEarliestScalarDate();
90         if (!Tools.isEmpty(earliestScalarDate)) {
91                 result.setEarliestScalarDate(earliestScalarDate);
92         }       
93         
94         Date earliestSingleDate = structuredDate.getEarliestSingleDate();
95         if (earliestSingleDate != null) {
96                 result.setEarliestSingleDate(toDateCommon(tenantDomain, earliestSingleDate));
97         }       
98         
99         result.setLatestDate(toDateCommon(tenantDomain, structuredDate.getLatestDate()));
100         Date latestDate = structuredDate.getLatestDate();
101         if (latestDate != null) {
102                 result.setLatestDate(toDateCommon(tenantDomain, latestDate));
103         }       
104         
105         return result;
106     }
107
108     private DateCommon toDateCommon(String tenantDomain, org.collectionspace.services.structureddate.Date date) {
109         DateCommon result = null;
110         
111         if (date != null) {
112                 result = new DateCommon();
113                 
114                 if (date.getCertainty() != null) {
115                         result.setCertainty(date.getCertainty().toString());
116                 }
117                 
118                 if (date.getDay() != null) {
119                         result.setDay(BigInteger.valueOf(date.getDay()));
120                 }
121                 
122                 if (date.getEra() != null) {
123                         result.setEra(date.getEra().toString(tenantDomain));
124                 }
125                 
126                 if (date.getMonth() != null) {
127                         result.setMonth(BigInteger.valueOf(date.getMonth()));
128                 }
129                 
130                 if (date.getQualifierType() != null) {
131                         result.setQualifierType(date.getQualifierType().toString());
132                 }
133                 
134                 if (date.getQualifierUnit() != null) {
135                         result.setQualifierUnit(date.getQualifierUnit().toString());
136                 }
137                 
138                 if (date.getQualifierValue() != null) {
139                         result.setQualifierValue(date.getQualifierValue().toString());
140                 }
141                 
142                 if (date.getYear() != null) {
143                         result.setYear(BigInteger.valueOf(date.getYear()));
144                 }
145         }
146         
147         return result;
148     }
149
150         @Override
151         public ServiceContextFactory<StructureddateCommon, StructureddateCommon> getServiceContextFactory() {
152         return (ServiceContextFactory<StructureddateCommon, StructureddateCommon>) RemoteServiceContextFactory.get();
153         }
154 }