001: package org.drools.brms.server.util;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.lang.reflect.Field;
020: import java.lang.reflect.InvocationTargetException;
021: import java.lang.reflect.Method;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import org.drools.brms.client.rpc.MetaData;
027: import org.drools.repository.RulesRepositoryException;
028:
029: /**
030: * This utility uses reflection to map from the MetaData DTO to
031: * the AssetItem back end class, to adhere to the DRY principle.
032: *
033: * AssetItem is not a remotable instance, but MetaData is.
034: *
035: * @author Michael Neale
036: */
037: public class MetaDataMapper {
038:
039: private Map writeMappingsForClass = new HashMap();
040:
041: private Map readMappipngsForClass = new HashMap();
042:
043: public void copyFromMetaData(MetaData data, Object target) {
044: Map writeMappings = getWriteMappings(data, target);
045:
046: for (Iterator iter = writeMappings.entrySet().iterator(); iter
047: .hasNext();) {
048: Map.Entry e = (Map.Entry) iter.next();
049: Field f = (Field) e.getKey();
050: Method m = (Method) e.getValue();
051:
052: try {
053: m.invoke(target, new Object[] { f.get(data) });
054: } catch (IllegalArgumentException e1) {
055: throw new RulesRepositoryException(e1);
056: } catch (IllegalAccessException e1) {
057: throw new RulesRepositoryException(e1);
058: } catch (InvocationTargetException e1) {
059: throw new RulesRepositoryException(e1);
060: }
061:
062: }
063:
064: }
065:
066: private Map getWriteMappings(MetaData data, Object target) {
067: if (!this .writeMappingsForClass.containsKey(target.getClass())) {
068: Map writeMappings = loadWriteMappings(data, target
069: .getClass());
070: writeMappingsForClass.put(target.getClass(), writeMappings);
071: }
072: return (Map) writeMappingsForClass.get(target.getClass());
073: }
074:
075: private Map loadWriteMappings(MetaData data, Class bean) {
076: Map mappings = new HashMap();
077: Field fields[] = data.getClass().getFields();
078: for (int i = 0; i < fields.length; i++) {
079: Field f = fields[i];
080: String old = f.getName();
081: String name = Character.toUpperCase(old.charAt(0))
082: + old.substring(1);
083:
084: name = "update" + name;
085:
086: Method m;
087: try {
088: m = bean.getMethod(name, new Class[] { f.getType() });
089: mappings.put(f, m);
090: } catch (SecurityException e) {
091: throw new RulesRepositoryException(
092: "Unable to map meta data", e);
093: } catch (NoSuchMethodException e) {
094: //ignore
095: }
096:
097: }
098: return mappings;
099: }
100:
101: public void copyToMetaData(MetaData data, Object source) {
102: Map readMappings = getReadMappings(data, source);
103:
104: for (Iterator iter = readMappings.entrySet().iterator(); iter
105: .hasNext();) {
106: Map.Entry e = (Map.Entry) iter.next();
107: Field f = (Field) e.getKey();
108: Method m = (Method) e.getValue();
109:
110: try {
111: f.set(data, m.invoke(source, null));
112: } catch (IllegalArgumentException e1) {
113: throw new RulesRepositoryException(e1);
114: } catch (IllegalAccessException e1) {
115: throw new RulesRepositoryException(e1);
116: } catch (InvocationTargetException e1) {
117: throw new RulesRepositoryException(e1);
118: }
119:
120: }
121:
122: }
123:
124: private Map getReadMappings(MetaData data, Object source) {
125: if (!this .readMappipngsForClass.containsKey(source.getClass())) {
126: this .readMappipngsForClass.put(source.getClass(),
127: loadReadMappings(data, source.getClass()));
128: }
129: return (Map) this .readMappipngsForClass.get(source.getClass());
130: }
131:
132: private Map loadReadMappings(MetaData data, Class bean) {
133:
134: Map mappings = new HashMap();
135: Field fields[] = data.getClass().getFields();
136: for (int i = 0; i < fields.length; i++) {
137: Field f = fields[i];
138: String old = f.getName();
139: String name = Character.toUpperCase(old.charAt(0))
140: + old.substring(1);
141:
142: if (f.getType() == Boolean.class) {
143: name = "is" + name;
144: } else {
145: name = "get" + name;
146: }
147:
148: Method m;
149: try {
150: m = bean.getMethod(name, null);
151: if (f.getType() == m.getReturnType()) {
152: mappings.put(f, m);
153: }
154: } catch (SecurityException e) {
155: throw new RulesRepositoryException(
156: "Unable to map meta data", e);
157: } catch (NoSuchMethodException e) {
158: //ignore
159: }
160:
161: }
162: return mappings;
163:
164: }
165:
166: }
|