View Javadoc

1   /**
2    * Copyright 2012 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   */
16  package org.kuali.student.common.uif.service.impl;
17  
18  import org.apache.log4j.Level;
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21  import org.kuali.rice.krad.uif.service.impl.ViewHelperServiceImpl;
22  import org.kuali.rice.krad.util.ObjectUtils;
23  import org.kuali.student.common.uif.service.KSViewHelperService;
24  import org.kuali.student.r2.common.dto.ContextInfo;
25  import org.kuali.student.r2.common.exceptions.*;
26  import org.kuali.student.r2.common.util.ContextUtils;
27  import org.kuali.student.r2.core.class1.state.dto.StateInfo;
28  import org.kuali.student.r2.core.class1.state.service.StateService;
29  import org.kuali.student.r2.core.class1.type.dto.TypeInfo;
30  import org.kuali.student.r2.core.class1.type.service.TypeService;
31  import org.kuali.student.r2.core.constants.StateServiceConstants;
32  import org.kuali.student.r2.core.constants.TypeServiceConstants;
33  
34  import javax.xml.namespace.QName;
35  import java.util.HashMap;
36  import java.util.Map;
37  
38  /**
39   * Base class for the KS View helper service.
40   *
41   * @author Kuali Student Team
42   */
43  public class KSViewHelperServiceImpl extends ViewHelperServiceImpl implements KSViewHelperService {
44      private static final long serialVersionUID = 1L;
45      private final static Logger LOG = Logger.getLogger(KSViewHelperServiceImpl.class);
46  
47      private transient StateService stateService;
48      private transient TypeService typeService;
49  
50      private transient Map<String,Class> helperClasses;
51      private transient Map<String,Object> helpers;
52  
53      public KSViewHelperServiceImpl(){
54          super();
55          helperClasses = new HashMap<String, Class>();
56          helpers = new HashMap<String, Object>();
57      }
58  
59      /**
60       * This method creates a new context, which is needed for the service call.  This is to create
61       * <code>ContextInfo</code> with the following information - current logged in user, current local
62       * date and locale information
63       *
64       * @return a new ContextInfo
65       */
66      public ContextInfo createContextInfo(){
67          return ContextUtils.createDefaultContextInfo();
68      }
69  
70      /**
71       * This method gets the <code>StateInfo</code> for a given state key
72       *
73       * @param stateKey
74       * @return StateInfo for the given state key
75       */
76      public StateInfo getStateInfo(String stateKey){
77          try {
78              return getStateService().getState(stateKey, createContextInfo());
79          }catch (Exception e){
80              throw convertServiceExceptionsToUI(e);
81          }
82  
83      }
84  
85      /**
86       * This method gets the <code>TypeInfo</code> for a given type key
87       *
88       * @param typeKey
89       * @return TypeInfo for the given type key
90       */
91      public TypeInfo getTypeInfo(String typeKey){
92          try {
93              return getTypeService().getType(typeKey, createContextInfo());
94          }catch (Exception e){
95              throw convertServiceExceptionsToUI(e);
96          }
97      }
98  
99      /**
100      * This is a helper method which handles all the services exceptions. This can be the central point to handle all
101      * the services exceptions. For now, we're converting all the exceptions to Runtime exceptions.
102      *
103      * Once we implement authz and when services fully implemented the decorators/exceptions, we need to
104      * refactor to handle some of the exceptions with HandlerExceptionResolver (Note, underlying exception is passed to
105      * the runtime exception as cause... So, this can be accessed at the exception resolver to determine the action)
106      *
107      * @param ex service exception to be handled
108      * @return service exception wrapped in a RuntimeException instance
109      */
110     public RuntimeException convertServiceExceptionsToUI(Exception ex){
111 
112         if (LOG.isEnabledFor(Level.ERROR)){
113             LOG.error(ex);
114         }
115          //Commented for now... once service throws MPE/IPE(KSENROLL-3446), needs to rework.
116         /*StringBuilder refinedErrorMessage = new StringBuilder();
117         refinedErrorMessage.append(ex.getMessage());
118         refinedErrorMessage.append("[Caused at ");
119         refinedErrorMessage.append(ex.getStackTrace()[0].getClass().getSimpleName() + ".");
120         refinedErrorMessage.append(ex.getStackTrace()[0].getMethodName() + "()");
121         refinedErrorMessage.append(ex.getStackTrace()[0].getLineNumber() + "]");*/
122 
123         if (ex instanceof DoesNotExistException){
124             return new RuntimeException("Does Not Exists - " + ex.getMessage(),ex);
125         } else if (ex instanceof InvalidParameterException){
126             return new RuntimeException("Invalid parameter - " + ex.getMessage(),ex);
127         } else if (ex instanceof MissingParameterException){
128             return new RuntimeException("Missing parameter - " + ex.getMessage(),ex);
129         } else if (ex instanceof OperationFailedException){
130             return new RuntimeException("Operation Failed - " + ex.getMessage(),ex);
131         } else if (ex instanceof PermissionDeniedException){
132             return new RuntimeException("Permission Denied - " + ex.getMessage(),ex);
133         } else {
134             return new RuntimeException(ex.getMessage(),ex);
135         }
136     }
137 
138     /**
139      * This is a map which holds all the helper classes by name. This can be used from the view xml file like
140      *
141      * <code>
142      *     <property name="viewHelperService.helperClasses">
143      *          <map>
144      *			<entry key="publishHelper">
145      *              <value  type="java.lang.Class">org.kuali.student.enrollment.class2.courseofferingset.service.impl.CourseOfferingSetPublishingHelper</value>
146      *              </entry>
147      * 		    </map>
148      *      </property>
149      * </code>
150      *
151      * And from the helper class, we can call like
152      *
153      * <code>
154      *     CourseOfferingSetPublishingHelper mpeHelper = (CourseOfferingSetPublishingHelper)getHelper("publishHelper");
155      * </code>
156      *
157      * @param helperClasses map of helper classes with name as keys and classes as values
158      */
159     public void setHelperClasses(Map<String, Class> helperClasses) {
160         this.helperClasses = helperClasses;
161     }
162 
163     /**
164      * This creates an instance of the helper class if it's already not there.
165      *
166      * @param helperName
167      * @return
168      */
169     public Object getHelper(String helperName) {
170         Object helper = helpers.get(helperName);
171         if (helper == null){
172             Class clazz = helperClasses.get(helperName);
173             if (clazz != null){
174                 helper = ObjectUtils.newInstance(clazz);
175                 helpers.put(helperName,helper);
176             }
177         }
178         return helper;
179     }
180 
181     protected StateService getStateService(){
182         if (stateService == null){
183             stateService = GlobalResourceLoader.getService(new QName(StateServiceConstants.NAMESPACE, StateServiceConstants.SERVICE_NAME_LOCAL_PART));
184         }
185         return stateService;
186     }
187 
188     protected TypeService getTypeService(){
189         if (typeService == null){
190             typeService = GlobalResourceLoader.getService(new QName(TypeServiceConstants.NAMESPACE, TypeServiceConstants.SERVICE_NAME_LOCAL_PART));
191         }
192         return typeService;
193     }
194 }