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