Subversion Repositories WebE

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
33 PointedEar 1
/*
2
 * Hibernate, Relational Persistence for Idiomatic Java
3
 *
4
 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5
 * indicated by the @author tags or express copyright attribution
6
 * statements applied by the authors.  All third-party contributions are
7
 * distributed under license by Red Hat Middleware LLC.
8
 *
9
 * This copyrighted material is made available to anyone wishing to use, modify,
10
 * copy, or redistribute it subject to the terms and conditions of the GNU
11
 * Lesser General Public License, as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
16
 * for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this distribution; if not, write to:
20
 * Free Software Foundation, Inc.
21
 * 51 Franklin Street, Fifth Floor
22
 * Boston, MA  02110-1301  USA
23
 *
24
 */
25
package org.hibernate;
26
 
27
import java.io.Serializable;
28
import java.util.HashMap;
29
import java.util.Map;
30
 
31
import org.hibernate.type.VersionType;
32
 
33
/**
34
 * Represents a replication strategy.
35
 *
36
 * @see Session#replicate(Object, ReplicationMode)
37
 * @author Gavin King
38
 */
39
public abstract class ReplicationMode implements Serializable {
40
        private final String name;
41
        private static final Map INSTANCES = new HashMap();
42
 
43
        public ReplicationMode(String name) {
44
                this.name=name;
45
        }
46
        public String toString() {
47
                return name;
48
        }
49
        public abstract boolean shouldOverwriteCurrentVersion(Object entity, Object currentVersion, Object newVersion, VersionType versionType);
50
        /**
51
         * Throw an exception when a row already exists.
52
         */
53
        public static final ReplicationMode EXCEPTION = new ReplicationMode("EXCEPTION") {
54
                public boolean shouldOverwriteCurrentVersion(Object entity, Object currentVersion, Object newVersion, VersionType versionType) {
55
                        throw new AssertionFailure("should not be called");
56
                }
57
        };
58
        /**
59
         * Ignore replicated entities when a row already exists.
60
         */
61
        public static final ReplicationMode IGNORE = new ReplicationMode("IGNORE") {
62
                public boolean shouldOverwriteCurrentVersion(Object entity, Object currentVersion, Object newVersion, VersionType versionType) {
63
                        return false;
64
                }
65
        };
66
        /**
67
         * Overwrite existing rows when a row already exists.
68
         */
69
        public static final ReplicationMode OVERWRITE = new ReplicationMode("OVERWRITE") {
70
                public boolean shouldOverwriteCurrentVersion(Object entity, Object currentVersion, Object newVersion, VersionType versionType) {
71
                        return true;
72
                }
73
        };
74
        /**
75
         * When a row already exists, choose the latest version.
76
         */
77
        public static final ReplicationMode LATEST_VERSION = new ReplicationMode("LATEST_VERSION") {
78
                public boolean shouldOverwriteCurrentVersion(Object entity, Object currentVersion, Object newVersion, VersionType versionType) {
79
                        if (versionType==null) return true; //always overwrite nonversioned data
80
                        return versionType.getComparator().compare(currentVersion, newVersion) <= 0;
81
                }
82
        };
83
 
84
        static {
85
                INSTANCES.put( LATEST_VERSION.name, LATEST_VERSION );
86
                INSTANCES.put( IGNORE.name, IGNORE );
87
                INSTANCES.put( OVERWRITE.name, OVERWRITE );
88
                INSTANCES.put( EXCEPTION.name, EXCEPTION );
89
        }
90
 
91
        private Object readResolve() {
92
                return INSTANCES.get(name);
93
        }
94
 
95
}
96
 
97
 
98
 
99
 
100
 
101