View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may	obtain a copy of the License at
7    *
8    * 	http://www.osedu.org/licenses/ECL-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.student.enrollment.class1.lpr.service.impl;
17  
18  import org.kuali.rice.core.api.criteria.QueryByCriteria;
19  import org.kuali.student.common.mock.MockService;
20  import org.kuali.student.common.util.UUIDHelper;
21  import org.kuali.student.enrollment.lpr.dto.LprInfo;
22  import org.kuali.student.enrollment.lpr.dto.LprTransactionInfo;
23  import org.kuali.student.enrollment.lpr.dto.LprTransactionItemInfo;
24  import org.kuali.student.enrollment.lpr.service.LprService;
25  import org.kuali.student.enrollment.lui.dto.LuiInfo;
26  import org.kuali.student.enrollment.lui.service.LuiService;
27  import org.kuali.student.r2.common.dto.BulkStatusInfo;
28  import org.kuali.student.r2.common.dto.ContextInfo;
29  import org.kuali.student.r2.common.dto.MetaInfo;
30  import org.kuali.student.r2.common.dto.StatusInfo;
31  import org.kuali.student.r2.common.dto.ValidationResultInfo;
32  import org.kuali.student.r2.common.exceptions.*;
33  import org.kuali.student.r2.common.util.constants.LprServiceConstants;
34  import org.kuali.student.r2.core.class1.util.ValidationUtils;
35  
36  import java.util.ArrayList;
37  import java.util.Date;
38  import java.util.LinkedHashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  public class LprServiceMockImpl implements LprService, MockService {
43  
44      private LuiService luiService;
45  
46      public LuiService getLuiService() {
47          return luiService;
48      }
49  
50      public void setLuiService(LuiService luiService) {
51          this.luiService = luiService;
52      }
53  
54      
55      @Override
56  	public void clear() {
57  		this.lprMap.clear();
58  		this.lprTransactionMap.clear();
59  	
60  	}
61  
62  	@Override
63      public LprInfo getLpr(String lprId, ContextInfo contextInfo)
64              throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
65              PermissionDeniedException {
66          if (!this.lprMap.containsKey(lprId)) {
67              throw new DoesNotExistException(lprId);
68          }
69          return this.lprMap.get(lprId);
70      }
71  
72      @Override
73      public List<LprInfo> getLprsByIds(List<String> lprIds, ContextInfo contextInfo)
74              throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
75              PermissionDeniedException {
76          List<LprInfo> list = new ArrayList<LprInfo>();
77          for (String id : lprIds) {
78              list.add(this.getLpr(id, contextInfo));
79          }
80          return list;
81      }
82  
83      @Override
84      public List<String> getLuiIdsByPersonAndTypeAndState(String personId, String lprTypeKey, String relationState, ContextInfo contextInfo)
85              throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
86              PermissionDeniedException {
87          List<String> list = new ArrayList<String>();
88          for (LprInfo info : lprMap.values()) {
89              if (personId.equals(info.getPersonId())) {
90                  if (lprTypeKey.equals(info.getTypeKey())) {
91                      if (relationState.equals(info.getStateKey())) {
92                          list.add(info.getLuiId());
93                      }
94                  }
95              }
96          }
97          return list;
98      }
99  
100     @Override
101     public List<String> getPersonIdsByLuiAndTypeAndState(String luiId, String lprTypeKey, String relationState, ContextInfo contextInfo)
102             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
103             PermissionDeniedException {
104         List<String> list = new ArrayList<String>();
105         for (LprInfo info : lprMap.values()) {
106             if (luiId.equals(info.getLuiId())) {
107                 if (lprTypeKey.equals(info.getTypeKey())) {
108                     if (relationState.equals(info.getStateKey())) {
109                         list.add(info.getId());
110                     }
111                 }
112             }
113         }
114         return list;
115     }
116 
117     @Override
118     public List<LprInfo> getLprsByPersonAndLui(String personId, String luiId, ContextInfo contextInfo)
119             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
120             PermissionDeniedException {
121         List<LprInfo> list = new ArrayList<LprInfo>();
122         for (LprInfo info : lprMap.values()) {
123             if (personId.equals(info.getPersonId())) {
124                 if (luiId.equals(info.getLuiId())) {
125                     list.add(info);
126                 }
127             }
128         }
129         return list;
130     }
131 
132     @Override
133     public List<LprInfo> getLprsByPerson(String personId, ContextInfo contextInfo)
134             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
135             PermissionDeniedException {
136         List<LprInfo> list = new ArrayList<LprInfo>();
137         for (LprInfo info : lprMap.values()) {
138             if (personId.equals(info.getPersonId())) {
139                 list.add(info);
140             }
141         }
142         return list;
143     }
144 
145     @Override
146     public List<LprInfo> getLprsByLui(String luiId, ContextInfo contextInfo)
147             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
148             PermissionDeniedException {
149         List<LprInfo> list = new ArrayList<LprInfo>();
150         for (LprInfo info : lprMap.values()) {
151             if (luiId.equals(info.getLuiId())) {
152                 list.add(info);
153             }
154         }
155         return list;
156     }
157 
158     @Override
159     public List<LprInfo> getLprsByLuiAndType(String luiId, String lprTypeKey, ContextInfo contextInfo)
160             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
161             PermissionDeniedException {
162         List<LprInfo> list = new ArrayList<LprInfo>();
163         for (LprInfo info : lprMap.values()) {
164             if (luiId.equals(info.getTypeKey())) {
165                 if (lprTypeKey.equals(info.getTypeKey())) {
166                     list.add(info);
167                 }
168             }
169         }
170         return list;
171     }
172 
173     @Override
174     public List<LprInfo> getLprsByPersonForAtp(String personId, String atpId, ContextInfo contextInfo)
175             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
176             PermissionDeniedException {
177         List<LprInfo> list = new ArrayList<LprInfo>();
178         for (LprInfo info : lprMap.values()) {
179             if (personId.equals(info.getPersonId())) {
180                 LuiInfo lui = luiService.getLui(info.getLuiId(), contextInfo);
181                 if (atpId.equals(lui.getAtpId())) {
182                     list.add(info);
183                 }
184             }
185         }
186         return list;
187     }
188 
189     @Override
190     public List<LprInfo> getLprsByPersonAndTypeForAtp(String personId, String atpId, String lprTypeKey, ContextInfo contextInfo)
191             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
192             PermissionDeniedException {
193         List<LprInfo> list = new ArrayList<LprInfo>();
194         for (LprInfo info : lprMap.values()) {
195             if (personId.equals(info.getPersonId())) {
196                 if (lprTypeKey.equals(info.getTypeKey())) {
197                     LuiInfo lui = luiService.getLui(info.getLuiId(), contextInfo);
198                     if (atpId.equals(lui.getAtpId())) {
199                         list.add(info);
200                     }
201                 }
202             }
203         }
204         return list;
205     }
206 
207     @Override
208     public List<LprInfo> getLprsByPersonAndLuiType(String personId, String luiTypeKey, ContextInfo contextInfo)
209             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
210             PermissionDeniedException {
211         List<LprInfo> list = new ArrayList<LprInfo>();
212         for (LprInfo info : lprMap.values()) {
213             if (personId.equals(info.getTypeKey())) {
214                 if (luiTypeKey.equals(info.getTypeKey())) {
215                     list.add(info);
216                 }
217             }
218         }
219         return list;
220     }
221 
222     @Override
223     public List<LprInfo> getLprsByPersonForAtpAndLuiType(String personId, String atpId, String luiTypeKey, ContextInfo contextInfo)
224             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
225             PermissionDeniedException {
226         List<LprInfo> list = new ArrayList<LprInfo>();
227         for (LprInfo info : lprMap.values()) {
228             if (personId.equals(info.getTypeKey())) {
229                 if (atpId.equals(info.getTypeKey())) {
230                     if (luiTypeKey.equals(info.getTypeKey())) {
231                         list.add(info);
232                     }
233                 }
234             }
235         }
236         return list;
237     }
238 
239     @Override
240     public List<ValidationResultInfo> validateLpr(String validationType, String luiId, String personId, String lprTypeKey, LprInfo lprInfo, ContextInfo contextInfo)
241             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
242             PermissionDeniedException {
243         // validate
244         return new ArrayList<ValidationResultInfo>();
245     }
246 
247     @Override
248     public List<String> searchForLprIds(QueryByCriteria criteria, ContextInfo contextInfo)
249             throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
250         throw new OperationFailedException("searchForLprIds has not been implemented");
251     }
252 
253     @Override
254     public List<LprInfo> searchForLprs(QueryByCriteria criteria, ContextInfo contextInfo)
255             throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
256         throw new OperationFailedException("searchForLprs has not been implemented");
257     }
258     // cache variable 
259     // The LinkedHashMap is just so the values come back in a predictable order
260     private Map<String, LprInfo> lprMap = new LinkedHashMap<String, LprInfo>();
261 
262     @Override
263     public LprInfo createLpr(String personId, String luiId, String lprTypeKey, LprInfo lprInfo, ContextInfo contextInfo)
264             throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException,
265             OperationFailedException, PermissionDeniedException, ReadOnlyException {
266         // create 
267         if (!lprTypeKey.equals(lprInfo.getTypeKey())) {
268             throw new InvalidParameterException("The type parameter does not match the type on the info object");
269         }
270         // TODO: check the rest of the readonly fields that are specified on the create to make sure they match the info object
271         LprInfo copy = new LprInfo(lprInfo);
272         if (copy.getId() == null) {
273             copy.setId(UUIDHelper.genStringUUID());
274         }
275         copy.setMeta(newMeta(contextInfo));
276         lprMap.put(copy.getId(), copy);
277         return new LprInfo(copy);
278     }
279 
280     @Override
281     public List<BulkStatusInfo> createLprsForPerson(String personId, String lprTypeKey, List<LprInfo> lprInfos, ContextInfo contextInfo)
282             throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException,
283             OperationFailedException, PermissionDeniedException, ReadOnlyException {
284         List<BulkStatusInfo> list = new ArrayList<BulkStatusInfo>(lprInfos.size());
285         for (LprInfo info : lprInfos) {
286             BulkStatusInfo bsi = new BulkStatusInfo();
287             try {
288                 LprInfo created = this.createLpr(personId, info.getLuiId(), lprTypeKey, info, contextInfo);
289                 bsi.setSuccess(Boolean.TRUE);
290                 bsi.setId(created.getId());
291             } catch (DataValidationErrorException de) {
292                 bsi.setSuccess(Boolean.FALSE);
293                 bsi.setMessage(asString(de.getValidationResults()));
294             }
295             list.add(bsi);
296         }
297         return list;
298     }
299 
300     private String asString(List<ValidationResultInfo> vris) {
301         StringBuilder sb = new StringBuilder();
302         String newLine = ";";
303         for (ValidationResultInfo vri : vris) {
304             sb.append(newLine);
305             newLine = "/n";
306             sb.append(vri.getMessage());
307         }
308         return sb.toString();
309     }
310 
311     @Override
312     public List<BulkStatusInfo> createLprsForLui(String luiId, String lprTypeKey, List<LprInfo> lprInfos, ContextInfo contextInfo)
313             throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException,
314             OperationFailedException, PermissionDeniedException, ReadOnlyException {
315         List<BulkStatusInfo> list = new ArrayList<BulkStatusInfo>(lprInfos.size());
316         for (LprInfo info : lprInfos) {
317             BulkStatusInfo bsi = new BulkStatusInfo();
318             try {
319                 LprInfo created = this.createLpr(info.getPersonId(), luiId, lprTypeKey, info, contextInfo);
320                 bsi.setSuccess(Boolean.TRUE);
321                 bsi.setId(created.getId());
322             } catch (DataValidationErrorException de) {
323                 bsi.setSuccess(Boolean.FALSE);
324                 bsi.setMessage(ValidationUtils.asString(de.getValidationResults()));
325             }
326             list.add(bsi);
327         }
328         return list;
329     }
330 
331     @Override
332     public LprInfo updateLpr(String lprId, LprInfo lprInfo, ContextInfo contextInfo)
333             throws DoesNotExistException, DataValidationErrorException, InvalidParameterException, MissingParameterException,
334             OperationFailedException, PermissionDeniedException, ReadOnlyException, VersionMismatchException {
335         // update
336         if (!lprId.equals(lprInfo.getId())) {
337             throw new InvalidParameterException("The id parameter does not match the id on the info object");
338         }
339         LprInfo copy = new LprInfo(lprInfo);
340         LprInfo old = this.getLpr(lprInfo.getId(), contextInfo);
341         if (!old.getMeta().getVersionInd().equals(copy.getMeta().getVersionInd())) {
342             throw new VersionMismatchException(old.getMeta().getVersionInd());
343         }
344         copy.setMeta(updateMeta(copy.getMeta(), contextInfo));
345         this.lprMap.put(lprInfo.getId(), copy);
346         return new LprInfo(copy);
347     }
348 
349     @Override
350     public StatusInfo deleteLpr(String lprId, ContextInfo contextInfo)
351             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
352             PermissionDeniedException {
353         if (this.lprMap.remove(lprId) == null) {
354             throw new DoesNotExistException(lprId);
355         }
356         return newStatus();
357     }
358     // cache variable 
359     // The LinkedHashMap is just so the values come back in a predictable order
360     private Map<String, LprTransactionInfo> lprTransactionMap = new LinkedHashMap<String, LprTransactionInfo>();
361 
362     @Override
363     public LprTransactionInfo createLprTransaction(String lprTransactionType, LprTransactionInfo lprTransactionInfo, ContextInfo contextInfo)
364             throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException,
365             OperationFailedException, PermissionDeniedException {
366         // create 
367         if (!lprTransactionType.equals(lprTransactionInfo.getTypeKey())) {
368             throw new InvalidParameterException("The type parameter does not match the type on the info object");
369         }
370         LprTransactionInfo copy = new LprTransactionInfo(lprTransactionInfo);
371         if (copy.getId() == null) {
372             copy.setId(UUIDHelper.genStringUUID());
373         }
374         copy.setMeta(newMeta(contextInfo));
375         
376         lprTransactionMap.put(copy.getId(), copy);
377         return new LprTransactionInfo(copy);
378     }
379 
380     @Override
381     public LprTransactionInfo createLprTransactionFromExisting(String lprTransactionId, ContextInfo contextInfo)
382             throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException,
383             OperationFailedException, PermissionDeniedException {
384         throw new OperationFailedException("createLprTransactionFromExisting has not been implemented");
385     }
386 
387     @Override
388     public LprTransactionInfo updateLprTransaction(String lprTransactionId, LprTransactionInfo lprTransactionInfo, ContextInfo contextInfo)
389             throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException,
390             OperationFailedException, PermissionDeniedException, VersionMismatchException {
391         // update
392         if (!lprTransactionId.equals(lprTransactionInfo.getId())) {
393             throw new InvalidParameterException("The id parameter does not match the id on the info object");
394         }
395         LprTransactionInfo copy = new LprTransactionInfo(lprTransactionInfo);
396         LprTransactionInfo old = this.getLprTransaction(lprTransactionInfo.getId(), contextInfo);
397         if (!old.getMeta().getVersionInd().equals(copy.getMeta().getVersionInd())) {
398             throw new VersionMismatchException(old.getMeta().getVersionInd());
399         }
400         copy.setMeta(updateMeta(copy.getMeta(), contextInfo));
401         this.lprTransactionMap.put(lprTransactionInfo.getId(), copy);
402         return new LprTransactionInfo(copy);
403     }
404 
405     @Override
406     public LprTransactionInfo getLprTransaction(String lprTransactionId, ContextInfo contextInfo)
407             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
408             PermissionDeniedException {
409         if (!this.lprTransactionMap.containsKey(lprTransactionId)) {
410             throw new DoesNotExistException(lprTransactionId);
411         }
412         return this.lprTransactionMap.get(lprTransactionId);
413     }
414 
415     @Override
416     public List<LprTransactionItemInfo> getLprTransactionItemsByPersonAndLui(String personId, String luiId, ContextInfo contextInfo)
417             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
418             PermissionDeniedException {
419         List<LprTransactionItemInfo> list = new ArrayList<LprTransactionItemInfo>();
420         for (LprTransactionInfo trans : lprTransactionMap.values()) {
421             for (LprTransactionItemInfo info : trans.getLprTransactionItems()) {
422                 if (personId.equals(info.getPersonId())) {
423                     if (luiId.equals(info.getNewLuiId())) {
424                         list.add(info);
425                     } else if (luiId.equals(info.getExistingLuiId())) {
426                         list.add(info);
427                     }
428                 }
429             }
430         }
431         return list;
432     }
433 
434     @Override
435     public List<LprTransactionInfo> getLprTransactionsByIds(List<String> lprTransactionIds, ContextInfo contextInfo)
436             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
437             PermissionDeniedException {
438         List<LprTransactionInfo> list = new ArrayList<LprTransactionInfo>();
439         for (String id : lprTransactionIds) {
440             list.add(this.getLprTransaction(id, contextInfo));
441         }
442         return list;
443     }
444 
445     @Override
446     public List<LprTransactionItemInfo> getLprTransactionItemsByResultingLpr(String lprId, ContextInfo contextInfo)
447             throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
448         List<LprTransactionItemInfo> list = new ArrayList<LprTransactionItemInfo>();
449         for (LprTransactionInfo trans : this.lprTransactionMap.values()) {
450             for (LprTransactionItemInfo info : trans.getLprTransactionItems()) {
451                 if (info.getLprTransactionItemResult() != null) {
452                     if (lprId.equals(info.getLprTransactionItemResult().getResultingLprId())) {
453                         list.add(info);
454                     }
455                 }
456             }
457         }
458         return list;
459     }
460 
461     @Override
462     public List<LprTransactionItemInfo> getLprTransactionItemsByLui(String luiId, ContextInfo contextInfo)
463             throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
464         List<LprTransactionItemInfo> list = new ArrayList<LprTransactionItemInfo>();
465 
466         for (LprTransactionInfo trans : lprTransactionMap.values()) {
467             for (LprTransactionItemInfo info : trans.getLprTransactionItems()) {
468                 if (luiId.equals(info.getNewLuiId())) {
469                     list.add(info);
470                 } else if (luiId.equals(info.getExistingLuiId())) {
471                     list.add(info);
472                 }
473             }
474         }
475         return list;
476     }
477 
478     @Override
479     public List<LprTransactionInfo> getUnsubmittedLprTransactionsByRequestingPersonAndAtp(String requestingPersonId, String atpId, ContextInfo contextInfo)
480             throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
481         List<LprTransactionInfo> list = new ArrayList<LprTransactionInfo>();
482         for (LprTransactionInfo info : lprTransactionMap.values()) {
483             if (requestingPersonId.equals(info.getRequestingPersonId())) {
484                 if (atpId.equals(info.getAtpId())) {
485                     if (info.getStateKey().equals(LprServiceConstants.LPRTRANS_NEW_STATE_KEY)) {
486                         list.add(info);
487                     }
488                 }
489             }
490         }
491         return list;
492     }
493 
494     @Override
495     public StatusInfo deleteLprTransaction(String lprTransactionId, ContextInfo contextInfo)
496             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
497             PermissionDeniedException {
498         if (this.lprTransactionMap.remove(lprTransactionId) == null) {
499             throw new DoesNotExistException(lprTransactionId);
500         }
501         return newStatus();
502     }
503 
504     @Override
505     public LprTransactionInfo processLprTransaction(String lprTransactionId, ContextInfo contextInfo)
506             throws AlreadyExistsException, DoesNotExistException, InvalidParameterException, MissingParameterException,
507             OperationFailedException, PermissionDeniedException {
508         throw new OperationFailedException("processLprTransaction has not been implemented");
509     }
510 
511     @Override
512     public List<ValidationResultInfo> verifyLprTransaction(String lprTransactionId, ContextInfo contextInfo)
513             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
514             PermissionDeniedException {
515         throw new OperationFailedException("verifyLprTransaction has not been implemented");
516     }
517 
518     @Override
519     public List<LprTransactionInfo> searchForLprTransactions(QueryByCriteria criteria, ContextInfo contextInfo)
520             throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
521         throw new OperationFailedException("searchForLprTransactions has not been implemented");
522     }
523 
524     @Override
525     public List<String> searchForLprTransactionIds(QueryByCriteria criteria, ContextInfo contextInfo)
526             throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
527         throw new OperationFailedException("searchForLprTransactionIds has not been implemented");
528     }
529 
530     private MetaInfo newMeta(ContextInfo context) {
531         MetaInfo meta = new MetaInfo();
532         meta.setCreateId(context.getPrincipalId());
533         meta.setCreateTime(new Date());
534         meta.setUpdateId(context.getPrincipalId());
535         meta.setUpdateTime(meta.getCreateTime());
536         meta.setVersionInd("0");
537         return meta;
538     }
539 
540     private StatusInfo newStatus() {
541         StatusInfo status = new StatusInfo();
542         status.setSuccess(Boolean.TRUE);
543         return status;
544     }
545 
546     private MetaInfo updateMeta(MetaInfo old, ContextInfo context) {
547         MetaInfo meta = new MetaInfo(old);
548         meta.setUpdateId(context.getPrincipalId());
549         meta.setUpdateTime(new Date());
550         meta.setVersionInd((Integer.parseInt(meta.getVersionInd()) + 1) + "");
551         return meta;
552     }
553 }