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.maintenance.MaintainableImpl;
22  import org.kuali.rice.krad.util.GlobalVariables;
23  import org.kuali.rice.krad.util.ObjectUtils;
24  import org.kuali.student.common.uif.service.KSMaintainable;
25  import org.kuali.student.r2.common.dto.AttributeInfo;
26  import org.kuali.student.r2.common.dto.ContextInfo;
27  import org.kuali.student.r2.common.exceptions.*;
28  import org.kuali.student.common.util.security.ContextUtils;
29  import org.kuali.student.r2.core.class1.state.dto.StateInfo;
30  import org.kuali.student.r2.core.class1.state.service.StateService;
31  import org.kuali.student.r2.core.class1.type.dto.TypeInfo;
32  import org.kuali.student.r2.core.class1.type.service.TypeService;
33  import org.kuali.student.r2.core.constants.StateServiceConstants;
34  import org.kuali.student.r2.core.constants.TypeServiceConstants;
35  
36  import javax.xml.namespace.QName;
37  import java.util.HashMap;
38  import java.util.Map;
39  
40  /**
41   * Default implementation of the <code>KSMaintainable</code> interface
42   *
43   * @author Kuali Student Team
44   */
45  public class KSMaintainableImpl extends MaintainableImpl implements KSMaintainable {
46      private static final long serialVersionUID = 1L;
47      private final static Logger LOG = Logger.getLogger(KSMaintainableImpl.class);
48  
49      private transient Map<String,Class> helperClasses;
50      private transient Map<String,Object> helpers;
51  
52      public KSMaintainableImpl(){
53          super();
54          helperClasses = new HashMap<String, Class>();
55          helpers = new HashMap<String, Object>();
56      }
57  
58      /**
59       * This method creates a new context, which is needed for the service call.  This is to create
60       * <code>ContextInfo</code> with the following information - current logged in user, current local
61       * date and locale information
62       *
63       * @return a new ContextInfo
64       */
65      public ContextInfo createContextInfo(){
66          return ContextUtils.createDefaultContextInfo();
67      }
68  
69      /**
70       * This method gets the <code>StateInfo</code> for a given state key
71       *
72       * @param stateKey state key
73       * @return StateInfo for the given state key
74       */
75      public StateInfo getStateInfo(String stateKey){
76          StateService stateService = GlobalResourceLoader.getService(new QName(StateServiceConstants.NAMESPACE, StateServiceConstants.SERVICE_NAME_LOCAL_PART));
77          try {
78              return stateService.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 type key
89       * @return TypeInfo for the given type key
90       */
91      public TypeInfo getTypeInfo(String typeKey){
92          TypeService typeService = GlobalResourceLoader.getService(new QName(TypeServiceConstants.NAMESPACE, TypeServiceConstants.SERVICE_NAME_LOCAL_PART));
93          try {
94              return typeService.getType(typeKey, createContextInfo());
95          }catch (Exception e){
96              throw convertServiceExceptionsToUI(e);
97          }
98      }
99  
100     /**
101      * This is a helper method which handles all the services exceptions. This can be the central point to handle all
102      * the services exceptions. For now, we're converting all the exceptions to Runtime exceptions.
103      *
104      * Once we implement authz and when services fully implemented the decorators/exceptions, we need to
105      * refactor to handle some of the exceptions with HandlerExceptionResolver (Note, underlying exception is passed to
106      * the runtime exception as cause... So, this can be accessed at the exception resolver to determine the action)
107      *
108      * @param ex service exception to be handled
109      * @return service exception wrapped in a RuntimeException instance
110      */
111     public RuntimeException convertServiceExceptionsToUI(Exception ex){
112 
113         if (LOG.isEnabledFor(Level.ERROR)){
114             LOG.error(ex);
115         }
116          //Commented for now... once service throws MPE/IPE(KSENROLL-3446), needs to rework.
117         /*StringBuilder refinedErrorMessage = new StringBuilder();
118         refinedErrorMessage.append(ex.getMessage());
119         refinedErrorMessage.append("[Caused at ");
120         refinedErrorMessage.append(ex.getStackTrace()[0].getClass().getSimpleName() + ".");
121         refinedErrorMessage.append(ex.getStackTrace()[0].getMethodName() + "()");
122         refinedErrorMessage.append(ex.getStackTrace()[0].getLineNumber() + "]");*/
123 
124         if (ex instanceof DoesNotExistException){
125             return new RuntimeException("Does Not Exists - " + ex.getMessage(),ex);
126         } else if (ex instanceof InvalidParameterException){
127             return new RuntimeException("Invalid parameter - " + ex.getMessage(),ex);
128         } else if (ex instanceof MissingParameterException){
129             return new RuntimeException("Missing parameter - " + ex.getMessage(),ex);
130         } else if (ex instanceof OperationFailedException){
131             return new RuntimeException("Operation Failed - " + ex.getMessage(),ex);
132         } else if (ex instanceof PermissionDeniedException){
133             return new RuntimeException("Permission Denied - " + ex.getMessage(),ex);
134         } else if (ex instanceof DataValidationErrorException){
135             return new RuntimeException("Data Validation Error - " + ex.getMessage(), ex);
136         } else {
137             return new RuntimeException(ex.getMessage(),ex);
138         }
139     }
140 
141     /**
142      * This is a map which holds all the helper classes by name. This can be used from the view xml file like
143      *
144      * <code>
145      *     <property name="viewHelperService.helperClasses">
146      *          <map>
147      *			<entry key="publishHelper">
148      *              <value  type="java.lang.Class">org.kuali.student.enrollment.class2.courseofferingset.service.impl.CourseOfferingSetPublishingHelper</value>
149      *              </entry>
150      * 		    </map>
151      *      </property>
152      * </code>
153      *
154      * And from the helper class, we can call like
155      *
156      * <code>
157      *     CourseOfferingSetPublishingHelper mpeHelper = (CourseOfferingSetPublishingHelper)getHelper("publishHelper");
158      * </code>
159      *
160      * @param helperClasses
161      */
162     public void setHelperClasses(Map<String, Class> helperClasses) {
163         this.helperClasses = helperClasses;
164     }
165 
166     /**
167      * This creates an instance of the helper class if it's already not there.
168      *
169      * @param helperName
170      * @return
171      */
172     public Object getHelper(String helperName) {
173         Object helper = helpers.get(helperName);
174         if (helper == null){
175             Class clazz = helperClasses.get(helperName);
176             if (clazz != null){
177                 helper = ObjectUtils.newInstance(clazz);
178                 helpers.put(helperName,helper);
179             }
180         }
181         return helper;
182     }
183 
184 }