001    package org.cumulus4j.store;
002    
003    import javax.jdo.PersistenceManager;
004    
005    /**
006     * <p>
007     * Connection to the underlying datastore(s).
008     * </p><p>
009     * Cumulus4j can be used with either one or two underlying datastores.
010     * If it is used with two datastores, the {@link #getDataPM() actual data} and the {@link #getIndexPM() index information} is
011     * stored separately. The meta-data of the persistence-capable classes is replicated (and thus present in both datastores).
012     * </p>
013     *
014     * @author Andy Jefferson
015     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de (added javadoc)
016     */
017    public class PersistenceManagerConnection
018    {
019            /** PM for data (never null). */
020            private PersistenceManager pmData;
021            /** PM for indexes, could be null in which case use pmData */
022            private PersistenceManager pmIndex;
023    
024            public PersistenceManagerConnection(PersistenceManager pmData, PersistenceManager pmIndex)
025            {
026                    if (pmData == null)
027                            throw new IllegalArgumentException("pmData == null");
028    
029                    if (pmIndex == pmData)
030                            throw new IllegalArgumentException("pmIndex == pmData :: If there is no pmIndex, it should be null and not the same as pmData!");
031    
032                    this.pmData = pmData;
033                    this.pmIndex = pmIndex;
034            }
035    
036            /**
037             * Determine, if there is a separate index-PM.
038             *
039             * @return <code>true</code>, if there is a separate index-PM configured (i.e. {@link #getDataPM()} and {@link #getIndexPM()}
040             * return different objects); <code>false</code> otherwise (i.e. {@link #getDataPM()} and {@link #getIndexPM()} return the same PM).
041             */
042            public boolean indexHasOwnPM() {
043                    return pmIndex != null;
044            }
045    
046            /**
047             * Accessor for the PM to use for data.
048             * @return The PM to use for data
049             */
050            public PersistenceManager getDataPM() {
051                    return pmData;
052            }
053    
054            /**
055             * Accessor for the PM to use for indexes. This method falls back to the {@link #getDataPM() data-PM},
056             * if there is no separate index-PM. To determine, if there is a separate index-PM, it is recommended
057             * to use {@link #indexHasOwnPM()}.
058             * @return the PM to use for indexes. If there is no separate index-PM, this method returns the same
059             * as {@link #getDataPM()}.
060             */
061            public PersistenceManager getIndexPM() {
062                    if (pmIndex != null) {
063                            return pmIndex;
064                    }
065                    return pmData;
066            }
067    }