001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.eclipselink;
022
023 import java.lang.reflect.Field;
024 import java.lang.reflect.Modifier;
025 import java.util.ArrayList;
026 import java.util.List;
027
028 import javax.persistence.Entity;
029
030 import org.granite.logging.Logger;
031 import org.granite.messaging.amf.io.util.DefaultClassGetter;
032 import org.granite.eclipselink.EclipseLinkClassGetter;
033 import org.granite.eclipselink.EclipseLinkProxy;
034 import org.granite.util.ClassUtil;
035
036 import org.eclipse.persistence.indirection.IndirectContainer;
037 import org.eclipse.persistence.indirection.ValueHolderInterface;
038
039 /**
040 * @author William DRAI
041 */
042 public class EclipseLinkClassGetter extends DefaultClassGetter {
043
044 private final static Logger log = Logger.getLogger(EclipseLinkClassGetter.class);
045
046 @Override
047 public Class<?> getClass(Object o) {
048
049 if (o instanceof ValueHolderInterface) {
050 ValueHolderInterface holder = (ValueHolderInterface)o;
051
052 String className = (
053 holder.isInstantiated() ?
054 holder.getValue().getClass().getName() :
055 Object.class.getName()
056 );
057
058 if (className != null && className.length() > 0) {
059 try {
060 return ClassUtil.forName(className);
061 } catch (Exception e) {
062 log.warn(e, "Could not get class with initializer: %s for: %s", className, className);
063 }
064 }
065 // fallback...
066 return Object.class;
067 }
068 else if (o instanceof EclipseLinkProxy) {
069 return ((EclipseLinkProxy)o).getProxiedClass();
070 }
071
072 return super.getClass(o);
073 }
074
075 @Override
076 public boolean isEntity(Object o) {
077 return o.getClass().isAnnotationPresent(Entity.class);
078 }
079
080 @Override
081 public boolean isInitialized(Object owner, String propertyName, Object propertyValue) {
082 if (propertyValue instanceof ValueHolderInterface)
083 return ((ValueHolderInterface)propertyValue).isInstantiated();
084 else if (propertyValue instanceof IndirectContainer)
085 return ((IndirectContainer)propertyValue).isInstantiated();
086
087 return true;
088 }
089
090 @Override
091 public void initialize(Object owner, String propertyName, Object propertyValue) {
092 if (propertyValue instanceof ValueHolderInterface)
093 ((ValueHolderInterface)propertyValue).getValue().toString();
094 else if (propertyValue instanceof IndirectContainer)
095 ((IndirectContainer)propertyValue).getValueHolder().getValue().toString();
096 }
097
098 @Override
099 public List<Object[]> getFieldValues(Object obj, Object dest) {
100 List<Object[]> fieldValues = new ArrayList<Object[]>();
101
102 List<String> topLinkVhs = new ArrayList<String>();
103
104 // Merges field values
105 try {
106 Class<?> clazz = obj.getClass();
107 while (clazz != null) {
108 Field[] fields = clazz.getDeclaredFields();
109 for (Field field : fields) {
110 if ((field.getModifiers() & Modifier.STATIC) != 0
111 || (field.getModifiers() & Modifier.FINAL) != 0
112 || (field.getModifiers() & Modifier.VOLATILE) != 0
113 || (field.getModifiers() & Modifier.NATIVE) != 0
114 || (field.getModifiers() & Modifier.TRANSIENT) != 0)
115 continue;
116
117 if (ValueHolderInterface.class.isAssignableFrom(field.getType())) {
118 field.setAccessible(true);
119 ValueHolderInterface vh = (ValueHolderInterface)field.get(obj);
120 if (!vh.isInstantiated()) {
121 topLinkVhs.add(field.getName());
122 field.set(dest, vh);
123 }
124 }
125 else if (!topLinkVhs.contains("_persistence_" + field.getName() + "_vh")) {
126 field.setAccessible(true);
127 Object o = field.get(obj);
128 if (dest != null) {
129 Object d = field.get(dest);
130 fieldValues.add(new Object[] { field, o, d });
131 }
132 else
133 fieldValues.add(new Object[] { field, o });
134 }
135 }
136 clazz = clazz.getSuperclass();
137 }
138 }
139 catch (Exception e) {
140 throw new RuntimeException("Could not merge entity ", e);
141 }
142
143 return fieldValues;
144 }
145 }