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/9/14
16   */
17  package org.kuali.student.enrollment.registration.client.service.impl.util;
18  
19  import org.codehaus.jackson.map.ObjectMapper;
20  import org.codehaus.jackson.type.TypeReference;
21  import org.kuali.student.enrollment.registration.client.service.dto.ConflictCourseResult;
22  import org.kuali.student.enrollment.registration.client.service.dto.RegistrationValidationConflictCourseResult;
23  import org.kuali.student.enrollment.registration.client.service.dto.RegistrationValidationMaxCreditResult;
24  import org.kuali.student.enrollment.registration.client.service.dto.RegistrationValidationResult;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import java.io.IOException;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * This class contains utility methods for passing complex messaging out of ValidationResultInfo objects
35   *
36   * @author Kuali Student Team
37   */
38  public class RegistrationValidationResultsUtil {
39      public static final Logger LOGGER = LoggerFactory.getLogger(RegistrationValidationResultsUtil.class);
40  
41      private RegistrationValidationResultsUtil() {
42      }
43  
44      public static String marshallSimpleMessage(String messageKey) {
45          return marshallResult(new RegistrationValidationResult(messageKey));
46      }
47  
48      public static String marshallConflictCourseMessage(String messageKey, List<ConflictCourseResult> conflictingCourses) {
49          return marshallResult(new RegistrationValidationConflictCourseResult(messageKey, conflictingCourses));
50      }
51  
52      public static String marshallMaxCreditMessage(String messageKey, String maxCredits) {
53          return marshallResult(new RegistrationValidationMaxCreditResult(messageKey, maxCredits));
54      }
55  
56      public static String marshallResult(RegistrationValidationResult result) {
57          ObjectMapper mapper = new ObjectMapper();
58          String json = null;
59          try {
60              json = mapper.writeValueAsString(result);
61          } catch (IOException ex) {
62              LOGGER.error("Unable to marshall result object", ex);
63          }
64          return json;
65      }
66  
67      public static Map<String, Object> unmarshallResult(String result) {
68          ObjectMapper mapper = new ObjectMapper();
69          try {
70              TypeReference<HashMap<String, Object>> typeRef
71                      = new TypeReference<HashMap<String, Object>>() {
72              };
73  
74              return mapper.readValue(result, typeRef);
75          } catch (IOException e) {
76              LOGGER.error("Unable to marshall result object", e);
77          }
78          return null;
79      }
80  
81  }