Source code: org/jdbf/engine/repository/RepositoryView.java
1 /*
2 * 03/12/2002 - 14:08:11
3 *
4 * RepositoryView.java - JDBF Object Relational mapping system
5 * Copyright (C) 2002 Giovanni Martone
6 * giovannimartone@hotmail.com
7 * http://jdbf.sourceforge.net
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23 package org.jdbf.engine.repository;
24
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import org.jdbf.engine.basic.ObjectMapped;
28 import org.jdbf.engine.caching.CachedObject;
29 import org.jdbf.engine.mapping.*;
30 import org.jdbf.engine.reflection.ReflectionManager;
31
32 /**
33 * <code>RepositoryView</code> is the class that represents
34 * the repositoryView specified in repository.
35 * His purpose is that to map the informations of Java object
36 * to RDBMS.
37 * In this way RepositoryView can fills and returns the Java object after
38 * the queries are executed.
39 */
40 public class RepositoryView implements Repository{
41
42 /** Class name */
43 private static final String CLASS_NAME = "org.jdbf.engine.repository.RepositoryView";
44
45 /** ClassDescriptor */
46 private BeanDescriptor beanDescriptor;
47
48
49 /**
50 * Create the object
51 */
52 public RepositoryView(){
53 beanDescriptor = null;
54 }
55
56
57 /**
58 * Create a ObjectMapped by a CLASS_NAME and a hashmap that contains
59 * a list with the names of the properties and relative values.
60 * If props is null the object created is null.
61 *
62 * @param name CLASS_NAME
63 * @param props list property/value
64 * @return ObjectMapped
65 */
66 public ObjectMapped createObject(String name,HashMap props)throws MappingException{
67 return ReflectionManager.createBean(name,props);
68 }
69
70
71 /**
72 * Return BeanDescriptor object.
73 * @return BeanDescriptor
74 */
75 public BeanDescriptor getBeanDescriptor(){
76 return beanDescriptor;
77 }
78
79
80 /**
81 * Return the current value of a property specified in propertyName.
82 * If the property isn't specified in repository, UnreportedPropertyException throws.
83 * @param object ObjectMapped object,propertyName name of property
84 * @return Object current value of property
85 * @throws UnreportedPropertyExcpetion
86 */
87 public Object getPropertyValue(ObjectMapped object, String propertyName) throws MappingException{
88
89 //return the array that ocntains the itemDescriptor
90 ArrayList items = beanDescriptor.getItemDescriptors();
91 Object propertyValue = null;
92
93 //scan the array
94 for(int i = 0; i < items.size(); i++){
95 ItemDescriptor itemDescriptor = (ItemDescriptor)items.get(i);
96 String name = itemDescriptor.getPropertyName();
97 //if name property exist....
98 if(name.equals(propertyName)){
99 //...return the value
100 propertyValue = ReflectionManager.getValueByName(object,propertyName);
101 break;
102 }
103 }
104
105 //if propertyValue is null
106 if(propertyValue == null){
107 throw new MappingException("mapping.invalidPropertyName", propertyName);
108 }
109 return propertyValue;
110 }
111
112
113 /**
114 * Set the BeanDescriptor object
115 * @param BeanDescriptor
116 */
117 public void setBeanDescriptor(BeanDescriptor beanDescriptor){
118 this.beanDescriptor = beanDescriptor;
119 }
120
121
122 /**
123 * Set the value of a property specified in propertyName.
124 * If the property isn't specified in repository, UnreportedPropertyException throws.
125 * @param object ObjectMapped object
126 * @param propertyName name of property
127 * @param propertyValue value to set
128 * @throws UnreportedPropertyExcpetion
129 */
130 public ObjectMapped setPropertyValue(ObjectMapped object,String propertyName, Object propertyValue)
131 throws MappingException{
132
133 //return the array that ocntains the itemDescriptor
134 ArrayList items = beanDescriptor.getItemDescriptors();
135 boolean found = false;
136
137 //scan the array
138 for(int i = 0; i < items.size(); i++){
139 ItemDescriptor itemDescriptor = (ItemDescriptor)items.get(i);
140 String name = itemDescriptor.getPropertyName();
141 //if name property exist....
142 if(name.equals(propertyName)){
143 //...set the value
144 object = ReflectionManager.setValueByName(object,propertyName,propertyValue);
145 System.out.println(object);
146 found = true;
147 break;
148 }
149 }
150
151 //if propertyValue is null
152 if(!found){
153 throw new MappingException("mapping.invalidPropertyName", propertyName);
154 }
155
156 return object;
157 }
158
159
160
161 public String toString(){
162
163 StringBuffer buff = new StringBuffer();
164 buff.append(CLASS_NAME).append("[ ").append("\n")
165 .append(beanDescriptor).append("\n").append("]");
166
167
168 return buff.toString();
169 }
170
171 }