View Javadoc
1   /**
2    * Copyright 2013 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   * Created by mahtabme on 7/4/13
16   */
17  package org.kuali.student.enrollment.class2.exam.service.impl;
18  
19  import org.kuali.rice.core.api.criteria.PredicateFactory;
20  import org.kuali.rice.core.api.criteria.QueryByCriteria;
21  import org.kuali.student.enrollment.class2.exam.service.transformer.ExamTransformer;
22  import org.kuali.student.enrollment.exam.dto.ExamInfo;
23  import org.kuali.student.enrollment.exam.service.ExamService;
24  import org.kuali.student.r1.common.dictionary.service.DictionaryService;
25  import org.kuali.student.r2.common.dto.ContextInfo;
26  import org.kuali.student.r2.common.dto.DtoConstants;
27  import org.kuali.student.r2.common.dto.MetaInfo;
28  import org.kuali.student.r2.common.dto.StatusInfo;
29  import org.kuali.student.r2.common.dto.ValidationResultInfo;
30  import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
31  import org.kuali.student.r2.common.exceptions.DependentObjectsExistException;
32  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
33  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
34  import org.kuali.student.r2.common.exceptions.MissingParameterException;
35  import org.kuali.student.r2.common.exceptions.OperationFailedException;
36  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
37  import org.kuali.student.r2.common.exceptions.ReadOnlyException;
38  import org.kuali.student.r2.common.exceptions.VersionMismatchException;
39  import org.kuali.student.r2.common.util.constants.ExamServiceConstants;
40  import org.kuali.student.r2.common.validator.ValidatorFactory;
41  import org.kuali.student.r2.lum.clu.dto.CluInfo;
42  import org.kuali.student.r2.lum.clu.service.CluService;
43  import org.springframework.transaction.annotation.Transactional;
44  
45  import java.util.ArrayList;
46  import java.util.Date;
47  import java.util.List;
48  
49  /**
50   * This class represents an implementation of the Exam service.
51   *
52   * @author Kuali Student Team
53   */
54  public class ExamServiceImpl implements ExamService {
55  
56      private static final String PREDICATE_FACTORY_PATH_FOR_CLUTYPE = "luType.id";
57  
58      private CluService cluService;
59  
60      private ExamTransformer examTransformer;
61  
62      @Override
63      public List<ValidationResultInfo> validateExam(String validationTypeKey, String examTypeKey, ExamInfo examInfo, ContextInfo contextInfo)
64              throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
65              PermissionDeniedException {
66  
67          return new ArrayList<ValidationResultInfo>();
68      }
69  
70      @Override
71      public ExamInfo createExam(String examTypeKey, ExamInfo examInfo, ContextInfo contextInfo)
72              throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException,
73              OperationFailedException, PermissionDeniedException, ReadOnlyException {
74  
75          CluInfo cluInfo = new CluInfo();
76          getExamTransformer().exam2Clu(examInfo, cluInfo, contextInfo);
77          CluInfo createdClu = getCluService().createClu(examTypeKey, cluInfo, contextInfo);
78          ExamInfo createdExam = new ExamInfo();
79          getExamTransformer().clu2Exam(createdClu, createdExam, contextInfo);
80          return createdExam;
81      }
82  
83      @Override
84      @Transactional(readOnly = true)
85      public ExamInfo getExam(String examId, ContextInfo contextInfo)
86              throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
87              PermissionDeniedException {
88  
89          CluInfo cluInfo = getCluService().getClu(examId, contextInfo);
90          ExamInfo examInfo = new ExamInfo();
91          getExamTransformer().clu2Exam(cluInfo, examInfo, contextInfo);
92          return examInfo;
93      }
94  
95      @Override
96      @Transactional(readOnly = false, noRollbackFor = {DoesNotExistException.class}, rollbackFor = {Throwable.class})
97      public ExamInfo updateExam(String examId, ExamInfo examInfo, ContextInfo contextInfo)
98              throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException,
99              OperationFailedException, PermissionDeniedException, ReadOnlyException, VersionMismatchException {
100 
101         CluInfo cluToUpdate = getCluService().getClu(examId, contextInfo);
102         getExamTransformer().exam2Clu(examInfo, cluToUpdate, contextInfo);
103         CluInfo updatedClu = getCluService().updateClu(examId, cluToUpdate, contextInfo);
104         ExamInfo updatedExam = new ExamInfo();
105         getExamTransformer().clu2Exam(updatedClu, updatedExam, contextInfo);
106         return updatedExam;
107     }
108 
109     @Override
110     @Transactional(readOnly = false, noRollbackFor = {DoesNotExistException.class}, rollbackFor = {Throwable.class})
111     public StatusInfo deleteExam(String examId, ContextInfo contextInfo)
112             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
113             PermissionDeniedException {
114 
115         try {
116             getCluService().deleteClu(examId, contextInfo);
117         } catch (DependentObjectsExistException e) {
118             StatusInfo failedStatus = new StatusInfo();
119             failedStatus.setSuccess(Boolean.FALSE);
120             failedStatus.setMessage("Exam could not be deleted because dependent child objects exist.");
121             return failedStatus;
122         }
123         return new StatusInfo();
124     }
125 
126     @Override
127     @Transactional(readOnly = true)
128     public List<ExamInfo> getExamsByIds(List<String> examIds, ContextInfo contextInfo)
129             throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException,
130             PermissionDeniedException {
131 
132         List<ExamInfo> exams = new ArrayList<ExamInfo>(examIds.size());
133         for (String examID : examIds) {
134             exams.add(getExam(examID, contextInfo));
135         }
136         return exams;
137     }
138 
139     @Override
140     @Transactional(readOnly = true)
141     public List<String> getExamIdsByType(String examTypeKey, ContextInfo contextInfo)
142             throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
143 
144         List<String> ids = null;
145         try {
146             //Note: CluService does not use stateservice yet, therefore we use DtoContstants.STATE_ACTIVE instead
147             // of ExamServiceConstants.EXAM_ACTIVE_STATE_KEY
148             ids = getCluService().getCluIdsByLuType(examTypeKey, DtoConstants.STATE_ACTIVE, contextInfo);
149         } catch (DoesNotExistException e) {
150             throw new OperationFailedException(e.getMessage());
151         }
152         if (ids == null) {
153             ids = new ArrayList<String>(0);
154         }
155         return ids;
156     }
157 
158     @Override
159     @Transactional(readOnly = true)
160     public List<String> searchForExamIds(QueryByCriteria criteria, ContextInfo contextInfo)
161             throws InvalidParameterException, MissingParameterException, OperationFailedException,
162             PermissionDeniedException {
163 
164         //Add cluType Predicate
165         QueryByCriteria newCriteria = addCluTypeEqualPredicate(criteria, ExamServiceConstants.EXAM_FINAL_TYPE_KEY);
166 
167         return this.getCluService().searchForCluIds(newCriteria, contextInfo);
168     }
169 
170     @Override
171     @Transactional(readOnly = true)
172     public List<ExamInfo> searchForExams(QueryByCriteria criteria, ContextInfo contextInfo)
173             throws InvalidParameterException, MissingParameterException, OperationFailedException,
174             PermissionDeniedException {
175 
176         //Add cluType Predicate
177         QueryByCriteria newCriteria = addCluTypeEqualPredicate(criteria, ExamServiceConstants.EXAM_FINAL_TYPE_KEY);
178 
179         List<ExamInfo> examInfos = new ArrayList<ExamInfo>();
180         List<CluInfo> cluInfos = this.getCluService().searchForClus(newCriteria, contextInfo);
181         for (CluInfo cluInfo : cluInfos) {
182             ExamInfo exam = new ExamInfo();
183             this.getExamTransformer().clu2Exam(cluInfo, exam, contextInfo);
184             examInfos.add(exam);
185         }
186 
187         return examInfos;
188     }
189 
190     private QueryByCriteria addCluTypeEqualPredicate(QueryByCriteria criteria, String cluType) {
191         QueryByCriteria.Builder qbcBuilder = QueryByCriteria.Builder.create();
192         qbcBuilder.setPredicates(PredicateFactory.and(
193                 criteria.getPredicate(),
194                 PredicateFactory.equal(PREDICATE_FACTORY_PATH_FOR_CLUTYPE, cluType)));
195         return qbcBuilder.build();
196     }
197 
198     private MetaInfo newMeta(ContextInfo context) {
199         MetaInfo meta = new MetaInfo();
200         meta.setCreateId(context.getPrincipalId());
201         meta.setCreateTime(new Date());
202         meta.setUpdateId(context.getPrincipalId());
203         meta.setUpdateTime(meta.getCreateTime());
204         meta.setVersionInd("0");
205         return meta;
206     }
207 
208     private MetaInfo updateMeta(MetaInfo old, ContextInfo context) {
209         MetaInfo meta = new MetaInfo(old);
210         meta.setUpdateId(context.getPrincipalId());
211         meta.setUpdateTime(new Date());
212         meta.setVersionInd((Integer.parseInt(meta.getVersionInd()) + 1) + "");
213         return meta;
214     }
215 
216     public CluService getCluService() {
217         return cluService;
218     }
219 
220     public void setCluService(CluService cluService) {
221         this.cluService = cluService;
222     }
223 
224     public void setExamTransformer(ExamTransformer examTransformer) {
225         this.examTransformer = examTransformer;
226     }
227 
228     public ExamTransformer getExamTransformer() {
229         return examTransformer;
230     }
231 
232 }