Coverage Report - org.kuali.student.common.assembly.transform.TransformationManager
 
Classes in this File Line Coverage Branch Coverage Complexity
TransformationManager
0%
0/71
0%
0/30
2
 
 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  
          * @param objectType TODO
 30  
          * @return
 31  
          * @throws Exception
 32  
          */
 33  
         public Data transform(Object value, String objectType) throws Exception{
 34  0
                 Metadata metadata = null;
 35  0
                 metadata = (metadata != null ? metadata:getMetadata(objectType, new HashMap<String,Object>()));
 36  
                 
 37  0
                 applyOutboundFilters(value.getClass().getName(), value, new HashMap<String,Object>());
 38  0
                 Data dataValue = mapper.convertFromBean(value, metadata);
 39  0
                 applyOutboundFilters(value.getClass().getName(), dataValue, new HashMap<String,Object>());
 40  
 
 41  0
                 return dataValue;
 42  
         }
 43  
 
 44  
         public Data transform(Object value, String objectType, Map<String,Object> filterProperties) throws Exception{
 45  0
                 Metadata metadata = null;
 46  0
                 metadata = (metadata != null ? metadata:getMetadata(objectType, new HashMap<String,Object>()));
 47  
 
 48  0
                 applyOutboundFilters(value.getClass().getName(), value, filterProperties);
 49  0
                 Data dataValue = mapper.convertFromBean(value, metadata);
 50  0
                 applyOutboundFilters(value.getClass().getName(), dataValue, filterProperties);
 51  
 
 52  0
                 return dataValue;
 53  
         }
 54  
 
 55  
 
 56  
         /**
 57  
          * This is an inbound transform request which will convert incoming Data objects
 58  
          * to the corresponding DTO object and apply inbound filter operations to both the data object
 59  
          * and the converted DTO object.
 60  
          *
 61  
          * @param value The incoming Data value to transform to DTO object
 62  
          * @param clazz The DTO object class represented by the Data value
 63  
          * @return The converted DTO with both inbound Data and DTO filters applied.
 64  
          */
 65  
         public Object transform(Data value, Class<?> clazz) throws Exception{
 66  0
                 Metadata metadata = null;
 67  0
                 metadata = (metadata != null ? metadata:getMetadata(clazz.getName(), new HashMap<String,Object>()));
 68  0
                 applyInboundFilters(clazz.getName(), value, new HashMap<String,Object>(),metadata);
 69  0
                 Object dtoValue = mapper.convertFromData(value, clazz,metadata);
 70  0
                 applyInboundFilters(clazz.getName(), dtoValue, new HashMap<String,Object>(),metadata);
 71  
 
 72  0
                 return dtoValue;
 73  
         }
 74  
 
 75  
         /**
 76  
          * This is an inbound transform request which will convert incoming Data objects
 77  
          * to the corresponding DTO object and apply inbound filters to both the data object
 78  
          * and the converted DTO object.
 79  
          *
 80  
          * @param value The incoming Data value to transform to DTO object
 81  
          * @param clazz The DTO object class represented by the Data value
 82  
          * @param filterProperties properties that can be consumed by the filters
 83  
          * @return The converted DTO with both inbound Data and DTO filters applied.
 84  
          */
 85  
         public Object transform(Data value, Class<?> clazz, Map<String,Object> filterProperties) throws Exception{
 86  0
                 Metadata metadata = null;
 87  0
                 metadata = (metadata != null ? metadata:getMetadata(clazz.getName(), filterProperties));
 88  0
         metadataCache.set(metadata);
 89  0
                 applyInboundFilters(clazz.getName(), value, filterProperties,metadata);
 90  0
                 Object dtoValue = mapper.convertFromData(value, clazz,metadata);
 91  0
                 applyInboundFilters(clazz.getName(), dtoValue, filterProperties,metadata);
 92  0
                 return dtoValue;
 93  
         }
 94  
 
 95  
         /**
 96  
          * This method applies all inbound filters known to his transformation manager.
 97  
          *
 98  
          * @param dtoName The name of the dto represented by a Data value, for Dto values,
 99  
          *  this can simply be the name of the dto class
 100  
          * @param value The dto or data object to apply filters to
 101  
          * @throws Exception
 102  
          */
 103  
         public void applyInboundFilters(String dtoName, Object value, Map<String,Object> properties, Metadata metadata) throws Exception{
 104  0
                 for (TransformFilter filter:filterList){
 105  0
                         if (filter.getType().isInstance(value)){
 106  0
                                 if (filter instanceof AbstractDataFilter) {
 107  0
                                         ((AbstractDataFilter)filter).applyInboundDataFilter((Data)value, metadata, properties);
 108  
                                 } else {
 109  0
                                         ((AbstractDTOFilter)filter).applyInboundDtoFilter(value, properties);
 110  
                                 }
 111  0
                                 LOG.info(filter.getClass().getName() + ": Filter Applied");
 112  
                         }
 113  
                 }
 114  0
         }
 115  
 
 116  
         /**
 117  
          * This method applies all outbound filters known to his transformation manager.
 118  
          *
 119  
          * @param dtoName The name of the dto represented by a Data value, for Dto values,
 120  
          *  this can simply be the name of the dto class
 121  
          * @param value The dto or data object to apply filters to
 122  
          * @throws Exception
 123  
          */
 124  
         public void applyOutboundFilters(String dtoName, Object value, Map<String,Object> properties) throws Exception{
 125  0
                 Metadata metadata = metadataCache.get();
 126  
 
 127  0
                 for (TransformFilter filter:filterList){
 128  0
                         if (filter.getType().isInstance(value)){
 129  0
                                 if (filter instanceof AbstractDataFilter) {
 130  0
                                         metadata = (metadata != null ? metadata:getMetadata(dtoName, properties));
 131  0
                                         ((AbstractDataFilter)filter).applyOutboundDataFilter((Data)value, metadata, properties);
 132  
                                 } else {
 133  0
                                         ((AbstractDTOFilter)filter).applyOutboundDtoFilter(value, properties);
 134  
                                 }
 135  0
                                 LOG.info(filter.getClass().getName() + ": Filter Applied");
 136  
                         }
 137  
                 }
 138  0
         }
 139  
 
 140  
         public Metadata getMetadata(String dtoName, Map<String,Object> filterProperties){
 141  0
                 String state = (String)filterProperties.get(DtoConstants.DTO_STATE);
 142  0
                 String nextState = (String)filterProperties.get(DtoConstants.DTO_NEXT_STATE);
 143  0
                 String workflowNode = (String)filterProperties.get(DtoConstants.DTO_WORKFLOW_NODE);
 144  
 
 145  
                 Metadata metadata;
 146  0
                 if (workflowNode == null || workflowNode.isEmpty()){
 147  0
                         metadata = metadataService.getMetadata(dtoName, null, state, nextState);
 148  
                 } else {
 149  0
                         metadata = metadataService.getMetadataByWorkflowNode(dtoName, workflowNode);
 150  
                 }                 
 151  
 
 152  0
                 applyMetadataFilters(dtoName, metadata, filterProperties);
 153  0
                 return metadata;
 154  
         }
 155  
 
 156  
         public void applyMetadataFilters(String dtoName, Metadata metadata, Map<String, Object> filterProperties){
 157  0
                 for (TransformFilter filter:filterList){
 158  0
                         if (filter instanceof MetadataFilter){
 159  0
                                 ((MetadataFilter) filter).applyMetadataFilter(dtoName, metadata, filterProperties);
 160  
                         }
 161  
                 }
 162  0
         }
 163  
 
 164  
         public Metadata getUnfilteredMetadata(String dtoName){
 165  0
                 Metadata metadata = metadataService.getMetadata(dtoName);
 166  0
                 return metadata;
 167  
         }
 168  
 
 169  
         public void setMetadataService(MetadataServiceImpl metadataService) {
 170  0
                 this.metadataService = metadataService;
 171  0
         }
 172  
         
 173  
         
 174  
         public DataBeanMapper getMapper() {
 175  0
                 return mapper;
 176  
         }
 177  
 
 178  
         public void setMapper(DataBeanMapper mapper) {
 179  0
                 this.mapper = mapper;
 180  0
         }
 181  
 
 182  
         public void addFilter(TransformFilter filter){
 183  0
                 filterList.add(filter);
 184  0
         }
 185  
 
 186  
         public void setFilters(List<TransformFilter> filters){
 187  0
                 filterList.addAll(filters);
 188  0
         }
 189  
         
 190  
         public void removeFilter(TransformFilter filter){
 191  0
                 filterList.remove(filter);
 192  0
         }
 193  
                 
 194  
 }