001    package com.khubla.pragmatach.plugin.mongodb.serializer;
002    
003    import java.lang.reflect.Field;
004    
005    import org.apache.commons.beanutils.PropertyUtils;
006    
007    import com.khubla.pragmatach.framework.api.PragmatachException;
008    import com.khubla.pragmatach.plugin.mongodb.MongoDBObjectPersister;
009    import com.khubla.pragmatach.plugin.mongodb.proxy.MongoProxyFactory;
010    import com.khubla.pragmatach.plugin.mongodb.util.ClassTypeUtils;
011    import com.mongodb.BasicDBObject;
012    import com.mongodb.DBObject;
013    
014    /**
015     * @author tom
016     */
017    public class EntityFieldSerializer implements FieldSerializer {
018       /**
019        * the type
020        */
021       private final Class<?> typeClazz;
022       /**
023        * type utils
024        */
025       private final ClassTypeUtils classTypeUtils;
026    
027       /**
028        * ctor
029        */
030       public EntityFieldSerializer(Class<?> typeClazz) {
031          this.typeClazz = typeClazz;
032          classTypeUtils = new ClassTypeUtils(this.typeClazz);
033       }
034    
035       @Override
036       public void deserializeField(Object object, Field field, DBObject dbObject) throws PragmatachException {
037          try {
038             /*
039              * get id
040              */
041             final String id = (String) dbObject.get(field.getName());
042             /*
043              * if it was null, we didn't save it, so move on
044              */
045             if (null != id) {
046                /*
047                 * lazy load?
048                 */
049                if (true == ClassTypeUtils.isLazyLoad(field)) {
050                   /*
051                    * create an empty object
052                    */
053                   Object o = MongoProxyFactory.getProxyObject(field.getType());
054                   /*
055                    * set the id and fetched properties
056                    */
057                   MongoProxyFactory.setID(o, id);
058                   MongoProxyFactory.setFetched(o, false);
059                   /*
060                    * set
061                    */
062                   PropertyUtils.setProperty(object, field.getName(), o);
063                } else {
064                   /*
065                    * get object
066                    */
067                   final MongoDBObjectPersister mongoDBObjectPersister = new MongoDBObjectPersister(field.getType());
068                   final DBObject containedObjectJSON = mongoDBObjectPersister.find(id);
069                   /*
070                    * load it
071                    */
072                   final Object o = mongoDBObjectPersister.load(containedObjectJSON);
073                   /*
074                    * set
075                    */
076                   PropertyUtils.setProperty(object, field.getName(), o);
077                }
078             }
079          } catch (final Exception e) {
080             throw new PragmatachException("Exception in deserializeField", e);
081          }
082       }
083    
084       @Override
085       public void serializeField(BasicDBObject parentDBObject, Object object, Field field) throws PragmatachException {
086          try {
087             /*
088              * get the object
089              */
090             final Object oo = PropertyUtils.getProperty(object, field.getName());
091             /*
092              * if the object is null, don't store it
093              */
094             if (null != oo) {
095                /*
096                 * save the object
097                 */
098                final MongoDBObjectPersister mongoDBObjectPersister = new MongoDBObjectPersister(field.getType());
099                mongoDBObjectPersister.save(oo);
100                /*
101                 * save the id
102                 */
103                parentDBObject.append(field.getName(), classTypeUtils.getId(oo));
104             }
105          } catch (final Exception e) {
106             throw new PragmatachException("Exception in serializeField", e);
107          }
108       }
109    }