~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/ReplicationMode.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: ReplicationMode.java 5060 2004-12-24 03:11:05Z oneovthafew $
 
2
package org.hibernate;
 
3
 
 
4
import java.io.Serializable;
 
5
import java.util.HashMap;
 
6
import java.util.Map;
 
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 {
 
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
        public String toString() {
 
24
                return name;
 
25
        }
 
26
        public abstract boolean shouldOverwriteCurrentVersion(Object entity, Object currentVersion, Object 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 entity, Object currentVersion, Object 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 entity, Object currentVersion, Object 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 entity, Object currentVersion, Object 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 entity, Object currentVersion, Object 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 readResolve() {
 
69
                return INSTANCES.get(name);
 
70
        }
 
71
 
 
72
}
 
73
 
 
74
 
 
75
 
 
76
 
 
77
 
 
78