Coverage Report - org.jtheque.films.services.impl.utils.closures.LendingCheckClosure
 
Classes in this File Line Coverage Branch Coverage Complexity
LendingCheckClosure
0 %
0/43
0 %
0/8
2.5
 
 1  
 package org.jtheque.films.services.impl.utils.closures;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.core.managers.Managers;
 20  
 import org.jtheque.core.managers.beans.IBeansManager;
 21  
 import org.jtheque.core.managers.error.IErrorManager;
 22  
 import org.jtheque.core.managers.error.InternationalizedError;
 23  
 import org.jtheque.core.managers.language.ILanguageManager;
 24  
 import org.jtheque.core.managers.log.IJThequeLogger;
 25  
 import org.jtheque.core.managers.log.Logger;
 26  
 import org.jtheque.core.managers.view.able.IViewManager;
 27  
 import org.jtheque.core.utils.MailUtils;
 28  
 import org.jtheque.films.IFilmsModule;
 29  
 import org.jtheque.films.persistence.dao.able.IDaoFilms;
 30  
 import org.jtheque.primary.od.able.Lending;
 31  
 import org.jtheque.primary.services.able.ILendingsService;
 32  
 import org.jtheque.utils.bean.Email;
 33  
 import org.jtheque.utils.collections.Closure;
 34  
 
 35  
 import javax.annotation.Resource;
 36  
 import javax.mail.MessagingException;
 37  
 
 38  
 /**
 39  
  * A closure to check all lendings and react to retards.
 40  
  *
 41  
  * @author Baptiste Wicht
 42  
  */
 43  0
 public final class LendingCheckClosure implements Closure<Lending> {
 44  
     @Resource
 45  
     private ILendingsService lendingsService;
 46  
 
 47  
     @Logger
 48  
     private IJThequeLogger logger;
 49  
 
 50  
     @Resource
 51  
     private IFilmsModule filmsModule;
 52  
 
 53  
     @Resource
 54  
     private IDaoFilms daoFilms;
 55  
 
 56  
     /**
 57  
      * Construct a new LendingCheckClosure.
 58  
      */
 59  
     public LendingCheckClosure() {
 60  0
         super();
 61  
 
 62  0
         Managers.getManager(IBeansManager.class).inject(this);
 63  0
     }
 64  
 
 65  
     @Override
 66  
     public void execute(Lending lending) {
 67  0
         if (lendingsService.isLate(lending, filmsModule.getConfiguration().getTimeBeforeAutomaticSend())) {
 68  0
             if (filmsModule.getConfiguration().areMailSendAutomatically()) {
 69  0
                 sendMailToBorrower(lending);
 70  
             }
 71  
 
 72  0
             Object[] objects = new Object[3];
 73  0
             objects[0] = daoFilms.getFilm(lending.getTheOther()).getDisplayableText();
 74  0
             objects[1] = lending.getThePerson().getDisplayableText();
 75  0
             objects[2] = lending.getDate().toString();
 76  
 
 77  0
             if (filmsModule.getConfiguration().alertWithMail()) {
 78  0
                 sendMailToUser(objects);
 79  
             }
 80  
 
 81  0
             if (filmsModule.getConfiguration().alertWithDialog()) {
 82  0
                 Managers.getManager(IViewManager.class).displayText(
 83  
                         Managers.getManager(ILanguageManager.class).getMessage("lendings.dialogs.message", objects));
 84  
             }
 85  
         }
 86  0
     }
 87  
 
 88  
     /**
 89  
      * Send an email to the user with the objects replaces for the message.
 90  
      *
 91  
      * @param objects The replaces for the message.
 92  
      */
 93  
     private void sendMailToUser(Object[] objects) {
 94  0
         Email mail = new Email();
 95  
 
 96  0
         mail.setFrom("JTheque@jtheque.com");
 97  0
         mail.setMessage(Managers.getManager(ILanguageManager.class).getMessage("lendings.mail.user.message", objects));
 98  0
         mail.setSubject(Managers.getManager(ILanguageManager.class).getMessage("lendings.mail.user.subject"));
 99  0
         mail.setTo(new String[]{Managers.getCore().getConfiguration().getUserEmail()});
 100  
 
 101  
         try {
 102  0
             MailUtils.send(mail, Managers.getCore().getConfiguration().getSmtpHost());
 103  0
         } catch (MessagingException e) {
 104  0
             Managers.getManager(IErrorManager.class).addError(new InternationalizedError("jtheque.errors.mail"));
 105  
 
 106  0
             logger.error(e);
 107  0
         }
 108  0
     }
 109  
 
 110  
     /**
 111  
      * Send an email to the borrower. Use the lending to fill the message.
 112  
      *
 113  
      * @param lending The lending to use to fill the message.
 114  
      */
 115  
     private void sendMailToBorrower(Lending lending) {
 116  0
         Email mail = new Email();
 117  
 
 118  0
         mail.setFrom(Managers.getCore().getConfiguration().getUserEmail());
 119  
 
 120  0
         String message = filmsModule.getConfiguration().getAutomaticMail();
 121  
 
 122  0
         message = message.replaceAll("\\$\\{emprunteur:firstname\\}", lending.getThePerson().getFirstName());
 123  0
         message = message.replaceAll("\\$\\{emprunteur:name\\}", lending.getThePerson().getName());
 124  0
         message = message.replaceAll("\\$\\{emprunt:date\\}", lending.getDate().toString());
 125  0
         message = message.replaceAll("\\$\\{film:title\\}", daoFilms.getFilm(lending.getTheOther()).getTitle());
 126  
 
 127  0
         mail.setMessage(message);
 128  0
         mail.setSubject(Managers.getManager(ILanguageManager.class).getMessage("lendings.mail.borrower.subject"));
 129  0
         mail.setTo(new String[]{lending.getThePerson().getEmail()});
 130  
 
 131  
         try {
 132  0
             MailUtils.send(mail, Managers.getCore().getConfiguration().getSmtpHost());
 133  0
         } catch (MessagingException e) {
 134  0
             Managers.getManager(IErrorManager.class).addError(new InternationalizedError("jtheque.errors.mail"));
 135  
 
 136  0
             logger.error(e);
 137  0
         }
 138  0
     }
 139  
 }