1 19 20 package org.apache.cayenne.access.jdbc; 21 22 import java.sql.ResultSet ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 29 import org.apache.cayenne.DataRow; 30 import org.apache.cayenne.access.trans.SelectTranslator; 31 import org.apache.cayenne.access.types.ExtendedType; 32 import org.apache.cayenne.dba.TypesMapping; 33 import org.apache.cayenne.map.Entity; 34 import org.apache.cayenne.map.EntityInheritanceTree; 35 import org.apache.cayenne.map.ObjAttribute; 36 import org.apache.cayenne.map.ObjEntity; 37 38 44 class DataRowPostProcessor { 45 46 private EntityInheritanceTree inheritanceTree; 47 private Map columnOverrides; 48 private Collection defaultOverrides; 49 50 static DataRowPostProcessor createPostProcessor(SelectTranslator translator) { 52 Map attributeOverrides = translator.getAttributeOverrides(); 53 if (attributeOverrides.isEmpty()) { 54 return null; 55 } 56 57 ColumnDescriptor[] columns = translator.getResultColumns(); 58 59 Map columnOverrides = new HashMap (2); 60 61 Iterator it = attributeOverrides.entrySet().iterator(); 62 while (it.hasNext()) { 63 Map.Entry entry = (Map.Entry ) it.next(); 64 65 ObjAttribute attribute = (ObjAttribute) entry.getKey(); 66 Entity entity = attribute.getEntity(); 67 68 String key = null; 69 int jdbcType = TypesMapping.NOT_DEFINED; 70 int index = -1; 71 for (int i = 0; i < columns.length; i++) { 72 if (columns[i] == entry.getValue()) { 73 74 if (!attribute.getType().equals(columns[i].getJavaClass())) { 76 index = i + 1; 78 jdbcType = columns[i].getJdbcType(); 79 key = columns[i].getLabel(); 80 } 81 82 break; 83 } 84 } 85 86 if (index < 1) { 87 continue; 88 } 89 90 ExtendedType converter = translator 91 .getAdapter() 92 .getExtendedTypes() 93 .getRegisteredType(attribute.getType()); 94 95 Collection overrides = (Collection ) columnOverrides.get(entity.getName()); 96 97 if (overrides == null) { 98 overrides = new ArrayList (3); 99 columnOverrides.put(entity.getName(), overrides); 100 } 101 102 overrides.add(new ColumnOverride(index, key, converter, jdbcType)); 103 } 104 105 return columnOverrides.isEmpty() ? null : new DataRowPostProcessor(translator 107 .getRootInheritanceTree(), columnOverrides); 108 } 109 110 private DataRowPostProcessor(EntityInheritanceTree inheritanceTree, 111 Map columnOverrides) { 112 113 if (inheritanceTree != null && inheritanceTree.getChildren().size() > 0) { 114 this.inheritanceTree = inheritanceTree; 115 this.columnOverrides = columnOverrides; 116 } 117 else { 118 if (columnOverrides.size() != 1) { 119 throw new IllegalArgumentException ( 120 "No inheritance - there must be only one override set"); 121 } 122 123 defaultOverrides = (Collection ) columnOverrides.values().iterator().next(); 124 } 125 } 126 127 void postprocessRow(ResultSet resultSet, DataRow row) throws Exception { 128 129 Collection overrides = getOverrides(row); 130 131 if (overrides != null) { 132 Iterator it = overrides.iterator(); 133 while (it.hasNext()) { 134 ColumnOverride override = (ColumnOverride) it.next(); 135 136 Object newValue = override.converter.materializeObject( 137 resultSet, 138 override.index, 139 override.jdbcType); 140 row.put(override.key, newValue); 141 } 142 } 143 } 144 145 private final Collection getOverrides(DataRow row) { 146 if (defaultOverrides != null) { 147 return defaultOverrides; 148 } 149 else { 150 ObjEntity entity = inheritanceTree.entityMatchingRow(row); 151 return entity != null 152 ? (Collection ) columnOverrides.get(entity.getName()) 153 : null; 154 } 155 } 156 157 static final class ColumnOverride { 158 159 int index; 160 int jdbcType; 161 String key; 162 ExtendedType converter; 163 164 ColumnOverride(int index, String key, ExtendedType converter, int jdbcType) { 165 this.index = index; 166 this.key = key; 167 this.converter = converter; 168 this.jdbcType = jdbcType; 169 } 170 } 171 } 172 | Popular Tags |