View Javadoc
1   /**
2    * Copyright 2014 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   * Created by pauldanielrichardson on 6/16/14
16   */
17  package org.kuali.student.enrollment.registration.client.service.impl;
18  
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.student.enrollment.registration.client.service.StaticUserDateClientService;
21  import org.kuali.student.enrollment.registration.client.service.impl.util.StaticUserDateUtil;
22  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import javax.ws.rs.core.Response;
27  import javax.ws.rs.core.Response.ResponseBuilder;
28  import java.util.Map;
29  
30  /**
31   * This class implements RESTful services for setting and retrieving
32   * static date for testing registration dates. These should be used
33   * instead of using the system date (since the time is constantly
34   * changing).
35   *
36   * @author Kuali Student Team
37   */
38  public class StaticUserDateClientServiceImpl implements StaticUserDateClientService {
39  
40      public static final Logger LOGGER = LoggerFactory.getLogger(ScheduleOfClassesClientServiceImpl.class);
41  
42      @Override
43      public Response setStaticDate(String userId, String date) {
44          ResponseBuilder response;
45  
46          try {
47              StaticUserDateUtil.putDateInMap(userId, date);
48              response = Response.ok();
49          } catch (InvalidParameterException ex) {
50              LOGGER.warn("Unable to set static date for userId {} / date {} ... invalid parameters.", userId, date, ex);
51              response = Response.status(Response.Status.BAD_REQUEST);
52          } catch (Exception ex) {
53              LOGGER.warn("Unable to set static date to {} for userId {}: {}", date, userId, ex.getMessage(), ex);
54              response = Response.serverError();
55          }
56  
57          return response.build();
58      }
59  
60      @Override
61      public Response getStaticDate(String userId) {
62          return getStaticDate(userId, null);
63      }
64  
65      @Override
66      public Response getStaticDate(String userId, String date) {
67          ResponseBuilder builder = null;
68          Response response = null;
69  
70          /*
71          Date is optional, but if it is passed in, attempt to update the user
72          before proceeding
73           */
74          if (StringUtils.isNotEmpty(date)) {
75              Response updateResponse = setStaticDate(userId, date);
76              if (updateResponse.getStatus() != Response.Status.OK.getStatusCode()) {
77                  response = updateResponse;
78              }
79          }
80  
81          if (response == null) {
82              try {
83                  builder = Response.ok(StaticUserDateUtil.getDateFromMap(userId));
84              } catch (InvalidParameterException ex) {
85                  LOGGER.warn("Unable to get static date for userId {} ... invalid parameters.", userId, ex);
86                  builder = Response.status(Response.Status.BAD_REQUEST);
87              } catch (Exception ex) {
88                  LOGGER.warn("Unable to get static date for userId {}: {}", userId, ex.getMessage(), ex);
89                  builder = Response.serverError();
90              } finally {
91                  if (builder == null) {
92                      builder = Response.serverError();
93                  }
94                  response = builder.build();
95              }
96          }
97  
98          return response;
99      }
100 
101     @Override
102     public Response clearStaticDate(String userId) {
103         ResponseBuilder response;
104 
105         try {
106             StaticUserDateUtil.removeDateFromMap(userId);
107             response = Response.ok();
108         } catch (InvalidParameterException ex) {
109             LOGGER.warn("Unable to remove static date for userId {} ... invalid parameters.", userId, ex);
110             response = Response.status(Response.Status.BAD_REQUEST);
111         } catch (Exception ex) {
112             LOGGER.warn("Unable to remove static date from map for userId {}: {}", userId, ex.getMessage(), ex);
113             response = Response.serverError();
114         }
115 
116         return response.build();
117     }
118 
119     @Override
120     public Response getStaticDates() {
121         ResponseBuilder response;
122 
123         try {
124             response = Response.ok(StaticUserDateUtil.getDatesFromMap());
125         } catch (Exception ex) {
126             LOGGER.warn("Unable to get static date map: {}", ex.getMessage(), ex);
127             response = Response.serverError();
128         }
129         return response.build();
130     }
131 
132     public void setRegUserMap(Map<String, String> regUserMap) {
133         StaticUserDateUtil.loadMap(regUserMap);
134     }
135 }