Coverage Report - org.kuali.rice.kns.service.impl.KualiExceptionIncidentServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
KualiExceptionIncidentServiceImpl
0%
0/87
0%
0/62
4.5
 
 1  
 /*
 2  
  * Copyright 2007-2008 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.kns.service.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.HashSet;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 import java.util.Set;
 23  
 
 24  
 import org.apache.log4j.Logger;
 25  
 import org.kuali.rice.kew.web.session.UserSession;
 26  
 import org.kuali.rice.kim.bo.Person;
 27  
 import org.kuali.rice.kns.exception.ExceptionIncident;
 28  
 import org.kuali.rice.kns.exception.KualiException;
 29  
 import org.kuali.rice.kns.exception.KualiExceptionIncident;
 30  
 import org.kuali.rice.kns.mail.MailMessage;
 31  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 32  
 import org.kuali.rice.kns.service.KualiExceptionIncidentService;
 33  
 import org.kuali.rice.kns.service.MailService;
 34  
 import org.kuali.rice.kns.util.KNSConstants;
 35  
 
 36  
 /**
 37  
  * This is a basic implementation of the KualiReporterService. Currently, it only has
 38  
  * a mail service as reporting mechanism.
 39  
  * 
 40  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 41  
  *
 42  
  */
 43  0
 public class KualiExceptionIncidentServiceImpl implements KualiExceptionIncidentService {
 44  0
     private Logger LOG=Logger.getLogger(KualiExceptionIncidentServiceImpl.class);
 45  
     /**
 46  
      * This property must be defined in the base configuration file for specifying
 47  
      * the mailing list for the report to be sent.
 48  
      * <p>Example:
 49  
      * <code>
 50  
      * <param name="KualiReporterServiceImpl.REPORT_MAIL_LIST">a@y,b@z</param>
 51  
      * </code>
 52  
      */
 53  0
     public static final String REPORT_MAIL_LIST=String.format(
 54  
             "%s.REPORT_MAIL_LIST",KualiExceptionIncidentServiceImpl.class.getSimpleName());
 55  
     /**
 56  
      * A mail service for sending report.
 57  
      */
 58  
     private MailService mailService;
 59  
     /**
 60  
      * An email template is used to construct an email to be sent by the mail service.
 61  
      */
 62  
     private MailMessage messageTemplate;
 63  
 
 64  
     /**
 65  
      * This mails the report using the mail service from the mail template.
 66  
      * 
 67  
      * @see org.kuali.rice.kns.service.KualiExceptionIncidentService#emailReport(java.lang.String, java.lang.String)
 68  
      */
 69  
     public void emailReport(String subject, String message) throws Exception {
 70  0
         if (LOG.isTraceEnabled()) {
 71  0
             String lm=String.format("ENTRY %s;%s",
 72  
                     (subject==null)?"null":subject.toString(),
 73  
                     (message==null)?"null":message.toString());
 74  0
             LOG.trace(lm);
 75  
         }
 76  
         
 77  0
         if (mailService == null) {
 78  0
             String errMessage="MailService ?";
 79  0
             LOG.fatal(errMessage);
 80  0
             throw new KualiException(errMessage);
 81  
         }
 82  
         
 83  
         // Send mail
 84  0
         MailMessage msg=createMailMessage(subject, message);
 85  0
         mailService.sendMessage(msg);
 86  
 
 87  0
         if (LOG.isTraceEnabled()) {
 88  0
             LOG.trace("EXIT");
 89  
         }
 90  0
     }
 91  
 
 92  
     /**
 93  
      * This method create an instance of MailMessage from the inputs using the given
 94  
      * template.
 95  
      * 
 96  
      * @param subject
 97  
      * @param message
 98  
      * @return
 99  
      * @exception
 100  
      */
 101  
     @SuppressWarnings("unchecked")
 102  
     private MailMessage createMailMessage(String subject, String message)
 103  
                 throws Exception{
 104  0
         if (LOG.isTraceEnabled()) {
 105  0
             String lm=String.format("ENTRY %s%n%s",
 106  
                     (subject==null)?"null":subject.toString(),
 107  
                     (message==null)?"null":message.toString());
 108  0
             LOG.trace(lm);
 109  
         }
 110  
         
 111  0
         if (messageTemplate == null) {
 112  0
             throw new KualiException(String.format(
 113  
                     "%s.templateMessage is null or not set",
 114  
                     this.getClass().getName()));
 115  
         }
 116  
         
 117  
         // Copy input message reference for creating an instance of mail message
 118  0
         MailMessage msg=new MailMessage();
 119  
         
 120  0
         Person actualUser = UserSession.getAuthenticatedUser().getActualPerson();
 121  0
         String fromEmail = actualUser.getEmailAddress();
 122  0
         if ((fromEmail != null) && (fromEmail != "")) {
 123  0
                 msg.setFromAddress(fromEmail);
 124  
             } else {
 125  0
                 msg.setFromAddress(messageTemplate.getFromAddress());
 126  
             }
 127  
             
 128  0
         msg.setBccAddresses(messageTemplate.getBccAddresses());
 129  0
         msg.setCcAddresses(messageTemplate.getCcAddresses());
 130  
         // First check if message template already define mailing list
 131  0
         Set emails=messageTemplate.getToAddresses();
 132  0
         if (emails == null || emails.isEmpty()) {
 133  0
             String mailingList=KNSServiceLocator.getKualiConfigurationService().getPropertyString(REPORT_MAIL_LIST);
 134  0
             if (mailingList == null || mailingList.trim().length() == 0) {
 135  0
                 String em=REPORT_MAIL_LIST+"?";
 136  0
                 LOG.error(em);
 137  0
                 throw new KualiException(em);
 138  
             } else {
 139  0
                 msg.setToAddresses(new HashSet<String>(split(mailingList,
 140  
                         KNSConstants.FIELD_CONVERSIONS_SEPARATOR)));
 141  
             }
 142  0
         } else {
 143  0
             msg.setToAddresses(emails);
 144  
         }
 145  
 
 146  
         // Set mail message subject
 147  0
         msg.setSubject((subject==null)?"":subject);
 148  
 
 149  
         // Set mail message body
 150  0
         msg.setMessage((message==null)?"":message);
 151  
         
 152  0
         if (LOG.isTraceEnabled()) {
 153  0
             String lm=String.format("EXIT %s",
 154  
                     (msg==null)?"null":msg.toString());
 155  0
             LOG.trace(lm);
 156  
         }
 157  
 
 158  0
         return msg;
 159  
     }
 160  
 
 161  
     /**
 162  
      * This overridden method send email to the specified list of addresses.
 163  
      * 
 164  
      * @see org.kuali.rice.kns.service.KualiExceptionIncidentService#report(org.kuali.rice.kns.exception.KualiExceptionIncident)
 165  
      */
 166  
     public void report(KualiExceptionIncident exceptionIncident) throws Exception {
 167  0
         if (LOG.isTraceEnabled()) {
 168  0
             String lm=String.format("ENTRY %s",
 169  
                     (exceptionIncident==null)?"null":exceptionIncident.toString());
 170  0
             LOG.trace(lm);
 171  
         }
 172  
         
 173  0
         emailReport(
 174  
                 exceptionIncident.getProperty(
 175  
                         KualiExceptionIncident.EXCEPTION_REPORT_SUBJECT),
 176  
                 exceptionIncident.getProperty(
 177  
                         KualiExceptionIncident.EXCEPTION_REPORT_MESSAGE));
 178  
         
 179  0
         if (LOG.isTraceEnabled()) {
 180  0
             String lm=String.format("EXIT");
 181  0
             LOG.trace(lm);
 182  
         }
 183  
         
 184  0
     }
 185  
 
 186  
     /**
 187  
      * This method first separate a composite string of the format
 188  
      * "string token string".
 189  
      * <p>Example: 1,2,a,b where ',' is the token
 190  
      * 
 191  
      * @param s
 192  
      * @param token
 193  
      * @return
 194  
      */
 195  
     public List<String> split(String s, String token) {
 196  0
         if (LOG.isTraceEnabled()) {
 197  0
             String lm=String.format("ENTRY %s;%s", s, token);
 198  0
             LOG.trace(lm);
 199  
         }
 200  
                 
 201  0
         String[] sarray=s.split(token);
 202  0
         List<String> list=new ArrayList<String>();
 203  0
         for (int i=0; i<sarray.length && sarray[i].length() > 0; i++) {
 204  0
             list.add(sarray[i]);
 205  
         }
 206  
         
 207  0
         if (LOG.isTraceEnabled()) {
 208  0
             String lm=String.format("EXIT %s", list.toString());
 209  0
             LOG.trace(lm);
 210  
         }
 211  
         
 212  0
         return list;
 213  
     }
 214  
 
 215  
     /**
 216  
      * @return the mailService
 217  
      */
 218  
     public final MailService getMailService() {
 219  0
         return this.mailService;
 220  
     }
 221  
 
 222  
     /**
 223  
      * @param mailService the mailService to set
 224  
      */
 225  
     public final void setMailService(MailService mailService) {
 226  0
         this.mailService = mailService;
 227  0
     }
 228  
 
 229  
     /**
 230  
      * @return the messageTemplate
 231  
      */
 232  
     public final MailMessage getMessageTemplate() {
 233  0
         return this.messageTemplate;
 234  
     }
 235  
 
 236  
     /**
 237  
      * @param messageTemplate the messageTemplate to set
 238  
      */
 239  
     public final void setMessageTemplate(MailMessage messageTemplate) {
 240  0
         this.messageTemplate = messageTemplate;
 241  0
     }
 242  
 
 243  
     /**
 244  
      * This overridden method create an instance of the KualiExceptionIncident.
 245  
      * 
 246  
      * @see org.kuali.rice.kns.service.KualiExceptionIncidentService#getExceptionIncident(
 247  
      * java.lang.Exception,java.util.Map)
 248  
      */
 249  
     public KualiExceptionIncident getExceptionIncident(Exception exception,
 250  
             Map<String, String> properties) {
 251  0
             if ( exception == null ) {
 252  0
                     return getExceptionIncident(properties);
 253  
             }
 254  0
         if (LOG.isTraceEnabled()) {
 255  0
             String lm=String.format("ENTRY %s;%s", exception.getMessage(),
 256  
                     properties.toString());
 257  0
             LOG.trace(lm);
 258  
         }
 259  
         
 260  0
         KualiExceptionIncident ei=new ExceptionIncident(exception, properties);
 261  
         
 262  0
         if (LOG.isTraceEnabled()) {
 263  0
             String lm=String.format("EXIT %s", ei.toProperties().toString());
 264  0
             LOG.trace(lm);
 265  
         }
 266  
                 
 267  0
         return ei;
 268  
     }
 269  
 
 270  
     /**
 271  
      * This overridden method create an instance of ExceptionIncident from list of
 272  
      * name-value pairs as exception incident information.
 273  
      * 
 274  
      * @see org.kuali.rice.kns.service.KualiExceptionIncidentService#getExceptionIncident(java.util.Map)
 275  
      */
 276  
     public KualiExceptionIncident getExceptionIncident(Map<String, String> properties) {
 277  0
         if (LOG.isTraceEnabled()) {
 278  0
             String lm=String.format("ENTRY %s", properties.toString());
 279  0
             LOG.trace(lm);
 280  
         }
 281  
         
 282  0
         ExceptionIncident ei=new ExceptionIncident(properties);
 283  
                 
 284  0
         if (LOG.isTraceEnabled()) {
 285  0
             String lm=String.format("EXIT %s", ei.toProperties().toString());
 286  0
             LOG.trace(lm);
 287  
         }
 288  
                 
 289  0
         return ei;
 290  
     }
 291  
 
 292  
 }