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.web.form;
17  
18  import org.kuali.rice.core.api.CoreApiServiceLocator;
19  import org.kuali.rice.krad.uif.UifConstants.ViewType;
20  
21  import java.io.PrintWriter;
22  import java.io.StringWriter;
23  
24  /**
25   * Form class for incident reports
26   *
27   * @author Kuali Rice Team (rice.collab@kuali.org)
28   */
29  public class IncidentReportForm extends UifFormBase {
30      private static final long serialVersionUID = -6677581167041430694L;
31  
32      protected String errorMessage = "The system has encountered an error and is unable to complete your request at this time. Please provide more information regarding this error by completing this Incident Report.";
33  
34      protected Exception exception;
35      protected String exceptionMessage;
36      protected String exceptionStackTrace;
37  
38      protected String userInput;
39      protected String incidentDocId;
40      protected String incidentViewId;
41      protected String controller;
42      protected String userName;
43      protected String userId;
44      protected String userEmail;
45  
46      protected boolean devMode;
47  
48      public IncidentReportForm() {
49          super();
50  
51          setViewTypeName(ViewType.INCIDENT);
52      }
53  
54      /**
55       * Creates the email message from the exception, form and user data.
56       *
57       * @return the email message
58       */
59      public String createEmailMessage() {
60          String format = "Document Id: %s%n" + "View Id: %s%n" + "Handler: %s%n%n" + "User Email: %s%n"
61                  + "Person User Identifier: %s%n" + "Person Name: %s%n" + "User Input: %s%n%n" + "errorMessage: %n"
62                  + "%s";
63          String message = String.format(format, (incidentDocId == null) ? "" : incidentDocId, (incidentViewId == null) ? "" : incidentViewId,
64                  (controller == null) ? "" : controller, (userEmail == null) ? "" : userEmail, (userId == null) ? ""
65                          : userId, (userName == null) ? "" : userName, (userInput == null) ? "" : userInput,
66                  (exceptionStackTrace == null) ? "" : exceptionStackTrace);
67  
68          return message;
69  
70      }
71  
72      /**
73       * Creates the email subject containing the mode, view id and the exception message.
74       *
75       * @return the email subject
76       */
77      public String createEmailSubject() {
78          String app = CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString("application.id");
79          String env = CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString("environment");
80          String format = "%s:%s:%s:%s";
81          String subject = String.format(format, app, env, (incidentViewId == null) ? "" : incidentViewId,
82                  truncateString(exceptionMessage, 180));
83          return subject;
84      }
85  
86      /**
87       * Truncate the string to specified length.
88       *
89       * @param str
90       *            the string to truncate
91       * @param maxLength
92       *            the max length
93       * @return the truncated string
94       */
95      protected String truncateString(String str, int maxLength) {
96          if (str != null && str.length() > maxLength)
97              str = str.substring(0, maxLength);
98          return str;
99      }
100 
101     /**
102      * Gets the stack trace from an exception.
103      *
104      * @param t
105      *            the throwable to get the stack trace from
106      * @return the stack trace
107      */
108     protected String getStackTrace(Throwable t) {
109         StringWriter sw = new StringWriter();
110         PrintWriter pw = new PrintWriter(sw, true);
111         t.printStackTrace(pw);
112         pw.flush();
113         sw.flush();
114         return sw.toString();
115     }
116 
117     /**
118      * @return the errorMessage
119      */
120     public String getErrorMessage() {
121         return this.errorMessage;
122     }
123 
124     /**
125      * @param errorMessage
126      *            the errorMessage to set
127      */
128     public void setErrorMessage(String errorMessage) {
129         this.errorMessage = errorMessage;
130     }
131 
132     /**
133      * @return the exceptionMessage
134      */
135     public String getExceptionMessage() {
136         return this.exceptionMessage;
137     }
138 
139     /**
140      * @param exceptionMessage
141      *            the exceptionMessage to set
142      */
143     public void setExceptionMessage(String exceptionMessage) {
144         this.exceptionMessage = exceptionMessage;
145     }
146 
147     /**
148      * @return the exceptionStackTrace
149      */
150     public String getExceptionStackTrace() {
151         return this.exceptionStackTrace;
152     }
153 
154     /**
155      * @param exceptionStackTrace
156      *            the exceptionStackTrace to set
157      */
158     public void setExceptionStackTrace(String exceptionStackTrace) {
159         this.exceptionStackTrace = exceptionStackTrace;
160     }
161 
162     /**
163      * @return the userInput
164      */
165     public String getUserInput() {
166         return this.userInput;
167     }
168 
169     /**
170      * @param userInput
171      *            the userInput to set
172      */
173     public void setUserInput(String userInput) {
174         this.userInput = userInput;
175     }
176 
177     /**
178      * @return the devMode
179      */
180     public boolean isDevMode() {
181         return this.devMode;
182     }
183 
184     /**
185      * @param devMode
186      *            the devMode to set
187      */
188     public void setDevMode(boolean devMode) {
189         this.devMode = devMode;
190     }
191 
192     /**
193      * @param incidentDocId
194      *            the incidentDocId to set
195      */
196     public void setIncidentDocId(String incidentDocId) {
197         this.incidentDocId = incidentDocId;
198     }
199 
200     /**
201      * @return the incidentDocId
202      */
203     public String getIncidentDocId() {
204         return incidentDocId;
205     }
206 
207     /**
208      * @param incidentViewId
209      *            the incidentViewId to set
210      */
211     public void setIncidentViewId(String incidentViewId) {
212         this.incidentViewId = incidentViewId;
213     }
214 
215     /**
216      * @return the incidentViewId
217      */
218     public String getIncidentViewId() {
219         return incidentViewId;
220     }
221 
222     /**
223      * @param exception
224      *            the exception to set
225      */
226     public void setException(Exception exception) {
227         this.exception = exception;
228         setExceptionStackTrace(getStackTrace(exception));
229         setExceptionMessage(exception.getMessage());
230     }
231 
232     /**
233      * @return the exception
234      */
235     public Exception getException() {
236         return exception;
237     }
238 
239     /**
240      * @param userName
241      *            the userName to set
242      */
243     public void setUserName(String userName) {
244         this.userName = userName;
245     }
246 
247     /**
248      * @return the userName
249      */
250     public String getUserName() {
251         return userName;
252     }
253 
254     /**
255      * @param userId
256      *            the userId to set
257      */
258     public void setUserId(String userId) {
259         this.userId = userId;
260     }
261 
262     /**
263      * @return the userId
264      */
265     public String getUserId() {
266         return userId;
267     }
268 
269     /**
270      * @param userEmail
271      *            the userEmail to set
272      */
273     public void setUserEmail(String userEmail) {
274         this.userEmail = userEmail;
275     }
276 
277     /**
278      * @return the userEmail
279      */
280     public String getUserEmail() {
281         return userEmail;
282     }
283 
284     /**
285      * @param controller
286      *            the controller to set
287      */
288     public void setController(String controller) {
289         this.controller = controller;
290     }
291 
292     /**
293      * @return the controller
294      */
295     public String getController() {
296         return controller;
297     }
298 
299     /**
300      * @param copiedSessionId
301      *            the session id to associate with the form
302      */
303 
304     public void setSessionId(String copiedSessionId) {
305         sessionId = copiedSessionId;
306     }
307 
308 }