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.service.impl;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.apache.log4j.Logger;
27  import org.kuali.rice.kns.bo.Parameter;
28  import org.kuali.rice.kns.bo.ParameterDetailType;
29  import org.kuali.rice.kns.service.BusinessObjectService;
30  import org.kuali.rice.kns.service.KNSServiceLocator;
31  import org.kuali.rice.kns.service.LookupService;
32  import org.kuali.rice.kns.service.ParameterServerService;
33  import org.kuali.rice.kns.util.KNSConstants;
34  
35  /**
36   * See ParameterService. The componentClass must be the business object, document, or step class that the parameter is associated
37   * with. Implementations of this class know how to translate that to a namespace (for ParameterService Impl, determine what module
38   * the Class is associated with by parsing the package) and detail type (for ParameterServiceImpl, document Class --> use simple
39   * class name minus the word Document / business object Class --> use simple class name, batch step class --> use the simple class
40   * name). In cases where the parameter is applicable to all documents, all lookups, all batch steps, or all components in a
41   * particular module, you should pass in the appropriate constant class in KfsParameterConstants for the component Class (e.g. all
42   * purchasing documents = PURCHASING_DOCUMENT.class, all purchasing lookups = PURCHASING_LOOKUP.class, all purchasing batch steps =
43   * PURCHASING_BATCH.class, and all purchasing components = PURCHASING_ALL.class). In addition, certain methods take
44   * constrainingValue and constrainedValue Strings. The constrainedValue is the value that you want to compare to the Parameter
45   * value, and the constrainingValue is used for complex parameters that limit one field value based on the value of another field,
46   * e.g VALID_OBJECT_LEVELS_BY_OBJECT_TYPE.
47   */
48  public class ParameterServiceImpl extends ParameterServiceBase implements ParameterServerService {
49  	protected BusinessObjectService businessObjectService;
50  	protected LookupService lookupService;
51  	
52  	public Parameter retrieveParameter(String namespaceCode, String detailTypeCode, String parameterName) {
53  	    String applicationNamespace = KNSServiceLocator.getKualiConfigurationService().getPropertyString(KNSConstants.APPLICATION_CODE);
54  	    if (StringUtils.isEmpty(applicationNamespace)) {
55  	        applicationNamespace = KNSConstants.DEFAULT_APPLICATION_CODE;
56  	    }
57  	    Parameter parameter = fetchFromCache(namespaceCode, detailTypeCode, parameterName);
58          if (parameter != null) {
59              return parameter;
60          }
61  	    HashMap<String, String> crit = new HashMap<String, String>(3);
62  	    crit.put("parameterNamespaceCode", namespaceCode);
63  	    crit.put("parameterDetailTypeCode", detailTypeCode);
64  	    crit.put("parameterName", parameterName);
65  	    //crit.put("parameterApplicationNamespaceCode", applicationNamespace);
66  	    
67  	    List<Parameter> parameters = (List<Parameter>)getBusinessObjectService().findMatching(Parameter.class, crit);
68  	    Parameter parameterDefault = null;
69  	    for (Parameter parm : parameters) {
70  	        if (StringUtils.equals(applicationNamespace, parm.getParameterApplicationNamespaceCode())) {
71  	            parameter = parm;
72  	            break;
73  	        } else if (StringUtils.equals(KNSConstants.DEFAULT_APPLICATION_CODE, parm.getParameterApplicationNamespaceCode())) {
74  	            parameterDefault = parm;
75  	        }
76  	    }
77  
78  	    if (parameter == null) {
79  	        parameter = parameterDefault;
80  	    }
81  	    
82  	    insertIntoCache(parameter); 
83  	    //if (parameter != null 
84  	    //        && StringUtils.equals(KNSConstants.DEFAULT_APPLICATION_CODE, parameter.getParameterApplicationNamespaceCode())
85  	    //        && !StringUtils.equals(KNSConstants.DEFAULT_APPLICATION_CODE, applicationNamespace)) {
86  	    //    insertIntoCache(parameter, applicationNamespace); 
87  	    //}
88  	    return parameter;
89  	}
90      
91     /**
92      * This method can be used to retrieve a list of parameters that
93      * match the given fieldValues criteria. You could also specify the "like"
94      * criteria in the Map.
95      * 
96      * @param   fieldValues The Map containing the key value pairs to be used 
97      *                      to build the criteria.
98      * @return  List of Parameters that match the criteria.
99      */
100 	@SuppressWarnings("unchecked")
101 	public List<Parameter> retrieveParametersGivenLookupCriteria(Map<String, String> fieldValues) {
102 		Collection<Parameter> results = getLookupService().findCollectionBySearch(Parameter.class, fieldValues);
103 		return new ArrayList<Parameter>( results );
104     }    
105 	
106 	public List<ParameterDetailType> getNonDatabaseComponents() {
107 		return KNSServiceLocator.getRiceApplicationConfigurationMediationService().getNonDatabaseComponents();
108 	}
109 	
110 	@SuppressWarnings("unchecked")
111 	public void setParameterForTesting(Class componentClass, String parameterName, String parameterText) {
112 	    Parameter parameter = (Parameter) getParameter(componentClass, parameterName);
113 	    parameter.setParameterValue(parameterText);
114 	    getBusinessObjectService().save(parameter);
115 	} 
116 	
117 	public void setBusinessObjectService(BusinessObjectService businessObjectService) {
118 	    this.businessObjectService = businessObjectService;
119 	}
120 
121 	protected LookupService getLookupService() {
122 		if ( lookupService == null ) {
123 			lookupService = KNSServiceLocator.getLookupService();
124 		}
125 		return lookupService;
126 	}
127 
128 	protected BusinessObjectService getBusinessObjectService() {
129 		if ( businessObjectService == null ) {
130 			businessObjectService = KNSServiceLocator.getBusinessObjectService();
131 		}
132 		return this.businessObjectService;
133 	}	
134 }