1 19 20 package org.apache.cayenne.tools; 21 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.regex.Pattern ; 25 26 import org.apache.tools.ant.Project; 27 import org.apache.tools.ant.Task; 28 import org.apache.cayenne.access.DataPort; 29 import org.apache.cayenne.access.DataPortDelegate; 30 import org.apache.cayenne.map.DataMap; 31 import org.apache.cayenne.map.DbEntity; 32 import org.apache.cayenne.query.Query; 33 34 41 class AntDataPortDelegate implements DataPortDelegate { 42 43 protected Task parentTask; 44 45 protected Pattern [] mapFilters; 46 47 protected long timestamp; 48 protected DbEntity lastEntity; 49 50 protected NamePatternMatcher namePatternMatcher; 51 52 AntDataPortDelegate() { 54 mapFilters = new Pattern [] {}; 55 } 56 57 AntDataPortDelegate(Task parentTask, String mapsPattern, 58 String includeEntitiesPattern, String excludeEntitiesPattern) { 59 this.parentTask = parentTask; 60 61 this.namePatternMatcher = new NamePatternMatcher( 62 new AntTaskLogger(parentTask), 63 includeEntitiesPattern, 64 excludeEntitiesPattern); 65 66 this.mapFilters = namePatternMatcher.createPatterns(mapsPattern); 67 } 68 69 73 protected List filterEntities(List entities) { 74 if (entities == null || entities.isEmpty()) { 75 return entities; 76 } 77 78 Iterator it = entities.iterator(); 79 while (it.hasNext()) { 80 DbEntity entity = (DbEntity) it.next(); 81 82 if (!passedDataMapFilter(entity.getDataMap())) { 83 it.remove(); 84 continue; 85 } 86 } 87 88 namePatternMatcher.filter(entities); 89 90 return entities; 91 } 92 93 97 protected boolean passedDataMapFilter(DataMap map) { 98 if (mapFilters.length == 0) { 99 return true; 100 } 101 102 if (map == null) { 103 return true; 104 } 105 106 String mapName = map.getName(); 107 for (int i = 0; i < mapFilters.length; i++) { 108 if (mapFilters[i].matcher(mapName).find()) { 109 return true; 110 } 111 } 112 113 return false; 114 } 115 116 120 public List willPortEntities(DataPort portTool, List entities) { 121 return filterEntities(entities); 122 } 123 124 127 public Query willPortEntity(DataPort portTool, DbEntity entity, Query query) { 128 parentTask.log("Porting '" + entity.getName() + "'"); 129 lastEntity = entity; 130 timestamp = System.currentTimeMillis(); 131 return query; 132 } 133 134 public void didPortEntity(DataPort portTool, DbEntity entity, int rowCount) { 135 String timestampLabel = ""; 136 if (lastEntity == entity) { 137 timestampLabel = " in " + (System.currentTimeMillis() - timestamp) + " ms."; 138 } 139 140 String label = (rowCount == 1) ? "1 row transferred" : rowCount 141 + " rows transferred"; 142 parentTask.log( 143 "Done porting " + entity.getName() + ", " + label + timestampLabel, 144 Project.MSG_VERBOSE); 145 } 146 147 public List willCleanData(DataPort portTool, List entities) { 148 return filterEntities(entities); 149 } 150 151 public Query willCleanData(DataPort portTool, DbEntity entity, Query query) { 152 parentTask.log("Deleting " + entity.getName(), Project.MSG_VERBOSE); 153 lastEntity = entity; 154 timestamp = System.currentTimeMillis(); 155 return query; 156 } 157 158 public void didCleanData(DataPort portTool, DbEntity entity, int rowCount) { 159 String timestampLabel = ""; 160 if (lastEntity == entity) { 161 timestampLabel = " in " + (System.currentTimeMillis() - timestamp) + " ms."; 162 } 163 164 String label = (rowCount == 1) ? "1 row deleted" : rowCount + " rows deleted"; 165 parentTask.log("Done deleting " 166 + entity.getName() 167 + ", " 168 + label 169 + timestampLabel, Project.MSG_VERBOSE); 170 } 171 } 172 | Popular Tags |