1 /**
2 * Copyright 2010 The Kuali Foundation Licensed under the
3 * Educational Community License, Version 2.0 (the "License"); you may
4 * not use this file except in compliance with the License. You may
5 * obtain a copy of the License at
6 *
7 * http://www.osedu.org/licenses/ECL-2.0
8 *
9 * Unless required by applicable law or agreed to in writing,
10 * software distributed under the License is distributed on an "AS IS"
11 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 package org.kuali.student.core.statement.naturallanguage;
17
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23
24 import org.kuali.student.core.exceptions.DoesNotExistException;
25 import org.kuali.student.core.exceptions.OperationFailedException;
26 import org.kuali.student.core.statement.dto.ReqCompFieldInfo;
27 import org.kuali.student.core.statement.dto.ReqCompFieldTypeInfo;
28 import org.kuali.student.core.statement.dto.ReqComponentInfo;
29 import org.kuali.student.core.statement.dto.ReqComponentTypeInfo;
30 import org.kuali.student.core.statement.service.StatementService;
31
32 /**
33 * This is an abstract class for creating a map (containing token/data) used
34 * in template translations.
35 *
36 * @param <T> Type of context to create
37 */
38 public abstract class AbstractContext<T> implements Context<T> {
39 /**
40 * <p>These common shared tokens are needed since velocity doesn't
41 * allow periods in tokens.</p>
42 * <p>E.g. kuali.reqComponent.field.type.totalCredits must either be convert to
43 * totalCredits or reqCompFieldType_totalCredits so a template would look
44 * like:</p>
45 * <p>'Student must take $totalCredits of MATH 100'</p>
46 * or
47 * <p>'Student must take $reqCompFieldType_totalCredits of MATH 100'</p>
48 */
49 protected final static String FIELDS_TOKEN = "fields";
50
51 /*private StatementService statementService;
52
53 public void setStatementService(StatementService statementService) {
54 this.statementService = statementService;
55 }
56
57 private void validateReqComponentFields(ReqComponentInfo reqComponent) throws OperationFailedException {
58 try {
59 ReqComponentTypeInfo reqComponentType = statementService.getReqComponentType(reqComponent.getType());
60 Set<String> set = new HashSet<String>();
61 for(ReqCompFieldTypeInfo fieldType : reqComponentType.getReqCompFieldTypeInfos()) {
62 set.add(fieldType.getFieldDescriptor().getId());
63 }
64
65 for(ReqCompFieldInfo field : reqComponent.getReqCompFields()) {
66 if(!set.contains(field.getType())) {
67 throw new OperationFailedException("Invalid field type: " + field.getType());
68 }
69 }
70 } catch (Exception e) {
71 throw new OperationFailedException(e.getMessage(), e);
72 }
73 }*/
74
75 /**
76 * Gets requirement component fields as a map.
77 *
78 * @param reqComponent Requirement component
79 * @return Map of requirement component fields
80 */
81 protected Map<String, String> getReqComponentFieldMap(ReqComponentInfo reqComponent) throws OperationFailedException {
82 //validateReqComponentFields(reqComponent);
83 List<ReqCompFieldInfo> fields = reqComponent.getReqCompFields();
84 Map<String, String> map = new HashMap<String, String>();
85 for (ReqCompFieldInfo field : fields) {
86 String type = field.getType();
87 String value = field.getValue();
88 map.put(type, value);
89 }
90 return map;
91 }
92
93 /**
94 * Gets the value of the ReqCompFieldInfo key.
95 * See {@link ReqCompFieldInfo#getKey()}
96 *
97 * @param reqComponent Requirement component
98 * @param key <code>ReqCompFieldInfo</code> key
99 * @return Value of <code>ReqCompFieldInfo</code>
100 */
101 protected String getReqComponentFieldValue(ReqComponentInfo reqComponent, String key) throws OperationFailedException {
102 return getReqComponentFieldMap(reqComponent).get(key);
103 }
104
105 /**
106 * Creates the context map (template data) for the requirement component.
107 * Also, adds the field token map to the context map.
108 *
109 * @param reqComponent Requirement component
110 * @throws DoesNotExistException If CLU, CluSet or relation does not exist
111 */
112 public Map<String, Object> createContextMap(ReqComponentInfo reqComponent) throws OperationFailedException {
113 Map<String, Object> contextMap = new HashMap<String, Object>();
114 contextMap.put(FIELDS_TOKEN, getReqComponentFieldMap(reqComponent));
115 return contextMap;
116 }
117 }