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.eclipselink;
018:
019: import java.util.Vector;
020:
021: import org.compass.core.mapping.CascadeMapping;
022: import org.compass.gps.device.jpa.AbstractDeviceJpaEntityListener;
023: import org.compass.gps.device.jpa.JpaGpsDevice;
024: import org.compass.gps.device.jpa.JpaGpsDeviceException;
025: import org.eclipse.persistence.descriptors.DescriptorEvent;
026: import org.eclipse.persistence.descriptors.DescriptorEventListener;
027: import org.eclipse.persistence.sessions.Session;
028:
029: /**
030: * An event listener that mirrors changes done through EclipseLink to Compass when using EclipseLink embedded support.
031: * Uses {@link EclipseLinkHelper#getCurrentCompassSession(org.eclipse.persistence.sessions.Session)}
032: * in order to get the current {@link org.compass.core.CompassSession} and perform with it the relevant mirror operations.
033: *
034: * @author kimchy
035: */
036: public class EclipseLinkEventListener extends
037: AbstractDeviceJpaEntityListener implements
038: DescriptorEventListener {
039:
040: private JpaGpsDevice device;
041:
042: public EclipseLinkEventListener(JpaGpsDevice device) {
043: this .device = device;
044: }
045:
046: @Override
047: protected JpaGpsDevice getDevice() {
048: return this .device;
049: }
050:
051: public void postUpdate(DescriptorEvent event) {
052: if (disable()) {
053: return;
054: }
055: Object entity = event.getObject();
056: if (!hasMappingForEntity(entity.getClass(),
057: CascadeMapping.Cascade.SAVE)) {
058: return;
059: }
060: try {
061: if (log.isDebugEnabled()) {
062: log.debug("Updating [" + entity + "]");
063: }
064: Session session = event.getSession();
065: EclipseLinkHelper.getCurrentCompassSession(session).save(
066: entity);
067: } catch (Exception e) {
068: log.error("Failed while updating [" + entity + "]", e);
069: if (throwExceptionOnError()) {
070: throw new JpaGpsDeviceException(
071: "Failed while updating [" + entity + "]", e);
072: }
073: }
074: }
075:
076: public void postDelete(DescriptorEvent event) {
077: if (disable()) {
078: return;
079: }
080: Object entity = event.getObject();
081: if (!hasMappingForEntity(entity.getClass(),
082: CascadeMapping.Cascade.DELETE)) {
083: return;
084: }
085: try {
086: if (log.isDebugEnabled()) {
087: log.debug("Deleting [" + entity + "]");
088: }
089: Session session = event.getSession();
090: EclipseLinkHelper.getCurrentCompassSession(session).delete(
091: entity);
092: } catch (Exception e) {
093: log.error("Failed while deleting [" + entity + "]", e);
094: if (throwExceptionOnError()) {
095: throw new JpaGpsDeviceException(
096: "Failed while deleting [" + entity + "]", e);
097: }
098: }
099: }
100:
101: public void postInsert(DescriptorEvent event) {
102: if (disable()) {
103: return;
104: }
105: Object entity = event.getObject();
106: if (!hasMappingForEntity(entity.getClass(),
107: CascadeMapping.Cascade.CREATE)) {
108: return;
109: }
110: try {
111: if (log.isDebugEnabled()) {
112: log.debug("Creating [" + entity + "]");
113: }
114: Session session = event.getSession();
115: EclipseLinkHelper.getCurrentCompassSession(session).create(
116: entity);
117: } catch (Exception e) {
118: log.error("Failed while creating [" + entity + "]", e);
119: if (throwExceptionOnError()) {
120: throw new JpaGpsDeviceException(
121: "Failed while creating [" + entity + "]", e);
122: }
123: }
124: }
125:
126: // things we don't use
127:
128: public void aboutToDelete(DescriptorEvent event) {
129: }
130:
131: public void aboutToInsert(DescriptorEvent event) {
132: }
133:
134: public void aboutToUpdate(DescriptorEvent event) {
135: }
136:
137: public boolean isOverriddenEvent(DescriptorEvent event,
138: Vector eventManagers) {
139: return false;
140: }
141:
142: public void postBuild(DescriptorEvent event) {
143: }
144:
145: public void postClone(DescriptorEvent event) {
146: }
147:
148: public void postMerge(DescriptorEvent event) {
149: }
150:
151: public void postRefresh(DescriptorEvent event) {
152: }
153:
154: public void postWrite(DescriptorEvent event) {
155: }
156:
157: public void preDelete(DescriptorEvent event) {
158: }
159:
160: public void preInsert(DescriptorEvent event) {
161: }
162:
163: public void prePersist(DescriptorEvent event) {
164: }
165:
166: public void preRemove(DescriptorEvent event) {
167: }
168:
169: public void preUpdate(DescriptorEvent event) {
170: }
171:
172: public void preUpdateWithChanges(DescriptorEvent event) {
173: }
174:
175: public void preWrite(DescriptorEvent event) {
176: }
177:
178: }
|