1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
40
41
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
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
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
75 coService.updateFormatOffering(existingFo.getId(), existingFo, context);
76 } else {
77
78 needToAssignPrefix.add(existingFo);
79 }
80 }
81 }
82
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);
89 break;
90 }
91 }
92 }
93
94 for (int i = 1; i < 10; i++) {
95 if (!usedPrefixes.contains(i)) {
96 return i;
97 }
98 }
99
100 throw new OperationFailedException("ERROR! Ran out of prefixes!");
101 }
102
103
104
105
106
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 }