001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.gps.device.jpa.embedded.openjpa;
018:
019: import javax.persistence.EntityManager;
020:
021: import org.apache.openjpa.event.DeleteListener;
022: import org.apache.openjpa.event.LifecycleEvent;
023: import org.apache.openjpa.event.PersistListener;
024: import org.apache.openjpa.event.StoreListener;
025: import org.apache.openjpa.persistence.OpenJPAPersistence;
026: import org.compass.core.mapping.CascadeMapping;
027: import org.compass.gps.device.jpa.AbstractDeviceJpaEntityListener;
028: import org.compass.gps.device.jpa.JpaGpsDevice;
029: import org.compass.gps.device.jpa.JpaGpsDeviceException;
030:
031: /**
032: * The embedded open JPA event listener listens for after delete/store/persist events and applies them
033: * to the index. No transaction handling is perfomed (assumes that it is a managed transaction) and usees
034: * {@link org.compass.gps.device.jpa.embedded.openjpa.OpenJPAHelper#getCurrentCompassSession(javax.persistence.EntityManager)}
035: * in order to get the actual compass session.
036: *
037: * @author kimchy
038: */
039: public class EmbeddedOpenJPAEventListener extends
040: AbstractDeviceJpaEntityListener implements DeleteListener,
041: PersistListener, StoreListener {
042:
043: private JpaGpsDevice device;
044:
045: public EmbeddedOpenJPAEventListener(JpaGpsDevice device) {
046: this .device = device;
047: }
048:
049: @Override
050: protected JpaGpsDevice getDevice() {
051: return this .device;
052: }
053:
054: public void beforeDelete(LifecycleEvent lifecycleEvent) {
055: }
056:
057: public void afterDelete(LifecycleEvent lifecycleEvent) {
058: if (disable()) {
059: return;
060: }
061: Object entity = lifecycleEvent.getSource();
062: if (!hasMappingForEntity(entity.getClass(),
063: CascadeMapping.Cascade.DELETE)) {
064: return;
065: }
066: try {
067: if (log.isDebugEnabled()) {
068: log.debug("Deleting [" + entity + "]");
069: }
070: EntityManager em = OpenJPAPersistence
071: .getEntityManager(entity);
072: OpenJPAHelper.getCurrentCompassSession(em).delete(entity);
073: } catch (Exception e) {
074: log.error("Failed while deleting [" + entity + "]", e);
075: if (throwExceptionOnError()) {
076: throw new JpaGpsDeviceException(
077: "Failed while deleting [" + entity + "]", e);
078: }
079: }
080: }
081:
082: public void beforePersist(LifecycleEvent lifecycleEvent) {
083: }
084:
085: public void afterPersist(LifecycleEvent lifecycleEvent) {
086: if (disable()) {
087: return;
088: }
089: Object entity = lifecycleEvent.getSource();
090: if (!hasMappingForEntity(entity.getClass(),
091: CascadeMapping.Cascade.CREATE)) {
092: return;
093: }
094: try {
095: if (log.isDebugEnabled()) {
096: log.debug("Creating [" + entity + "]");
097: }
098: EntityManager em = OpenJPAPersistence
099: .getEntityManager(entity);
100: OpenJPAHelper.getCurrentCompassSession(em).create(entity);
101: } catch (Exception e) {
102: log.error("Failed while creating [" + entity + "]", e);
103: if (throwExceptionOnError()) {
104: throw new JpaGpsDeviceException(
105: "Failed while creating [" + entity + "]", e);
106: }
107: }
108: }
109:
110: public void beforeStore(LifecycleEvent lifecycleEvent) {
111: }
112:
113: public void afterStore(LifecycleEvent lifecycleEvent) {
114: if (disable()) {
115: return;
116: }
117: Object entity = lifecycleEvent.getSource();
118: if (!hasMappingForEntity(entity.getClass(),
119: CascadeMapping.Cascade.SAVE)) {
120: return;
121: }
122: try {
123: if (log.isDebugEnabled()) {
124: log.debug("Updating [" + entity + "]");
125: }
126: EntityManager em = OpenJPAPersistence
127: .getEntityManager(entity);
128: OpenJPAHelper.getCurrentCompassSession(em).save(entity);
129: } catch (Exception e) {
130: log.error("Failed while updating [" + entity + "]", e);
131: if (throwExceptionOnError()) {
132: throw new JpaGpsDeviceException(
133: "Failed while updating [" + entity + "]", e);
134: }
135: }
136: }
137:
138: }
|