001    package org.cumulus4j.store.model;
002    
003    import javax.jdo.JDOObjectNotFoundException;
004    import javax.jdo.PersistenceManager;
005    import javax.jdo.identity.LongIdentity;
006    import javax.jdo.identity.SingleFieldIdentity;
007    
008    public class EncryptionCoordinateSetDAO extends AbstractDAO {
009    
010            public EncryptionCoordinateSetDAO() { }
011    
012            /**
013             * @param pm the backend-{@link PersistenceManager} (the one used for data, if there is a separate index-DB used).
014             */
015            public EncryptionCoordinateSetDAO(PersistenceManager pm) {
016                    super(pm);
017            }
018    
019            /**
020             * {@inheritDoc}
021             * @param pm the backend-{@link PersistenceManager} (the one used for data, if there is a separate index-DB used).
022             */
023            @Override
024            public void setPersistenceManager(PersistenceManager pm) {
025                    super.setPersistenceManager(pm);
026            }
027    
028            /**
029             * Get an existing <code>EncryptionCoordinateSet</code> identified by its {@link #getEncryptionCoordinateSetID() encryptionCoordinateSetID}.
030             *
031             * @param encryptionCoordinateSetID the {@link #getEncryptionCoordinateSetID() identifier} of the searched instance.
032             * @return the <code>EncryptionCoordinateSet</code> identified by the given <code>encryptionCoordinateSetID</code> or
033             * <code>null</code>, if no such instance exists in the datastore.
034             */
035            public EncryptionCoordinateSet getEncryptionCoordinateSet(int encryptionCoordinateSetID)
036            {
037                    SingleFieldIdentity id = new LongIdentity(EncryptionCoordinateSet.class, encryptionCoordinateSetID);
038                    try {
039                            EncryptionCoordinateSet encryptionCoordinateSet = (EncryptionCoordinateSet) pm.getObjectById(id);
040                            return encryptionCoordinateSet;
041                    } catch (JDOObjectNotFoundException x) {
042                            return null;
043                    }
044            }
045    
046            /**
047             * <p>
048             * Get an existing <code>EncryptionCoordinateSet</code> identified by its unique properties.
049             * </p>
050             * <p>
051             * As each <code>EncryptionCoordinateSet</code> maps all encryption settings to an ID, all
052             * properties of this class except for the ID form a unique index together. At the moment,
053             * these are: {@link #getCipherTransformation() cipher-transformation} and {@link #getMACAlgorithm() MAC-algorithm}.
054             * </p>
055             *
056             * @param cipherTransformation the {@link #getCipherTransformation() cipher-transformation} of the searched instance.
057             * Must not be <code>null</code>.
058             * @param macAlgorithm the {@link #getMACAlgorithm()} of the searched instance. Must not be <code>null</code>
059             * (use {@value #MAC_ALGORITHM_NONE} for no MAC).
060             * @return the <code>EncryptionCoordinateSet</code> identified by the given properties or
061             * <code>null</code>, if no such instance exists in the datastore.
062             * @see #createEncryptionCoordinateSet(PersistenceManager, String, String)
063             */
064            public EncryptionCoordinateSet getEncryptionCoordinateSet(String cipherTransformation, String macAlgorithm)
065            {
066                    if (cipherTransformation == null)
067                            throw new IllegalArgumentException("cipherTransformation == null");
068    
069                    if (macAlgorithm == null)
070                            throw new IllegalArgumentException("macAlgorithm == null");
071    
072                    javax.jdo.Query q = pm.newNamedQuery(EncryptionCoordinateSet.class, "getEncryptionCoordinateSetByAllAlgorithms");
073                    return (EncryptionCoordinateSet) q.execute(cipherTransformation, macAlgorithm);
074                    // UNIQUE query does not need to be closed, because there is no result list lingering.
075            }
076    
077            /**
078             * <p>
079             * Get an existing <code>EncryptionCoordinateSet</code> identified by its unique properties or create one
080             * if necessary.
081             * </p>
082             * <p>
083             * This method is similar to {@link #getEncryptionCoordinateSet(PersistenceManager, String, String)}, but
084             * creates a new <code>EncryptionCoordinateSet</code> instead of returning <code>null</code>, if there is
085             * no existing instance, yet.
086             * </p>
087             *
088             * @param cipherTransformation the {@link #getCipherTransformation() cipher-transformation} of the searched instance.
089             * Must not be <code>null</code>.
090             * @param macAlgorithm the {@link #getMACAlgorithm()} of the searched instance. Must not be <code>null</code>
091             * (use {@value #MAC_ALGORITHM_NONE} for no MAC).
092             * @return the <code>EncryptionCoordinateSet</code> identified by the given properties. This method never returns
093             * <code>null</code>, but instead creates and persists a new instance if needed.
094             * @see #getEncryptionCoordinateSet(PersistenceManager, String, String)
095             */
096            public EncryptionCoordinateSet createEncryptionCoordinateSet(String cipherTransformation, String macAlgorithm)
097            {
098                    EncryptionCoordinateSet encryptionCoordinateSet = getEncryptionCoordinateSet(cipherTransformation, macAlgorithm);
099                    if (encryptionCoordinateSet == null) {
100                            encryptionCoordinateSet = pm.makePersistent(new EncryptionCoordinateSet(cipherTransformation, macAlgorithm));
101                            // It is essential that the first ID is 0 (and never a negative value), because
102                            // we encode this ID into the binary data assuming that it is positive or 0!
103                            // Hence, we check here, already.
104                            if (encryptionCoordinateSet.getEncryptionCoordinateSetID() < 0)
105                                    throw new IllegalStateException("encryptionCoordinateSetID = " + encryptionCoordinateSet.getEncryptionCoordinateSetID() + " < 0!!!");
106                    }
107    
108                    return encryptionCoordinateSet;
109            }
110    
111    }