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 Charles on 4/10/13
16   */
17  package org.kuali.student.enrollment.class2.courseoffering.service.impl;
18  
19  import org.kuali.student.enrollment.courseoffering.dto.FormatOfferingInfo;
20  import org.kuali.student.enrollment.courseoffering.dto.RegistrationGroupInfo;
21  import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
22  import org.kuali.student.r2.common.dto.AttributeInfo;
23  import org.kuali.student.r2.common.dto.ContextInfo;
24  import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
25  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
26  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
27  import org.kuali.student.r2.common.exceptions.MissingParameterException;
28  import org.kuali.student.r2.common.exceptions.OperationFailedException;
29  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
30  import org.kuali.student.r2.common.exceptions.ReadOnlyException;
31  import org.kuali.student.r2.common.exceptions.VersionMismatchException;
32  
33  import java.util.ArrayList;
34  import java.util.HashSet;
35  import java.util.List;
36  import java.util.Set;
37  
38  /**
39   * This class provides utility methods for generating registration group codes
40   *
41   * @author Kuali Student Team
42   */
43  public class RegistrationGroupCodeUtil {
44      public static final String REG_CODE_PREFIX_DYN_ATTR = "regCodePrefix";
45      public static int computeRegCodePrefixForFo(List<FormatOfferingInfo> existingFos, CourseOfferingService coService, ContextInfo context)
46              throws PermissionDeniedException, MissingParameterException, InvalidParameterException,
47              OperationFailedException, DoesNotExistException, ReadOnlyException, DataValidationErrorException,
48              VersionMismatchException {
49          List<FormatOfferingInfo> needToAssignPrefix = new ArrayList<FormatOfferingInfo>();
50          // See if these FOs already have a prefix
51          Set<Integer> usedPrefixes = new HashSet<Integer>();
52          for (FormatOfferingInfo existingFo: existingFos) {
53              List<AttributeInfo> attrs = existingFo.getAttributes();
54              boolean found = false;
55              for (AttributeInfo attr: attrs) {
56                  if (REG_CODE_PREFIX_DYN_ATTR.equals(attr.getKey())) {
57                      String prefixStr = attr.getValue();
58                      int prefix = Integer.parseInt(prefixStr);
59                      usedPrefixes.add(prefix);
60                      found = true;
61                      break;
62                  }
63              }
64              if (!found) {
65                  // Try to find reg groups already generated
66                  List<RegistrationGroupInfo> rgInfos = coService.getRegistrationGroupsByFormatOffering(existingFo.getId(), context);
67                  int firstRegistrationGroupInfo = 0;
68                  if (rgInfos != null && !rgInfos.isEmpty()) {
69                      RegistrationGroupInfo sample = rgInfos.get(firstRegistrationGroupInfo);
70                      String rgCodePrefixStr = sample.getName().substring(0, 1);
71                      int rgCodePrefix = Integer.parseInt(rgCodePrefixStr);
72                      usedPrefixes.add(rgCodePrefix);
73                      addRegCodePrefixAttributeToFo(rgCodePrefixStr, existingFo);
74                      // Store this information
75                      coService.updateFormatOffering(existingFo.getId(), existingFo, context);
76                  } else {
77                      // Couldn't find a prefix, store to assing it later.
78                      needToAssignPrefix.add(existingFo);
79                  }
80              }
81          }
82          // Go through list of FOs and assign a prefix (from 1-9)
83          for (FormatOfferingInfo unassigned: needToAssignPrefix) {
84              for (int i = 1; i < 10; i++) {
85                  if (!usedPrefixes.contains(i)) {
86                      addRegCodePrefixAttributeToFo("" + i, unassigned);
87                      coService.updateFormatOffering(unassigned.getId(), unassigned, context);
88                      usedPrefixes.add(i); // Now i is "used"
89                      break;
90                  }
91              }
92          }
93          // Finally, return smallest unused number to use as a prefix
94          for (int i = 1; i < 10; i++) {
95              if (!usedPrefixes.contains(i)) {
96                  return i;
97              }
98          }
99          // All prefixes used up, so throw exception
100         throw new OperationFailedException("ERROR! Ran out of prefixes!");
101     }
102 
103     /**
104      * Store information in fo about prefix
105      * @param rgCodePrefixStr One of {"1" , "2", ..., "9"}
106      * @param fo the format offering to add this dynamic attribute to
107      */
108     public static void addRegCodePrefixAttributeToFo(String rgCodePrefixStr, FormatOfferingInfo fo) {
109         AttributeInfo newAttr = new AttributeInfo();
110         newAttr.setKey(REG_CODE_PREFIX_DYN_ATTR);
111         newAttr.setValue(rgCodePrefixStr);
112         if (fo.getAttributes() == null) {
113             fo.setAttributes(new ArrayList<AttributeInfo>());
114         }
115         fo.getAttributes().add(newAttr);
116     }
117 
118     public static int getRegCodePrefixFromFo(FormatOfferingInfo fetched) throws OperationFailedException {
119         for (AttributeInfo attr: fetched.getAttributes()) {
120             if (REG_CODE_PREFIX_DYN_ATTR.equals(attr.getKey())) {
121                 return Integer.parseInt(attr.getValue());
122             }
123         }
124         throw new OperationFailedException("Unable to find dynamic attribute for: " + REG_CODE_PREFIX_DYN_ATTR);
125     }
126 }