001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.web.form;
017
018import org.kuali.rice.krad.service.KRADServiceLocator;
019import org.kuali.rice.krad.uif.UifConstants;
020import org.kuali.rice.krad.uif.UifConstants.ViewType;
021
022import java.io.PrintWriter;
023import java.io.StringWriter;
024
025/**
026 * Form class for incident reports
027 * 
028 * @author Kuali Rice Team (rice.collab@kuali.org)
029 */
030public class IncidentReportForm extends UifFormBase {
031
032    private static final long serialVersionUID = -6677581167041430694L;
033
034    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.";
035
036    protected Exception exception;
037    protected String exceptionMessage;
038    protected String exceptionStackTrace;
039
040    protected String userInput;
041    protected String incidentDocId;
042    protected String incidentViewId;
043    protected String controller;
044    protected String userName;
045    protected String userId;
046    protected String userEmail;
047
048    protected boolean devMode;
049
050    public IncidentReportForm() {
051        super();
052        this.setRenderFullView(true);
053        setViewTypeName(ViewType.INCIDENT);
054    }
055
056    /**
057     * Creates the email message from the exception, form and user data.
058     * 
059     * @return the email message
060     */
061    public String createEmailMessage() {
062        String format = "Document Id: %s%n" + "View Id: %s%n" + "Handler: %s%n%n" + "User Email: %s%n"
063                + "Person User Identifier: %s%n" + "Person Name: %s%n" + "User Input: %s%n%n" + "errorMessage: %n"
064                + "%s";
065        String message = String.format(format, (incidentDocId == null) ? "" : incidentDocId, (incidentViewId == null) ? "" : incidentViewId,
066                (controller == null) ? "" : controller, (userEmail == null) ? "" : userEmail, (userId == null) ? ""
067                        : userId, (userName == null) ? "" : userName, (userInput == null) ? "" : userInput,
068                (exceptionStackTrace == null) ? "" : exceptionStackTrace);
069
070        return message;
071
072    }
073
074    /**
075     * Creates the email subject containing the mode, view id and the exception message.
076     * 
077     * @return the email subject
078     */
079    public String createEmailSubject() {
080        String app = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString("application.id");
081        String env = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString("environment");
082        String format = "%s:%s:%s:%s";
083        String subject = String.format(format, app, env, (incidentViewId == null) ? "" : incidentViewId,
084                truncateString(exceptionMessage, 180));
085        return subject;
086    }
087
088    /**
089     * Truncate the string to specified length.
090     * 
091     * @param str
092     *            the string to truncate
093     * @param maxLength
094     *            the max length
095     * @return the truncated string
096     */
097    protected String truncateString(String str, int maxLength) {
098        if (str != null && str.length() > maxLength)
099            str = str.substring(0, maxLength);
100        return str;
101    }
102
103    /**
104     * Gets the stack trace from an exception.
105     * 
106     * @param t
107     *            the throwable to get the stack trace from
108     * @return the stack trace
109     */
110    protected String getStackTrace(Throwable t) {
111        StringWriter sw = new StringWriter();
112        PrintWriter pw = new PrintWriter(sw, true);
113        t.printStackTrace(pw);
114        pw.flush();
115        sw.flush();
116        return sw.toString();
117    }
118
119    /**
120     * @return the errorMessage
121     */
122    public String getErrorMessage() {
123        return this.errorMessage;
124    }
125
126    /**
127     * @param errorMessage
128     *            the errorMessage to set
129     */
130    public void setErrorMessage(String errorMessage) {
131        this.errorMessage = errorMessage;
132    }
133
134    /**
135     * @return the exceptionMessage
136     */
137    public String getExceptionMessage() {
138        return this.exceptionMessage;
139    }
140
141    /**
142     * @param exceptionMessage
143     *            the exceptionMessage to set
144     */
145    public void setExceptionMessage(String exceptionMessage) {
146        this.exceptionMessage = exceptionMessage;
147    }
148
149    /**
150     * @return the exceptionStackTrace
151     */
152    public String getExceptionStackTrace() {
153        return this.exceptionStackTrace;
154    }
155
156    /**
157     * @param exceptionStackTrace
158     *            the exceptionStackTrace to set
159     */
160    public void setExceptionStackTrace(String exceptionStackTrace) {
161        this.exceptionStackTrace = exceptionStackTrace;
162    }
163
164    /**
165     * @return the userInput
166     */
167    public String getUserInput() {
168        return this.userInput;
169    }
170
171    /**
172     * @param userInput
173     *            the userInput to set
174     */
175    public void setUserInput(String userInput) {
176        this.userInput = userInput;
177    }
178
179    /**
180     * @return the devMode
181     */
182    public boolean isDevMode() {
183        return this.devMode;
184    }
185
186    /**
187     * @param devMode
188     *            the devMode to set
189     */
190    public void setDevMode(boolean devMode) {
191        this.devMode = devMode;
192    }
193
194    /**
195     * @param incidentDocId
196     *            the incidentDocId to set
197     */
198    public void setIncidentDocId(String incidentDocId) {
199        this.incidentDocId = incidentDocId;
200    }
201
202    /**
203     * @return the incidentDocId
204     */
205    public String getIncidentDocId() {
206        return incidentDocId;
207    }
208
209    /**
210     * @param incidentViewId
211     *            the incidentViewId to set
212     */
213    public void setIncidentViewId(String incidentViewId) {
214        this.incidentViewId = incidentViewId;
215    }
216
217    /**
218     * @return the incidentViewId
219     */
220    public String getIncidentViewId() {
221        return incidentViewId;
222    }
223
224    /**
225     * @param exception
226     *            the exception to set
227     */
228    public void setException(Exception exception) {
229        this.exception = exception;
230        setExceptionStackTrace(getStackTrace(exception));
231        setExceptionMessage(exception.getMessage());
232    }
233
234    /**
235     * @return the exception
236     */
237    public Exception getException() {
238        return exception;
239    }
240
241    /**
242     * @param userName
243     *            the userName to set
244     */
245    public void setUserName(String userName) {
246        this.userName = userName;
247    }
248
249    /**
250     * @return the userName
251     */
252    public String getUserName() {
253        return userName;
254    }
255
256    /**
257     * @param userId
258     *            the userId to set
259     */
260    public void setUserId(String userId) {
261        this.userId = userId;
262    }
263
264    /**
265     * @return the userId
266     */
267    public String getUserId() {
268        return userId;
269    }
270
271    /**
272     * @param userEmail
273     *            the userEmail to set
274     */
275    public void setUserEmail(String userEmail) {
276        this.userEmail = userEmail;
277    }
278
279    /**
280     * @return the userEmail
281     */
282    public String getUserEmail() {
283        return userEmail;
284    }
285
286    /**
287     * @param controller
288     *            the controller to set
289     */
290    public void setController(String controller) {
291        this.controller = controller;
292    }
293
294    /**
295     * @return the controller
296     */
297    public String getController() {
298        return controller;
299    }
300
301}