001: package com.completex.objective.components.persistency.mapper.impl;
002:
003: import com.completex.objective.components.persistency.mapper.Mapper;
004: import com.completex.objective.components.persistency.mapper.ValueHandler;
005: import com.completex.objective.components.persistency.mapper.MappingHandler;
006: import com.completex.objective.components.persistency.PersistentObject;
007: import com.completex.objective.components.log.Log;
008:
009: /**
010: * Implementation of the Mapper based on class reflection.
011: * Use of this class makes sense when the Persistent Objects and the correponding
012: * POJO beans are close in structure. If their structure is the same this class
013: * will perform the conversion based on Persistent Objects/POJO beans map (see the <t>map()</t> method).
014: *
015: * @see Mapper
016: * @author Gennady Krizhevsky
017: */
018: public class DynamicMapperImpl implements Mapper {
019:
020: private Log logger = Log.NULL_LOGGER;
021:
022: private boolean validatePo = true;
023:
024: protected final Object mapLock = new Object();
025:
026: private final DynamicPoToBeanMapperImpl poToBeanMapper;
027: private final DynamicBeanToPoMapperImpl beanToPoMapper;
028:
029: public DynamicMapperImpl() {
030: poToBeanMapper = new DynamicPoToBeanMapperImpl(this );
031: beanToPoMapper = new DynamicBeanToPoMapperImpl(this );
032: }
033:
034: public void map(Class beanClass, Class poClass) {
035: synchronized (mapLock) {
036: validate(poClass);
037: poToBeanMapper.map(poClass, beanClass);
038: beanToPoMapper.map(beanClass, poClass);
039: }
040: }
041:
042: public void remap(Class beanClass, Class poClass) {
043: synchronized (mapLock) {
044: validate(poClass);
045: poToBeanMapper.remap(poClass, beanClass);
046: beanToPoMapper.remap(beanClass, poClass);
047: }
048: }
049:
050: protected void validate(Class poClass) {
051: if (isValidatePo()
052: && !PersistentObject.class.isAssignableFrom(poClass)) {
053: throw new IllegalArgumentException(
054: "poClass argument is not assignable from PersistentObject");
055: }
056: }
057:
058: /**
059: * Registers handler to overrride the default value mapping from PO to POJO bean.
060: *
061: * @see ValueHandler
062: * @param valuePath value path
063: * @param valueHandler value handler
064: */
065: public void registerPoToBeanValueHandler(String valuePath,
066: ValueHandler valueHandler) {
067: synchronized (mapLock) {
068: poToBeanMapper
069: .registerValueHandler(valuePath, valueHandler);
070: }
071: }
072:
073: /**
074: * Registers handler to overrride the default value mapping from POJO bean to PO.
075: *
076: * @see ValueHandler
077: * @param valuePath value path
078: * @param valueHandler value handler
079: */
080: public void registerBeanToPoValueHandler(String valuePath,
081: ValueHandler valueHandler) {
082: synchronized (mapLock) {
083: beanToPoMapper
084: .registerValueHandler(valuePath, valueHandler);
085: }
086: }
087:
088: public boolean mappingExists(Class beanClass, Class poClass) {
089: return beanToPoMapper.mappingExists(poClass, beanClass);
090: }
091:
092: public Object convertPoToBean(Object po, boolean forModification) {
093: return poToBeanMapper.convert(po, forModification);
094: }
095:
096: public Object convertBeanToPo(Object bean, boolean forModification) {
097: return beanToPoMapper.convert(bean, forModification);
098: }
099:
100: public Log getLogger() {
101: return logger;
102: }
103:
104: public void setLogger(Log logger) {
105: if (logger != null) {
106: this .logger = logger;
107: if (poToBeanMapper != null) {
108: poToBeanMapper.setLogger(logger);
109: }
110: if (beanToPoMapper != null) {
111: beanToPoMapper.setLogger(logger);
112: }
113: }
114: }
115:
116: public boolean isValidatePo() {
117: return validatePo;
118: }
119:
120: public void setValidatePo(boolean validatePo) {
121: this .validatePo = validatePo;
122: }
123:
124: public DynamicPoToBeanMapperImpl getPoToBeanMapper() {
125: return poToBeanMapper;
126: }
127:
128: public DynamicBeanToPoMapperImpl getBeanToPoMapper() {
129: return beanToPoMapper;
130: }
131:
132: }
|