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