001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: SLSBListenerLoggerAccess.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger;
025:
026: import java.util.List;
027:
028: import javax.ejb.Remote;
029: import javax.ejb.Stateless;
030: import javax.persistence.EntityManager;
031: import javax.persistence.EntityManagerFactory;
032: import javax.persistence.PersistenceUnit;
033: import javax.persistence.Query;
034:
035: import org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackType;
036: import org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.ListenerLogger;
037:
038: /**
039: * Accesses the entity bean that is responsible for registering the callback events.
040: * @author Gisele Pinheiro Souza
041: * @author Eduardo Studzinski Estima de Castro
042: *
043: */
044: @Stateless
045: @Remote(ItfListenerLoggerAccess.class)
046: public class SLSBListenerLoggerAccess extends CallbackLoggerAccessBase
047: implements ItfListenerLoggerAccess {
048:
049: /**
050: * The Entity Manager Factory used during the tests.
051: */
052: @PersistenceUnit
053: private EntityManagerFactory entityManagerFactory;
054:
055: /**
056: * Creates an instance of ListenerLogger with the parameters and the current
057: * time.
058: * @param className the class for which the callback event was called.
059: * @param callbackEvent the callback event name.
060: * @param callbackClassName the name of the class that contains the callback
061: * method.
062: * @param entityId the identifier of the entity that the listenr methdos was
063: * called.
064: */
065: public void insertCallbackLogger(final String className,
066: final CallbackType callbackEvent,
067: final String callbackClassName, final int entityId) {
068: EntityManager entityManager = entityManagerFactory
069: .createEntityManager();
070:
071: ListenerLogger logger = new ListenerLogger();
072: logger.setCallbackClassName(callbackClassName);
073: logger.setCallbackEvent(callbackEvent);
074: logger.setClassName(className);
075: logger.setInsertionDate(getTime());
076: logger.setEntityId(entityId);
077: entityManager.persist(logger);
078: entityManager.flush();
079: }
080:
081: /**
082: * Finds a callback event that was called for the class in the className
083: * parameter and is defined in the class callbackClassName.
084: * @param className the class for each the callback was called.
085: * @param callbackEvent the callback method that was called.
086: * @param entityId the the entity for which the listener was called.
087: * @return the list of results.
088: */
089: public List findLifecycleEventByEntity(final String className,
090: final CallbackType callbackEvent, final int entityId) {
091: EntityManager entityManager = entityManagerFactory
092: .createEntityManager();
093: Query query = entityManager
094: .createQuery("SELECT e FROM ListenerManager "
095: + "e WHERE e.className = :className AND e.callbackEvent= :event AND e.entityId = :entityId");
096: query.setParameter("className", className);
097: query.setParameter("event", callbackEvent);
098: query.setParameter("entityId", new Integer(entityId));
099: return query.getResultList();
100: }
101: }
|