1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.uif.service.impl;
17
18 import org.apache.log4j.Level;
19 import org.apache.log4j.Logger;
20 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21 import org.kuali.rice.krad.uif.service.impl.ViewHelperServiceImpl;
22 import org.kuali.rice.krad.util.ObjectUtils;
23 import org.kuali.student.common.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 import org.kuali.student.r2.core.constants.StateServiceConstants;
32 import org.kuali.student.r2.core.constants.TypeServiceConstants;
33
34 import javax.xml.namespace.QName;
35 import java.util.HashMap;
36 import java.util.Map;
37
38
39
40
41
42
43 public class KSViewHelperServiceImpl extends ViewHelperServiceImpl implements KSViewHelperService {
44 private static final long serialVersionUID = 1L;
45 private final static Logger LOG = Logger.getLogger(KSViewHelperServiceImpl.class);
46
47 private transient StateService stateService;
48 private transient TypeService typeService;
49
50 private transient Map<String,Class> helperClasses;
51 private transient Map<String,Object> helpers;
52
53 public KSViewHelperServiceImpl(){
54 super();
55 helperClasses = new HashMap<String, Class>();
56 helpers = new HashMap<String, Object>();
57 }
58
59
60
61
62
63
64
65
66 public ContextInfo createContextInfo(){
67 return ContextUtils.createDefaultContextInfo();
68 }
69
70
71
72
73
74
75
76 public StateInfo getStateInfo(String stateKey){
77 try {
78 return getStateService().getState(stateKey, createContextInfo());
79 }catch (Exception e){
80 throw convertServiceExceptionsToUI(e);
81 }
82
83 }
84
85
86
87
88
89
90
91 public TypeInfo getTypeInfo(String typeKey){
92 try {
93 return getTypeService().getType(typeKey, createContextInfo());
94 }catch (Exception e){
95 throw convertServiceExceptionsToUI(e);
96 }
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110 public RuntimeException convertServiceExceptionsToUI(Exception ex){
111
112 if (LOG.isEnabledFor(Level.ERROR)){
113 LOG.error(ex);
114 }
115
116
117
118
119
120
121
122
123 if (ex instanceof DoesNotExistException){
124 return new RuntimeException("Does Not Exists - " + ex.getMessage(),ex);
125 } else if (ex instanceof InvalidParameterException){
126 return new RuntimeException("Invalid parameter - " + ex.getMessage(),ex);
127 } else if (ex instanceof MissingParameterException){
128 return new RuntimeException("Missing parameter - " + ex.getMessage(),ex);
129 } else if (ex instanceof OperationFailedException){
130 return new RuntimeException("Operation Failed - " + ex.getMessage(),ex);
131 } else if (ex instanceof PermissionDeniedException){
132 return new RuntimeException("Permission Denied - " + ex.getMessage(),ex);
133 } else {
134 return new RuntimeException(ex.getMessage(),ex);
135 }
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 public void setHelperClasses(Map<String, Class> helperClasses) {
160 this.helperClasses = helperClasses;
161 }
162
163
164
165
166
167
168
169 public Object getHelper(String helperName) {
170 Object helper = helpers.get(helperName);
171 if (helper == null){
172 Class clazz = helperClasses.get(helperName);
173 if (clazz != null){
174 helper = ObjectUtils.newInstance(clazz);
175 helpers.put(helperName,helper);
176 }
177 }
178 return helper;
179 }
180
181 protected StateService getStateService(){
182 if (stateService == null){
183 stateService = GlobalResourceLoader.getService(new QName(StateServiceConstants.NAMESPACE, StateServiceConstants.SERVICE_NAME_LOCAL_PART));
184 }
185 return stateService;
186 }
187
188 protected TypeService getTypeService(){
189 if (typeService == null){
190 typeService = GlobalResourceLoader.getService(new QName(TypeServiceConstants.NAMESPACE, TypeServiceConstants.SERVICE_NAME_LOCAL_PART));
191 }
192 return typeService;
193 }
194 }