View Javadoc

1   /*
2   * E-nspire Gemini.
3   * A Java and AspectJ based framework that enables transparent 
4   * bidirectional relationships between Plain Old Java Objects.
5   * 
6   * Copyright (C) 2005 Dragan Djuric
7   * 
8   * This program is free software; you can redistribute it and/or
9   * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
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
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  * 
22  * Contact the author at dragand@dev.java.net
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 }