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