Coverage Report - org.kuali.student.common.assembly.transform.TransformationManager
 
Classes in this File Line Coverage Branch Coverage Complexity
TransformationManager
0%
0/61
0%
0/22
1.846
 
 1  
 package org.kuali.student.common.assembly.transform;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.HashMap;
 5  
 import java.util.List;
 6  
 import java.util.Map;
 7  
 
 8  
 import org.apache.log4j.Logger;
 9  
 import org.kuali.student.common.assembly.data.Data;
 10  
 import org.kuali.student.common.assembly.data.Metadata;
 11  
 import org.kuali.student.common.assembly.dictionary.MetadataServiceImpl;
 12  
 import org.kuali.student.common.dto.DtoConstants;
 13  
 
 14  0
 public class TransformationManager {
 15  0
         final Logger LOG = Logger.getLogger(TransformationManager.class);
 16  
 
 17  
         private MetadataServiceImpl metadataService;
 18  0
         private DataBeanMapper mapper = new DefaultDataBeanMapper();
 19  0
         private List<TransformFilter> filterList = new ArrayList<TransformFilter>();
 20  
 
 21  0
     private static ThreadLocal<Metadata> metadataCache = new ThreadLocal<Metadata>();
 22  
 
 23  
         /**
 24  
          * This is an outbound transform request which will convert incoming DTO objects
 25  
          * to a Data map and apply outbound filter operation to both the data object
 26  
          * and the converted DTO object.
 27  
          *
 28  
          * @param value
 29  
          * @return
 30  
          * @throws Exception
 31  
          */
 32  
         public Data transform(Object value) throws Exception{
 33  0
                 applyOutboundFilters(value.getClass().getName(), value, new HashMap<String,Object>());
 34  0
                 Data dataValue = mapper.convertFromBean(value);
 35  0
                 applyOutboundFilters(value.getClass().getName(), dataValue, new HashMap<String,Object>());
 36  
 
 37  0
                 return dataValue;
 38  
         }
 39  
 
 40  
         public Data transform(Object value, Map<String,Object> filterProperties) throws Exception{
 41  0
                 applyOutboundFilters(value.getClass().getName(), value, filterProperties);
 42  0
                 Data dataValue = mapper.convertFromBean(value);
 43  0
                 applyOutboundFilters(value.getClass().getName(), dataValue, filterProperties);
 44  
 
 45  0
                 return dataValue;
 46  
         }
 47  
 
 48  
 
 49  
         /**
 50  
          * This is an inbound transform request which will convert incoming Data objects
 51  
          * to the corresponding DTO object and apply inbound filter operations to both the data object
 52  
          * and the converted DTO object.
 53  
          *
 54  
          * @param value The incoming Data value to transform to DTO object
 55  
          * @param clazz The DTO object class represented by the Data value
 56  
          * @return The converted DTO with both inbound Data and DTO filters applied.
 57  
          */
 58  
         public Object transform(Data value, Class<?> clazz) throws Exception{
 59  0
                 Metadata metadata = null;
 60  0
                 metadata = (metadata != null ? metadata:getMetadata(clazz.getName(), new HashMap<String,Object>()));
 61  0
                 applyInboundFilters(clazz.getName(), value, new HashMap<String,Object>(),metadata);
 62  0
                 Object dtoValue = mapper.convertFromData(value, clazz,metadata);
 63  0
                 applyInboundFilters(clazz.getName(), dtoValue, new HashMap<String,Object>(),metadata);
 64  
 
 65  0
                 return dtoValue;
 66  
         }
 67  
 
 68  
         /**
 69  
          * This is an inbound transform request which will convert incoming Data objects
 70  
          * to the corresponding DTO object and apply inbound filters to both the data object
 71  
          * and the converted DTO object.
 72  
          *
 73  
          * @param value The incoming Data value to transform to DTO object
 74  
          * @param clazz The DTO object class represented by the Data value
 75  
          * @param filterProperties properties that can be consumed by the filters
 76  
          * @return The converted DTO with both inbound Data and DTO filters applied.
 77  
          */
 78  
         public Object transform(Data value, Class<?> clazz, Map<String,Object> filterProperties) throws Exception{
 79  0
                 Metadata metadata = null;
 80  0
                 metadata = (metadata != null ? metadata:getMetadata(clazz.getName(), filterProperties));
 81  0
         metadataCache.set(metadata);
 82  0
                 applyInboundFilters(clazz.getName(), value, filterProperties,metadata);
 83  0
                 Object dtoValue = mapper.convertFromData(value, clazz,metadata);
 84  0
                 applyInboundFilters(clazz.getName(), dtoValue, filterProperties,metadata);
 85  0
                 return dtoValue;
 86  
         }
 87  
 
 88  
         /**
 89  
          * This method applies all inbound filters known to his transformation manager.
 90  
          *
 91  
          * @param dtoName The name of the dto represented by a Data value, for Dto values,
 92  
          *  this can simply be the name of the dto class
 93  
          * @param value The dto or data object to apply filters to
 94  
          * @throws Exception
 95  
          */
 96  
         public void applyInboundFilters(String dtoName, Object value, Map<String,Object> properties, Metadata metadata) throws Exception{
 97  0
                 for (TransformFilter filter:filterList){
 98  0
                         if (filter.getType().isInstance(value)){
 99  0
                                 if (filter instanceof AbstractDataFilter) {
 100  0
                                         ((AbstractDataFilter)filter).applyInboundDataFilter((Data)value, metadata, properties);
 101  
                                 } else {
 102  0
                                         ((AbstractDTOFilter)filter).applyInboundDtoFilter(value, properties);
 103  
                                 }
 104  0
                                 LOG.info(filter.getClass().getName() + ": Filter Applied");
 105  
                         }
 106  
                 }
 107  0
         }
 108  
 
 109  
         /**
 110  
          * This method applies all outbound filters known to his transformation manager.
 111  
          *
 112  
          * @param dtoName The name of the dto represented by a Data value, for Dto values,
 113  
          *  this can simply be the name of the dto class
 114  
          * @param value The dto or data object to apply filters to
 115  
          * @throws Exception
 116  
          */
 117  
         public void applyOutboundFilters(String dtoName, Object value, Map<String,Object> properties) throws Exception{
 118  0
                 Metadata metadata = metadataCache.get();
 119  
 
 120  0
                 for (TransformFilter filter:filterList){
 121  0
                         if (filter.getType().isInstance(value)){
 122  0
                                 if (filter instanceof AbstractDataFilter) {
 123  0
                                         metadata = (metadata != null ? metadata:getMetadata(dtoName, properties));
 124  0
                                         ((AbstractDataFilter)filter).applyOutboundDataFilter((Data)value, metadata, properties);
 125  
                                 } else {
 126  0
                                         ((AbstractDTOFilter)filter).applyOutboundDtoFilter(value, properties);
 127  
                                 }
 128  0
                                 LOG.info(filter.getClass().getName() + ": Filter Applied");
 129  
                         }
 130  
                 }
 131  0
         }
 132  
 
 133  
         public Metadata getMetadata(String dtoName, Map<String,Object> filterProperties){
 134  0
                 String state = (String)filterProperties.get(DtoConstants.DTO_STATE);
 135  0
                 String nextState = (String)filterProperties.get(DtoConstants.DTO_NEXT_STATE);
 136  
 
 137  0
                 Metadata metadata = metadataService.getMetadata(dtoName, null, state, nextState);
 138  
 
 139  0
                 applyMetadataFilters(dtoName, metadata, filterProperties);
 140  0
                 return metadata;
 141  
         }
 142  
 
 143  
         public void applyMetadataFilters(String dtoName, Metadata metadata, Map<String, Object> filterProperties){
 144  0
                 for (TransformFilter filter:filterList){
 145  0
                         if (filter instanceof MetadataFilter){
 146  0
                                 ((MetadataFilter) filter).applyMetadataFilter(dtoName, metadata, filterProperties);
 147  
                         }
 148  
                 }
 149  0
         }
 150  
 
 151  
         public Metadata getUnfilteredMetadata(String dtoName){
 152  0
                 Metadata metadata = metadataService.getMetadata(dtoName);
 153  0
                 return metadata;
 154  
         }
 155  
 
 156  
         public void setMetadataService(MetadataServiceImpl metadataService) {
 157  0
                 this.metadataService = metadataService;
 158  0
         }
 159  
 
 160  
         public void addFilter(TransformFilter filter){
 161  0
                 filterList.add(filter);
 162  0
         }
 163  
 
 164  
         public void setFilters(List<TransformFilter> filters){
 165  0
                 filterList.addAll(filters);
 166  0
         }
 167  
         
 168  
         public void removeFilter(TransformFilter filter){
 169  0
                 filterList.remove(filter);
 170  0
         }
 171  
 }