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 javax.jdo.annotations.Column;
021    import javax.jdo.annotations.IdentityType;
022    import javax.jdo.annotations.NullValue;
023    import javax.jdo.annotations.PersistenceCapable;
024    import javax.jdo.annotations.Persistent;
025    import javax.jdo.annotations.PrimaryKey;
026    
027    import org.cumulus4j.store.Cumulus4jIncrementGenerator;
028    
029    /**
030     * Persistent sequence entity used by {@link Cumulus4jIncrementGenerator}.
031     *
032     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
033     * @deprecated This class did not support multitenancy-in-single-database and was therefore replaced by {@link Sequence2}.
034     */
035    @Deprecated
036    @PersistenceCapable(identityType=IdentityType.APPLICATION, detachable="true")
037    public class Sequence
038    {
039    //      /**
040    //       * Get the <code>Sequence</code> identified by the given <code>sequenceName</code>.
041    //       * If no such <code>Sequence</code> exists, this method returns <code>null</code>.
042    //       * @param pm the backend-<code>PersistenceManager</code> used to access the underlying datastore; must not be <code>null</code>.
043    //       * @param sequenceName the name of the sequence; must not be <code>null</code>.
044    //       * @return the <code>Sequence</code> identified by the given <code>sequenceName</code> or <code>null</code>, if no such
045    //       * <code>Sequence</code> exists.
046    //       */
047    //      public static Sequence getSequence(PersistenceManager pm, String sequenceName)
048    //      {
049    //              StringIdentity id = new StringIdentity(Sequence.class, sequenceName);
050    //              Sequence sequence;
051    //              try {
052    //                      sequence = (Sequence) pm.getObjectById(id);
053    //              } catch (JDOObjectNotFoundException x) {
054    //                      sequence = null;
055    //              }
056    //              return sequence;
057    //      }
058    //
059    //      /**
060    //       * Get the <code>Sequence</code> identified by the given <code>sequenceName</code>.
061    //       * If no such <code>Sequence</code> exists, this method creates &amp; persists one.
062    //       * @param pm the backend-<code>PersistenceManager</code> used to access the underlying datastore; must not be <code>null</code>.
063    //       * @param sequenceName the name of the sequence; must not be <code>null</code>.
064    //       * @return the <code>Sequence</code> identified by the given <code>sequenceName</code>; never <code>null</code>.
065    //       */
066    //      public static Sequence createSequence(PersistenceManager pm, String sequenceName)
067    //      {
068    //              Sequence sequence = getSequence(pm, sequenceName);
069    //              if (sequence == null)
070    //                      sequence = pm.makePersistent(new Sequence(sequenceName));
071    //
072    //              return sequence;
073    //      }
074    
075            @PrimaryKey
076            @Persistent(nullValue=NullValue.EXCEPTION)
077            @Column(length=255)
078            private String sequenceName;
079    
080            private long nextValue = 1;
081    
082            /**
083             * Default constructor. Should never be used by actual code! It exists only to fulfill the JDO requirements.
084             */
085            protected Sequence() { }
086    
087            /**
088             * Constructor creating a <code>Sequence</code> with the given primary key.
089             * @param sequenceName the name of the sequence; must not be <code>null</code>.
090             */
091            protected Sequence(String sequenceName)
092            {
093                    if (sequenceName == null)
094                            throw new IllegalArgumentException("sequenceName == null");
095    
096                    this.sequenceName = sequenceName;
097            }
098    
099            /**
100             * Get the name of the sequence.
101             * @return the name of the sequence.
102             */
103            public String getSequenceName() {
104                    return sequenceName;
105            }
106    
107            /**
108             * Get the next value (i.e. the first unused value) for this sequence.
109             * @return the next value (i.e. the first unused value) for this sequence.
110             */
111            public long getNextValue() {
112                    return nextValue;
113            }
114    
115            /**
116             * Set the next value (i.e. the first unused value) for this sequence.
117             * @param nextValue the next value (i.e. the first unused value) for this sequence.
118             */
119            public void setNextValue(long nextValue) {
120                    this.nextValue = nextValue;
121            }
122    }