1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.r2.lum.statement.config.context;
17
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.List ;
21 import org.kuali.student.r1.lum.lrc.dto.ResultComponentTypeInfo;
22
23 import org.kuali.student.r1.core.statement.dto.ReqComponentInfo;
24 import org.kuali.student.r2.lum.lrc.dto.ResultValuesGroupInfo;
25 import org.kuali.student.r1.lum.lrc.service.LrcService;
26 import org.kuali.student.r1.lum.statement.typekey.ReqComponentFieldTypes;
27 import org.kuali.student.r2.common.exceptions.OperationFailedException;
28 import org.kuali.student.r2.common.dto.ContextInfo;
29
30
31
32
33 public class LrcContextImpl extends BasicContextImpl {
34
35 private LrcService lrcService;
36
37
38 public final static String GRADE_TOKEN = "grade";
39 public final static String GRADE_TYPE_TOKEN = "gradeType";
40
41 public void setLrcService(LrcService lrcService) {
42 this.lrcService = lrcService;
43 }
44
45 private ResultValuesGroupInfo getResultComponentByResultComponentId(String resultComponentId, ContextInfo contextInfo) throws OperationFailedException {
46 if (resultComponentId == null) {
47 return null;
48 }
49 try {
50
51
52 return lrcService.getResultValuesGroup(resultComponentId, contextInfo);
53 } catch (Exception e) {
54 throw new OperationFailedException(e.getMessage(), e);
55 }
56 }
57
58 private String getResultValue(ResultValuesGroupInfo resultComponent, String resultValue) throws OperationFailedException {
59 if(resultComponent == null || resultValue == null) {
60 return null;
61 }
62 for(String rv : resultComponent.getResultValueKeys()) {
63 if(rv.equals(resultValue)) {
64 return rv;
65 }
66 }
67 throw new OperationFailedException("Result value not found: "+resultValue);
68 }
69
70 private ResultValuesGroupInfo getResultComponentByResultValueId(String resultValueId, ContextInfo contextInfo) throws OperationFailedException {
71 if(resultValueId == null) {
72 return null;
73 }
74
75 try {
76
77 List<ResultComponentTypeInfo> typeList = lrcService.getResultComponentTypes(contextInfo);
78 for(ResultComponentTypeInfo type : typeList) {
79 List<String> resultComponentIdList = lrcService.getResultValuesGroupIdsByType(type.getId(), contextInfo);
80 for(String resultComponentId : resultComponentIdList) {
81 ResultValuesGroupInfo resultComponent = lrcService.getResultValuesGroup(resultComponentId, contextInfo);
82 if(resultComponent.getResultValueKeys().contains(resultValueId)) {
83 return resultComponent;
84 }
85 }
86 }
87 } catch (Exception e) {
88 throw new OperationFailedException(e.getMessage(), e);
89 }
90 throw new OperationFailedException("Result value id not found: "+resultValueId);
91 }
92
93
94
95
96
97
98
99
100 public Map<String, Object> createContextMap(ReqComponentInfo reqComponent, ContextInfo contextInfo) throws OperationFailedException {
101 Map<String, Object> contextMap = new HashMap<String, Object>();
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 ResultValuesGroupInfo gradeTypeResultComponent = null;
117 String gradeId = getReqComponentFieldValue(reqComponent, ReqComponentFieldTypes.GRADE_KEY.getId());
118 if (gradeId == null) {
119 String gradeTypeId = getReqComponentFieldValue(reqComponent, ReqComponentFieldTypes.GRADE_TYPE_KEY.getId());
120 gradeTypeResultComponent = getResultComponentByResultComponentId(gradeTypeId, contextInfo);
121 if (gradeTypeResultComponent != null) {
122 contextMap.put(GRADE_TYPE_TOKEN, gradeTypeResultComponent);
123 }
124 } else {
125 gradeTypeResultComponent = getResultComponentByResultValueId(gradeId, contextInfo);
126 }
127 if (gradeTypeResultComponent != null) {
128 contextMap.put(GRADE_TYPE_TOKEN, gradeTypeResultComponent);
129 }
130
131 String grade = getResultValue(gradeTypeResultComponent, gradeId);
132 if(grade != null) {
133 contextMap.put(GRADE_TOKEN, grade);
134 }
135
136 return contextMap;
137 }
138 }