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