001: /**********************************************************************
002: Copyright (c) 2004 Erik Bengtson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: ...
017: **********************************************************************/package org.jpox.metadata;
018:
019: import java.io.Serializable;
020:
021: /**
022: * Three common strategies for versioning instances are supported by standard
023: * metadata. These include state-comparison, timestamp, and version-number.
024: * <ul>
025: * <li><b>state-image</b> involves comparing the values in specific columns to
026: * determine if the database row was changed.</li>
027: * <li><b>date-time</b> involves comparing the value in a date-time column in the table.
028: * The first time in a transaction the row is updated, the timestamp value is
029: * updated to the current time.</li>
030: * <li><b>version-number</b> involves comparing the value in a numeric column in the table.
031: * The first time in a transaction the row is updated, the version-number column
032: * value is incremented.</li>
033: * </ul>
034: *
035: * @version $Revision: 1.6 $
036: */
037: public class VersionStrategy implements Serializable {
038: /**
039: * strategy="none"
040: */
041: public static final VersionStrategy NONE = new VersionStrategy(0);
042:
043: /**
044: * strategy="state-image"
045: */
046: public static final VersionStrategy STATE_IMAGE = new VersionStrategy(
047: 1);
048:
049: /**
050: * strategy="date-time"
051: */
052: public static final VersionStrategy DATE_TIME = new VersionStrategy(
053: 2);
054:
055: /**
056: * strategy="version-number"
057: */
058: public static final VersionStrategy VERSION_NUMBER = new VersionStrategy(
059: 3);
060:
061: /**
062: * state-image|date-time|version-number
063: */
064: private final int typeId;
065:
066: /**
067: * constructor
068: * @param i type id
069: */
070: private VersionStrategy(int i) {
071: this .typeId = i;
072: }
073:
074: /**
075: * Indicates whether some other object is "equal to" this one.
076: * @param o the reference object with which to compare.
077: * @return true if this object is the same as the obj argument; false otherwise.
078: */
079: public boolean equals(Object o) {
080: if (o instanceof VersionStrategy) {
081: return ((VersionStrategy) o).typeId == typeId;
082: }
083: return false;
084: }
085:
086: /**
087: * Returns a string representation of the object.
088: * @return a string representation of the object.
089: */
090: public String toString() {
091: switch (typeId) {
092: case 0:
093: return "none";
094: case 1:
095: return "state-image";
096: case 2:
097: return "date-time";
098: case 3:
099: return "version-number";
100: }
101: return "";
102: }
103:
104: protected int getType() {
105: return typeId;
106: }
107:
108: /**
109: * Return VersionStrategy from String.
110: * @param value strategy attribute value
111: * @return Instance of VersionStrategy.
112: * If value invalid, return null.
113: */
114: public static VersionStrategy getVersionStrategy(final String value) {
115: if (value == null) {
116: return null;
117: } else if (VersionStrategy.NONE.toString().equalsIgnoreCase(
118: value)) {
119: return VersionStrategy.NONE;
120: } else if (VersionStrategy.STATE_IMAGE.toString()
121: .equalsIgnoreCase(value)) {
122: return VersionStrategy.STATE_IMAGE;
123: } else if (VersionStrategy.DATE_TIME.toString()
124: .equalsIgnoreCase(value)) {
125: return VersionStrategy.DATE_TIME;
126: } else if (VersionStrategy.VERSION_NUMBER.toString()
127: .equalsIgnoreCase(value)) {
128: return VersionStrategy.VERSION_NUMBER;
129: }
130: return null;
131: }
132: }
|