001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
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: */
016:
017: package org.romaframework.core.entity;
018:
019: import java.lang.reflect.Constructor;
020: import java.lang.reflect.InvocationTargetException;
021:
022: import org.romaframework.core.exception.LocalizedRuntimeException;
023:
024: public class EntityHelper {
025:
026: public static Object[] getEntities(ComposedEntity[] iEntities) {
027: Object[] objects = new Object[iEntities.length];
028: for (int i = 0; i < iEntities.length; ++i) {
029: objects[i] = iEntities[i].getEntity();
030: }
031: return objects;
032: }
033:
034: public static Object createObject(Object iSourceEntity, Class iClass)
035: throws IllegalArgumentException, InstantiationException,
036: IllegalAccessException, InvocationTargetException {
037: Object newInstance = null;
038:
039: if (iSourceEntity == null) {
040: try {
041: newInstance = iClass.newInstance();
042: } catch (InstantiationException e) {
043: throw new LocalizedRuntimeException(
044: "Cannot create object of class "
045: + iClass
046: + " since it has no default constructor!",
047: e);
048: } catch (IllegalAccessError e) {
049: throw new LocalizedRuntimeException(
050: "Cannot create object of class "
051: + iClass
052: + " since constructor is not visible! Change its visibility to public.",
053: e);
054: } catch (Exception e) {
055: throw new LocalizedRuntimeException(
056: "Cannot create object of class "
057: + iClass
058: + " since an error was thrown by executing the default constructor!",
059: e);
060: }
061: } else {
062: Class currentClass = iSourceEntity.getClass();
063: do {
064: try {
065: // TRY TO ASSIGN THE ENTITY IN THE CONSTRUCTOR
066: Constructor constructor = iClass
067: .getConstructor(new Class[] { currentClass });
068: newInstance = constructor
069: .newInstance(new Object[] { iSourceEntity });
070: break;
071: } catch (NoSuchMethodException e) {
072: currentClass = currentClass.getSuperclass();
073: }
074: } while (currentClass != null
075: && currentClass != Object.class);
076:
077: if (newInstance == null) {
078: // INSTANCE THE OBJECT AND ASSIGN THE ENTITY AFTER BY SETTER METHOD
079: newInstance = iClass.newInstance();
080:
081: // CREATE THE COMPOSED EDIT INSTANCE
082: if (newInstance instanceof ComposedEntity)
083: ((ComposedEntity) newInstance)
084: .setEntity(iSourceEntity);
085: }
086: }
087:
088: return newInstance;
089: }
090:
091: public static Object getInstanceValue(Object iSource) {
092: if (iSource instanceof ComposedEntity)
093: return ((ComposedEntity) iSource).getEntity();
094: return iSource;
095: }
096:
097: public static void assignEntity(Object iComposedObject,
098: Object iSourceEntity) {
099: if (iComposedObject instanceof ComposedEntity)
100: // CREATE THE COMPOSED EDIT INSTANCE
101: ((ComposedEntity) iComposedObject).setEntity(iSourceEntity);
102: }
103: }
|