KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import org.apache.cayenne.access.DataDomain;
28 import org.apache.cayenne.access.DataNode;
29 import org.apache.cayenne.access.DataPort;
30 import org.apache.cayenne.conf.Configuration;
31 import org.apache.cayenne.conf.FileConfiguration;
32 import org.apache.cayenne.map.DataMap;
33 import org.apache.cayenne.util.Util;
34 import org.apache.tools.ant.BuildException;
35 import org.apache.tools.ant.Project;
36 import org.apache.tools.ant.Task;
37
38 /**
39  * A "cdataport" Ant task implementing a frontend to DataPort allowing porting database
40  * data using Ant build scripts.
41  *
42  * @author Andrus Adamchik
43  * @since 1.2: Prior to 1.2 DataPort classes were a part of cayenne-examples package.
44  */

45 public class DataPortTask extends Task {
46
47     protected File JavaDoc projectFile;
48     protected String JavaDoc maps;
49     protected String JavaDoc srcNode;
50     protected String JavaDoc destNode;
51     protected String JavaDoc includeTables;
52     protected String JavaDoc excludeTables;
53     protected boolean cleanDest = true;
54
55     public DataPortTask() {
56         // set defaults
57
this.cleanDest = true;
58     }
59
60     public void execute() throws BuildException {
61         validateParameters();
62
63         FileConfiguration configuration = new FileConfiguration(projectFile);
64
65         try {
66             configuration.initialize();
67         }
68         catch (Exception JavaDoc ex) {
69             throw new BuildException("Error loading Cayenne configuration from "
70                     + projectFile, ex);
71         }
72
73         // perform project validation
74
DataNode source = findNode(configuration, srcNode);
75         if (source == null) {
76             throw new BuildException("srcNode not found in the project: " + srcNode);
77         }
78
79         DataNode destination = findNode(configuration, destNode);
80         if (destination == null) {
81             throw new BuildException("destNode not found in the project: " + destNode);
82         }
83
84         log("Porting from '" + srcNode + "' to '" + destNode + "'.");
85
86         AntDataPortDelegate portDelegate = new AntDataPortDelegate(
87                 this,
88                 maps,
89                 includeTables,
90                 excludeTables);
91         DataPort dataPort = new DataPort(portDelegate);
92         dataPort.setEntities(getAllEntities(source, destination));
93         dataPort.setCleaningDestination(cleanDest);
94         dataPort.setSourceNode(source);
95         dataPort.setDestinationNode(destination);
96
97         try {
98             dataPort.execute();
99         }
100         catch (Exception JavaDoc e) {
101             Throwable JavaDoc topOfStack = Util.unwindException(e);
102             throw new BuildException(
103                     "Error porting data: " + topOfStack.getMessage(),
104                     topOfStack);
105         }
106     }
107
108     protected DataNode findNode(Configuration configuration, String JavaDoc name) {
109         Iterator JavaDoc domains = configuration.getDomains().iterator();
110         while (domains.hasNext()) {
111             DataDomain domain = (DataDomain) domains.next();
112             DataNode node = domain.getNode(name);
113             if (node != null) {
114                 return node;
115             }
116         }
117
118         return null;
119     }
120
121     protected Collection JavaDoc getAllEntities(DataNode source, DataNode target) {
122         // use a set to exclude duplicates, though a valid project will probably have
123
// none...
124
Collection JavaDoc allEntities = new HashSet JavaDoc();
125
126         Iterator JavaDoc maps = source.getDataMaps().iterator();
127         while (maps.hasNext()) {
128             DataMap map = (DataMap) maps.next();
129             allEntities.addAll(map.getDbEntities());
130         }
131
132         maps = target.getDataMaps().iterator();
133         while (maps.hasNext()) {
134             DataMap map = (DataMap) maps.next();
135             allEntities.addAll(map.getDbEntities());
136         }
137
138         log("Number of entities: " + allEntities.size(), Project.MSG_VERBOSE);
139
140         if (allEntities.size() == 0) {
141             log("No entities found for either source or target.");
142         }
143         return allEntities;
144     }
145
146     protected void validateParameters() throws BuildException {
147         if (projectFile == null) {
148             throw new BuildException("Required 'projectFile' parameter is missing.");
149         }
150
151         if (!projectFile.exists()) {
152             throw new BuildException("'projectFile' does not exist: " + projectFile);
153         }
154
155         if (srcNode == null) {
156             throw new BuildException("Required 'srcNode' parameter is missing.");
157         }
158
159         if (destNode == null) {
160             throw new BuildException("Required 'destNode' parameter is missing.");
161         }
162     }
163
164     public void setDestNode(String JavaDoc destNode) {
165         this.destNode = destNode;
166     }
167
168     public void setExcludeTables(String JavaDoc excludeTables) {
169         this.excludeTables = excludeTables;
170     }
171
172     public void setIncludeTables(String JavaDoc includeTables) {
173         this.includeTables = includeTables;
174     }
175
176     public void setMaps(String JavaDoc maps) {
177         this.maps = maps;
178     }
179
180     public void setProjectFile(File JavaDoc projectFile) {
181         this.projectFile = projectFile;
182     }
183
184     public void setSrcNode(String JavaDoc srcNode) {
185         this.srcNode = srcNode;
186     }
187
188     public void setCleanDest(boolean flag) {
189         this.cleanDest = flag;
190     }
191 }
192
Popular Tags