public void addListener(JREntityListener t) { JpaEntityManager entityManager = null; try { // Create an entity manager for use in this function entityManager = (JpaEntityManager) entityManagerFactory.createEntityManager(); // Use the entity manager to get a ClassDescriptor for the Entity class ClassDescriptor desc = entityManager.getSession().getClassDescriptor(t.getTarget()); JRDescriptorEventListener jrDescriptorEventListener = new JRDescriptorEventListener(t); desc.getEventManager().addListener(jrDescriptorEventListener); logger.info("Entity Listener for " + t.getTarget().getCanonicalName() + " is added"); } catch (Exception e) { logger.error(e.getMessage(), e); } finally { if (entityManager != null) { // Cleanup the entity manager entityManager.close(); } } }
public EntityInformation[] locate(EntityManagerFactory entityManagerFactory, JpaGpsDevice device) throws JpaGpsDeviceException { CompassGpsInterfaceDevice gps = (CompassGpsInterfaceDevice) device.getGps(); JpaEntityManager entityManager = (JpaEntityManager) entityManagerFactory.createEntityManager(); Session session = entityManager.getServerSession(); entityManager.close(); ArrayList<EntityInformation> entitiesList = new ArrayList<EntityInformation>(); Map descriptors = session.getDescriptors(); for (Object o : descriptors.values()) { ClassDescriptor classDescriptor = (ClassDescriptor) o; String entityname = classDescriptor.getJavaClassName(); if (!gps.hasMappingForEntityForIndex((entityname))) { if (log.isDebugEnabled()) { log.debug("Entity [" + entityname + "] does not have compass mapping, filtering it out"); } continue; } if (shouldFilter(entityname, classDescriptor, device)) { continue; } Class<?> clazz = classDescriptor.getJavaClass(); ResourceMapping resourceMapping = gps.getMappingForEntityForIndex(entityname); EntityInformation entityInformation = new EntityInformation(clazz, classDescriptor.getAlias(), resourceMapping.getSubIndexHash().getSubIndexes()); entitiesList.add(entityInformation); if (log.isDebugEnabled()) { log.debug("Entity [" + entityname + "] will be indexed"); } } return entitiesList.toArray(new EntityInformation[entitiesList.size()]); }
public void removeLifecycle(EntityManagerFactory entityManagerFactory, JpaGpsDevice device) throws JpaGpsDeviceException { CompassGpsInterfaceDevice gps = (CompassGpsInterfaceDevice) device.getGps(); JpaEntityManager entityManager = (JpaEntityManager) entityManagerFactory.createEntityManager(); Session session = entityManager.getServerSession(); entityManager.close(); Map descriptors = session.getDescriptors(); for (Object o : descriptors.values()) { ClassDescriptor classDescriptor = (ClassDescriptor) o; Class mappedClass = classDescriptor.getJavaClass(); // if we have a parent class that is mapped in JPA and in Compass, then don't add an event listner // since we will add it to the parent descriptor and it will notify this class as well if (classDescriptor.isChildDescriptor()) { Class parentClass = classDescriptor.getInheritancePolicy().getParentDescriptor().getJavaClass(); if (gps.hasMappingForEntityForMirror(parentClass, Cascade.ALL)) { classDescriptor.getDescriptorEventManager().removeListener(dummyEventListener); continue; } } if (gps.hasMappingForEntityForMirror(mappedClass, Cascade.ALL)) { classDescriptor.getDescriptorEventManager().removeListener(eventListener); } } } }
public void injectLifecycle(EntityManagerFactory entityManagerFactory, JpaGpsDevice device) throws JpaGpsDeviceException { CompassGpsInterfaceDevice gps = (CompassGpsInterfaceDevice) device.getGps(); if (eventListener == null) { eventListener = new TopLinkEssentialsEventListener(device); } JpaEntityManager entityManager = (JpaEntityManager) entityManagerFactory.createEntityManager(); Session session = entityManager.getServerSession(); entityManager.close(); Map descriptors = session.getDescriptors(); for (Object o : descriptors.values()) { ClassDescriptor classDescriptor = (ClassDescriptor) o; Class mappedClass = classDescriptor.getJavaClass(); // if we have a parent class that is mapped in JPA and in Compass, then don't add an event listner // since we will add it to the parent descriptor and it will notify this class as well if (classDescriptor.isChildDescriptor()) { Class parentClass = classDescriptor.getInheritancePolicy().getParentDescriptor().getJavaClass(); if (gps.hasMappingForEntityForMirror(parentClass, Cascade.ALL)) { // we need to inject a dummy listener, just so it will kick in the listener notifications classDescriptor.getDescriptorEventManager().addListener(dummyEventListener); continue; } } if (gps.hasMappingForEntityForMirror(mappedClass, Cascade.ALL)) { classDescriptor.getDescriptorEventManager().addListener(eventListener); } } }