1 /**
2 * Copyright 2012 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 */
16 package org.kuali.student.enrollment.uif.service.impl;
17
18 import org.apache.log4j.Level;
19 import org.apache.log4j.Logger;
20 import org.kuali.rice.krad.uif.service.impl.ViewHelperServiceImpl;
21 import org.kuali.rice.krad.util.ObjectUtils;
22 import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingResourceLoader;
23 import org.kuali.student.enrollment.uif.service.KSViewHelperService;
24 import org.kuali.student.r2.common.dto.ContextInfo;
25 import org.kuali.student.r2.common.exceptions.*;
26 import org.kuali.student.r2.common.util.ContextUtils;
27 import org.kuali.student.r2.core.class1.state.dto.StateInfo;
28 import org.kuali.student.r2.core.class1.state.service.StateService;
29 import org.kuali.student.r2.core.class1.type.dto.TypeInfo;
30 import org.kuali.student.r2.core.class1.type.service.TypeService;
31
32 import java.util.HashMap;
33 import java.util.Map;
34
35 /**
36 * Base class for the KS View helper service.
37 *
38 * @author Kuali Student Team
39 */
40 public class KSViewHelperServiceImpl extends ViewHelperServiceImpl implements KSViewHelperService {
41 private static final long serialVersionUID = 1L;
42 private final static Logger LOG = Logger.getLogger(KSViewHelperServiceImpl.class);
43
44 private transient StateService stateService;
45 private transient TypeService typeService;
46
47 private transient Map<String,Class> helperClasses;
48 private transient Map<String,Object> helpers;
49
50 public KSViewHelperServiceImpl(){
51 super();
52 helperClasses = new HashMap<String, Class>();
53 helpers = new HashMap<String, Object>();
54 }
55
56 /**
57 * This method creates a new context, which is needed for the service call. This is to create
58 * <code>ContextInfo</code> with the following information - current logged in user, current local
59 * date and locale information
60 *
61 * @return a new ContextInfo
62 */
63 public ContextInfo createContextInfo(){
64 return ContextUtils.createDefaultContextInfo();
65 }
66
67 /**
68 * This method gets the <code>StateInfo</code> for a given state key
69 *
70 * @param stateKey
71 * @return StateInfo for the given state key
72 */
73 public StateInfo getStateInfo(String stateKey){
74 try {
75 return getStateService().getState(stateKey, createContextInfo());
76 }catch (Exception e){
77 throw convertServiceExceptionsToUI(e);
78 }
79
80 }
81
82 /**
83 * This method gets the <code>TypeInfo</code> for a given type key
84 *
85 * @param typeKey
86 * @return TypeInfo for the given type key
87 */
88 public TypeInfo getTypeInfo(String typeKey){
89 try {
90 return getTypeService().getType(typeKey, createContextInfo());
91 }catch (Exception e){
92 throw convertServiceExceptionsToUI(e);
93 }
94 }
95
96 /**
97 * This is a helper method which handles all the services exceptions. This can be the central point to handle all
98 * the services exceptions. For now, we're converting all the exceptions to Runtime exceptions.
99 *
100 * Once we implement authz and when services fully implemented the decorators/exceptions, we need to
101 * refactor to handle some of the exceptions with HandlerExceptionResolver (Note, underlying exception is passed to
102 * the runtime exception as cause... So, this can be accessed at the exception resolver to determine the action)
103 *
104 * @param ex service exception to be handled
105 * @return service exception wrapped in a RuntimeException instance
106 */
107 public RuntimeException convertServiceExceptionsToUI(Exception ex){
108
109 if (LOG.isEnabledFor(Level.ERROR)){
110 LOG.error(ex);
111 }
112 //Commented for now... once service throws MPE/IPE(KSENROLL-3446), needs to rework.
113 /*StringBuilder refinedErrorMessage = new StringBuilder();
114 refinedErrorMessage.append(ex.getMessage());
115 refinedErrorMessage.append("[Caused at ");
116 refinedErrorMessage.append(ex.getStackTrace()[0].getClass().getSimpleName() + ".");
117 refinedErrorMessage.append(ex.getStackTrace()[0].getMethodName() + "()");
118 refinedErrorMessage.append(ex.getStackTrace()[0].getLineNumber() + "]");*/
119
120 if (ex instanceof DoesNotExistException){
121 return new RuntimeException("Does Not Exists - " + ex.getMessage(),ex);
122 } else if (ex instanceof InvalidParameterException){
123 return new RuntimeException("Invalid parameter - " + ex.getMessage(),ex);
124 } else if (ex instanceof MissingParameterException){
125 return new RuntimeException("Missing parameter - " + ex.getMessage(),ex);
126 } else if (ex instanceof OperationFailedException){
127 return new RuntimeException("Operation Failed - " + ex.getMessage(),ex);
128 } else if (ex instanceof PermissionDeniedException){
129 return new RuntimeException("Permission Denied - " + ex.getMessage(),ex);
130 } else {
131 return new RuntimeException(ex.getMessage(),ex);
132 }
133 }
134
135 /**
136 * This is a map which holds all the helper classes by name. This can be used from the view xml file like
137 *
138 * <code>
139 * <property name="viewHelperService.helperClasses">
140 * <map>
141 * <entry key="publishHelper">
142 * <value type="java.lang.Class">org.kuali.student.enrollment.class2.courseofferingset.service.impl.CourseOfferingSetPublishingHelper</value>
143 * </entry>
144 * </map>
145 * </property>
146 * </code>
147 *
148 * And from the helper class, we can call like
149 *
150 * <code>
151 * CourseOfferingSetPublishingHelper mpeHelper = (CourseOfferingSetPublishingHelper)getHelper("publishHelper");
152 * </code>
153 *
154 * @param helperClasses map of helper classes with name as keys and classes as values
155 */
156 public void setHelperClasses(Map<String, Class> helperClasses) {
157 this.helperClasses = helperClasses;
158 }
159
160 /**
161 * This creates an instance of the helper class if it's already not there.
162 *
163 * @param helperName
164 * @return
165 */
166 public Object getHelper(String helperName) {
167 Object helper = helpers.get(helperName);
168 if (helper == null){
169 Class clazz = helperClasses.get(helperName);
170 if (clazz != null){
171 helper = ObjectUtils.newInstance(clazz);
172 helpers.put(helperName,helper);
173 }
174 }
175 return helper;
176 }
177
178 protected StateService getStateService(){
179 if (stateService == null){
180 stateService = CourseOfferingResourceLoader.loadStateService();
181 }
182 return stateService;
183 }
184
185 protected TypeService getTypeService(){
186 if (typeService == null){
187 typeService = CourseOfferingResourceLoader.loadTypeService();
188 }
189 return typeService;
190 }
191 }