Coverage Report - org.kuali.student.common.assembly.transform.TransformationManager
 
Classes in this File Line Coverage Branch Coverage Complexity
TransformationManager
0%
0/71
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  
 import org.kuali.student.common.rice.StudentWorkflowConstants;
 14  
 
 15  0
 public class TransformationManager {
 16  0
         final Logger LOG = Logger.getLogger(TransformationManager.class);
 17  
 
 18  
         private MetadataServiceImpl metadataService;
 19  0
         private DataBeanMapper mapper = new DefaultDataBeanMapper();
 20  0
         private List<TransformFilter> inboundFilterList = new ArrayList<TransformFilter>();
 21  0
         private List<TransformFilter> outboundFilterList = new ArrayList<TransformFilter>();        
 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 = getMetadata(clazz.getName(), filterProperties);
 87  0
                 applyInboundFilters(clazz.getName(), value, filterProperties,metadata);
 88  0
                 Object dtoValue = mapper.convertFromData(value, clazz,metadata);
 89  0
                 applyInboundFilters(clazz.getName(), dtoValue, filterProperties,metadata);
 90  0
                 return dtoValue;
 91  
         }
 92  
 
 93  
         /**
 94  
          * This method applies all inbound filters known to his transformation manager.
 95  
          *
 96  
          * @param dtoName The name of the dto represented by a Data value, for Dto values,
 97  
          *  this can simply be the name of the dto class
 98  
          * @param value The dto or data object to apply filters to
 99  
          * @throws Exception
 100  
          */
 101  
         public void applyInboundFilters(String dtoName, Object value, Map<String,Object> properties, Metadata metadata) throws Exception{
 102  0
                 for (TransformFilter filter:inboundFilterList){
 103  0
                         if (filter.getType().isInstance(value)){
 104  0
                                 if (filter instanceof AbstractDataFilter) {
 105  0
                                         ((AbstractDataFilter)filter).applyInboundDataFilter((Data)value, metadata, properties);
 106  
                                 } else {
 107  0
                                         ((AbstractDTOFilter)filter).applyInboundDtoFilter(value, properties);
 108  
                                 }
 109  0
                                 LOG.info(filter.getClass().getName() + ": Filter Applied");
 110  
                         }
 111  
                 }
 112  0
         }
 113  
 
 114  
         /**
 115  
          * This method applies all outbound filters known to his transformation manager.
 116  
          *
 117  
          * @param dtoName The name of the dto represented by a Data value, for Dto values,
 118  
          *  this can simply be the name of the dto class
 119  
          * @param value The dto or data object to apply filters to
 120  
          * @throws Exception
 121  
          */
 122  
         public void applyOutboundFilters(String dtoName, Object value, Map<String,Object> properties) throws Exception{
 123  0
                 for (TransformFilter filter:outboundFilterList){
 124  0
                         if (filter.getType().isInstance(value)){
 125  0
                                 if (filter instanceof AbstractDataFilter) {
 126  
                                         //FIXME: It might be more efficient to getMetadata outside of the for loop (unless metadata might be different)
 127  0
                                         Metadata metadata = getMetadata(dtoName, properties);
 128  0
                                         ((AbstractDataFilter)filter).applyOutboundDataFilter((Data)value, metadata, properties);
 129  0
                                 } else {
 130  0
                                         ((AbstractDTOFilter)filter).applyOutboundDtoFilter(value, properties);
 131  
                                 }
 132  0
                                 LOG.info(filter.getClass().getName() + ": Filter Applied");
 133  
                         }
 134  
                 }
 135  0
         }
 136  
 
 137  
         public Metadata getMetadata(String dtoName, Map<String,Object> filterProperties){
 138  0
                 String state = (String)filterProperties.get(DtoConstants.DTO_STATE);
 139  0
                 String nextState = (String)filterProperties.get(DtoConstants.DTO_NEXT_STATE);
 140  0
                 String workflowNode = (String)filterProperties.get(DtoConstants.DTO_WORKFLOW_NODE);
 141  
                 //The docTypeName is actually set with ProposalWorkflowFilter.WORKFLOW_DOC_TYPE, however it is not visible in this project.
 142  0
                 String documentTypeName = (String)filterProperties.get(StudentWorkflowConstants.WORKFLOW_DOCUMENT_TYPE);
 143  
 
 144  
                 Metadata metadata;
 145  0
                 if (workflowNode == null || workflowNode.isEmpty()){
 146  0
                         metadata = metadataService.getMetadata(dtoName, null, state, nextState, documentTypeName);
 147  
                 } else {
 148  0
                         metadata = metadataService.getMetadataByWorkflowNode(dtoName, workflowNode, documentTypeName);
 149  
                 }                 
 150  
 
 151  0
                 applyMetadataFilters(dtoName, metadata, filterProperties);
 152  0
                 return metadata;
 153  
         }
 154  
 
 155  
         public void applyMetadataFilters(String dtoName, Metadata metadata, Map<String, Object> filterProperties){
 156  0
                 for (TransformFilter filter:outboundFilterList){
 157  0
                         if (filter instanceof MetadataFilter){
 158  0
                                 ((MetadataFilter) filter).applyMetadataFilter(dtoName, metadata, filterProperties);
 159  
                         }
 160  
                 }
 161  0
         }
 162  
 
 163  
         public Metadata getUnfilteredMetadata(String dtoName){
 164  0
                 Metadata metadata = metadataService.getMetadata(dtoName);
 165  0
                 return metadata;
 166  
         }
 167  
 
 168  
         public void setMetadataService(MetadataServiceImpl metadataService) {
 169  0
                 this.metadataService = metadataService;
 170  0
         }
 171  
         
 172  
         
 173  
         public DataBeanMapper getMapper() {
 174  0
                 return mapper;
 175  
         }
 176  
 
 177  
         public void setMapper(DataBeanMapper mapper) {
 178  0
                 this.mapper = mapper;
 179  0
         }
 180  
 
 181  
         /**
 182  
          * Use setInboundFilters and setOutboundFilters instead. This sets both
 183  
          * the inbound and outbound filter chain to be the same.
 184  
          *  
 185  
          * @param filters
 186  
          */
 187  
         @Deprecated
 188  
         public void setFilters(List<TransformFilter> filters){
 189  0
                 inboundFilterList.addAll(filters);
 190  0
                 outboundFilterList.addAll(filters);
 191  0
         }        
 192  
 
 193  
         public void setInboundFilters(List<TransformFilter> filters){
 194  0
                 inboundFilterList.addAll(filters);
 195  0
         }        
 196  
                 
 197  
         public void setOutboundFilters(List<TransformFilter> filters){
 198  0
                 outboundFilterList.addAll(filters);
 199  0
         }        
 200  
 
 201  
 }