View Javadoc
1   package org.kuali.ole.describe.rule;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.describe.bo.OleLocation;
5   import org.kuali.rice.kim.api.role.Role;
6   import org.kuali.rice.kim.impl.role.RoleServiceImpl;
7   import org.kuali.rice.krad.maintenance.MaintenanceDocument;
8   import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
9   import org.kuali.rice.krad.util.GlobalVariables;
10  
11  import java.util.ArrayList;
12  import java.util.HashMap;
13  import java.util.List;
14  import java.util.Map;
15  
16  /**
17   * OleLocationRule validates maintenance object for Ole Location Maintenance Document
18   */
19  
20  public class OleLocationRule extends MaintenanceDocumentRuleBase {
21  
22      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
23          boolean isValid = true;
24          OleLocation oleLocation = (OleLocation) document.getNewMaintainableObject().getDataObject();
25          isValid &= validateLocationCode(oleLocation);
26          return isValid;
27      }
28  
29      /**
30       * This method  validates duplicate oleLocation code and return boolean value.
31       *
32       * @param oleLocation
33       * @return boolean
34       */
35      private boolean validateLocationCode(OleLocation oleLocation) {
36          if (oleLocation.getLocationCode() != null) {
37              Map<String, String> criteria = new HashMap<String, String>();
38              criteria.put(OLEConstants.LOC_CD, oleLocation.getLocationCode());
39              List<OleLocation> oleLocationInDatabase = (List<OleLocation>) getBoService().findMatching(OleLocation.class, criteria);
40              if ((oleLocationInDatabase.size() > 0)) {
41                  for (OleLocation oleLocationObj : oleLocationInDatabase) {
42                      String locationId = oleLocationObj.getLocationId();
43                      if (null == oleLocation.getLocationId() || (!oleLocation.getLocationId().equals(locationId))) {
44                          this.putFieldError(OLEConstants.LOC_CODE, "error.duplicate.code");
45                          return false;
46                      }
47                  }
48              }
49          }
50          if (!oleLocation.getLevelId().equals("5") || !(oleLocation.getLevelCode() != null && oleLocation.getLevelCode().equals(OLEConstants.LOC_LEVEL_SHELVING))) {
51              RoleServiceImpl roleServce = new RoleServiceImpl();
52              List<String> roleIdList = new ArrayList<String>();
53              Role role = roleServce.getRoleByNamespaceCodeAndName(OLEConstants.OlePatron.PATRON_NAMESPACE, OLEConstants.LOC_ADMIN);
54  
55              roleIdList.add(role.getId());
56  
57              boolean havingRole = roleServce.principalHasRole(GlobalVariables.getUserSession().getPrincipalId(), roleIdList, new HashMap<String, String>());
58              if (!havingRole) {
59                  this.putFieldError(OLEConstants.LOC_LEVEl_ID, OLEConstants.LOC_LEVEL_ERROR);
60                  return false;
61              }
62  
63          }
64          return true;
65      }
66  }