001 package com.khubla.pragmatach.plugin.mongodb.serializer; 002 003 import java.lang.reflect.Field; 004 005 import org.apache.commons.beanutils.BeanUtils; 006 import org.apache.commons.beanutils.PropertyUtils; 007 008 import com.khubla.pragmatach.framework.api.PragmatachException; 009 import com.mongodb.BasicDBObject; 010 import com.mongodb.DBObject; 011 012 /** 013 * @author tom 014 */ 015 public class EnumFieldSerializer implements FieldSerializer { 016 /** 017 * ctor 018 */ 019 public EnumFieldSerializer(Class<?> typeClazz) { 020 } 021 022 @SuppressWarnings({ "rawtypes", "unchecked" }) 023 @Override 024 public void deserializeField(Object object, Field field, DBObject dbObject) throws PragmatachException { 025 try { 026 final String v = (String) dbObject.get(field.getName()); 027 final Enum e = Enum.valueOf((Class<Enum>) field.getType(), v); 028 PropertyUtils.setProperty(object, field.getName(), e); 029 } catch (final Exception e) { 030 throw new PragmatachException("Exception in deserializeField", e); 031 } 032 } 033 034 @Override 035 public void serializeField(BasicDBObject parentDBObject, Object object, Field field) throws PragmatachException { 036 try { 037 final String v = BeanUtils.getProperty(object, field.getName()); 038 parentDBObject.append(field.getName(), v); 039 } catch (final Exception e) { 040 throw new PragmatachException("Exception in serializeField", e); 041 } 042 } 043 }