View Javadoc

1   /**
2    * Copyright 2005-2012 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.krad.service.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.core.api.config.ConfigurationException;
21  import org.kuali.rice.core.api.config.module.RunMode;
22  import org.kuali.rice.core.api.config.property.ConfigContext;
23  import org.kuali.rice.coreservice.framework.CoreFrameworkServiceLocator;
24  import org.kuali.rice.coreservice.framework.parameter.ParameterConstants;
25  import org.kuali.rice.kns.service.BusinessObjectDictionaryService;
26  import org.kuali.rice.kns.service.KNSServiceLocator;
27  import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
28  import org.kuali.rice.krad.service.BusinessObjectService;
29  import org.kuali.rice.krad.service.KRADServiceLocator;
30  import org.kuali.rice.krad.service.ModuleService;
31  import org.kuali.rice.krad.util.KRADConstants;
32  import org.kuali.rice.krad.util.UrlFactory;
33  
34  import java.util.List;
35  import java.util.Map;
36  import java.util.Properties;
37  
38  /**
39   * This class implements ModuleService interface.
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  public class ModuleServiceBase extends RemoteModuleServiceBase implements ModuleService {
44  
45      protected static final Logger LOG = Logger.getLogger(ModuleServiceBase.class);
46  
47      protected BusinessObjectService businessObjectService;
48      protected BusinessObjectDictionaryService businessObjectDictionaryService;
49  
50  
51      /**
52       * @see org.kuali.rice.krad.service.ModuleService#getExternalizableBusinessObject(java.lang.Class, java.util.Map)
53       */
54      public <T extends ExternalizableBusinessObject> T getExternalizableBusinessObject(Class<T> businessObjectClass,
55              Map<String, Object> fieldValues) {
56          Class<? extends ExternalizableBusinessObject> implementationClass =
57                  getExternalizableBusinessObjectImplementation(businessObjectClass);
58          ExternalizableBusinessObject businessObject =
59                  (ExternalizableBusinessObject) getBusinessObjectService().findByPrimaryKey(implementationClass,
60                          fieldValues);
61          return (T) businessObject;
62      }
63  
64      /**
65       * @see org.kuali.rice.krad.service.ModuleService#getExternalizableBusinessObject(java.lang.Class, java.util.Map)
66       */
67      public <T extends ExternalizableBusinessObject> List<T> getExternalizableBusinessObjectsList(
68              Class<T> externalizableBusinessObjectClass, Map<String, Object> fieldValues) {
69          Class<? extends ExternalizableBusinessObject> implementationClass =
70                  getExternalizableBusinessObjectImplementation(externalizableBusinessObjectClass);
71          return (List<T>) getBusinessObjectService().findMatching(implementationClass, fieldValues);
72      }
73  
74  
75  
76      @Deprecated
77      public String getExternalizableBusinessObjectInquiryUrl(Class inquiryBusinessObjectClass,
78              Map<String, String[]> parameters) {
79          if (!isExternalizable(inquiryBusinessObjectClass)) {
80              return KRADConstants.EMPTY_STRING;
81          }
82          String businessObjectClassAttribute;
83  
84          Class implementationClass = getExternalizableBusinessObjectImplementation(inquiryBusinessObjectClass);
85          if (implementationClass == null) {
86              LOG.error("Can't find ExternalizableBusinessObject implementation class for " + inquiryBusinessObjectClass
87                      .getName());
88              throw new RuntimeException("Can't find ExternalizableBusinessObject implementation class for interface "
89                      + inquiryBusinessObjectClass.getName());
90          }
91          businessObjectClassAttribute = implementationClass.getName();
92          return UrlFactory.parameterizeUrl(getInquiryUrl(inquiryBusinessObjectClass), getUrlParameters(
93                  businessObjectClassAttribute, parameters));
94      }
95  
96      @Deprecated
97      @Override
98      protected String getInquiryUrl(Class inquiryBusinessObjectClass) {
99          
100         String riceBaseUrl = "";
101         String potentialUrlAddition = "";
102 
103         if (goToCentralRiceForInquiry()) {
104             riceBaseUrl = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(KRADConstants.KUALI_RICE_URL_KEY); 
105         } else {
106             riceBaseUrl = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(KRADConstants.APPLICATION_URL_KEY);
107             potentialUrlAddition = "kr/";
108         }
109         
110         String inquiryUrl = riceBaseUrl;
111         if (!inquiryUrl.endsWith("/")) {
112             inquiryUrl = inquiryUrl + "/";
113         }
114         return inquiryUrl + potentialUrlAddition + KRADConstants.INQUIRY_ACTION;
115     }
116 
117     @Override
118     public boolean isExternalizableBusinessObjectLookupable(Class boClass) {
119         return getBusinessObjectDictionaryService().isLookupable(boClass);
120     }
121 
122     @Override
123     public boolean isExternalizableBusinessObjectInquirable(Class boClass) {
124         return getBusinessObjectDictionaryService().isInquirable(boClass);
125     }
126 
127     /**
128      * This overridden method ...
129      *
130      * @see org.kuali.rice.krad.service.ModuleService#getExternalizableBusinessObjectLookupUrl(java.lang.Class,
131      *      java.util.Map)
132      */
133     @Deprecated
134     @Override
135     public String getExternalizableBusinessObjectLookupUrl(Class inquiryBusinessObjectClass,
136             Map<String, String> parameters) {
137         Properties urlParameters = new Properties();
138 
139         String riceBaseUrl = "";
140         String potentialUrlAddition = "";
141 
142         if (goToCentralRiceForInquiry()) {
143             riceBaseUrl = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(KRADConstants.KUALI_RICE_URL_KEY);
144         } else {
145             riceBaseUrl = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(KRADConstants.APPLICATION_URL_KEY);
146             potentialUrlAddition = "kr/";
147         }
148         
149         String lookupUrl = riceBaseUrl;
150         if (!lookupUrl.endsWith("/")) {
151             lookupUrl = lookupUrl + "/";
152         }
153         
154         if (parameters.containsKey(KRADConstants.MULTIPLE_VALUE)) {
155             lookupUrl = lookupUrl + potentialUrlAddition + KRADConstants.MULTIPLE_VALUE_LOOKUP_ACTION;
156         }
157         else {
158             lookupUrl = lookupUrl + potentialUrlAddition + KRADConstants.LOOKUP_ACTION;
159         }
160            
161         for (String paramName : parameters.keySet()) {
162             urlParameters.put(paramName, parameters.get(paramName));
163         }
164 
165         /*Class clazz;
166         if (inquiryBusinessObjectClass.getClass().isInterface()) {*/
167         Class clazz = getExternalizableBusinessObjectImplementation(inquiryBusinessObjectClass);
168         /*} else {
169             clazz = inquiryBusinessObjectClass;
170         }*/
171         urlParameters.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, clazz == null ? "" : clazz.getName());
172 
173         return UrlFactory.parameterizeUrl(lookupUrl, urlParameters);
174     }
175 
176     protected BusinessObjectDictionaryService getBusinessObjectDictionaryService() {
177         if (businessObjectDictionaryService == null) {
178             businessObjectDictionaryService = KNSServiceLocator.getBusinessObjectDictionaryService();
179         }
180         return businessObjectDictionaryService;
181     }
182 
183     /**
184      * @return the businessObjectService
185      */
186     protected BusinessObjectService getBusinessObjectService() {
187         if (businessObjectService == null) {
188             businessObjectService = KRADServiceLocator.getBusinessObjectService();
189         }
190         return businessObjectService;
191     }
192 
193     public boolean goToCentralRiceForInquiry() { 
194         return false;
195     }
196         
197     protected RunMode getRunMode(String module) {
198         String propertyName = module + ".mode";
199         String runMode = ConfigContext.getCurrentContextConfig().getProperty(propertyName);
200         if (StringUtils.isBlank(runMode)) {
201             throw new ConfigurationException("Failed to determine run mode for module '" + module + "'.  Please be sure to set configuration parameter '" + propertyName + "'");
202         }
203         return RunMode.valueOf(runMode.toUpperCase());
204     }
205 
206 }
207