001 package org.cumulus4j.store.model; 002 003 import java.util.Collection; 004 import java.util.HashSet; 005 import java.util.Set; 006 007 import javax.jdo.JDOObjectNotFoundException; 008 import javax.jdo.PersistenceManager; 009 import javax.jdo.identity.LongIdentity; 010 011 import org.cumulus4j.store.crypto.CryptoContext; 012 013 public class DataEntryDAO extends AbstractDAO { 014 015 private int keyStoreRefID; 016 017 public DataEntryDAO() { } 018 019 /** 020 * Create a new instance with the backend-<code>PersistenceManager</code> used for data. 021 * @param pmData the backend-<code>PersistenceManager</code>. Must not be <code>null</code>. 022 * @param keyStoreRefID the key-store-reference-ID obtained usually from {@link CryptoContext#getKeyStoreRefID()}. 023 */ 024 public DataEntryDAO(PersistenceManager pmData, int keyStoreRefID) { 025 super(pmData); 026 this.keyStoreRefID = keyStoreRefID; 027 } 028 029 /** 030 * Get the <code>DataEntry</code> identified by the specified {@link #getDataEntryID() dataEntryID} or 031 * <code>null</code> if no such instance exists. 032 * @param dataEntryID the <code>DataEntry</code>'s {@link #getDataEntryID() identifier}. 033 * @return the <code>DataEntry</code> matching the given <code>dataEntryID</code> or <code>null</code>, if no such instance exists. 034 */ 035 public DataEntry getDataEntry(long dataEntryID) 036 { 037 DataEntry dataEntry; 038 try { 039 dataEntry = (DataEntry) pm.getObjectById(new LongIdentity(DataEntry.class, dataEntryID)); 040 } catch (JDOObjectNotFoundException x) { 041 dataEntry = null; 042 } 043 return dataEntry; 044 } 045 046 /** 047 * Get the <code>DataEntry</code> identified by the given type and JDO/JPA-object-ID. 048 * 049 * @param classMeta reference to the searched <code>DataEntry</code>'s {@link #getClassMeta() classMeta} (which must match 050 * the searched instance's concrete type - <b>not</b> the root-type of the inheritance tree!). 051 * @param objectID the <code>String</code>-representation of the JDO/JPA-object-ID. 052 * @return the <code>DataEntry</code> matching the given combination of <code>classMeta</code> and <code>objectID</code>; 053 * or <code>null</code>, if no such instance exists. 054 */ 055 public DataEntry getDataEntry(ClassMeta classMeta, String objectID) 056 { 057 javax.jdo.Query q = pm.newNamedQuery(DataEntry.class, DataEntry.NamedQueries.getDataEntryByClassMetaClassIDAndObjectID); 058 return (DataEntry) q.execute(keyStoreRefID, classMeta.getClassID(), objectID); 059 // UNIQUE query does not need to be closed, because there is no result list lingering. 060 } 061 062 /** 063 * <p> 064 * Get the {@link #getDataEntryID() dataEntryID} of the <code>DataEntry</code> identified by the 065 * given type and JDO/JPA-object-ID. 066 * </p> 067 * <p> 068 * This method is equivalent to first calling 069 * </p> 070 * <pre>DataEntry e = {@link #getDataEntry(PersistenceManager, ClassMeta, String)}</pre> 071 * <p> 072 * and then 073 * </p> 074 * <pre>e == null ? null : Long.valueOf({@link #getDataEntryID() e.getDataEntryID()})</pre> 075 * <p> 076 * but faster, because it does not query unnecessary data from the underlying database. 077 * </p> 078 * 079 * @param classMeta reference to the searched <code>DataEntry</code>'s {@link #getClassMeta() classMeta} (which must match 080 * the searched instance's concrete type - <b>not</b> the root-type of the inheritance tree!). 081 * @param objectID the <code>String</code>-representation of the JDO/JPA-object-ID. 082 * @return the {@link #getDataEntryID() dataEntryID} of the <code>DataEntry</code> matching the 083 * given combination of <code>classMeta</code> and <code>objectID</code>; 084 * or <code>null</code>, if no such instance exists. 085 */ 086 public Long getDataEntryID(ClassMeta classMeta, String objectID) 087 { 088 javax.jdo.Query q = pm.newNamedQuery(DataEntry.class, DataEntry.NamedQueries.getDataEntryIDByClassMetaClassIDAndObjectID); 089 return (Long) q.execute(keyStoreRefID, classMeta.getClassID(), objectID); 090 // UNIQUE query does not need to be closed, because there is no result list lingering. 091 } 092 093 /** 094 * <p> 095 * Get the {@link #getDataEntryID() dataEntryID}s of all those <code>DataEntry</code> instances 096 * which do <b>not</b> match the given type and JDO/JPA-object-ID. 097 * </p> 098 * <p> 099 * This method is thus the negation of {@link #getDataEntryID(PersistenceManager, ClassMeta, String)}. 100 * </p> 101 * 102 * @param classMeta reference to the searched <code>DataEntry</code>'s {@link #getClassMeta() classMeta} (which must match 103 * the searched instance's concrete type - <b>not</b> the root-type of the inheritance tree!). 104 * @param notThisObjectID the <code>String</code>-representation of the JDO/JPA-object-ID, which should be 105 * excluded. 106 * @return the {@link #getDataEntryID() dataEntryID}s of those <code>DataEntry</code>s which match the given 107 * <code>classMeta</code> but have an object-ID different from the one specified as <code>notThisObjectID</code>. 108 */ 109 public Set<Long> getDataEntryIDsNegated(ClassMeta classMeta, String notThisObjectID) 110 { 111 javax.jdo.Query q = pm.newNamedQuery(DataEntry.class, DataEntry.NamedQueries.getDataEntryIDsByClassMetaClassIDAndObjectIDNegated); 112 @SuppressWarnings("unchecked") 113 Collection<Long> dataEntryIDsColl = (Collection<Long>) q.execute(keyStoreRefID, classMeta.getClassID(), notThisObjectID); 114 Set<Long> dataEntryIDsSet = new HashSet<Long>(dataEntryIDsColl); 115 q.closeAll(); 116 return dataEntryIDsSet; 117 } 118 }