001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.backend.util;
016:
017: import java.io.Serializable;
018: import java.util.List;
019:
020: /**
021: * This class provides a way to manipulate Bean fields. This class
022: * assumes that the class passed to constructor (<code>BeanClass</code>)
023: * implements the Bean pattern - that is to open it's fields using
024: * getters and setters (read-only fields are permitted). The only names
025: * permitted are those starting with "get", "is" and "set". Another requirement
026: * is that Beans must have a constructor that doesn't take any
027: * parameters.
028: *
029: * @author <a href="mailto:rein@araneaframework.org">Rein Raudjärv</a>
030: *
031: * @see BeanUtil
032: */
033: public class BeanMapper implements Serializable {
034:
035: //*******************************************************************
036: // FIELDS
037: //*******************************************************************
038:
039: /**
040: * Holds the Bean <code>Class</code>.
041: */
042: private Class beanClass;
043:
044: /**
045: * Whetther to create missing beans during writing bean subfields.
046: */
047: private boolean createMissingBeans = false;
048:
049: //*********************************************************************
050: //* PUBLIC METHODS
051: //*********************************************************************
052:
053: /**
054: * Initializes the BeanMapper.
055: * @param beanClass the class implementing the Bean pattern.
056: */
057: public BeanMapper(Class beanClass) {
058: this .beanClass = beanClass;
059: }
060:
061: /**
062: * Initializes the BeanMapper.
063: *
064: * @param beanClass
065: * the class implementing the Bean pattern.
066: * @param createMissingBeans
067: * whetther to create missing beans during writing bean subfields
068: * (default is false).
069: */
070: public BeanMapper(Class beanClass, boolean createMissingBeans) {
071: this (beanClass);
072: this .createMissingBeans = createMissingBeans;
073: }
074:
075: /**
076: * Returns <code>List<String></code>- the <code>List</code> of Bean
077: * field names.
078: * @return <code>List<String></code>- the <code>List</code> of Bean
079: * field names.
080: */
081: public List getFields() {
082: return BeanUtil.getFields(beanClass);
083: }
084:
085: /**
086: * Returns the value of Bean field identified with name <code>field</code>
087: * for object <code>bean</code>
088: *
089: * @param bean
090: * Object, which value to return.
091: * @param fieldName
092: * The name of Bean field.
093: * @return The value of the field.
094: */
095: public Object getFieldValue(Object bean, String fieldName) {
096: return BeanUtil.getFieldValue(bean, fieldName);
097: }
098:
099: /**
100: * Sets the value of Bean field identified by name <code>field</code> for
101: * object <code>bean</code>.
102: *
103: * @param bean
104: * bean Object, which value to set.
105: * @param fieldName
106: * The name of Bean field.
107: * @param value
108: * The new value of the field.
109: */
110: public void setFieldValue(Object bean, String fieldName,
111: Object value) {
112: if (createMissingBeans) {
113: BeanUtil.fillFieldValue(bean, fieldName, value);
114: } else {
115: BeanUtil.setFieldValue(bean, fieldName, value);
116: }
117: }
118:
119: /**
120: * Returns type of Bean field identified by name <code>field</code>.
121: *
122: * @param fieldName
123: * The name of Bean field.
124: * @return The type of the field.
125: */
126: public Class getFieldType(String fieldName) {
127: return BeanUtil.getFieldType(beanClass, fieldName);
128: }
129:
130: /**
131: * Checks that the field identified by <code>fieldName</code> is a readable
132: * Bean field.
133: *
134: * @param fieldName
135: * Bean field name.
136: * @return if this field is in Bean.
137: */
138: public boolean isReadable(String fieldName) {
139: return BeanUtil.isReadable(beanClass, fieldName);
140: }
141:
142: /**
143: * Checks that the field identified by <code>fieldName</code> is a writable
144: * Bean field.
145: *
146: * @param fieldName
147: * Bean field name.
148: * @return if this field is in Bean.
149: */
150: public boolean isWritable(String fieldName) {
151: return BeanUtil.isWritable(beanClass, fieldName);
152: }
153: }
|