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          Map<String, Object> oleBoMap = new HashMap<>();
56          if(StringUtils.isNotBlank(oleAccessActivationConfiguration.getRecipientRoleId())) {
57              oleBoMap.put(OLEConstants.ID, oleAccessActivationConfiguration.getRecipientRoleId());
58              OLERoleBo oleRoleBo = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OLERoleBo.class, oleBoMap);
59              if(oleRoleBo != null) {
60                  oleAccessActivationConfiguration.setRecipientRoleName(oleRoleBo.getName());
61              }
62          }
63          if(StringUtils.isNotBlank(oleAccessActivationConfiguration.getRecipientUserId())){
64              Person person = KimApiServiceLocator.getPersonService().getPerson(oleAccessActivationConfiguration.getRecipientUserId());
65              if(person !=null){
66                  oleAccessActivationConfiguration.setRecipientUserName(person.getPrincipalName());
67              }
68          }
69          for (OLEAccessActivationWorkFlow oleAccessActivationWorkFlow : accessActivationWorkFlowList) {
70               oleBoMap = new HashMap<>();
71              if(StringUtils.isNotBlank(oleAccessActivationWorkFlow.getRoleId())) {
72                  oleBoMap.put(OLEConstants.ID, oleAccessActivationWorkFlow.getRoleId());
73                  OLERoleBo oleRoleBo = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OLERoleBo.class, oleBoMap);
74                  if(oleRoleBo != null) {
75                      oleAccessActivationWorkFlow.setRoleName(oleRoleBo.getName());
76                  }
77              }
78              if(StringUtils.isNotBlank(oleAccessActivationWorkFlow.getPersonId())){
79                  Person person = KimApiServiceLocator.getPersonService().getPerson(oleAccessActivationWorkFlow.getPersonId());
80                  if(person !=null){
81                      oleAccessActivationWorkFlow.setPersonName(person.getPrincipalName());
82                  }
83              }
84  
85          }
86          return oleAccessActivationConfiguration;
87      }
88  
89  
90  
91       @Override
92       public boolean validateAccessActivationWorkFlow(List<OLEAccessActivationWorkFlow> accessActivationWorkFlowList, OLEAccessActivationWorkFlow accessActivationWorkFlow, String selector) {
93  
94          boolean duplicateOrderNumber = false;
95          boolean duplicateStatus = false;
96          boolean validRole = true;
97          boolean validPerson = false;
98          for (OLEAccessActivationWorkFlow activationWorkFlow : accessActivationWorkFlowList) {
99              if (accessActivationWorkFlow.getOrderNo() != null) {
100                 if (activationWorkFlow.getOrderNo() == accessActivationWorkFlow.getOrderNo()) {
101                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLE_ACCESS_ACTIVATION, OLEConstants.ERROR_DUPLICATE_ORDER_NO);
102                     duplicateOrderNumber = true;
103                 }
104             }
105             if (StringUtils.isNotBlank(accessActivationWorkFlow.getStatus())) {
106                 if (activationWorkFlow.getStatus().equalsIgnoreCase(accessActivationWorkFlow.getStatus())) {
107                     GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLE_ACCESS_ACTIVATION, OLEConstants.ERROR_DUPLICATE_STATUS);
108                     duplicateStatus = true;
109                 }
110             }
111         }
112          if(StringUtils.isNotBlank(selector) && selector.equals(OLEConstants.SELECTOR_ROLE)) {
113              validRole = validateRole(accessActivationWorkFlow);
114          }
115 
116         if (duplicateStatus || duplicateOrderNumber || !validRole || !validatePerson(accessActivationWorkFlow)) {
117             return false;
118         }
119         return true;
120     }
121 
122    @Override
123     public List<Principal> getPrincipals(OLEAccessActivationWorkFlow oleAccessActivationWorkFlow){
124         List<Principal> principals = new ArrayList<Principal>();
125         org.kuali.rice.kim.api.role.RoleService roleService = (org.kuali.rice.kim.api.role.RoleService) KimApiServiceLocator.getRoleService();
126         Collection<String> principalIds = new ArrayList<>();
127         if(StringUtils.isNotBlank(oleAccessActivationWorkFlow.getRoleId())) {
128             Role role = roleService.getRole(oleAccessActivationWorkFlow.getRoleId());
129             principalIds = (Collection<String>) roleService.getRoleMemberPrincipalIds(role.getNamespaceCode(), role.getName(), new HashMap<String, String>());
130         }
131         IdentityService identityService = KimApiServiceLocator.getIdentityService();
132         List<String> principalList = new ArrayList<String>();
133         if(CollectionUtils.isNotEmpty(principalIds)) {
134             principalList.addAll(principalIds);
135         }
136         if(StringUtils.isNotBlank(oleAccessActivationWorkFlow.getPersonId())){
137             principalList.add(oleAccessActivationWorkFlow.getPersonId());
138         }
139         principals = identityService.getPrincipals(principalList);
140         return principals;
141     }
142 
143 
144 
145 
146     private boolean validateRole(OLEAccessActivationWorkFlow accessActivationWorkFlow){
147         Map<String, String> criteria = new HashMap<String, String>();
148         List<RoleBo> dataSourceNameInDatabaseroleName;
149         RoleBo roleBo;
150         boolean validRole = false;
151 
152         if (StringUtils.isNotBlank(accessActivationWorkFlow.getRoleId()) && StringUtils.isNotBlank(accessActivationWorkFlow.getRoleName())) {
153             criteria.put(OLEConstants.ACCESS_ROLE_ID, accessActivationWorkFlow.getRoleId());
154             criteria.put(OLEConstants.ACCESS_ROLE_NAME, accessActivationWorkFlow.getRoleName());
155             dataSourceNameInDatabaseroleName = (List<RoleBo>) getBusinessObjectService().findMatching(RoleBo.class, criteria);
156             if (dataSourceNameInDatabaseroleName != null && dataSourceNameInDatabaseroleName.size() > 0) {
157                 validRole = true;
158             } else {
159                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLE_ACCESS_ACTIVATION, OLEConstants.ERROR_INVALID_ID_NAME);
160                 validRole = false;
161             }
162         } else if (StringUtils.isBlank(accessActivationWorkFlow.getRoleId()) && StringUtils.isNotBlank(accessActivationWorkFlow.getRoleName())) {
163             criteria = new HashMap<String, String>();
164             criteria.put(OLEConstants.ACCESS_ROLE_NAME, accessActivationWorkFlow.getRoleName());
165             dataSourceNameInDatabaseroleName = (List<RoleBo>) getBusinessObjectService()
166                     .findMatching(RoleBo.class, criteria);
167             if (dataSourceNameInDatabaseroleName != null && dataSourceNameInDatabaseroleName.size() > 0) {
168                 roleBo = dataSourceNameInDatabaseroleName.get(0);
169                 accessActivationWorkFlow.setRoleId(roleBo.getId());
170                 validRole = true;
171             } else {
172                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLE_ACCESS_ACTIVATION, OLEConstants.ERROR_INVALID_NAME);
173                 validRole = false;
174             }
175         } else if (StringUtils.isNotBlank(accessActivationWorkFlow.getRoleId()) && StringUtils.isBlank(accessActivationWorkFlow.getRoleName())) {
176             criteria = new HashMap<String, String>();
177             criteria.put(OLEConstants.ACCESS_ROLE_ID, accessActivationWorkFlow.getRoleId());
178             dataSourceNameInDatabaseroleName = (List<RoleBo>) getBusinessObjectService()
179                     .findMatching(RoleBo.class, criteria);
180             if (dataSourceNameInDatabaseroleName != null && dataSourceNameInDatabaseroleName.size() > 0) {
181                 roleBo = dataSourceNameInDatabaseroleName.get(0);
182                 accessActivationWorkFlow.setRoleName(roleBo.getName());
183                 validRole = true;
184             } else {
185                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLE_ACCESS_ACTIVATION, OLEConstants.ERROR_INVALID_ID);
186                 validRole = false;
187             }
188         }
189 
190 
191         return validRole ;
192     }
193 
194 
195     private boolean validatePerson(OLEAccessActivationWorkFlow accessActivationWorkFlow){
196         boolean validPerson = false;
197         if(StringUtils.isNotBlank(accessActivationWorkFlow.getPersonId()) && StringUtils.isNotBlank(accessActivationWorkFlow.getPersonName())){
198             Map<String,String> criteriaMap = new HashMap<String,String>();
199             criteriaMap.put("principalId",accessActivationWorkFlow.getPersonId());
200             criteriaMap.put("principalName",accessActivationWorkFlow.getPersonName());
201             List<Person> personList = getPersonService().findPeople(criteriaMap);
202             if(personList.size()>0){
203                 validPerson = true;
204             }else {
205                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLE_ACCESS_ACTIVATION, OLEConstants.ERROR_INVALID_PERSON_ID_NAME);
206                 validPerson = false;
207             }
208         } else if(StringUtils.isNotBlank(accessActivationWorkFlow.getPersonId()) && StringUtils.isBlank(accessActivationWorkFlow.getPersonName())){
209             Person person = getPersonService().getPerson(accessActivationWorkFlow.getPersonId());
210             if(person!=null){
211                 validPerson =true;
212                 accessActivationWorkFlow.setPersonName(person.getName());
213             }else{
214                 validPerson = false;
215                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLE_ACCESS_ACTIVATION, OLEConstants.ERROR_INVALID_PERSON_ID);
216             }
217 
218         }
219         else if(StringUtils.isBlank(accessActivationWorkFlow.getPersonId()) && StringUtils.isNotBlank(accessActivationWorkFlow.getPersonName())){
220             Person person = getPersonService().getPersonByPrincipalName(accessActivationWorkFlow.getPersonName());
221             if(person!=null){
222                 validPerson =true;
223                 accessActivationWorkFlow.setPersonId(person.getPrincipalId());
224             }else{
225                 validPerson = false;
226                 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLE_ACCESS_ACTIVATION, OLEConstants.ERROR_INVALID_PERSON_NAME);
227             }
228 
229         }
230         if(StringUtils.isBlank(accessActivationWorkFlow.getPersonId()) && StringUtils.isBlank(accessActivationWorkFlow.getPersonName())){
231             accessActivationWorkFlow.setPersonName(null);
232             validPerson=true;
233         }
234         return validPerson;
235     }
236 
237 
238 
239 
240 
241 }