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