View Javadoc

1   /*
2    * Copyright 2013 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.class2.bundledoffering.service.impl;
17  
18  
19  import java.util.ArrayList;
20  import java.util.Date;
21  import java.util.LinkedHashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.kuali.rice.core.api.criteria.QueryByCriteria;
26  import org.kuali.student.common.mock.MockService;
27  import org.kuali.student.common.util.UUIDHelper;
28  import org.kuali.student.enrollment.bundledoffering.dto.BundledOfferingInfo;
29  import org.kuali.student.enrollment.bundledoffering.service.BundledOfferingService;
30  import org.kuali.student.r2.common.dto.ContextInfo;
31  import org.kuali.student.r2.common.dto.MetaInfo;
32  import org.kuali.student.r2.common.dto.StatusInfo;
33  import org.kuali.student.r2.common.dto.ValidationResultInfo;
34  import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
35  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
36  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
37  import org.kuali.student.r2.common.exceptions.MissingParameterException;
38  import org.kuali.student.r2.common.exceptions.OperationFailedException;
39  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
40  import org.kuali.student.r2.common.exceptions.ReadOnlyException;
41  import org.kuali.student.r2.common.exceptions.VersionMismatchException;
42  
43  
44  public class BundledOfferingServiceMapImpl implements MockService, BundledOfferingService {
45      // cache variable
46      // The LinkedHashMap is just so the values come back in a predictable order
47      private Map<String, BundledOfferingInfo> bundledOfferingMap = new LinkedHashMap<String, BundledOfferingInfo>();
48  
49      @Override
50      public void clear() {
51          this.bundledOfferingMap.clear();
52      }
53  
54  
55      @Override
56      public BundledOfferingInfo getBundledOffering(String bundledOfferingId, ContextInfo contextInfo)
57              throws DoesNotExistException
58              , InvalidParameterException
59              , MissingParameterException
60              , OperationFailedException
61              , PermissionDeniedException {
62          // GET_BY_ID
63          if (!this.bundledOfferingMap.containsKey(bundledOfferingId)) {
64              throw new DoesNotExistException(bundledOfferingId);
65          }
66          return new BundledOfferingInfo(this.bundledOfferingMap.get(bundledOfferingId));
67      }
68  
69      @Override
70      public List<BundledOfferingInfo> getBundledOfferingsByIds(List<String> bundledOfferingIds, ContextInfo contextInfo)
71              throws DoesNotExistException
72              , InvalidParameterException
73              , MissingParameterException
74              , OperationFailedException
75              , PermissionDeniedException {
76          // GET_BY_IDS
77          List<BundledOfferingInfo> list = new ArrayList<BundledOfferingInfo>();
78          for (String id : bundledOfferingIds) {
79              list.add(this.getBundledOffering(id, contextInfo));
80          }
81          return list;
82      }
83  
84      @Override
85      public List<String> getBundledOfferingIdsByType(String bundledOfferingTypeKey, ContextInfo contextInfo)
86              throws InvalidParameterException
87              , MissingParameterException
88              , OperationFailedException
89              , PermissionDeniedException {
90          // GET_IDS_BY_TYPE
91          List<String> list = new ArrayList<String>();
92          for (BundledOfferingInfo info : bundledOfferingMap.values()) {
93              if (bundledOfferingTypeKey.equals(info.getTypeKey())) {
94                  list.add(info.getId());
95              }
96          }
97          return list;
98      }
99  
100     @Override
101     public List<BundledOfferingInfo> getBundledOfferingsByCourseBundle(String courseBundleId, ContextInfo contextInfo)
102             throws InvalidParameterException
103             , MissingParameterException
104             , OperationFailedException
105             , PermissionDeniedException {
106         List<BundledOfferingInfo> list = new ArrayList<BundledOfferingInfo>();
107         for (BundledOfferingInfo info : bundledOfferingMap.values()) {
108             if (courseBundleId.equals(info.getCourseBundleId())) {
109                 list.add(new BundledOfferingInfo(info));
110             }
111         }
112         return list;
113     }
114 
115     @Override
116     public List<BundledOfferingInfo> getBundledOfferingsByTerm(String termId, ContextInfo contextInfo)
117             throws InvalidParameterException
118             , MissingParameterException
119             , OperationFailedException
120             , PermissionDeniedException {
121         List<BundledOfferingInfo> list = new ArrayList<BundledOfferingInfo>();
122         for (BundledOfferingInfo info : bundledOfferingMap.values()) {
123             if (termId.equals(info.getTermId())) {
124                 list.add(new BundledOfferingInfo(info));
125             }
126         }
127         return list;
128     }
129 
130     @Override
131     public List<BundledOfferingInfo> getBundledOfferingsByCourseBundleAndTerm(String courseBundleId, String termId, ContextInfo contextInfo)
132             throws InvalidParameterException
133             , MissingParameterException
134             , OperationFailedException
135             , PermissionDeniedException {
136         List<BundledOfferingInfo> list = new ArrayList<BundledOfferingInfo>();
137         for (BundledOfferingInfo info : bundledOfferingMap.values()) {
138             if (courseBundleId.equals(info.getCourseBundleId())) {
139                 if (termId.equals(info.getTermId())) {
140                     list.add(new BundledOfferingInfo(info));
141                 }
142             }
143         }
144         return list;
145     }
146 
147     @Override
148     public List<BundledOfferingInfo> getBundledOfferingsByRegistrationGroup(String registrationGroupId, ContextInfo contextInfo)
149             throws InvalidParameterException
150             , MissingParameterException
151             , OperationFailedException
152             , PermissionDeniedException {
153         List<BundledOfferingInfo> list = new ArrayList<BundledOfferingInfo>();
154         for (BundledOfferingInfo info : bundledOfferingMap.values()) {
155             if(info.getRegistrationGroupIds().contains(registrationGroupId)) {
156                 list.add(new BundledOfferingInfo(info));
157             }
158         }
159         return list;
160     }
161 
162     @Override
163     public List<BundledOfferingInfo> getBundledOfferingsByTermAndCode(String termId, String code, ContextInfo contextInfo)
164             throws InvalidParameterException
165             , MissingParameterException
166             , OperationFailedException
167             , PermissionDeniedException {
168         List<BundledOfferingInfo> list = new ArrayList<BundledOfferingInfo>();
169         for (BundledOfferingInfo info : bundledOfferingMap.values()) {
170             if (termId.equals(info.getTermId())) {
171                 if (code.equals(info.getBundledOfferingCode())) {
172                     list.add(new BundledOfferingInfo(info));
173                 }
174             }
175         }
176         return list;
177     }
178 
179     @Override
180     public List<BundledOfferingInfo> getBundledOfferingsByTermAndSubjectAreaOrg(String termId, String subjectAreaOrgId, ContextInfo contextInfo)
181             throws InvalidParameterException
182             , MissingParameterException
183             , OperationFailedException
184             , PermissionDeniedException {
185         List<BundledOfferingInfo> list = new ArrayList<BundledOfferingInfo>();
186         for (BundledOfferingInfo info : bundledOfferingMap.values()) {
187             if (termId.equals(info.getTermId())) {
188                 if (subjectAreaOrgId.equals(info.getSubjectAreaOrgId())) {
189                     list.add(new BundledOfferingInfo(info));
190                 }
191             }
192         }
193         return list;
194     }
195 
196     @Override
197     public List<String> searchForBundledOfferingIds(QueryByCriteria criteria, ContextInfo contextInfo)
198             throws InvalidParameterException
199             , MissingParameterException
200             , OperationFailedException
201             , PermissionDeniedException {
202         // UNKNOWN
203         throw new OperationFailedException("searchForBundledOfferingIds has not been implemented");
204     }
205 
206     @Override
207     public List<BundledOfferingInfo> searchForBundledOfferings(QueryByCriteria criteria, ContextInfo contextInfo)
208             throws InvalidParameterException
209             , MissingParameterException
210             , OperationFailedException
211             , PermissionDeniedException {
212         // UNKNOWN
213         throw new OperationFailedException("searchForBundledOfferings has not been implemented");
214     }
215 
216     @Override
217     public List<ValidationResultInfo> validateBundledOffering(String validationTypeKey, String courseBundleId, String termId, String bundledOfferingTypeKey, BundledOfferingInfo bundledOfferingInfo, ContextInfo contextInfo)
218             throws DoesNotExistException
219             , InvalidParameterException
220             , MissingParameterException
221             , OperationFailedException
222             , PermissionDeniedException {
223         // VALIDATE
224         return new ArrayList<ValidationResultInfo>();
225     }
226 
227     @Override
228     public BundledOfferingInfo createBundledOffering(String courseBundleId, String termId, String bundledOfferingTypeKey, BundledOfferingInfo bundledOfferingInfo, ContextInfo contextInfo)
229             throws DataValidationErrorException
230             , InvalidParameterException
231             , MissingParameterException
232             , OperationFailedException
233             , PermissionDeniedException
234             , ReadOnlyException {
235         // CREATE
236         if (!bundledOfferingTypeKey.equals(bundledOfferingInfo.getTypeKey())) {
237             throw new InvalidParameterException("The type parameter does not match the type on the info object");
238         }
239         if (!courseBundleId.equals(bundledOfferingInfo.getCourseBundleId())) {
240             throw new InvalidParameterException("The course bundle id parameter does not match the value in the info object");
241         }
242         if (!termId.equals(bundledOfferingInfo.getTermId())) {
243             throw new InvalidParameterException("The term id parameter does not match the value in the info object");
244         }
245         BundledOfferingInfo copy = new BundledOfferingInfo(bundledOfferingInfo);
246         if (copy.getId() == null) {
247             copy.setId(UUIDHelper.genStringUUID());
248         }
249         copy.setMeta(newMeta(contextInfo));
250         bundledOfferingMap.put(copy.getId(), copy);
251         return new BundledOfferingInfo(copy);
252     }
253 
254     @Override
255     public BundledOfferingInfo updateBundledOffering(String bundledOfferingId, BundledOfferingInfo bundledOfferingInfo, ContextInfo contextInfo)
256             throws DataValidationErrorException
257             , DoesNotExistException
258             , InvalidParameterException
259             , MissingParameterException
260             , OperationFailedException
261             , PermissionDeniedException
262             , ReadOnlyException
263             , VersionMismatchException {
264         // UPDATE
265         if (!bundledOfferingId.equals(bundledOfferingInfo.getId())) {
266             throw new InvalidParameterException("The id parameter does not match the id on the info object");
267         }
268         BundledOfferingInfo copy = new BundledOfferingInfo(bundledOfferingInfo);
269         BundledOfferingInfo old = this.getBundledOffering(bundledOfferingInfo.getId(), contextInfo);
270         if (!old.getMeta().getVersionInd().equals(copy.getMeta().getVersionInd())) {
271             throw new VersionMismatchException(old.getMeta().getVersionInd());
272         }
273         copy.setMeta(updateMeta(copy.getMeta(), contextInfo));
274         this.bundledOfferingMap.put(bundledOfferingInfo.getId(), copy);
275         return new BundledOfferingInfo(copy);
276     }
277 
278     @Override
279     public StatusInfo changeBundledOfferingState(String bundledOfferingId, String stateKey, ContextInfo contextInfo)
280             throws DoesNotExistException
281             , InvalidParameterException
282             , MissingParameterException
283             , OperationFailedException
284             , PermissionDeniedException {
285       BundledOfferingInfo info = getBundledOffering(bundledOfferingId, contextInfo);
286       info.setStateKey(stateKey);
287         try {
288             updateBundledOffering(bundledOfferingId, info, contextInfo);
289         } catch (DataValidationErrorException e) {
290            throw new OperationFailedException("Failed to update bundled offering with new state", e);
291         } catch (ReadOnlyException e) {
292             throw new OperationFailedException("Failed to update bundled offering with new state", e);
293         } catch (VersionMismatchException e) {
294             throw new OperationFailedException("Failed to update bundled offering with new state", e);
295         }
296         return new StatusInfo();
297     }
298 
299     @Override
300     public StatusInfo deleteBundledOffering(String bundledOfferingId, ContextInfo contextInfo)
301             throws DoesNotExistException
302             , InvalidParameterException
303             , MissingParameterException
304             , OperationFailedException
305             , PermissionDeniedException {
306         // DELETE
307         if (this.bundledOfferingMap.remove(bundledOfferingId) == null) {
308             throw new OperationFailedException(bundledOfferingId);
309         }
310         return new StatusInfo();
311     }
312 
313     private MetaInfo newMeta(ContextInfo context) {
314         MetaInfo meta = new MetaInfo();
315         meta.setCreateId(context.getPrincipalId());
316         meta.setCreateTime(new Date());
317         meta.setUpdateId(context.getPrincipalId());
318         meta.setUpdateTime(meta.getCreateTime());
319         meta.setVersionInd("0");
320         return meta;
321     }
322 
323     private MetaInfo updateMeta(MetaInfo old, ContextInfo context) {
324         MetaInfo meta = new MetaInfo(old);
325         meta.setUpdateId(context.getPrincipalId());
326         meta.setUpdateTime(new Date());
327         meta.setVersionInd((Integer.parseInt(meta.getVersionInd()) + 1) + "");
328         return meta;
329     }
330 
331 }
332