View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.coreservice.web.parameter;
17  
18  import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
19  import org.kuali.rice.coreservice.api.component.Component;
20  import org.kuali.rice.core.api.util.RiceKeyConstants;
21  import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
22  import org.kuali.rice.kim.api.KimConstants;
23  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
24  import org.kuali.rice.kns.document.MaintenanceDocument;
25  
26  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
27  import org.kuali.rice.krad.util.GlobalVariables;
28  import org.kuali.rice.krad.util.KRADConstants;
29  import org.apache.commons.lang.StringUtils;
30  import java.util.Collections;
31  import java.util.HashMap;
32  import java.util.Map;
33  
34  /**
35   * This is a description of what this class does - kellerj don't forget to fill
36   * this in.
37   *
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  public class ParameterRule extends MaintenanceDocumentRuleBase {
41  
42      /**
43       * This overridden method ...
44       *
45       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
46       */
47      @Override
48  	protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
49  		boolean result = super.processCustomRouteDocumentBusinessRules( document );
50  
51  		result &= checkAllowsMaintenanceEdit( document.getDocumentHeader().getWorkflowDocument()
52  				.getInitiatorPrincipalId(), (ParameterBo) document.getNewMaintainableObject().getDataObject() );
53  
54  		result &= checkComponent((ParameterBo) document.getNewMaintainableObject().getDataObject());
55  
56  
57  
58  		return result;
59  	}
60  
61  	protected boolean checkAllowsMaintenanceEdit(String initiatorPrincipalId, ParameterBo newBO) {
62  
63  		 boolean allowsEdit = false;
64  	        ParameterBo parm = newBO;
65  	        
66  	        Map<String, String> permissionDetails = new HashMap<String, String>();
67  	        permissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, parm.getNamespaceCode());
68  	        permissionDetails.put(KimConstants.AttributeConstants.COMPONENT_NAME, parm.getComponentCode());
69  	        permissionDetails.put(KimConstants.AttributeConstants.PARAMETER_NAME, parm.getName());
70  	        allowsEdit = KimApiServiceLocator.getPermissionService().isAuthorizedByTemplate(
71                      GlobalVariables.getUserSession().getPerson().getPrincipalId(), KRADConstants.KNS_NAMESPACE,
72                      KimConstants.PermissionTemplateNames.MAINTAIN_SYSTEM_PARAMETER, permissionDetails,
73                      Collections.<String, String>emptyMap());
74  	        if(!allowsEdit){
75  	        	putGlobalError(RiceKeyConstants.AUTHORIZATION_ERROR_PARAMETER);
76  	        }
77  	        return allowsEdit;
78  	}
79  
80      public boolean checkComponent(ParameterBo param) {
81          String componentCode = param.getComponentCode();
82          String namespace = param.getNamespaceCode();
83          boolean result = false;
84          if(StringUtils.isNotBlank(componentCode) && StringUtils.isNotBlank(namespace)){
85              Component component = CoreServiceApiServiceLocator.getComponentService().getComponentByCode(namespace, componentCode);
86              if (component != null) {
87                  result = true;
88              }
89              if (!result) {
90                  putFieldError("componentCode", "error.document.parameter.detailType.invalid", componentCode);
91              }
92          }
93          return result;
94      }
95  
96  }