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.maintenance.MaintainableImpl;
22 import org.kuali.rice.krad.util.GlobalVariables;
23 import org.kuali.rice.krad.util.ObjectUtils;
24 import org.kuali.student.common.uif.service.KSMaintainable;
25 import org.kuali.student.r2.common.dto.AttributeInfo;
26 import org.kuali.student.r2.common.dto.ContextInfo;
27 import org.kuali.student.r2.common.exceptions.*;
28 import org.kuali.student.common.util.security.ContextUtils;
29 import org.kuali.student.r2.core.class1.state.dto.StateInfo;
30 import org.kuali.student.r2.core.class1.state.service.StateService;
31 import org.kuali.student.r2.core.class1.type.dto.TypeInfo;
32 import org.kuali.student.r2.core.class1.type.service.TypeService;
33 import org.kuali.student.r2.core.constants.StateServiceConstants;
34 import org.kuali.student.r2.core.constants.TypeServiceConstants;
35
36 import javax.xml.namespace.QName;
37 import java.util.HashMap;
38 import java.util.Map;
39
40
41
42
43
44
45 public class KSMaintainableImpl extends MaintainableImpl implements KSMaintainable {
46 private static final long serialVersionUID = 1L;
47 private final static Logger LOG = Logger.getLogger(KSMaintainableImpl.class);
48
49 private transient Map<String,Class> helperClasses;
50 private transient Map<String,Object> helpers;
51
52 public KSMaintainableImpl(){
53 super();
54 helperClasses = new HashMap<String, Class>();
55 helpers = new HashMap<String, Object>();
56 }
57
58
59
60
61
62
63
64
65 public ContextInfo createContextInfo(){
66 return ContextUtils.createDefaultContextInfo();
67 }
68
69
70
71
72
73
74
75 public StateInfo getStateInfo(String stateKey){
76 StateService stateService = GlobalResourceLoader.getService(new QName(StateServiceConstants.NAMESPACE, StateServiceConstants.SERVICE_NAME_LOCAL_PART));
77 try {
78 return stateService.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 TypeService typeService = GlobalResourceLoader.getService(new QName(TypeServiceConstants.NAMESPACE, TypeServiceConstants.SERVICE_NAME_LOCAL_PART));
93 try {
94 return typeService.getType(typeKey, createContextInfo());
95 }catch (Exception e){
96 throw convertServiceExceptionsToUI(e);
97 }
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111 public RuntimeException convertServiceExceptionsToUI(Exception ex){
112
113 if (LOG.isEnabledFor(Level.ERROR)){
114 LOG.error(ex);
115 }
116
117
118
119
120
121
122
123
124 if (ex instanceof DoesNotExistException){
125 return new RuntimeException("Does Not Exists - " + ex.getMessage(),ex);
126 } else if (ex instanceof InvalidParameterException){
127 return new RuntimeException("Invalid parameter - " + ex.getMessage(),ex);
128 } else if (ex instanceof MissingParameterException){
129 return new RuntimeException("Missing parameter - " + ex.getMessage(),ex);
130 } else if (ex instanceof OperationFailedException){
131 return new RuntimeException("Operation Failed - " + ex.getMessage(),ex);
132 } else if (ex instanceof PermissionDeniedException){
133 return new RuntimeException("Permission Denied - " + ex.getMessage(),ex);
134 } else if (ex instanceof DataValidationErrorException){
135 return new RuntimeException("Data Validation Error - " + ex.getMessage(), ex);
136 } else {
137 return new RuntimeException(ex.getMessage(),ex);
138 }
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162 public void setHelperClasses(Map<String, Class> helperClasses) {
163 this.helperClasses = helperClasses;
164 }
165
166
167
168
169
170
171
172 public Object getHelper(String helperName) {
173 Object helper = helpers.get(helperName);
174 if (helper == null){
175 Class clazz = helperClasses.get(helperName);
176 if (clazz != null){
177 helper = ObjectUtils.newInstance(clazz);
178 helpers.put(helperName,helper);
179 }
180 }
181 return helper;
182 }
183
184 }