001    /*
002     * Cumulus4j - Securing your data in the cloud - http://cumulus4j.org
003     * Copyright (C) 2011 NightLabs Consulting GmbH
004     *
005     * This program is free software: you can redistribute it and/or modify
006     * it under the terms of the GNU Affero General Public License as
007     * published by the Free Software Foundation, either version 3 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU Affero General Public License for more details.
014     *
015     * You should have received a copy of the GNU Affero General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    package org.cumulus4j.store.model;
019    
020    import javax.jdo.PersistenceManager;
021    
022    /**
023     * Helper to find an {@link IndexEntry} for an object relation (1-1, 1-n or m-n).
024     * Even though {@link DefaultIndexEntryFactory} and {@link IndexEntryLong} are used for such relations, these
025     * classes should <b>not</b> be directly accessed in order to make refactorings easier (if this class is used for all
026     * object relations, it is possible to search for references of this class).
027     *
028     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
029     */
030    public class IndexEntryObjectRelationHelper
031    {
032            private static final IndexEntryFactory indexEntryFactoryLong = new DefaultIndexEntryFactory(IndexEntryLong.class);
033    
034            public static IndexEntryFactory getIndexEntryFactory() {
035                    return indexEntryFactoryLong;
036            }
037    
038            /**
039             * Get an existing {@link IndexEntry} or <code>null</code>, if it does not exist.
040             * This method looks up an <code>IndexEntry</code> for a relation to the object referenced
041             * by the given <code>indexedDataEntryID</code> and the relation-type specified by the given <code>fieldMeta</code>.
042             *
043             * @param pmIndex the backend-<code>PersistenceManager</code> used to access the index-datastore.
044             * @param fieldMeta the field pointing to the referenced object.
045             * @param indexedDataEntryID the {@link DataEntry#getDataEntryID() DataEntry.dataEntryID} of the referenced object.
046             * @return the appropriate {@link IndexEntry} or <code>null</code>.
047             */
048            public static IndexEntry getIndexEntry(PersistenceManager pmIndex, FieldMeta fieldMeta, Long indexedDataEntryID)
049            {
050                    return indexEntryFactoryLong.getIndexEntry(pmIndex, fieldMeta, indexedDataEntryID);
051            }
052    
053            /**
054             * Get an existing {@link IndexEntry} or create it, if it does not yet exist. This method behaves
055             * just like {@link #getIndexEntry(PersistenceManager, FieldMeta, Long)}, but instead of returning <code>null</code>,
056             * it creates an <code>IndexEntry</code>, if it does not yet exist.
057             *
058             * @param pmIndex the backend-<code>PersistenceManager</code> used to access the index-datastore.
059             * @param fieldMeta the field pointing to the referenced object.
060             * @param indexedDataEntryID the {@link DataEntry#getDataEntryID() DataEntry.dataEntryID} of the referenced object.
061             * @return the appropriate {@link IndexEntry}; never <code>null</code>.
062             */
063            public static IndexEntry createIndexEntry(PersistenceManager pmIndex, FieldMeta fieldMeta, Long indexedDataEntryID)
064            {
065                    return indexEntryFactoryLong.createIndexEntry(pmIndex, fieldMeta, indexedDataEntryID);
066            }
067    }