View Javadoc
1   /**
2    * Copyright 2005-2014 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         return KRADServiceLocatorWeb.getLegacyDataAdapter().hasLocalLookup(boClass);
110     }
111 
112     @Override
113     public boolean isExternalizableBusinessObjectInquirable(Class boClass) {
114         return KRADServiceLocatorWeb.getLegacyDataAdapter().hasLocalInquiry(boClass);
115     }
116 
117     /**
118      * This overridden method ...
119      *
120      * @see org.kuali.rice.krad.service.ModuleService#getExternalizableBusinessObjectLookupUrl(java.lang.Class,
121      *      java.util.Map)
122      */
123     @Deprecated
124     @Override
125     public String getExternalizableBusinessObjectLookupUrl(Class inquiryBusinessObjectClass,
126             Map<String, String> parameters) {
127         Properties urlParameters = new Properties();
128 
129         String riceBaseUrl = "";
130         String potentialUrlAddition = "";
131 
132         if (goToCentralRiceForInquiry()) {
133             riceBaseUrl = CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString(KRADConstants.KUALI_RICE_URL_KEY);
134         } else {
135             riceBaseUrl = CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString(KRADConstants.APPLICATION_URL_KEY);
136             potentialUrlAddition = "kr/";
137         }
138         
139         String lookupUrl = riceBaseUrl;
140         if (!lookupUrl.endsWith("/")) {
141             lookupUrl = lookupUrl + "/";
142         }
143         
144         if (parameters.containsKey(KRADConstants.MULTIPLE_VALUE)) {
145             lookupUrl = lookupUrl + potentialUrlAddition + KRADConstants.MULTIPLE_VALUE_LOOKUP_ACTION;
146         }
147         else {
148             lookupUrl = lookupUrl + potentialUrlAddition + KRADConstants.LOOKUP_ACTION;
149         }
150            
151         for (String paramName : parameters.keySet()) {
152             urlParameters.put(paramName, parameters.get(paramName));
153         }
154 
155         /*Class clazz;
156         if (inquiryBusinessObjectClass.getClass().isInterface()) {*/
157         Class clazz = getExternalizableBusinessObjectImplementation(inquiryBusinessObjectClass);
158         /*} else {
159             clazz = inquiryBusinessObjectClass;
160         }*/
161         urlParameters.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, clazz == null ? "" : clazz.getName());
162 
163         return UrlFactory.parameterizeUrl(lookupUrl, urlParameters);
164     }
165 
166     public boolean goToCentralRiceForInquiry() {
167         return false;
168     }
169 
170     /**
171      * Returns the base URL to use for lookup requests to objects within the module
172      *
173      * @return String base lookup URL
174      */
175     @Override
176     protected String getBaseLookupUrl() {
177         if (goToCentralRiceForInquiry()) {
178             return BaseLookupUrlsHolder.remoteKradBaseLookupUrl;
179         } else {
180             return BaseLookupUrlsHolder.localKradBaseLookupUrl;
181         }
182     }
183 
184     protected RunMode getRunMode(String module) {
185         String propertyName = module + ".mode";
186         String runMode = ConfigContext.getCurrentContextConfig().getProperty(propertyName);
187         if (StringUtils.isBlank(runMode)) {
188             throw new ConfigurationException("Failed to determine run mode for module '" + module + "'.  Please be sure to set configuration parameter '" + propertyName + "'");
189         }
190         return RunMode.valueOf(runMode.toUpperCase());
191     }
192 }
193