View Javadoc

1   /*
2    * Copyright (C) 2004 TiongHiang Lee
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not,  write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
19   */
20  
21  package org.onemind.commons.relabean;
22  
23  import java.io.IOException;
24  import java.io.Writer;
25  import java.lang.reflect.InvocationTargetException;
26  import java.util.Collection;
27  import java.util.HashMap;
28  import java.util.Iterator;
29  import java.util.Map;
30  /***
31   * A RelationalBeanModel describe a set of inter-related beans in terms of their properties and relation to each others.
32   * 
33   * @author TiongHiang Lee (thlee@onemindsoft.org)
34   * @version $Id: RelationalBeanModel.java,v 1.2 2004/08/26 16:28:58 thlee Exp $ $Name:  $
35   */
36  public class RelationalBeanModel
37  {
38  
39      /*** the descriptor * */
40      private RelationalBeanModelDescriptor _desc;
41  
42      /*** the bean infos * */
43      private Map _beanInfos = new HashMap();
44  
45      /***
46       * Constructor {@inheritDoc}
47       */
48      public RelationalBeanModel(RelationalBeanModelDescriptor desc)
49      {
50          _desc = desc;
51      }
52  
53      /***
54       * Add a bean info
55       * 
56       * @param info the relational bean info
57       */
58      public void addBeanInfo(RelationalBeanInfo info)
59      {
60          _beanInfos.put(info.getBeanClass(), info);
61      }
62  
63      /***
64       * Get a bean info
65       * 
66       * @param beanClass the class
67       * @return the bean info for the class
68       */
69      public RelationalBeanInfo getBeanInfo(Class beanClass)
70      {
71          return (RelationalBeanInfo) _beanInfos.get(beanClass);
72      }
73  
74      /***
75       * Get all the bean infos
76       * 
77       * @return the bean infos
78       */
79      public Collection getBeanInfos()
80      {
81          return _beanInfos.values();
82      }
83  
84      /***
85       * The descriptor
86       * 
87       * @return the descriptor
88       */
89      public RelationalBeanModelDescriptor getDescriptor()
90      {
91          return _desc;
92      }
93  
94      /***
95       * Set the descriptor
96       * 
97       * @param descriptor the descriptor
98       */
99      public void setDesc(RelationalBeanModelDescriptor descriptor)
100     {
101         _desc = descriptor;
102     }
103 
104     /***
105      * Dump the property
106      * 
107      * @param prefix the prefix
108      * @param writer the writer
109      * @throws IOException if there's IO problem
110      */
111     public void dump(String prefix, Writer writer) throws IOException
112     {
113         Iterator it = getBeanInfos().iterator();
114         writer.write(prefix + "RelationBeanModel name: "
115                 + getDescriptor().getModelName() + "\n");
116         while (it.hasNext())
117         {
118             RelationalBeanInfo info = (RelationalBeanInfo) it.next();
119             info.dump(prefix + "\t", writer);
120         }
121     }
122 
123     /***
124      * Read the value of the property of bean
125      * 
126      * @param obj the bean
127      * @param propName the property
128      * @return the value
129      * @throws IllegalAccessException if there's access problem
130      * @throws InvocationTargetException if there's exception invoking method
131      */
132     public Object readProperty(Object obj, String propName)
133             throws IllegalAccessException, InvocationTargetException
134     {
135         RelationalBeanInfo info = getBeanInfo(obj.getClass());
136         if (info == null)
137         {
138             throw new IllegalArgumentException("Unknown class "
139                     + obj.getClass());
140         }
141         PropertyDescriptor desc = info.getProperty(propName);
142         if (desc != null)
143         {
144             return desc.getReadMethod().invoke(obj, new Object[]{});
145         }
146         RelationDescriptor rdesc = info.getRelation(propName);
147         if (rdesc != null)
148         {
149             return rdesc.getReadMethod().invoke(obj, new Object[]{});
150         }
151         throw new IllegalArgumentException("Unknown property " + propName
152                 + " of " + obj.getClass());
153     }
154 
155     /***
156      * Write the property to the bean
157      * 
158      * @param obj the bean
159      * @param propName the property name
160      * @param value the bean value to set/write
161      * @return return the value set
162      * @throws IllegalAccessException if there's access problem
163      * @throws InvocationTargetException if there's exception invoking method
164      */
165     public Object writeProperty(Object obj, String propName, Object value)
166             throws IllegalAccessException, InvocationTargetException
167     {
168         RelationalBeanInfo info = getBeanInfo(obj.getClass());
169         if (info == null)
170         {
171             throw new IllegalArgumentException("Unknown class "
172                     + obj.getClass());
173         }
174         PropertyDescriptor desc = info.getProperty(propName);
175         if (desc != null)
176         {
177             return desc.getWriteMethod().invoke(obj, new Object[]{value});
178         }
179         RelationDescriptor rdesc = info.getRelation(propName);
180         if (rdesc != null)
181         {
182             return rdesc.getWriteMethod().invoke(obj, new Object[]{value});
183         }
184         throw new IllegalArgumentException("Unknown property " + propName
185                 + " of " + obj.getClass());
186     }
187 }