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.service.impl;
017
018import org.apache.log4j.Logger;
019import org.kuali.rice.krad.exception.ExceptionIncident;
020import org.kuali.rice.krad.exception.KualiExceptionIncident;
021import org.kuali.rice.krad.service.KualiExceptionIncidentService;
022
023import java.util.ArrayList;
024import java.util.List;
025import java.util.Map;
026
027/**
028 * Modified this service so that it now extends the KualiFeedbackServiceImpl.
029 * This has been done to allow user feedback and exception incidents to be
030 * reported in the same way, but to potentially different email lists.  Part
031 * of this refactor included moving the mailer and messageTemplate properties
032 * and the emailReport and createMailMessage methods to the new parent class.
033 * 
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 *
036 */
037public class KualiExceptionIncidentServiceImpl extends KualiFeedbackServiceImpl implements KualiExceptionIncidentService {
038    private Logger LOG=Logger.getLogger(KualiExceptionIncidentServiceImpl.class);
039    
040    /**
041     * An list to send incident emails to.
042     */
043    private String incidentMailingList;
044    
045    /**
046     * This property must be defined in the base configuration file for specifying
047     * the mailing list for the report to be sent.
048     * <p>Example:
049     * <code>
050     * <param name="KualiReporterServiceImpl.REPORT_MAIL_LIST">a@y,b@z</param>
051     * </code>
052     */
053    public static final String REPORT_MAIL_LIST=String.format("%s.REPORT_MAIL_LIST", KualiExceptionIncidentServiceImpl.class.getSimpleName());
054
055    @Override
056    protected String getToAddressesPropertyName() {
057        return REPORT_MAIL_LIST;
058    }
059
060    /**
061     * This overridden method send email to the specified list of addresses.
062     * 
063     * @see org.kuali.rice.krad.service.KualiExceptionIncidentService#report(org.kuali.rice.krad.exception.KualiExceptionIncident)
064     */
065    @Override
066        public void report(KualiExceptionIncident exceptionIncident) throws Exception {
067        if (LOG.isTraceEnabled()) {
068            String lm=String.format("ENTRY %s",
069                    (exceptionIncident==null)?"null":exceptionIncident.toString());
070            LOG.trace(lm);
071        }
072        
073        emailReport(
074                exceptionIncident.getProperty(
075                        KualiExceptionIncident.EXCEPTION_REPORT_SUBJECT),
076                exceptionIncident.getProperty(
077                        KualiExceptionIncident.EXCEPTION_REPORT_MESSAGE));
078        
079        if (LOG.isTraceEnabled()) {
080            String lm=String.format("EXIT");
081            LOG.trace(lm);
082        }
083        
084    }
085
086    /**
087     * This method first separate a composite string of the format
088     * "string token string".
089     * <p>Example: 1,2,a,b where ',' is the token
090     * 
091     * @param s
092     * @param token
093     * @return
094     */
095    public List<String> split(String s, String token) {
096        if (LOG.isTraceEnabled()) {
097            String lm=String.format("ENTRY %s;%s", s, token);
098            LOG.trace(lm);
099        }
100                
101        String[] sarray=s.split(token);
102        List<String> list=new ArrayList<String>();
103        for (int i=0; i<sarray.length && sarray[i].length() > 0; i++) {
104            list.add(sarray[i]);
105        }
106        
107        if (LOG.isTraceEnabled()) {
108            String lm=String.format("EXIT %s", list.toString());
109            LOG.trace(lm);
110        }
111        
112        return list;
113    }
114
115    /**
116     * This overridden method create an instance of the KualiExceptionIncident.
117     * 
118     * @see org.kuali.rice.krad.service.KualiExceptionIncidentService#getExceptionIncident(
119     * java.lang.Exception,java.util.Map)
120     */
121    @Override
122        public KualiExceptionIncident getExceptionIncident(Exception exception,
123            Map<String, String> properties) {
124        if ( exception == null ) {
125                return getExceptionIncident(properties);
126        }
127        if (LOG.isTraceEnabled()) {
128            String lm=String.format("ENTRY %s;%s", exception.getMessage(),
129                    properties.toString());
130            LOG.trace(lm);
131        }
132        
133        KualiExceptionIncident ei=new ExceptionIncident(exception, properties);
134        
135        if (LOG.isTraceEnabled()) {
136            String lm=String.format("EXIT %s", ei.toProperties().toString());
137            LOG.trace(lm);
138        }
139                
140        return ei;
141    }
142
143    /**
144     * This overridden method create an instance of ExceptionIncident from list of
145     * name-value pairs as exception incident information.
146     * 
147     * @see org.kuali.rice.krad.service.KualiExceptionIncidentService#getExceptionIncident(java.util.Map)
148     */
149    @Override
150        public KualiExceptionIncident getExceptionIncident(Map<String, String> properties) {
151        if (LOG.isTraceEnabled()) {
152            String lm=String.format("ENTRY %s", properties.toString());
153            LOG.trace(lm);
154        }
155        
156        ExceptionIncident ei=new ExceptionIncident(properties);
157                
158        if (LOG.isTraceEnabled()) {
159            String lm=String.format("EXIT %s", ei.toProperties().toString());
160            LOG.trace(lm);
161        }
162                
163        return ei;
164    }
165    
166        /**
167     * Returns the incident report mailing list.
168         * @return the incidentMailingList
169         */
170        public String getIncidentMailingList() {
171                return this.incidentMailingList;
172        }
173
174        /**
175     * Sets the incident report mailing list.
176         * @param incidentMailingList the incidentMailingList to set
177         */
178        public void setIncidentMailingList(String incidentMailingList) {
179                this.incidentMailingList = incidentMailingList;
180        }
181
182}