View Javadoc
1   package org.kuali.ole.select.service.impl;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.apache.commons.lang3.StringUtils;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.select.bo.OLEAccessActivationConfiguration;
7   import org.kuali.ole.select.bo.OLEAccessActivationWorkFlow;
8   import org.kuali.ole.select.bo.OLERoleBo;
9   import org.kuali.ole.select.service.OLEAccessActivationService;
10  import org.kuali.rice.kim.api.identity.IdentityService;
11  import org.kuali.rice.kim.api.identity.Person;
12  import org.kuali.rice.kim.api.identity.PersonService;
13  import org.kuali.rice.kim.api.identity.principal.Principal;
14  import org.kuali.rice.kim.api.role.Role;
15  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
16  import org.kuali.rice.kim.impl.role.RoleBo;
17  import org.kuali.rice.krad.service.BusinessObjectService;
18  import org.kuali.rice.krad.service.KRADServiceLocator;
19  import org.kuali.rice.krad.util.GlobalVariables;
20  
21  import java.util.*;
22  
23  /**
24   * Created by maheswarang on 5/13/15.
25   */
26  public class OLEAccessActivationServiceImpl implements OLEAccessActivationService {
27  
28      private PersonService personService;
29      private BusinessObjectService businessObjectService;
30  
31      public PersonService getPersonService() {
32          if(personService == null){
33              personService = KimApiServiceLocator.getPersonService();
34          }
35          return personService;
36      }
37      public void setPersonService(PersonService personService) {
38          this.personService = personService;
39      }
40  
41      public BusinessObjectService getBusinessObjectService() {
42          if(businessObjectService == null){
43              businessObjectService = KRADServiceLocator.getBusinessObjectService();
44          }
45          return businessObjectService;
46      }
47  
48      public void setBusinessObjectService(BusinessObjectService businessObjectService) {
49          this.businessObjectService = businessObjectService;
50      }
51  
52      @Override
53      public OLEAccessActivationConfiguration setRoleAndPersonName(OLEAccessActivationConfiguration oleAccessActivationConfiguration) {
54          List<OLEAccessActivationWorkFlow> accessActivationWorkFlowList = oleAccessActivationConfiguration.getAccessActivationWorkflowList();
55          for (OLEAccessActivationWorkFlow oleAccessActivationWorkFlow : accessActivationWorkFlowList) {
56              Map<String, Object> oleBoMap = new HashMap<>();
57              if(StringUtils.isNotBlank(oleAccessActivationWorkFlow.getRoleId())) {
58                  oleBoMap.put(OLEConstants.ID, oleAccessActivationWorkFlow.getRoleId());
59                  OLERoleBo oleRoleBo = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OLERoleBo.class, oleBoMap);
60                  if(oleRoleBo != null) {
61                      oleAccessActivationWorkFlow.setRoleName(oleRoleBo.getName());
62                  }
63              }
64              if(StringUtils.isNotBlank(oleAccessActivationWorkFlow.getPersonId())){
65                  Person person = KimApiServiceLocator.getPersonService().getPerson(oleAccessActivationWorkFlow.getPersonId());
66                  if(person !=null){
67                      oleAccessActivationWorkFlow.setPersonName(person.getPrincipalName());
68                  }
69              }
70  
71          }
72          return oleAccessActivationConfiguration;
73      }
74  
75  
76  
77       @Override
78       public boolean validateAccessActivationWorkFlow(List<OLEAccessActivationWorkFlow> accessActivationWorkFlowList, OLEAccessActivationWorkFlow accessActivationWorkFlow, String selector) {
79  
80          boolean duplicateOrderNumber = false;
81          boolean duplicateStatus = false;
82          boolean validRole = true;
83          boolean validPerson = false;
84          for (OLEAccessActivationWorkFlow activationWorkFlow : accessActivationWorkFlowList) {
85              if (accessActivationWorkFlow.getOrderNo() != null) {
86                  if (activationWorkFlow.getOrderNo() == accessActivationWorkFlow.getOrderNo()) {
87                      GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLE_ACCESS_ACTIVATION, OLEConstants.ERROR_DUPLICATE_ORDER_NO);
88                      duplicateOrderNumber = true;
89                  }
90              }
91              if (StringUtils.isNotBlank(accessActivationWorkFlow.getStatus())) {
92                  if (activationWorkFlow.getStatus().equalsIgnoreCase(accessActivationWorkFlow.getStatus())) {
93                      GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLE_ACCESS_ACTIVATION, OLEConstants.ERROR_DUPLICATE_STATUS);
94                      duplicateStatus = true;
95                  }
96              }
97          }
98           if(StringUtils.isNotBlank(selector) && selector.equals(OLEConstants.SELECTOR_ROLE)) {
99               validRole = validateRole(accessActivationWorkFlow);
100          }
101 
102         if (duplicateStatus || duplicateOrderNumber || !validRole || !validatePerson(accessActivationWorkFlow)) {
103             return false;
104         }
105         return true;
106     }
107 
108    @Override
109     public List<Principal> getPrincipals(OLEAccessActivationWorkFlow oleAccessActivationWorkFlow){
110         List<Principal> principals = new ArrayList<Principal>();
111         org.kuali.rice.kim.api.role.RoleService roleService = (org.kuali.rice.kim.api.role.RoleService) KimApiServiceLocator.getRoleService();
112         Collection<String> principalIds = new ArrayList<>();
113         if(StringUtils.isNotBlank(oleAccessActivationWorkFlow.getRoleId())) {
114             Role role = roleService.getRole(oleAccessActivationWorkFlow.getRoleId());
115             principalIds = (Collection<String>) roleService.getRoleMemberPrincipalIds(role.getNamespaceCode(), role.getName(), new HashMap<String, String>());
116         }
117         IdentityService identityService = KimApiServiceLocator.getIdentityService();
118         List<String> principalList = new ArrayList<String>();
119         if(CollectionUtils.isNotEmpty(principalIds)) {
120             principalList.addAll(principalIds);
121         }
122         if(StringUtils.isNotBlank(oleAccessActivationWorkFlow.getPersonId())){
123             principalList.add(oleAccessActivationWorkFlow.getPersonId());
124         }
125         principals = identityService.getPrincipals(principalList);
126         return principals;
127     }
128 
129 
130 
131 
132     private boolean validateRole(OLEAccessActivationWorkFlow accessActivationWorkFlow){
133         Map<String, String> criteria = new HashMap<String, String>();
134         List<RoleBo> dataSourceNameInDatabaseroleName;
135         RoleBo roleBo;
136         boolean validRole = false;
137 
138         if (StringUtils.isNotBlank(accessActivationWorkFlow.getRoleId()) && StringUtils.isNotBlank(accessActivationWorkFlow.getRoleName())) {
139             criteria.put(OLEConstants.ACCESS_ROLE_ID, accessActivationWorkFlow.getRoleId());
140             criteria.put(OLEConstants.ACCESS_ROLE_NAME, accessActivationWorkFlow.getRoleName());
141             dataSourceNameInDatabaseroleName = (List<RoleBo>) getBusinessObjectService().findMatching(RoleBo.class, criteria);
142             if (dataSourceNameInDatabaseroleName != null && dataSourceNameInDatabaseroleName.size() > 0) {
143                 validRole = true;
144             } else {
145                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLE_ACCESS_ACTIVATION, OLEConstants.ERROR_INVALID_ID_NAME);
146                 validRole = false;
147             }
148         } else if (StringUtils.isBlank(accessActivationWorkFlow.getRoleId()) && StringUtils.isNotBlank(accessActivationWorkFlow.getRoleName())) {
149             criteria = new HashMap<String, String>();
150             criteria.put(OLEConstants.ACCESS_ROLE_NAME, accessActivationWorkFlow.getRoleName());
151             dataSourceNameInDatabaseroleName = (List<RoleBo>) getBusinessObjectService()
152                     .findMatching(RoleBo.class, criteria);
153             if (dataSourceNameInDatabaseroleName != null && dataSourceNameInDatabaseroleName.size() > 0) {
154                 roleBo = dataSourceNameInDatabaseroleName.get(0);
155                 accessActivationWorkFlow.setRoleId(roleBo.getId());
156                 validRole = true;
157             } else {
158                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLE_ACCESS_ACTIVATION, OLEConstants.ERROR_INVALID_NAME);
159                 validRole = false;
160             }
161         } else if (StringUtils.isNotBlank(accessActivationWorkFlow.getRoleId()) && StringUtils.isBlank(accessActivationWorkFlow.getRoleName())) {
162             criteria = new HashMap<String, String>();
163             criteria.put(OLEConstants.ACCESS_ROLE_ID, accessActivationWorkFlow.getRoleId());
164             dataSourceNameInDatabaseroleName = (List<RoleBo>) getBusinessObjectService()
165                     .findMatching(RoleBo.class, criteria);
166             if (dataSourceNameInDatabaseroleName != null && dataSourceNameInDatabaseroleName.size() > 0) {
167                 roleBo = dataSourceNameInDatabaseroleName.get(0);
168                 accessActivationWorkFlow.setRoleName(roleBo.getName());
169                 validRole = true;
170             } else {
171                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLE_ACCESS_ACTIVATION, OLEConstants.ERROR_INVALID_ID);
172                 validRole = false;
173             }
174         }
175 
176 
177         return validRole ;
178     }
179 
180 
181     private boolean validatePerson(OLEAccessActivationWorkFlow accessActivationWorkFlow){
182         boolean validPerson = false;
183         if(StringUtils.isNotBlank(accessActivationWorkFlow.getPersonId()) && StringUtils.isNotBlank(accessActivationWorkFlow.getPersonName())){
184             Map<String,String> criteriaMap = new HashMap<String,String>();
185             criteriaMap.put("principalId",accessActivationWorkFlow.getPersonId());
186             criteriaMap.put("principalName",accessActivationWorkFlow.getPersonName());
187             List<Person> personList = getPersonService().findPeople(criteriaMap);
188             if(personList.size()>0){
189                 validPerson = true;
190             }else {
191                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLE_ACCESS_ACTIVATION, OLEConstants.ERROR_INVALID_PERSON_ID_NAME);
192                 validPerson = false;
193             }
194         } else if(StringUtils.isNotBlank(accessActivationWorkFlow.getPersonId()) && StringUtils.isBlank(accessActivationWorkFlow.getPersonName())){
195             Person person = getPersonService().getPerson(accessActivationWorkFlow.getPersonId());
196             if(person!=null){
197                 validPerson =true;
198                 accessActivationWorkFlow.setPersonName(person.getName());
199             }else{
200                 validPerson = false;
201                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLE_ACCESS_ACTIVATION, OLEConstants.ERROR_INVALID_PERSON_ID);
202             }
203 
204         }
205         else if(StringUtils.isBlank(accessActivationWorkFlow.getPersonId()) && StringUtils.isNotBlank(accessActivationWorkFlow.getPersonName())){
206             Person person = getPersonService().getPersonByPrincipalName(accessActivationWorkFlow.getPersonName());
207             if(person!=null){
208                 validPerson =true;
209                 accessActivationWorkFlow.setPersonId(person.getPrincipalId());
210             }else{
211                 validPerson = false;
212                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLE_ACCESS_ACTIVATION, OLEConstants.ERROR_INVALID_PERSON_NAME);
213             }
214 
215         }
216         if(StringUtils.isBlank(accessActivationWorkFlow.getPersonId()) && StringUtils.isBlank(accessActivationWorkFlow.getPersonName())){
217             accessActivationWorkFlow.setPersonName(null);
218             validPerson=true;
219         }
220         return validPerson;
221     }
222 
223 
224 
225 
226 
227 }