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