001/**
002 * Copyright 2005-2018 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.core.api.CoreApiServiceLocator;
019import org.kuali.rice.krad.uif.UifConstants.ViewType;
020
021import java.io.PrintWriter;
022import java.io.StringWriter;
023
024/**
025 * Form class for incident reports
026 *
027 * @author Kuali Rice Team (rice.collab@kuali.org)
028 */
029public class IncidentReportForm extends UifFormBase {
030    private static final long serialVersionUID = -6677581167041430694L;
031
032    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.";
033
034    protected Exception exception;
035    protected String exceptionMessage;
036    protected String exceptionStackTrace;
037
038    protected String userInput;
039    protected String incidentDocId;
040    protected String incidentViewId;
041    protected String controller;
042    protected String userName;
043    protected String userId;
044    protected String userEmail;
045
046    protected boolean devMode;
047
048    public IncidentReportForm() {
049        super();
050
051        setViewTypeName(ViewType.INCIDENT);
052    }
053
054    /**
055     * Creates the email message from the exception, form and user data.
056     *
057     * @return the email message
058     */
059    public String createEmailMessage() {
060        String format = "Document Id: %s%n" + "View Id: %s%n" + "Handler: %s%n%n" + "User Email: %s%n"
061                + "Person User Identifier: %s%n" + "Person Name: %s%n" + "User Input: %s%n%n" + "errorMessage: %n"
062                + "%s";
063        String message = String.format(format, (incidentDocId == null) ? "" : incidentDocId, (incidentViewId == null) ? "" : incidentViewId,
064                (controller == null) ? "" : controller, (userEmail == null) ? "" : userEmail, (userId == null) ? ""
065                        : userId, (userName == null) ? "" : userName, (userInput == null) ? "" : userInput,
066                (exceptionStackTrace == null) ? "" : exceptionStackTrace);
067
068        return message;
069
070    }
071
072    /**
073     * Creates the email subject containing the mode, view id and the exception message.
074     *
075     * @return the email subject
076     */
077    public String createEmailSubject() {
078        String app = CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString("application.id");
079        String env = CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString("environment");
080        String format = "%s:%s:%s:%s";
081        String subject = String.format(format, app, env, (incidentViewId == null) ? "" : incidentViewId,
082                truncateString(exceptionMessage, 180));
083        return subject;
084    }
085
086    /**
087     * Truncate the string to specified length.
088     *
089     * @param str
090     *            the string to truncate
091     * @param maxLength
092     *            the max length
093     * @return the truncated string
094     */
095    protected String truncateString(String str, int maxLength) {
096        if (str != null && str.length() > maxLength)
097            str = str.substring(0, maxLength);
098        return str;
099    }
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}