1 56 package org.objectstyle.cayenne.tools; 57 58 import java.util.Iterator ; 59 import java.util.List ; 60 61 import org.apache.oro.text.perl.Perl5Util; 62 import org.apache.tools.ant.Project; 63 import org.apache.tools.ant.Task; 64 import org.objectstyle.cayenne.access.DataPort; 65 import org.objectstyle.cayenne.access.DataPortDelegate; 66 import org.objectstyle.cayenne.map.DataMap; 67 import org.objectstyle.cayenne.map.DbEntity; 68 import org.objectstyle.cayenne.query.GenericSelectQuery; 69 import org.objectstyle.cayenne.query.Query; 70 71 78 class AntDataPortDelegate implements DataPortDelegate { 79 80 private static final Perl5Util regexUtil = new Perl5Util(); 81 82 protected Task parentTask; 83 84 protected String [] mapFilters; 85 86 protected long timestamp; 87 protected DbEntity lastEntity; 88 89 protected NamePatternMatcher namePatternMatcher; 90 91 public AntDataPortDelegate(Task parentTask, String mapsPattern, 92 String includeEntitiesPattern, String excludeEntitiesPattern) { 93 this.parentTask = parentTask; 94 95 this.namePatternMatcher = new NamePatternMatcher(parentTask, includeEntitiesPattern, excludeEntitiesPattern); 96 97 this.mapFilters = namePatternMatcher.tokenizePattern(mapsPattern); 98 } 99 100 104 protected List filterEntities(List entities) { 105 if (entities == null || entities.isEmpty()) { 106 return entities; 107 } 108 109 if (mapFilters.length == 0) { 110 return entities; 111 } 112 113 Iterator it = entities.iterator(); 114 while (it.hasNext()) { 115 DbEntity entity = (DbEntity) it.next(); 116 117 if (!passedDataMapFilter(entity)) { 118 it.remove(); 119 continue; 120 } 121 } 122 123 namePatternMatcher.filter(entities); 124 125 return entities; 126 } 127 128 132 protected boolean passedDataMapFilter(DbEntity entity) { 133 if (mapFilters.length == 0) { 134 return true; 135 } 136 137 DataMap map = entity.getDataMap(); 138 if (map == null) { 139 return true; 140 } 141 142 String mapName = map.getName(); 143 for (int i = 0; i < mapFilters.length; i++) { 144 if (regexUtil.match(mapFilters[i], mapName)) { 145 return true; 146 } 147 } 148 149 return false; 150 } 151 152 156 public List willPortEntities(DataPort portTool, List entities) { 157 return filterEntities(entities); 158 } 159 160 163 public GenericSelectQuery willPortEntity( 164 DataPort portTool, 165 DbEntity entity, 166 GenericSelectQuery query) { 167 parentTask.log("Porting '" + entity.getName() + "'"); 168 lastEntity = entity; 169 timestamp = System.currentTimeMillis(); 170 171 return query; 172 } 173 174 public void didPortEntity(DataPort portTool, DbEntity entity, int rowCount) { 175 String timestampLabel = ""; 176 if (lastEntity == entity) { 177 timestampLabel = " in " + (System.currentTimeMillis() - timestamp) + " ms."; 178 } 179 180 String label = (rowCount == 1) ? "1 row transferred" : rowCount 181 + " rows transferred"; 182 parentTask.log("Done porting " 183 + entity.getName() 184 + ", " 185 + label 186 + timestampLabel, Project.MSG_VERBOSE); 187 } 188 189 public List willCleanData(DataPort portTool, List entities) { 190 return filterEntities(entities); 191 } 192 193 public Query willCleanData(DataPort portTool, DbEntity entity, Query query) { 194 parentTask.log("Deleting " + entity.getName(), Project.MSG_VERBOSE); 195 lastEntity = entity; 196 timestamp = System.currentTimeMillis(); 197 return query; 198 } 199 200 public void didCleanData(DataPort portTool, DbEntity entity, int rowCount) { 201 String timestampLabel = ""; 202 if (lastEntity == entity) { 203 timestampLabel = " in " + (System.currentTimeMillis() - timestamp) + " ms."; 204 } 205 206 String label = (rowCount == 1) ? "1 row deleted" : rowCount + " rows deleted"; 207 parentTask.log("Done deleting " 208 + entity.getName() 209 + ", " 210 + label 211 + timestampLabel, Project.MSG_VERBOSE); 212 } 213 } | Popular Tags |