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            Object value = propertyValue;
083            if (owner != null) {
084                Field vhf = null;
085                Class<?> c = owner.getClass();
086                while (c != null && c != Object.class) {
087                    for (Field f : c.getDeclaredFields()) {
088                        if (f.getName().equals("_persistence_" + propertyName + "_vh")) {
089                            vhf = f;
090                            break;
091                        }
092                    }
093                    if (vhf != null)
094                        break;
095                    c = c.getSuperclass();
096                }
097                if (vhf != null) {
098                    try {
099                        vhf.setAccessible(true);
100                        value = vhf.get(owner);
101                    }
102                    catch (Exception e) {
103                        log.error(e, "Could not get persistence ValueHolder " + propertyName + " for class " + owner.getClass());
104                    }
105                }
106            }        
107            if (value instanceof ValueHolderInterface)
108                return ((ValueHolderInterface)value).isInstantiated();
109            else if (value instanceof IndirectContainer)
110                return ((IndirectContainer)value).isInstantiated();
111            
112            return true;
113        }
114        
115        @Override
116        public void initialize(Object owner, String propertyName, Object propertyValue) {
117            if (propertyValue instanceof ValueHolderInterface)
118                ((ValueHolderInterface)propertyValue).getValue().toString();
119            else if (propertyValue instanceof IndirectContainer)
120                ((IndirectContainer)propertyValue).getValueHolder().getValue().toString();
121        }
122        
123        @Override
124        public List<Object
125        []> getFieldValues(Object obj, Object dest) {
126            List<Object[]> fieldValues = new ArrayList<Object[]>();
127            
128            List<String> topLinkVhs = new ArrayList<String>();
129            
130            // Merges field values
131            try {
132                Class<?> clazz = obj.getClass();
133                while (clazz != null) {
134                    Field[] fields = clazz.getDeclaredFields();
135                    for (Field field : fields) {
136                        if ((field.getModifiers() & Modifier.STATIC) != 0 
137                            || (field.getModifiers() & Modifier.FINAL) != 0 
138                            || (field.getModifiers() & Modifier.VOLATILE) != 0 
139                            || (field.getModifiers() & Modifier.NATIVE) != 0 
140                            || (field.getModifiers() & Modifier.TRANSIENT) != 0)
141                            continue;
142                        
143                        if (ValueHolderInterface.class.isAssignableFrom(field.getType())) {
144                            field.setAccessible(true);
145                            ValueHolderInterface vh = (ValueHolderInterface)field.get(obj);
146                            if (!vh.isInstantiated()) {
147                                topLinkVhs.add(field.getName());
148                                field.set(dest, vh);
149                            }
150                        }
151                        else if (!topLinkVhs.contains("_persistence_" + field.getName() + "_vh")) {
152                            field.setAccessible(true);
153                            Object o = field.get(obj);
154                            if (dest != null) {
155                                Object d = field.get(dest);
156                                fieldValues.add(new Object[] { field, o, d });
157                            }
158                            else
159                                fieldValues.add(new Object[] { field, o });
160                        }
161                    }
162                    clazz = clazz.getSuperclass();
163                }
164            }
165            catch (Exception e) {
166                throw new RuntimeException("Could not merge entity ", e);
167            }
168            
169            return fieldValues;
170        }
171    }