001    package org.cumulus4j.store.model;
002    
003    import javax.jdo.annotations.Column;
004    import javax.jdo.annotations.IdGeneratorStrategy;
005    import javax.jdo.annotations.IdentityType;
006    import javax.jdo.annotations.NullValue;
007    import javax.jdo.annotations.PersistenceCapable;
008    import javax.jdo.annotations.Persistent;
009    import javax.jdo.annotations.PrimaryKey;
010    import javax.jdo.annotations.Queries;
011    import javax.jdo.annotations.Query;
012    import javax.jdo.annotations.Sequence;
013    import javax.jdo.annotations.SequenceStrategy;
014    import javax.jdo.annotations.Unique;
015    import javax.jdo.annotations.Version;
016    import javax.jdo.annotations.VersionStrategy;
017    
018    @PersistenceCapable(identityType=IdentityType.APPLICATION, detachable="true")
019    @Version(strategy=VersionStrategy.VERSION_NUMBER)
020    @Sequence(name="KeyStoreRefSequence", datastoreSequence="KeyStoreRefSequence", initialValue=0, strategy=SequenceStrategy.CONTIGUOUS)
021    @Queries({
022            @Query(name="getKeyStoreRefByKeyStoreID", value="SELECT UNIQUE WHERE this.keyStoreID == :keyStoreID")
023    })
024    public class KeyStoreRef {
025            /**
026             * Reserved value for {@link #getKeyStoreRefID() keyStoreRefID} meaning that the object referencing this <code>keyStoreRefID</code>
027             * is not related to any key-store, at all, but global.
028             */
029            public static final int GLOBAL_KEY_STORE_REF_ID = -1;
030    
031            /**
032             * Internal constructor. This exists only for JDO and should not be used by application code!
033             */
034            protected KeyStoreRef() { }
035    
036            /**
037             * Create an instance of <code>DataEntry</code>.
038             * @param keyStoreID the <code>KeyStore</code>'s ID.
039             */
040            public KeyStoreRef(String keyStoreID) {
041                    this.keyStoreID = keyStoreID;
042            }
043    
044            @PrimaryKey
045            @Persistent(valueStrategy=IdGeneratorStrategy.NATIVE, sequence="KeyStoreRefSequence")
046            private Long keyStoreRefID;
047    
048            @Persistent(nullValue=NullValue.EXCEPTION)
049            @Unique(name="KeyStoreRef_keyStoreID")
050            @Column(length=255)
051            private String keyStoreID;
052    
053            public int getKeyStoreRefID() {
054                    Long result = keyStoreRefID;
055                    if (result == null)
056                            return -666;
057    
058                    if (result.longValue() > Integer.MAX_VALUE)
059                            throw new IllegalStateException("keyStoreRefID > Integer.MAX_VALUE :: " + result + " > " + Integer.MAX_VALUE);
060    
061                    return result.intValue();
062            }
063    
064            public String getKeyStoreID() {
065                    return keyStoreID;
066            }
067    
068            @Override
069            public int hashCode() {
070                    final int keyStoreRefID = getKeyStoreRefID();
071                    return keyStoreRefID;
072            }
073    
074            @Override
075            public boolean equals(Object obj) {
076                    if (this == obj) return true;
077                    if (obj == null) return false;
078                    if (getClass() != obj.getClass()) return false;
079                    KeyStoreRef other = (KeyStoreRef) obj;
080                    return this.getKeyStoreRefID() == -666 ? false : this.getKeyStoreRefID() == other.getKeyStoreRefID();
081            }
082    }