/* Copyright 2009 - 2010 Under Dusken Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package no.dusken.barweb.plugin.leekplugin.control; import no.dusken.barweb.model.BarPerson; import no.dusken.barweb.model.Gjeng; import no.dusken.barweb.service.BarPersonService; import no.dusken.barweb.service.GjengService; import no.dusken.common.model.Mail; import no.dusken.common.util.MailSender; import org.kantega.jexmec.store.PluginStore; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import javax.inject.Inject; import java.util.*; /** * @author Marvin B. Lillehaug <lillehau@underdusken.no> */ public class LeekController { @Inject private BarPersonService barPersonService; @Inject private GjengService gjengService; @Inject private MailSender mailSender; private String mailDomain; private int lowLimit; private String lowBalanceTemplate; @Inject private PluginStore pluginStore; private final String notifiedPersonsKey = "notifiedPersons"; /** * Id for persons that have been notified that they have balance between 0 and 300. */ private Set<Long> notifiedPersonsIds; private final Logger logger = LoggerFactory.getLogger(getClass()); /** * Send mail to active every week if they have less than lowlimit. Send mail to nonactive if they have * negative balance. */ @Scheduled(cron = "0 0 12 ? * WED") public void notifyBlackPeople() { notifiedPersonsIds = getNotifiedPersons(); Gjeng gjeng = gjengService.getByDefaultGjengTrue(); List<BarPerson> active = barPersonService.getActivePersons(gjeng); List<BarPerson> nonActive = barPersonService.getNonActivePersons(gjeng); for(BarPerson barPerson : active){ Long id = barPerson.getId(); if(barPerson.getMoney() < lowLimit){ if (barPerson.getMoney() >= 0 && !notifiedPersonsIds.contains(id) || barPerson.getMoney() < 0) { logger.info("Markerer " + barPerson.getName() + " som svart."); // Notify this barPerson notifiedPersonsIds.add(id); sendLowBalanceMail(barPerson); } } if(barPerson.getMoney() >= lowLimit && notifiedPersonsIds.contains(id)){ logger.info("Markerer " + barPerson.getName() + " som hvit."); notifiedPersonsIds.remove(id); } } for(BarPerson barPerson : nonActive){ if(barPerson.getMoney() < 0){ sendLowBalanceMail(barPerson); } } saveNotifiedPersons(); } private void sendLowBalanceMail(BarPerson barPerson){ logger.info("Sender mail til ".concat(barPerson.getName()) + " som har kr " + barPerson.getMoney() + " på barkonto."); Mail mail = new Mail(); if (barPerson.getMoney() >= 0) { mail.setSubject("Mindre enn ".concat(String.valueOf(lowLimit)).concat(" kr på barkonto") ); } else { mail.setSubject("Negativt beløp på barkonto"); } mail.setFromName("Barweb"); mail.setToAddress(barPerson.getUsername().concat("@").concat(mailDomain)); Map<String, Object> map = new HashMap<String, Object>(); map.put("barPerson", barPerson); mailSender.sendEmail(mail, map, lowBalanceTemplate); } public void setMailDomain(String mailDomain) { this.mailDomain = mailDomain; } public void setLowLimit(int lowLimit) { this.lowLimit = lowLimit; } public void setLowBalanceTemplate(String lowBalanceTemplate) { this.lowBalanceTemplate = lowBalanceTemplate; } private Set<Long> getNotifiedPersons(){ Set<Long> ids = new HashSet<Long>(); String idsFromStore = pluginStore.getString(notifiedPersonsKey, ""); String[] splitted = idsFromStore.split(","); for(String s : splitted){ try { ids.add(Long.parseLong(s)); } catch (NumberFormatException e) { //empty string or something. } } return ids; } private void saveNotifiedPersons(){ StringBuffer sb = new StringBuffer(); for(Long l : notifiedPersonsIds){ sb.append(l); sb.append(","); } pluginStore.setString(notifiedPersonsKey, sb.toString()); } }