01: //$Id: ReplicationMode.java 5060 2004-12-24 03:11:05Z oneovthafew $
02: package org.hibernate;
03:
04: import java.io.Serializable;
05: import java.util.HashMap;
06: import java.util.Map;
07:
08: import org.hibernate.type.VersionType;
09:
10: /**
11: * Represents a replication strategy.
12: *
13: * @see Session#replicate(Object, ReplicationMode)
14: * @author Gavin King
15: */
16: public abstract class ReplicationMode implements Serializable {
17: private final String name;
18: private static final Map INSTANCES = new HashMap();
19:
20: public ReplicationMode(String name) {
21: this .name = name;
22: }
23:
24: public String toString() {
25: return name;
26: }
27:
28: public abstract boolean shouldOverwriteCurrentVersion(
29: Object entity, Object currentVersion, Object newVersion,
30: VersionType versionType);
31:
32: /**
33: * Throw an exception when a row already exists.
34: */
35: public static final ReplicationMode EXCEPTION = new ReplicationMode(
36: "EXCEPTION") {
37: public boolean shouldOverwriteCurrentVersion(Object entity,
38: Object currentVersion, Object newVersion,
39: VersionType versionType) {
40: throw new AssertionFailure("should not be called");
41: }
42: };
43: /**
44: * Ignore replicated entities when a row already exists.
45: */
46: public static final ReplicationMode IGNORE = new ReplicationMode(
47: "IGNORE") {
48: public boolean shouldOverwriteCurrentVersion(Object entity,
49: Object currentVersion, Object newVersion,
50: VersionType versionType) {
51: return false;
52: }
53: };
54: /**
55: * Overwrite existing rows when a row already exists.
56: */
57: public static final ReplicationMode OVERWRITE = new ReplicationMode(
58: "OVERWRITE") {
59: public boolean shouldOverwriteCurrentVersion(Object entity,
60: Object currentVersion, Object newVersion,
61: VersionType versionType) {
62: return true;
63: }
64: };
65: /**
66: * When a row already exists, choose the latest version.
67: */
68: public static final ReplicationMode LATEST_VERSION = new ReplicationMode(
69: "LATEST_VERSION") {
70: public boolean shouldOverwriteCurrentVersion(Object entity,
71: Object currentVersion, Object newVersion,
72: VersionType versionType) {
73: if (versionType == null)
74: return true; //always overwrite nonversioned data
75: return versionType.getComparator().compare(currentVersion,
76: newVersion) <= 0;
77: }
78: };
79:
80: static {
81: INSTANCES.put(LATEST_VERSION.name, LATEST_VERSION);
82: INSTANCES.put(IGNORE.name, IGNORE);
83: INSTANCES.put(OVERWRITE.name, OVERWRITE);
84: INSTANCES.put(EXCEPTION.name, EXCEPTION);
85: }
86:
87: private Object readResolve() {
88: return INSTANCES.get(name);
89: }
90:
91: }
|