View Javadoc

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