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: 2006 Andy Jefferson - merged with DeleteAction, UpdateAction classes
017: ...
018: **********************************************************************/package org.jpox.metadata;
019:
020: import java.io.Serializable;
021:
022: /**
023: * Foreign keys represent a consistency constraint in the database that must be
024: * maintained. This class enumerates the actions which happens when foreign-keys
025: * are updated or deleted.
026: *
027: * @version $Revision: 1.5 $
028: */
029: public class ForeignKeyAction implements Serializable {
030: /**
031: * update/delete-action="cascade". The database will automatically delete all rows
032: * that refer to the row being deleted
033: */
034: public static final ForeignKeyAction CASCADE = new ForeignKeyAction(
035: 1);
036:
037: /**
038: * update/delete-action="restrict". The user is required to explicitly make the
039: * relationship valid by application code
040: */
041: public static final ForeignKeyAction RESTRICT = new ForeignKeyAction(
042: 2);
043:
044: /**
045: * update/delete-action="null". The database will automatically nullify the columns
046: * in all rows that refer to the row being deleted
047: */
048: public static final ForeignKeyAction NULL = new ForeignKeyAction(3);
049:
050: /**
051: * update/delete-action="default". The database will automatically set the columns
052: * in all rows that refer to the row being deleted to their default value
053: */
054: public static final ForeignKeyAction DEFAULT = new ForeignKeyAction(
055: 4);
056:
057: /**
058: * update/delete-action="none". No foreign-key should be created.
059: */
060: public static final ForeignKeyAction NONE = new ForeignKeyAction(5);
061:
062: /** The type id */
063: private final int typeId;
064:
065: /**
066: * constructor
067: * @param i type id
068: */
069: protected ForeignKeyAction(int i) {
070: this .typeId = i;
071: }
072:
073: /**
074: * Returns a string representation of the object.
075: * @return a string representation of the object.
076: */
077: public String toString() {
078: switch (getType()) {
079: case 1:
080: return "cascade";
081: case 2:
082: return "restrict";
083: case 3:
084: return "null";
085: case 4:
086: return "default";
087: case 5:
088: return "none";
089: }
090: return "";
091: }
092:
093: /**
094: * Indicates whether some other object is "equal to" this one.
095: * @param o the reference object with which to compare.
096: * @return true if this object is the same as the obj argument; false otherwise.
097: */
098: public boolean equals(Object o) {
099: if (o instanceof ForeignKeyAction) {
100: return ((ForeignKeyAction) o).typeId == typeId;
101: }
102: return false;
103: }
104:
105: /**
106: * Accessor for the type.
107: * @return The type
108: */
109: protected int getType() {
110: return typeId;
111: }
112:
113: /**
114: * Return ForeignKeyDeleteAction from String.
115: * @param value delete-action attribute value
116: * @return Instance of ForeignKeyDeleteAction.
117: * If value invalid, return null.
118: */
119: public static ForeignKeyAction getForeignKeyAction(
120: final String value) {
121: if (value == null) {
122: return null;
123: } else if (ForeignKeyAction.CASCADE.toString()
124: .equalsIgnoreCase(value)) {
125: return ForeignKeyAction.CASCADE;
126: } else if (ForeignKeyAction.DEFAULT.toString()
127: .equalsIgnoreCase(value)) {
128: return ForeignKeyAction.DEFAULT;
129: } else if (ForeignKeyAction.NULL.toString().equalsIgnoreCase(
130: value)) {
131: return ForeignKeyAction.NULL;
132: } else if (ForeignKeyAction.RESTRICT.toString()
133: .equalsIgnoreCase(value)) {
134: return ForeignKeyAction.RESTRICT;
135: } else if (ForeignKeyAction.NONE.toString().equalsIgnoreCase(
136: value)) {
137: return ForeignKeyAction.NONE;
138: }
139: return null;
140: }
141: }
|