/* * Copyright (c) 2010-2011, ITArchitects * All rights reserved. * * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package at.itarchitects.wist.server.ejb; import at.itarchitects.wist.server.entities.accidentcase.AccidentCase; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import javax.annotation.PostConstruct; import javax.annotation.Resource; import javax.ejb.EJB; import javax.ejb.EJBException; import javax.ejb.LocalBean; import javax.ejb.Singleton; import javax.ejb.Startup; import javax.ejb.Timeout; import javax.ejb.Timer; import javax.ejb.TimerService; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.TypedQuery; /** * * @author Clemens Lanthaler clemens.lanthaler@itarchitects.at */ @Singleton @LocalBean @Startup public class StartupBean { //@EJB //private InvoiceBeanLocal invoiceBean; @Resource TimerService timerService; @PersistenceContext private EntityManager em; @PostConstruct private void startup() { Collection timerSet = timerService.getTimers(); List allActiveAccidentIDs = getAllActiveAccidentIDs(); // for (Long refNr : allActiveAccidentIDs) { // boolean checkIfTimerExists = checkIfTimerExists(refNr, timerSet); // if (checkIfTimerExists == false) { // createTimer(refNr); // } // } } /** * Checks if a timer is already running * @param refNr case ID * @param timerSet set of actual running timers * @return true if running timer found * @throws EJBException * @throws IllegalStateException */ // @SuppressWarnings("unchecked") // private boolean checkIfTimerExists(long refNr, Collection timerSet) throws EJBException, IllegalStateException { // for (Iterator it = timerSet.iterator(); it.hasNext();) { // Timer timer = it.next(); // ArrayList data = (ArrayList) timer.getInfo(); // long caseID = (Long) data.get(0); // if (refNr == caseID) { // return true; // } // } // return false; // } /* * finds all accident case IDs which are not locked * @Return null if nothing is found */ private List getAllActiveAccidentIDs() { TypedQuery findAllRefNumbersNotLocked = em.createQuery("SELECT acase.referenceNumber FROM AccidentCase acase WHERE acase.locked=false", Long.class); List resultList = findAllRefNumbersNotLocked.getResultList(); return resultList; } private void createTimer(long refNr) throws EJBException { // AccidentCase aCase = em.find(AccidentCase.class, refNr); // if (aCase != null) { // Collection timerSet = timerService.getTimers(); // ArrayList data = new ArrayList(); // data.add(refNr); // data.add(aCase.getInvoiceAdress()); // data.add(aCase.getUserID()); // Timer timer = timerService.createTimer(aCase.getLockDate(), // data); // } } @Timeout @SuppressWarnings(value = "unchecked") public void timeout(Timer timer) { //invoiceBean.timeout(timer); } }