KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ReplicationMode


1 //$Id: ReplicationMode.java,v 1.3 2004/12/24 03:11:02 oneovthafew Exp $
2
package org.hibernate;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import org.hibernate.type.VersionType;
9
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 JavaDoc {
17     private final String JavaDoc name;
18     private static final Map JavaDoc INSTANCES = new HashMap JavaDoc();
19
20     public ReplicationMode(String JavaDoc name) {
21         this.name=name;
22     }
23     public String JavaDoc toString() {
24         return name;
25     }
26     public abstract boolean shouldOverwriteCurrentVersion(Object JavaDoc entity, Object JavaDoc currentVersion, Object JavaDoc newVersion, VersionType versionType);
27     /**
28      * Throw an exception when a row already exists.
29      */

30     public static final ReplicationMode EXCEPTION = new ReplicationMode("EXCEPTION") {
31         public boolean shouldOverwriteCurrentVersion(Object JavaDoc entity, Object JavaDoc currentVersion, Object JavaDoc newVersion, VersionType versionType) {
32             throw new AssertionFailure("should not be called");
33         }
34     };
35     /**
36      * Ignore replicated entities when a row already exists.
37      */

38     public static final ReplicationMode IGNORE = new ReplicationMode("IGNORE") {
39         public boolean shouldOverwriteCurrentVersion(Object JavaDoc entity, Object JavaDoc currentVersion, Object JavaDoc newVersion, VersionType versionType) {
40             return false;
41         }
42     };
43     /**
44      * Overwrite existing rows when a row already exists.
45      */

46     public static final ReplicationMode OVERWRITE = new ReplicationMode("OVERWRITE") {
47         public boolean shouldOverwriteCurrentVersion(Object JavaDoc entity, Object JavaDoc currentVersion, Object JavaDoc newVersion, VersionType versionType) {
48             return true;
49         }
50     };
51     /**
52      * When a row already exists, choose the latest version.
53      */

54     public static final ReplicationMode LATEST_VERSION = new ReplicationMode("LATEST_VERSION") {
55         public boolean shouldOverwriteCurrentVersion(Object JavaDoc entity, Object JavaDoc currentVersion, Object JavaDoc newVersion, VersionType versionType) {
56             if (versionType==null) return true; //always overwrite nonversioned data
57
return versionType.getComparator().compare(currentVersion, newVersion) <= 0;
58         }
59     };
60
61     static {
62         INSTANCES.put( LATEST_VERSION.name, LATEST_VERSION );
63         INSTANCES.put( IGNORE.name, IGNORE );
64         INSTANCES.put( OVERWRITE.name, OVERWRITE );
65         INSTANCES.put( EXCEPTION.name, EXCEPTION );
66     }
67
68     private Object JavaDoc readResolve() {
69         return INSTANCES.get(name);
70     }
71
72 }
73
74
75
76
77
78
79
Popular Tags