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 java.util.Properties;
020: import javax.persistence.EntityManager;
021: import javax.persistence.EntityManagerFactory;
022:
023: import org.apache.openjpa.persistence.OpenJPAEntityManager;
024: import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
025: import org.apache.openjpa.persistence.OpenJPAPersistence;
026: import org.compass.core.Compass;
027: import org.compass.core.CompassSession;
028: import org.compass.gps.device.jpa.embedded.JpaCompassGps;
029:
030: /**
031: * Helper class to get different Compass constructs embedded with Open JPA.
032: *
033: * @author kimchy
034: */
035: public abstract class OpenJPAHelper {
036:
037: private OpenJPAHelper() {
038:
039: }
040:
041: /**
042: * Returns the Compass instance assoicated with the given OpenJPA {@link javax.persistence.EntityManagerFactory}.
043: * This allows to get a Compass instnace in order to perform search operations for example outside of a JPA
044: * transaction (for performance reasons, mostly there is no need to start a DB transaction).
045: */
046: public static Compass getCompass(EntityManagerFactory emf) {
047: OpenJPAEntityManagerFactory openJpaEmf = OpenJPAPersistence
048: .cast(emf);
049: return (Compass) openJpaEmf
050: .getUserObject(CompassProductDerivation.COMPASS_USER_OBJECT_KEY);
051: }
052:
053: /**
054: * Returns the Compass instance assoicated with the given OpenJPA {@link javax.persistence.EntityManager}.
055: * This allows to get a Compass instnace in order to perform search operations for example outside of a JPA
056: * transaction (for performance reasons, mostly there is no need to start a DB transaction).
057: */
058: public static Compass getCompass(EntityManager em) {
059: OpenJPAEntityManagerFactory openJpaEmf = OpenJPAPersistence
060: .cast(em).getEntityManagerFactory();
061: return (Compass) openJpaEmf
062: .getUserObject(CompassProductDerivation.COMPASS_USER_OBJECT_KEY);
063: }
064:
065: /**
066: * Returns the Compass Gps instance associated with the given OpenJPA {@link javax.persistence.EntityManagerFactory}.
067: * Used in order to perform {@link org.compass.gps.device.jpa.embedded.JpaCompassGps#index()} operation. Note, the index
068: * operation should not be perfomed within a running transaction.
069: */
070: public static JpaCompassGps getCompassGps(EntityManagerFactory emf) {
071: OpenJPAEntityManagerFactory openJpaEmf = OpenJPAPersistence
072: .cast(emf);
073: return (JpaCompassGps) openJpaEmf
074: .getUserObject(CompassProductDerivation.COMPASS_GPS_USER_OBJECT_KEY);
075: }
076:
077: /**
078: * Returns the Compass Gps instance associated with the given OpenJPA {@link javax.persistence.EntityManager}.
079: * Used in order to perform {@link org.compass.gps.device.jpa.embedded.JpaCompassGps#index()} operation. Note, the index
080: * operation should not be perfomed within a running transaction.
081: */
082: public static JpaCompassGps getCompassGps(EntityManager em) {
083: OpenJPAEntityManagerFactory openJpaEmf = OpenJPAPersistence
084: .cast(em).getEntityManagerFactory();
085: return (JpaCompassGps) openJpaEmf
086: .getUserObject(CompassProductDerivation.COMPASS_GPS_USER_OBJECT_KEY);
087: }
088:
089: /**
090: * Returns the current Compass session associated with the {@link javax.persistence.EntityManager}.
091: * Compass Session is associated with an Entity Manager when a transaction is started and removed when the
092: * transaction commits/rollsback.
093: *
094: * <p>The session can be used to perform searches that needs to take into account current transactional changes
095: * or to perform additional Compass operations that are not reflected by the mirroring feature.
096: */
097: public static CompassSession getCurrentCompassSession(
098: EntityManager em) {
099: OpenJPAEntityManager openJPAEntityManager = OpenJPAPersistence
100: .cast(em);
101: return (CompassSession) openJPAEntityManager
102: .getUserObject(CompassProductDerivation.COMPASS_SESSION_USER_OBJECT_KEY);
103: }
104:
105: /**
106: * Returns the index settings that are configured within the {@link javax.persistence.EntityManagerFactory}
107: * configuration. Can be used to configure exteranally a {@link org.compass.gps.device.jpa.embedded.JpaCompassGps}
108: * instance.
109: */
110: public Properties getIndexSettings(EntityManagerFactory emf) {
111: OpenJPAEntityManagerFactory openJpaEmf = OpenJPAPersistence
112: .cast(emf);
113: return (Properties) openJpaEmf
114: .getUserObject(CompassProductDerivation.COMPASS_INDEX_SETTINGS_USER_OBJECT_KEY);
115: }
116:
117: /**
118: * Returns the index settings that are configured within the {@link javax.persistence.EntityManager}
119: * configuration. Can be used to configure exteranally a {@link org.compass.gps.device.jpa.embedded.JpaCompassGps}
120: * instnace.
121: */
122: public Properties getIndexSettings(EntityManager em) {
123: OpenJPAEntityManagerFactory openJpaEmf = OpenJPAPersistence
124: .cast(em).getEntityManagerFactory();
125: return (Properties) openJpaEmf
126: .getUserObject(CompassProductDerivation.COMPASS_INDEX_SETTINGS_USER_OBJECT_KEY);
127: }
128: }
|