001    package org.cumulus4j.store.model;
002    
003    import javax.jdo.JDOObjectNotFoundException;
004    import javax.jdo.PersistenceManager;
005    import javax.jdo.identity.StringIdentity;
006    
007    import org.cumulus4j.store.crypto.CryptoContext;
008    
009    public class Sequence2DAO extends AbstractDAO {
010    
011            private int keyStoreRefID;
012    
013            public Sequence2DAO() { }
014    
015            /**
016             * @param pmData the backend-<code>PersistenceManager</code> used to access the underlying datastore; must not be <code>null</code>.
017             * If there are multiple datastores (data + index), then this is the one used for data.
018             * @param keyStoreRefID the key-store-reference-ID obtained usually from {@link CryptoContext#getKeyStoreRefID()}.
019             */
020            public Sequence2DAO(PersistenceManager pmData, int keyStoreRefID) {
021                    super(pmData);
022                    this.keyStoreRefID = keyStoreRefID;
023            }
024    
025            /**
026             * Get the <code>Sequence</code> identified by the given <code>sequenceName</code>.
027             * If no such <code>Sequence</code> exists, this method returns <code>null</code>.
028             * @param sequenceName the name of the sequence; must not be <code>null</code>.
029             * @return the <code>Sequence</code> identified by the given <code>sequenceName</code> or <code>null</code>, if no such
030             * <code>Sequence</code> exists.
031             */
032            public Sequence2 getSequence2(String sequenceName)
033            {
034                    StringIdentity id = new StringIdentity(Sequence2.class, Sequence2.createSequenceID(keyStoreRefID, sequenceName));
035                    Sequence2 sequence;
036                    try {
037                            sequence = (Sequence2) pm.getObjectById(id);
038                    } catch (JDOObjectNotFoundException x) {
039                            sequence = null;
040                    }
041                    return sequence;
042            }
043    
044            /**
045             * Get the <code>Sequence</code> identified by the given <code>sequenceName</code>.
046             * If no such <code>Sequence</code> exists, this method creates &amp; persists one.
047             * @param sequenceName the name of the sequence; must not be <code>null</code>.
048             * @return the <code>Sequence</code> identified by the given <code>sequenceName</code>; never <code>null</code>.
049             */
050            public Sequence2 createSequence2(String sequenceName)
051            {
052                    Sequence2 sequence = getSequence2(sequenceName);
053                    if (sequence == null)
054                            sequence = pm.makePersistent(new Sequence2(keyStoreRefID, sequenceName));
055    
056                    return sequence;
057            }
058    
059    }