1
2
3
4
5
6
7
8
9
10
11
12
13
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
37
38
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
58
59
60
61
62
63 public ContextInfo createContextInfo(){
64 return ContextUtils.createDefaultContextInfo();
65 }
66
67
68
69
70
71
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
84
85
86
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
98
99
100
101
102
103
104
105
106
107 public RuntimeException convertServiceExceptionsToUI(Exception ex){
108
109 if (LOG.isEnabledFor(Level.ERROR)){
110 LOG.error(ex);
111 }
112
113
114
115
116
117
118
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 public void setHelperClasses(Map<String, Class> helperClasses) {
157 this.helperClasses = helperClasses;
158 }
159
160
161
162
163
164
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 }