1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.services.persistence.generic.dto;
18
19 import java.io.Serializable;
20
21 import ch.elca.el4j.services.persistence.generic.primarykey.PrimaryKeyGenerator;
22
23 /**
24 * This abstract dto brings some basic elements for managing optimistic locking.
25 * Optimistic locking is implemented by using a
26 * <code>PrimaryKeyGenerator</code>. Primary key are strings in this case.
27 *
28 * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/services/persistence/generic/dto/AbstractDto.java $
29 *
30 * @deprecated Please use <code>AbstractIntOptimisticLockingDto</code> instead.
31 * @see AbstractIntOptimisticLockingDto
32 * @author Martin Zeltner (MZE)
33 */
34 public abstract class AbstractDto implements Serializable {
35 /**
36 * Is used for optimistic locking. This last modification key must be equals
37 * with the one in database to be able to edit an entry, otherwise the entry
38 * has been edited.
39 */
40 private String m_lastModificationKey;
41
42 /**
43 * Is used for optimistic locking. This is the next modification key. If the
44 * dto will be saved successfully on database, the last modification will be
45 * set to the current modification key to indicate, that this entry in
46 * database has been modificated.
47 */
48 private String m_currentModificationKey;
49
50 /**
51 * Primary key generator to generate modification keys for dtos.
52 */
53 private PrimaryKeyGenerator m_modificationKeyGenerator;
54
55 /**
56 * @param modificationKeyGenerator
57 * Is the modificationKeyGenerator to set.
58 */
59 public void setModificationKeyGenerator(
60 final PrimaryKeyGenerator modificationKeyGenerator) {
61 this.m_modificationKeyGenerator = modificationKeyGenerator;
62 }
63
64 /**
65 * @return Returns the lastModificationKey.
66 */
67 public String getLastModificationKey() {
68 return m_lastModificationKey;
69 }
70
71 /**
72 * @param lastModificationKey
73 * The lastModificationKey to set.
74 */
75 public void setLastModificationKey(String lastModificationKey) {
76 m_lastModificationKey = lastModificationKey;
77 }
78
79 /**
80 * This method will be called to get the current modification key, which
81 * will be used to save the current modification key as the last
82 * modification key on database.
83 *
84 * @return Generates and returns for each instance a new modification key.
85 */
86 public String getCurrentModificationKey() {
87 if (m_currentModificationKey == null) {
88 m_currentModificationKey = getNewModificationKey();
89 }
90 return m_currentModificationKey;
91 }
92
93 /**
94 * This method will be called by user if this dto was written to database
95 * and so the last modification key has been replaced by the current
96 * modification key. This method sets the last modification key to the
97 * current modification key and generates a new current modififation key.
98 */
99 public void useNextModificationKey() {
100 m_lastModificationKey = m_currentModificationKey;
101 m_currentModificationKey = getNewModificationKey();
102 }
103
104 /**
105 * This method generates a new modification key.
106 *
107 * @return Returns the new generated modification key.
108 */
109 private String getNewModificationKey() {
110 return m_modificationKeyGenerator.getPrimaryKey();
111 }
112 }