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