KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > tools > AntDataPortDelegate


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.tools;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
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 /**
35  * DataPortDelegate implementation that works in the context of Ant DataPortTask task
36  * execution, performing entity filtering and logging functions.
37  *
38  * @author Andrus Adamchik
39  * @since 1.2: Prior to 1.2 DataPort classes were a part of cayenne-examples package.
40  */

41 class AntDataPortDelegate implements DataPortDelegate {
42
43     protected Task parentTask;
44
45     protected Pattern JavaDoc[] mapFilters;
46
47     protected long timestamp;
48     protected DbEntity lastEntity;
49
50     protected NamePatternMatcher namePatternMatcher;
51
52     // exists for testing and such
53
AntDataPortDelegate() {
54         mapFilters = new Pattern JavaDoc[] {};
55     }
56
57     AntDataPortDelegate(Task parentTask, String JavaDoc mapsPattern,
58             String JavaDoc includeEntitiesPattern, String JavaDoc 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     /**
70      * Applies preconfigured list of filters to the list, removing entities that do not
71      * pass the filter.
72      */

73     protected List JavaDoc filterEntities(List JavaDoc entities) {
74         if (entities == null || entities.isEmpty()) {
75             return entities;
76         }
77
78         Iterator JavaDoc 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     /**
94      * Returns true if the DataMap passes a set of DataMap filters or if there is no
95      * DataMap filters.
96      */

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 JavaDoc 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     /**
117      * Implements the delegate method to filter the list of entities applying filtering
118      * rules encapsulated by this object.
119      */

120     public List JavaDoc willPortEntities(DataPort portTool, List JavaDoc entities) {
121         return filterEntities(entities);
122     }
123
124     /**
125      * Logs entity porting event using Ant logger.
126      */

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 JavaDoc timestampLabel = "";
136         if (lastEntity == entity) {
137             timestampLabel = " in " + (System.currentTimeMillis() - timestamp) + " ms.";
138         }
139
140         String JavaDoc 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 JavaDoc willCleanData(DataPort portTool, List JavaDoc 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 JavaDoc timestampLabel = "";
160         if (lastEntity == entity) {
161             timestampLabel = " in " + (System.currentTimeMillis() - timestamp) + " ms.";
162         }
163
164         String JavaDoc 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