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 java.util.List;
021    
022    import javax.jdo.PersistenceManager;
023    
024    import org.cumulus4j.store.Cumulus4jStoreManager;
025    import org.cumulus4j.store.crypto.CryptoContext;
026    import org.datanucleus.store.ExecutionContext;
027    
028    /**
029     * Helper to find an {@link IndexEntry} for an object relation (1-1, 1-n or m-n).
030     * Even though {@link DefaultIndexEntryFactory} and {@link IndexEntryLong} are used for such relations, these
031     * classes should <b>not</b> be directly accessed in order to make refactorings easier (if this class is used for all
032     * object relations, it is possible to search for references of this class).
033     *
034     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
035     */
036    public class IndexEntryObjectRelationHelper
037    {
038            private static final IndexEntryFactory indexEntryFactoryLong = new DefaultIndexEntryFactory(IndexEntryLong.class);
039    
040            public static IndexEntryFactory getIndexEntryFactory() {
041                    return indexEntryFactoryLong;
042            }
043    
044            public static List<IndexEntry> getIndexEntriesIncludingSubClasses(CryptoContext cryptoContext, PersistenceManager pmIndex, FieldMeta fieldMeta, ClassMeta classMeta, Long indexedDataEntryID)
045            {
046                    ExecutionContext ec = cryptoContext.getExecutionContext();
047                    Cumulus4jStoreManager storeManager = (Cumulus4jStoreManager) ec.getStoreManager();
048                    List<ClassMeta> classMetaWithSubClassMetas = storeManager.getClassMetaWithSubClassMetas(ec, classMeta);
049                    return getIndexEntries(cryptoContext, pmIndex, fieldMeta, classMetaWithSubClassMetas, indexedDataEntryID);
050            }
051    
052            public static List<IndexEntry> getIndexEntries(CryptoContext cryptoContext, PersistenceManager pmIndex, FieldMeta fieldMeta, List<ClassMeta> classMetas, Long indexedDataEntryID)
053            {
054    //              List<IndexEntry> result = new ArrayList<IndexEntry>(classMetas.size());
055    //              for (ClassMeta classMeta : classMetas) {
056    //                      IndexEntry indexEntry = getIndexEntry(cryptoContext, pmIndex, fieldMeta, classMeta, indexedDataEntryID);
057    //                      if (indexEntry != null)
058    //                              result.add(indexEntry);
059    //              }
060    //              return result;
061                    return indexEntryFactoryLong.getIndexEntries(cryptoContext, pmIndex, fieldMeta, classMetas, indexedDataEntryID);
062            }
063    
064            /**
065             * Get an existing {@link IndexEntry} or <code>null</code>, if it does not exist.
066             * This method looks up an <code>IndexEntry</code> for a relation to the object referenced
067             * by the given <code>indexedDataEntryID</code> and the relation-type specified by the given <code>fieldMeta</code>.
068             * @param cryptoContext the crypto-context.
069             * @param pmIndex the backend-<code>PersistenceManager</code> used to access the index-datastore.
070             * @param fieldMeta the field pointing to the referenced object.
071             * @param classMeta the concrete owner type holding the field (might be a sub-class of {@link FieldMeta#getClassMeta() fieldMeta.classMeta}.
072             * @param indexedDataEntryID the {@link DataEntry#getDataEntryID() DataEntry.dataEntryID} of the referenced object.
073             * @return the appropriate {@link IndexEntry} or <code>null</code>.
074             */
075            public static IndexEntry getIndexEntry(CryptoContext cryptoContext, PersistenceManager pmIndex, FieldMeta fieldMeta, ClassMeta classMeta, Long indexedDataEntryID)
076            {
077                    return indexEntryFactoryLong.getIndexEntry(cryptoContext, pmIndex, fieldMeta, classMeta, indexedDataEntryID);
078            }
079    
080            /**
081             * Get an existing {@link IndexEntry} or create it, if it does not yet exist. This method behaves
082             * just like {@link #getIndexEntry(CryptoContext, PersistenceManager, FieldMeta, ClassMeta, Long)}, but instead of returning <code>null</code>,
083             * it creates an <code>IndexEntry</code>, if it does not yet exist.
084             * @param cryptoContext the crypto-context.
085             * @param pmIndex the backend-<code>PersistenceManager</code> used to access the index-datastore.
086             * @param fieldMeta the field pointing to the referenced object.
087             * @param classMeta the concrete owner type holding the field (might be a sub-class of {@link FieldMeta#getClassMeta() fieldMeta.classMeta}.
088             * @param indexedDataEntryID the {@link DataEntry#getDataEntryID() DataEntry.dataEntryID} of the referenced object.
089             * @return the appropriate {@link IndexEntry}; never <code>null</code>.
090             */
091            public static IndexEntry createIndexEntry(CryptoContext cryptoContext, PersistenceManager pmIndex, FieldMeta fieldMeta, ClassMeta classMeta, Long indexedDataEntryID)
092            {
093                    return indexEntryFactoryLong.createIndexEntry(cryptoContext, pmIndex, fieldMeta, classMeta, indexedDataEntryID);
094            }
095    }