1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.student.enrollment.class2.exam.service.impl;
18
19 import org.kuali.rice.core.api.criteria.QueryByCriteria;
20 import org.kuali.student.common.UUIDHelper;
21 import org.kuali.student.common.mock.MockService;
22 import org.kuali.student.enrollment.exam.dto.ExamInfo;
23 import org.kuali.student.enrollment.exam.service.ExamService;
24 import org.kuali.student.r2.common.dto.ContextInfo;
25 import org.kuali.student.r2.common.dto.MetaInfo;
26 import org.kuali.student.r2.common.dto.StatusInfo;
27 import org.kuali.student.r2.common.dto.ValidationResultInfo;
28 import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
29 import org.kuali.student.r2.common.exceptions.DoesNotExistException;
30 import org.kuali.student.r2.common.exceptions.InvalidParameterException;
31 import org.kuali.student.r2.common.exceptions.MissingParameterException;
32 import org.kuali.student.r2.common.exceptions.OperationFailedException;
33 import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
34 import org.kuali.student.r2.common.exceptions.ReadOnlyException;
35 import org.kuali.student.r2.common.exceptions.VersionMismatchException;
36
37 import java.util.ArrayList;
38 import java.util.Date;
39 import java.util.LinkedHashMap;
40 import java.util.List;
41 import java.util.Map;
42
43
44
45
46
47
48 public class ExamServiceMapImpl implements MockService, ExamService
49 {
50
51
52
53
54
55
56 private Map<String, ExamInfo> examMap = new LinkedHashMap<String, ExamInfo>();
57
58
59
60
61
62 @Override
63 public void clear()
64 {
65 this.examMap.clear ();
66 }
67
68 @Override
69 public List<ValidationResultInfo> validateExam(String validationTypeKey, String examTypeKey, ExamInfo examInfo, ContextInfo contextInfo)
70 throws DoesNotExistException
71 ,InvalidParameterException
72 ,MissingParameterException
73 ,OperationFailedException
74 ,PermissionDeniedException
75 {
76
77 return new ArrayList<ValidationResultInfo>();
78 }
79
80 @Override
81 public ExamInfo createExam(String examTypeKey, ExamInfo examInfo, ContextInfo contextInfo)
82 throws DataValidationErrorException
83 ,DoesNotExistException
84 ,InvalidParameterException
85 ,MissingParameterException
86 ,OperationFailedException
87 ,PermissionDeniedException
88 ,ReadOnlyException
89 {
90
91 if (!examTypeKey.equals (examInfo.getTypeKey())) {
92 throw new InvalidParameterException ("The type parameter does not match the type on the info object");
93 }
94 ExamInfo copy = new ExamInfo(examInfo);
95 if (copy.getId() == null) {
96 copy.setId(UUIDHelper.genStringUUID());
97 }
98 copy.setMeta(newMeta(contextInfo));
99 examMap.put(copy.getId(), copy);
100 return new ExamInfo(copy);
101 }
102
103 @Override
104 public ExamInfo getExam(String examId, ContextInfo contextInfo)
105 throws DoesNotExistException
106 ,InvalidParameterException
107 ,MissingParameterException
108 ,OperationFailedException
109 ,PermissionDeniedException
110 {
111 if (!this.examMap.containsKey(examId)) {
112 throw new DoesNotExistException(examId);
113 }
114 return new ExamInfo(this.examMap.get (examId));
115 }
116
117 @Override
118 public ExamInfo updateExam(String examId, ExamInfo examInfo, ContextInfo contextInfo)
119 throws DataValidationErrorException
120 ,DoesNotExistException
121 ,InvalidParameterException
122 ,MissingParameterException
123 ,OperationFailedException
124 ,PermissionDeniedException
125 ,ReadOnlyException
126 ,VersionMismatchException
127 {
128
129 if (!examId.equals (examInfo.getId())) {
130 throw new InvalidParameterException ("The id parameter does not match the id on the info object");
131 }
132 ExamInfo copy = new ExamInfo(examInfo);
133 ExamInfo old = this.getExam(examInfo.getId(), contextInfo);
134 if (!old.getMeta().getVersionInd().equals(copy.getMeta().getVersionInd())) {
135 throw new VersionMismatchException(old.getMeta().getVersionInd());
136 }
137 copy.setMeta(updateMeta(copy.getMeta(), contextInfo));
138 this.examMap .put(examInfo.getId(), copy);
139 return new ExamInfo(copy);
140 }
141
142 @Override
143 public StatusInfo deleteExam(String examId, ContextInfo contextInfo)
144 throws DoesNotExistException
145 ,InvalidParameterException
146 ,MissingParameterException
147 ,OperationFailedException
148 ,PermissionDeniedException
149 {
150 if (this.examMap.remove(examId) == null) {
151 throw new DoesNotExistException(examId);
152 }
153 return newStatus();
154 }
155
156 @Override
157 public List<ExamInfo> getExamsByIds(List<String> examIds, ContextInfo contextInfo)
158 throws DoesNotExistException
159 ,InvalidParameterException
160 ,MissingParameterException
161 ,OperationFailedException
162 ,PermissionDeniedException
163 {
164 List<ExamInfo> list = new ArrayList<ExamInfo> ();
165 for (String id: examIds) {
166 list.add (this.getExam(id, contextInfo));
167 }
168 return list;
169 }
170
171 @Override
172 public List<String> getExamIdsByType(String examTypeKey, ContextInfo contextInfo)
173 throws InvalidParameterException
174 ,MissingParameterException
175 ,OperationFailedException
176 ,PermissionDeniedException
177 {
178 List<String> list = new ArrayList<String> ();
179 for (ExamInfo info: examMap.values ()) {
180 if (examTypeKey.equals(info.getTypeKey())) {
181 list.add (info.getId ());
182 }
183 }
184 return list;
185 }
186
187 @Override
188 public List<String> searchForExamIds(QueryByCriteria criteria, ContextInfo contextInfo)
189 throws InvalidParameterException
190 ,MissingParameterException
191 ,OperationFailedException
192 ,PermissionDeniedException
193 {
194 throw new OperationFailedException ("searchForExamIds has not been implemented");
195 }
196
197 @Override
198 public List<ExamInfo> searchForExams(QueryByCriteria criteria, ContextInfo contextInfo)
199 throws InvalidParameterException
200 ,MissingParameterException
201 ,OperationFailedException
202 ,PermissionDeniedException
203 {
204 throw new OperationFailedException ("searchForExams has not been implemented");
205 }
206
207 private StatusInfo newStatus() {
208 StatusInfo status = new StatusInfo();
209 status.setSuccess(Boolean.TRUE);
210 return status;
211 }
212
213 private MetaInfo newMeta(ContextInfo context) {
214 MetaInfo meta = new MetaInfo();
215 meta.setCreateId(context.getPrincipalId());
216 meta.setCreateTime(new Date());
217 meta.setUpdateId(context.getPrincipalId());
218 meta.setUpdateTime(meta.getCreateTime());
219 meta.setVersionInd("0");
220 return meta;
221 }
222
223 private MetaInfo updateMeta(MetaInfo old, ContextInfo context) {
224 MetaInfo meta = new MetaInfo(old);
225 meta.setUpdateId(context.getPrincipalId());
226 meta.setUpdateTime(new Date());
227 meta.setVersionInd((Integer.parseInt(meta.getVersionInd()) + 1) + "");
228 return meta;
229 }
230
231 }