1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2008 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.dao;
18
19 import java.util.ArrayList;
20 import java.util.IdentityHashMap;
21 import java.util.List;
22
23 /**
24 * This class represents a policy on how to merge object graphs in the identity fixer.
25 *
26 * @svnLink $Revision: 3875 $;$Date: 2009-08-04 14:35:53 +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/dao/IdentityFixerMergePolicy.java $
27 *
28 * @see AbstractIdentityFixer
29 *
30 * @author Andreas Rueedlinger (ARR)
31 */
32 public class IdentityFixerMergePolicy {
33
34 /** The update policy. */
35 private UpdatePolicy m_updatePolicy;
36
37 /** The objects to update, only set when <code>m_updatePolicy == UpdatePolicy.UPDATE_CHOSEN</code>. */
38 private List<Object> m_objectsToUpdate;
39
40 /** Should Preparation be performed. */
41 private boolean m_performPreparation;
42
43 /**
44 * A map of [updated -> anchor] used to correctly merge collections.
45 */
46 private IdentityHashMap<Object, Object> m_collectionEntryMapping;
47
48 /**
49 * Default constructor.
50 * Sets update policy to all and preparation will be performed.
51 */
52 protected IdentityFixerMergePolicy() {
53 m_updatePolicy = UpdatePolicy.UPDATE_ALL;
54 m_performPreparation = true;
55 m_collectionEntryMapping = new IdentityHashMap<Object, Object>();
56 }
57
58 /**
59 * Constructor to customize the policy.
60 * @param updatePolicy the update policy to use.
61 * @param objectsToUpdate the objectsToUpdate if UDPATE_CHOSEN policy is chosen above.
62 * @param performPreparation should preparation be performed (eg. for unwrapping proxies).
63 * @param collectionEntryMapping the collectionEntryMapping [updated -> anchor]
64 * used to correctly merge collections.
65 */
66 protected IdentityFixerMergePolicy(UpdatePolicy updatePolicy, List<Object> objectsToUpdate,
67 boolean performPreparation, IdentityHashMap<Object, Object> collectionEntryMapping) {
68 m_updatePolicy = updatePolicy;
69 m_objectsToUpdate = objectsToUpdate;
70 m_performPreparation = performPreparation;
71 m_collectionEntryMapping = collectionEntryMapping;
72 }
73
74 /**
75 * @return the update policy of this id fixer merge policy.
76 */
77 public UpdatePolicy getUpdatePolicy() {
78 return m_updatePolicy;
79 }
80
81 /**
82 * Set the objects to update explicitly.
83 * @param objectsToUpdate the list of objects to update.
84 */
85 public void setObjectsToUpdate(List<Object> objectsToUpdate) {
86 m_objectsToUpdate = objectsToUpdate;
87 }
88
89 /**
90 * @return The objects to update,
91 * only set when <code>getUpdatePolicy == UpdatePolicy.UPDATE_CHOSEN</code>.
92 */
93 public List<Object> getObjectsToUpdate() {
94 return m_objectsToUpdate;
95 }
96
97 /**
98 * @return if preparation is needed.
99 */
100 public boolean needsPreparation() {
101 return m_performPreparation;
102 }
103
104 /**
105 * @return A map of [updated -> anchor] used to correctly merge collections.
106 */
107 public IdentityHashMap<Object, Object> getCollectionEntryMapping() {
108 return m_collectionEntryMapping;
109 }
110
111 /**
112 * This enumeration describes how the identity fixer should handle object updates.
113 */
114 public enum UpdatePolicy {
115 /**
116 * Only complement the identity fixer representatives,
117 * no update of the old objects.
118 */
119 NO_UPDATE,
120
121 /**
122 * Update the specified objects only, do not touch the others.
123 * Add new objects.
124 */
125 UPDATE_CHOSEN,
126
127 /**
128 * Update all the objects.
129 */
130 UPDATE_ALL
131
132 }
133
134 /**
135 * @return a policy forcing all objects to be updated.
136 */
137 public static IdentityFixerMergePolicy reloadAllPolicy() {
138 return new IdentityFixerMergePolicy();
139 }
140
141 /**
142 * @param objectsToUpdate the objects to be updated.
143 * @return a policy forcing only the specified objects to be updated,
144 * leaving the rest untouched.
145 */
146 public static IdentityFixerMergePolicy reloadObjectsPolicy(List<Object> objectsToUpdate) {
147 IdentityFixerMergePolicy obj = new IdentityFixerMergePolicy(UpdatePolicy.UPDATE_CHOSEN,
148 new ArrayList<Object>(objectsToUpdate), true, new IdentityHashMap<Object, Object>());
149 return obj;
150 }
151
152 /**
153 * @return a policy leaving all the objects of the previous graph untouched,
154 * only extending it by the new objects.
155 */
156 public static IdentityFixerMergePolicy extendOnlyPolicy() {
157 IdentityFixerMergePolicy obj = new IdentityFixerMergePolicy(UpdatePolicy.NO_UPDATE, null, true,
158 new IdentityHashMap<Object, Object>());
159 return obj;
160 }
161
162 /**
163 * @param collectionEntryMapping the collectionEntryMapping [updated -> anchor]
164 * used to correctly merge collections.
165 * @return a policy forcing all objects to be updated.
166 */
167 public static IdentityFixerMergePolicy reloadAllPolicy(IdentityHashMap<Object, Object> collectionEntryMapping) {
168 IdentityFixerMergePolicy obj = new IdentityFixerMergePolicy(UpdatePolicy.UPDATE_ALL, null,
169 true, collectionEntryMapping);
170 return obj;
171 }
172
173 /**
174 * @param objectsToUpdate the objects to be updated.
175 * @param collectionEntryMapping the collectionEntryMapping [updated -> anchor]
176 * used to correctly merge collections.
177 * @return a policy forcing only the specified objects to be updated,
178 * leaving the rest untouched.
179 */
180 public static IdentityFixerMergePolicy reloadObjectsPolicy(List<Object> objectsToUpdate,
181 IdentityHashMap<Object, Object> collectionEntryMapping) {
182 IdentityFixerMergePolicy obj = new IdentityFixerMergePolicy(UpdatePolicy.UPDATE_CHOSEN,
183 new ArrayList<Object>(objectsToUpdate), true, collectionEntryMapping);
184 return obj;
185 }
186
187 /**
188 * @param collectionEntryMapping the collectionEntryMapping [updated -> anchor]
189 * used to correctly merge collections.
190 * @return a policy leaving all the objects of the previous graph untouched,
191 * only extending it by the new objects.
192 */
193 public static IdentityFixerMergePolicy extendOnlyPolicy(IdentityHashMap<Object, Object> collectionEntryMapping) {
194 IdentityFixerMergePolicy obj = new IdentityFixerMergePolicy(UpdatePolicy.NO_UPDATE, null,
195 true, collectionEntryMapping);
196 return obj;
197 }
198
199 /**
200 * @param updatePolicy the update policy to use.
201 * @param objectsToUpdate the objectsToUpdate if UDPATE_CHOSEN policy is chosen above.
202 * @param performPreparation should preparation be performed (eg. for unwrapping proxies).
203 * @param collectionEntryMapping the collectionEntryMapping [updated -> anchor]
204 * used to correctly merge collections.
205 * @return the custom policy specified by the arguments.
206 */
207 public static IdentityFixerMergePolicy customPolicy(UpdatePolicy updatePolicy, List<Object> objectsToUpdate,
208 boolean performPreparation, IdentityHashMap<Object, Object> collectionEntryMapping) {
209
210 List<Object> objs = null;
211 if (updatePolicy == UpdatePolicy.UPDATE_CHOSEN) {
212 objs = new ArrayList<Object>(objectsToUpdate);
213 }
214 IdentityFixerMergePolicy obj = new IdentityFixerMergePolicy(updatePolicy, objs,
215 performPreparation, collectionEntryMapping);
216 return obj;
217 }
218
219
220
221 }
222