View Javadoc
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   * OleCirculationDeskRule validates maintenance object for Circulation Desk Maintenance Document
23   */
24  public class OleCirculationDeskRule extends MaintenanceDocumentRuleBase {
25  
26      /**
27       * @param document
28       * @return boolean
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       * This method  validates duplicate circulation Desk Code and return boolean value.
43       *
44       * @param circulationDesk
45       * @return boolean
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             if ((circulationDeskLocationInDatabase.size() > 0)) {
117                 if (!circulationDeskAction.equalsIgnoreCase(OLEConstants.OleCirculationDesk.COPY)) {
118                     if (!(circulationDeskLocationInDatabase.get(0).getCirculationDeskId().equalsIgnoreCase(oleCirculationDeskLocation.getCirculationDeskId()))) {
119                         this.putFieldError(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION, OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION_MAPPED_ERROR);
120                         return false;
121                     }
122                 } else {
123                     this.putFieldError(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION, OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION_MAPPED_ERROR);
124                     return false;
125                 }
126             }
127         }
128         return true;
129     }
130 
131     /**
132      * This method is to check whether the circulation desk is having any dependency to the operator before de-activating the document
133      *
134      * @param circulationDeskAction
135      * @param circulationDesk
136      * @return boolen
137      */
138     private boolean validateCirculationDeskBeforeEdit(String circulationDeskAction, OleCirculationDesk circulationDesk) {
139         List<OleCirculationDeskLocation> oleCirculationDeskLocationList = new ArrayList<OleCirculationDeskLocation>();
140         List<OleCirculationDeskLocation> olePickupCirculationDeskLocations = new ArrayList<OleCirculationDeskLocation>();
141         if (CollectionUtils.isNotEmpty(circulationDesk.getOleCirculationDeskLocations())) {
142             for (OleCirculationDeskLocation oleCirculationDeskLocation : circulationDesk.getOleCirculationDeskLocations()) {
143                     if (StringUtils.isNotBlank(oleCirculationDeskLocation.getCirculationPickUpDeskLocation())) {
144                         olePickupCirculationDeskLocations.add(oleCirculationDeskLocation);
145                     } else {
146                         oleCirculationDeskLocationList.add(oleCirculationDeskLocation);
147                     }
148             }
149         }
150         if (circulationDeskAction.equalsIgnoreCase(OLEConstants.OleCirculationDesk.EDIT)) {
151             Map<String, String> circulationDeskIdMap = new HashMap<String, String>();
152             circulationDeskIdMap.put(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_ID, circulationDesk.getCirculationDeskId());
153             List<OleCirculationDeskDetail> circulationDeskDetailInDatabase = (List<OleCirculationDeskDetail>) getBoService().findMatching(OleCirculationDeskDetail.class, circulationDeskIdMap);
154             if ((circulationDeskDetailInDatabase.size() > 0)) {
155                 if (!circulationDesk.isActive()) {
156                     this.putFieldError(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_MAP, OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_MAP_ERROR);
157                     return false;
158                 }
159             }
160             List<String> locationCodes = new ArrayList<String>();
161             if (oleCirculationDeskLocationList.size() > 0 && locationCodes.size() == 0) {
162                 locationCodes.add(oleCirculationDeskLocationList.get(0).getCirculationLocationCode());
163             }
164 
165             for (int i = 1; i < oleCirculationDeskLocationList.size(); i++) {
166 
167                 for (int j = 0; j < locationCodes.size() - 1; j++) {
168 
169                     if (locationCodes.get(j).equals(oleCirculationDeskLocationList.get(i).getCirculationLocationCode())) {
170                         this.putFieldError(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION, OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION_DUPLICATE_ERROR);
171                         return false;
172                     } else {
173                         locationCodes.add(oleCirculationDeskLocationList.get(i).getCirculationLocationCode());
174                         j++;
175                     }
176                 }
177             }
178             List<OleLocation> oleLocations = (List<OleLocation>) getBoService().findAll(OleLocation.class);
179             List<String> validLocationCodes = new ArrayList<String>();
180             for (OleLocation oleLocation : oleLocations) {
181                 for (String locationCode : locationCodes) {
182                     if (oleLocation.getLocationCode().equals(locationCode))
183                         validLocationCodes.add(locationCode);
184                 }
185             }
186             if (validLocationCodes.size() != locationCodes.size()) {
187                 this.putFieldError(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION, OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION_VALID_ERROR);
188                 return false;
189             }
190 
191         }
192         return true;
193     }
194 
195     private void setDefaultValues(OleCirculationDesk oleCirculationDesk){
196         if(oleCirculationDesk!=null && oleCirculationDesk.getDefaultRequestTypeCode()!=null){
197             Map<String,String> requestTypeMap = new HashMap<String,String>();
198             requestTypeMap.put("requestTypeCode",oleCirculationDesk.getDefaultRequestTypeCode());
199             List<OleDeliverRequestType> oleDeliverRequestTypes = (List<OleDeliverRequestType>) KRADServiceLocator.getBusinessObjectService().findMatching(OleDeliverRequestType.class,requestTypeMap);
200             if(oleDeliverRequestTypes!=null && oleDeliverRequestTypes.size()>0){
201                 oleCirculationDesk.setDefaultRequestTypeId(oleDeliverRequestTypes.get(0).getRequestTypeId());
202                 oleCirculationDesk.setDefaultRequestType(oleDeliverRequestTypes.get(0));
203             }
204 
205         }
206     }
207 }
208 
209 
210 
211