001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.persistency;
010:
011: /**
012: * Record state representation. <br>
013: * The state diagram it describes goes as follows:<br>
014: *
015: * <PRE>
016: * Completely new record:
017: * NEW -> NEW_MODIFIED -> DATA_SAVED <=> DATA_MODIFIED <-|
018: *
019: * Record retrived from DB:
020: * NEW_INITIALIZING -> NEW_INITIALIZED -> DATA_MODIFIED <=> DATA_SAVED <-|
021: *
022: * On delete:
023: * NEW_MODIFIED -> DELETED
024: * DATA_SAVED -> DELETED
025: * DATA_MODIFIED-> DELETED
026: * NEW_INITIALIZED-> DELETED
027: * </PRE>
028: * @author Gennady Krizhevsky
029: */
030: public class State {
031: private String name;
032:
033: protected State(String name) {
034: this .name = name;
035: }
036:
037: public String getName() {
038: return name;
039: }
040:
041: public boolean isDirty() {
042: boolean newModified = this == NEW_MODIFIED;
043: boolean dataModified = this == DATA_MODIFIED;
044: return newModified || dataModified;
045: }
046:
047: public boolean isInitializedNonModified() {
048: return this == NEW_INITIALIZED || this == DATA_SAVED;
049: }
050:
051: public boolean isInitialized() {
052: return isInitializedNonModified() || this == DATA_MODIFIED;
053: }
054:
055: public boolean isDeleted() {
056: return this == DELETED;
057: }
058:
059: public boolean isSaved() {
060: return this == DATA_SAVED;
061: }
062:
063: public static boolean isToInsert(State state) {
064: return state == NEW_MODIFIED || state == NEW;
065: }
066:
067: public static boolean isToInsert(PersistentObject persistentObject) {
068: if (persistentObject == null
069: || persistentObject.record() == null) {
070: return false;
071: } else {
072: return isToInsert(persistentObject.record().getState());
073: }
074: }
075:
076: public static State name2state(String type) {
077: State state = null;
078: for (int i = 0; i < states.length; i++) {
079: if (states[i].getName().equals(type)) {
080: state = states[i];
081: break;
082: }
083: }
084: if (state != null) {
085: return state;
086: } else {
087: throw new OdalRuntimePersistencyException(
088: "Cannot get type by state for type " + type);
089: }
090: }
091:
092: public static boolean isInitialized(Record record) {
093: return record != null && record.isInitialized();
094: }
095:
096: public static State[] getStates() {
097: return states;
098: }
099:
100: public String toString() {
101: return name;
102: }
103:
104: public static final State NEW = new State("new");
105: public static final State NEW_MODIFIED = new State("new_modified");
106: public static final State DATA_SAVED = new State("data_saved");
107: public static final State DATA_MODIFIED = new State("data_modified");
108: public static final State NEW_INITIALIZING = new State(
109: "new_initializing");
110: public static final State NEW_INITIALIZED = new State(
111: "new_initialized");
112: public static final State DELETED = new State("deleted");
113:
114: private static final State[] states = new State[] { NEW,
115: NEW_MODIFIED, DATA_SAVED, DATA_MODIFIED, NEW_INITIALIZING,
116: NEW_INITIALIZED, DELETED, };
117:
118: }
|