1 package org.kuali.ole.deliver.rule;
2
3
4 import org.apache.commons.collections.CollectionUtils;
5 import org.apache.commons.lang3.StringUtils;
6 import org.kuali.ole.OLEConstants;
7 import org.kuali.ole.deliver.bo.OleCirculationDesk;
8 import org.kuali.ole.deliver.bo.OleCirculationDeskDetail;
9 import org.kuali.ole.deliver.bo.OleCirculationDeskLocation;
10 import org.kuali.ole.describe.bo.OleLocation;
11 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
12 import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
13
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18
19
20
21
22 public class OleCirculationDeskRule extends MaintenanceDocumentRuleBase {
23
24
25
26
27
28
29 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
30 boolean isValid = true;
31 OleCirculationDesk circulationDesk = (OleCirculationDesk) document.getNewMaintainableObject().getDataObject();
32 String circulationDeskAction = document.getOldMaintainableObject().getMaintenanceAction();
33
34 isValid &= validateCirculationDeskCode(circulationDeskAction, circulationDesk);
35 isValid &= validateCirculationDeskBeforeEdit(circulationDeskAction, circulationDesk);
36 return isValid;
37 }
38
39
40
41
42
43
44
45 private boolean validateCirculationDeskCode(String circulationDeskAction, OleCirculationDesk circulationDesk) {
46 List<OleCirculationDeskLocation> oleCirculationDeskLocationList = new ArrayList<OleCirculationDeskLocation>();
47 List<OleCirculationDeskLocation> olePickupCirculationDeskLocations = new ArrayList<OleCirculationDeskLocation>();
48 if (CollectionUtils.isNotEmpty(circulationDesk.getOleCirculationDeskLocations())) {
49 for (OleCirculationDeskLocation oleCirculationDeskLocation : circulationDesk.getOleCirculationDeskLocations()) {
50 if (StringUtils.isNotBlank(oleCirculationDeskLocation.getCirculationPickUpDeskLocation())) {
51 olePickupCirculationDeskLocations.add(oleCirculationDeskLocation);
52 } else {
53 oleCirculationDeskLocationList.add(oleCirculationDeskLocation);
54 }
55 }
56 }
57 if (circulationDesk.getCirculationDeskCode() != null) {
58 Map<String, String> criteria = new HashMap<String, String>();
59 criteria.put(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_CD, circulationDesk.getCirculationDeskCode());
60 List<OleCirculationDesk> circulationDeskInDatabase = (List<OleCirculationDesk>) getBoService().findMatching(OleCirculationDesk.class, criteria);
61 if ((circulationDeskInDatabase.size() > 0)) {
62 for (OleCirculationDesk circulationDeskObj : circulationDeskInDatabase) {
63 String circulationDeskId = circulationDeskObj.getCirculationDeskId();
64 if (null == circulationDesk.getCirculationDeskId() || !(circulationDesk.getCirculationDeskId().equalsIgnoreCase(circulationDeskId))) {
65 this.putFieldError(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_CODE, OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_CD_EXIST);
66 return false;
67 }
68 }
69 }
70 }
71 if (oleCirculationDeskLocationList.size() == 0) {
72 this.putFieldError(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION, OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION_ERROR);
73 return false;
74 }
75 List<String> locationCodes = new ArrayList<String>();
76 for (OleCirculationDeskLocation oleCirculationDeskLocation : oleCirculationDeskLocationList) {
77 if (oleCirculationDeskLocation.getCirculationDeskLocation() == null) {
78 Map<String, String> map = new HashMap<String, String>();
79 map.put(OLEConstants.LOC_CD, oleCirculationDeskLocation.getCirculationLocationCode());
80 OleLocation oleLocation = getBoService().findByPrimaryKey(OleLocation.class, map);
81 oleCirculationDeskLocation.setLocation(oleLocation);
82 oleCirculationDeskLocation.setCirculationDeskLocation(oleLocation != null ? oleLocation.getLocationId() : null);
83 }
84 locationCodes.add(oleCirculationDeskLocation.getCirculationLocationCode());
85 }
86 List<OleLocation> oleLocations = (List<OleLocation>) getBoService().findAll(OleLocation.class);
87 List<String> validLocationCodes = new ArrayList<String>();
88 for (OleLocation oleLocation : oleLocations) {
89 for (String locationCode : locationCodes) {
90 if (oleLocation.getLocationCode().equals(locationCode))
91 validLocationCodes.add(locationCode);
92 }
93 }
94 if (validLocationCodes.size() != locationCodes.size()) {
95 this.putFieldError(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION, OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION_VALID_ERROR);
96 return false;
97 }
98 for (String locationCode : locationCodes) {
99 boolean duplicate = false;
100 for (String dupLocationCode : validLocationCodes) {
101 if (locationCode.equalsIgnoreCase(dupLocationCode)) {
102 if (duplicate) {
103 this.putFieldError(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION, OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION_DUPLICATE_ERROR);
104 return false;
105 }
106 duplicate = true;
107 }
108 }
109 }
110 for (OleCirculationDeskLocation oleCirculationDeskLocation : oleCirculationDeskLocationList) {
111 Map<String, String> circDeskLocation = new HashMap<String, String>();
112 circDeskLocation.put(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION, oleCirculationDeskLocation.getCirculationDeskLocation());
113 List<OleCirculationDeskLocation> circulationDeskLocationInDatabase = (List<OleCirculationDeskLocation>) getBoService().findMatching(OleCirculationDeskLocation.class, circDeskLocation);
114 if ((circulationDeskLocationInDatabase.size() > 0)) {
115 if (!circulationDeskAction.equalsIgnoreCase(OLEConstants.OleCirculationDesk.COPY)) {
116 if (!(circulationDeskLocationInDatabase.get(0).getCirculationDeskId().equalsIgnoreCase(oleCirculationDeskLocation.getCirculationDeskId()))) {
117 this.putFieldError(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION, OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION_MAPPED_ERROR);
118 return false;
119 }
120 } else {
121 this.putFieldError(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION, OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION_MAPPED_ERROR);
122 return false;
123 }
124 }
125 }
126 return true;
127 }
128
129
130
131
132
133
134
135
136 private boolean validateCirculationDeskBeforeEdit(String circulationDeskAction, OleCirculationDesk circulationDesk) {
137 List<OleCirculationDeskLocation> oleCirculationDeskLocationList = new ArrayList<OleCirculationDeskLocation>();
138 List<OleCirculationDeskLocation> olePickupCirculationDeskLocations = new ArrayList<OleCirculationDeskLocation>();
139 if (CollectionUtils.isNotEmpty(circulationDesk.getOleCirculationDeskLocations())) {
140 for (OleCirculationDeskLocation oleCirculationDeskLocation : circulationDesk.getOleCirculationDeskLocations()) {
141 if (StringUtils.isNotBlank(oleCirculationDeskLocation.getCirculationPickUpDeskLocation())) {
142 olePickupCirculationDeskLocations.add(oleCirculationDeskLocation);
143 } else {
144 oleCirculationDeskLocationList.add(oleCirculationDeskLocation);
145 }
146 }
147 }
148 if (circulationDeskAction.equalsIgnoreCase(OLEConstants.OleCirculationDesk.EDIT)) {
149 Map<String, String> circulationDeskIdMap = new HashMap<String, String>();
150 circulationDeskIdMap.put(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_ID, circulationDesk.getCirculationDeskId());
151 List<OleCirculationDeskDetail> circulationDeskDetailInDatabase = (List<OleCirculationDeskDetail>) getBoService().findMatching(OleCirculationDeskDetail.class, circulationDeskIdMap);
152 if ((circulationDeskDetailInDatabase.size() > 0)) {
153 if (!circulationDesk.isActive()) {
154 this.putFieldError(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_MAP, OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_MAP_ERROR);
155 return false;
156 }
157 }
158 List<String> locationCodes = new ArrayList<String>();
159 if (oleCirculationDeskLocationList.size() > 0 && locationCodes.size() == 0) {
160 locationCodes.add(oleCirculationDeskLocationList.get(0).getCirculationLocationCode());
161 }
162
163 for (int i = 1; i < oleCirculationDeskLocationList.size(); i++) {
164
165 for (int j = 0; j < locationCodes.size() - 1; j++) {
166
167 if (locationCodes.get(j).equals(oleCirculationDeskLocationList.get(i).getCirculationLocationCode())) {
168 this.putFieldError(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION, OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION_DUPLICATE_ERROR);
169 return false;
170 } else {
171 locationCodes.add(oleCirculationDeskLocationList.get(i).getCirculationLocationCode());
172 j++;
173 }
174 }
175 }
176 List<OleLocation> oleLocations = (List<OleLocation>) getBoService().findAll(OleLocation.class);
177 List<String> validLocationCodes = new ArrayList<String>();
178 for (OleLocation oleLocation : oleLocations) {
179 for (String locationCode : locationCodes) {
180 if (oleLocation.getLocationCode().equals(locationCode))
181 validLocationCodes.add(locationCode);
182 }
183 }
184 if (validLocationCodes.size() != locationCodes.size()) {
185 this.putFieldError(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION, OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION_VALID_ERROR);
186 return false;
187 }
188
189 }
190 return true;
191 }
192 }
193
194
195
196