1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.enspire.gemini.updaters;
25
26 import java.util.Collection;
27
28 import com.enspire.gemini.RelationshipUpdater;
29 import com.enspire.reflection.PropertyReflection;
30
31 /***
32 * Updates one end of a relationship, that have the upper multiplicity
33 * greater than 1, and is represented as a Collection or its subclasses.
34 *
35 * @author Dragan Djuric <dragand@dev.java.net>
36 * @since 1.0
37 **/
38 public class CollectionPropertyRelationshipUpdater implements RelationshipUpdater {
39
40 private PropertyReflection propertyReflection;
41
42 /***
43 * Constructs an object leaving its dependencies unset.
44 */
45 public CollectionPropertyRelationshipUpdater() {
46 super();
47 }
48
49 /***
50 * Constructs an object and sets the propertyReflection dependency.
51 * @param reflection the object that is used to manipulate JavaBean properties.
52 */
53 public CollectionPropertyRelationshipUpdater(PropertyReflection reflection) {
54 super();
55 this.propertyReflection = reflection;
56 }
57
58 /***
59 * Gets propertyReflection - the object that is used to manipulate
60 * JavaBean properties.
61 * @return propertyReflection the object that is used to manipulate
62 * JavaBean properties
63 */
64 public PropertyReflection getPropertyReflection() {
65 return this.propertyReflection;
66 }
67
68 /***
69 * Sets propertyReflection - the object that is used to manipulate
70 * JavaBean properties.
71 * @param propertyReflection the propertyReflection to set.
72 */
73 public void setPropertyReflection(PropertyReflection propertyReflection) {
74 this.propertyReflection = propertyReflection;
75 }
76
77 /***
78 * Adds the new element to the collection if it does not already contain it.
79 *
80 * @see com.enspire.gemini.RelationshipUpdater#set(java.lang.Object, java.lang.String, java.lang.Object)
81 */
82 public Object set(Object owner, String propertyName, Object value) {
83 if ((owner == null) || (value == null)) {
84 return null;
85 }
86 Collection existing = (Collection)getPropertyReflection().
87 getSimpleProperty(owner, propertyName);
88 if ((existing != null) && (!existing.contains(value))) {
89 existing.add(value);
90 }
91 return null;
92 }
93
94 /***
95 * Removes the element from the collection.
96 *
97 * @see com.enspire.gemini.RelationshipUpdater#unset(java.lang.Object, java.lang.String, java.lang.Object)
98 */
99 public Object unset(Object owner, String propertyName, Object value) {
100 if (owner == null) {
101 return null;
102 }
103 Collection existing = (Collection)getPropertyReflection().
104 getSimpleProperty(owner, propertyName);
105 if (existing != null) {
106 existing.remove(value);
107 }
108 return null;
109 }
110
111 }