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