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.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   * OleCirculationDeskRule validates maintenance object for Circulation Desk Maintenance Document
21   */
22  public class OleCirculationDeskRule extends MaintenanceDocumentRuleBase {
23  
24      /**
25       * @param document
26       * @return boolean
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       * This method  validates duplicate circulation Desk Code and return boolean value.
41       *
42       * @param circulationDesk
43       * @return boolean
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         return true;
111     }
112 
113     /**
114      * This method is to check whether the circulation desk is having any dependency to the operator before de-activating the document
115      *
116      * @param circulationDeskAction
117      * @param circulationDesk
118      * @return boolen
119      */
120     private boolean validateCirculationDeskBeforeEdit(String circulationDeskAction, OleCirculationDesk circulationDesk) {
121         List<OleCirculationDeskLocation> oleCirculationDeskLocationList = new ArrayList<OleCirculationDeskLocation>();
122         List<OleCirculationDeskLocation> olePickupCirculationDeskLocations = new ArrayList<OleCirculationDeskLocation>();
123         if (CollectionUtils.isNotEmpty(circulationDesk.getOleCirculationDeskLocations())) {
124             for (OleCirculationDeskLocation oleCirculationDeskLocation : circulationDesk.getOleCirculationDeskLocations()) {
125                     if (StringUtils.isNotBlank(oleCirculationDeskLocation.getCirculationPickUpDeskLocation())) {
126                         olePickupCirculationDeskLocations.add(oleCirculationDeskLocation);
127                     } else {
128                         oleCirculationDeskLocationList.add(oleCirculationDeskLocation);
129                     }
130             }
131         }
132         if (circulationDeskAction.equalsIgnoreCase(OLEConstants.OleCirculationDesk.EDIT)) {
133             Map<String, String> circulationDeskIdMap = new HashMap<String, String>();
134             circulationDeskIdMap.put(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_ID, circulationDesk.getCirculationDeskId());
135             List<OleCirculationDeskDetail> circulationDeskDetailInDatabase = (List<OleCirculationDeskDetail>) getBoService().findMatching(OleCirculationDeskDetail.class, circulationDeskIdMap);
136             if ((circulationDeskDetailInDatabase.size() > 0)) {
137                 if (!circulationDesk.isActive()) {
138                     this.putFieldError(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_MAP, OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_MAP_ERROR);
139                     return false;
140                 }
141             }
142             List<String> locationCodes = new ArrayList<String>();
143             if (oleCirculationDeskLocationList.size() > 0 && locationCodes.size() == 0) {
144                 locationCodes.add(oleCirculationDeskLocationList.get(0).getCirculationLocationCode());
145             }
146 
147             for (int i = 1; i < oleCirculationDeskLocationList.size(); i++) {
148 
149                 for (int j = 0; j < locationCodes.size() - 1; j++) {
150 
151                     if (locationCodes.get(j).equals(oleCirculationDeskLocationList.get(i).getCirculationLocationCode())) {
152                         this.putFieldError(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION, OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION_DUPLICATE_ERROR);
153                         return false;
154                     } else {
155                         locationCodes.add(oleCirculationDeskLocationList.get(i).getCirculationLocationCode());
156                         j++;
157                     }
158                 }
159             }
160             List<OleLocation> oleLocations = (List<OleLocation>) getBoService().findAll(OleLocation.class);
161             List<String> validLocationCodes = new ArrayList<String>();
162             for (OleLocation oleLocation : oleLocations) {
163                 for (String locationCode : locationCodes) {
164                     if (oleLocation.getLocationCode().equals(locationCode))
165                         validLocationCodes.add(locationCode);
166                 }
167             }
168             if (validLocationCodes.size() != locationCodes.size()) {
169                 this.putFieldError(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION, OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION_VALID_ERROR);
170                 return false;
171             }
172 
173         }
174         return true;
175     }
176 }
177 
178 
179 
180