View Javadoc

1   /*
2    * Copyright 2007-2009 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.kns.rules;
17  
18  import java.util.HashMap;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.kuali.rice.kim.bo.impl.KimAttributes;
23  import org.kuali.rice.kim.bo.types.dto.AttributeSet;
24  import org.kuali.rice.kim.service.KIMServiceLocator;
25  import org.kuali.rice.kim.util.KimConstants;
26  import org.kuali.rice.kns.bo.Parameter;
27  import org.kuali.rice.kns.bo.ParameterDetailType;
28  import org.kuali.rice.kns.datadictionary.DataDictionaryException;
29  import org.kuali.rice.kns.document.MaintenanceDocument;
30  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
31  import org.kuali.rice.kns.service.KNSServiceLocator;
32  import org.kuali.rice.kns.util.GlobalVariables;
33  import org.kuali.rice.kns.util.KNSConstants;
34  import org.kuali.rice.kns.util.ObjectUtils;
35  import org.kuali.rice.kns.util.RiceKeyConstants;
36  
37  /**
38   * This is a description of what this class does - kellerj don't forget to fill
39   * this in.
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  public class ParameterRule extends MaintenanceDocumentRuleBase {
44  
45  	/**
46  	 * This overridden method ...
47  	 *
48  	 * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
49  	 */
50  	@Override
51  	protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
52  		boolean result = super.processCustomRouteDocumentBusinessRules( document );
53  
54  		result &= checkAllowsMaintenanceEdit( document.getDocumentHeader().getWorkflowDocument()
55  				.getRouteHeader().getInitiatorPrincipalId(), (Parameter)getNewBo() );
56  
57  		result &= checkComponent((Parameter) getNewBo());
58  		
59  		return result;
60  	}
61  
62  	protected boolean checkAllowsMaintenanceEdit(String initiatorPrincipalId, Parameter newBO) {
63  
64  		 boolean allowsEdit = false;
65  	        Parameter parm = (Parameter)newBO;
66  	        
67  	        AttributeSet permissionDetails = new AttributeSet();
68  	        permissionDetails.put(KimAttributes.NAMESPACE_CODE, parm.getParameterNamespaceCode());
69  	        permissionDetails.put(KimAttributes.COMPONENT_NAME, parm.getParameterDetailTypeCode());
70  	        permissionDetails.put(KimAttributes.PARAMETER_NAME, parm.getParameterName());
71  	        allowsEdit = KIMServiceLocator.getIdentityManagementService().isAuthorizedByTemplateName(
72  	        				GlobalVariables.getUserSession().getPerson().getPrincipalId(),
73  	        				KNSConstants.KNS_NAMESPACE,
74  	        				KimConstants.PermissionTemplateNames.MAINTAIN_SYSTEM_PARAMETER,
75  	        				permissionDetails, null);
76  	        if(!allowsEdit){
77  	        	putGlobalError(RiceKeyConstants.AUTHORIZATION_ERROR_PARAMETER);
78  	        }
79  	        return allowsEdit;
80  	}
81  
82      public boolean checkComponent(Parameter param) {
83          String component = param.getParameterDetailTypeCode();
84          String namespace = param.getParameterNamespaceCode();
85          boolean result = false;
86  
87          try {
88              List<ParameterDetailType> dataDictionaryAndSpringComponents = KNSServiceLocator.getParameterServerService().getNonDatabaseComponents();
89              for (ParameterDetailType pdt : dataDictionaryAndSpringComponents) {
90                  if (pdt.getParameterNamespaceCode().equals(namespace) && pdt.getParameterDetailTypeCode().equals(component)) {
91                      result = true;
92                      break;
93                  }
94              }
95  
96              if (!result) {
97                  Map<String, String> primaryKeys = new HashMap<String, String>(2);
98                  primaryKeys.put("parameterNamespaceCode", namespace);
99                  primaryKeys.put("parameterDetailTypeCode", component);
100                 result = ObjectUtils.isNotNull(getBoService().findByPrimaryKey(ParameterDetailType.class, primaryKeys));
101             }
102 
103             if (!result) {
104                 putFieldError("parameterDetailTypeCode", "error.document.parameter.detailType.invalid", component);
105             }
106 
107             return result;
108         }
109         catch (DataDictionaryException ex) {
110             throw new RuntimeException("Problem parsing data dictionary during full load required for rule validation: " + ex.getMessage(), ex);
111         }
112     }
113 }
114