KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.tools;
57
58 import java.io.File JavaDoc;
59 import java.util.Collection JavaDoc;
60 import java.util.HashSet JavaDoc;
61 import java.util.Iterator JavaDoc;
62
63 import org.apache.tools.ant.BuildException;
64 import org.apache.tools.ant.Project;
65 import org.objectstyle.cayenne.access.DataDomain;
66 import org.objectstyle.cayenne.access.DataNode;
67 import org.objectstyle.cayenne.access.DataPort;
68 import org.objectstyle.cayenne.conf.Configuration;
69 import org.objectstyle.cayenne.conf.FileConfiguration;
70 import org.objectstyle.cayenne.map.DataMap;
71 import org.objectstyle.cayenne.util.Util;
72
73 /**
74  * A "cdataport" Ant task implementing a frontend to DataPort allowing porting database
75  * data using Ant build scripts.
76  *
77  * @author Andrei Adamchik
78  * @since 1.2: Prior to 1.2 DataPort classes were a part of cayenne-examples package.
79  */

80 public class DataPortTask extends CayenneTask {
81
82     protected File JavaDoc projectFile;
83     protected String JavaDoc maps;
84     protected String JavaDoc srcNode;
85     protected String JavaDoc destNode;
86     protected String JavaDoc includeTables;
87     protected String JavaDoc excludeTables;
88     protected boolean cleanDest = true;
89
90     public DataPortTask() {
91         // set defaults
92
this.cleanDest = true;
93     }
94
95     public void execute() throws BuildException {
96         configureLogging();
97         validateParameters();
98
99         FileConfiguration configuration = new FileConfiguration(projectFile);
100
101         // setup the right ClassLoader
102
configuration.setClassLoader(getClass().getClassLoader());
103
104         try {
105             configuration.initialize();
106         }
107         catch (Exception JavaDoc ex) {
108             throw new BuildException("Error loading Cayenne configuration from "
109                     + projectFile);
110         }
111
112         // perform project validation
113
DataNode source = findNode(configuration, srcNode);
114         if (source == null) {
115             throw new BuildException("srcNode not found in the project: " + srcNode);
116         }
117
118         DataNode destination = findNode(configuration, destNode);
119         if (destination == null) {
120             throw new BuildException("destNode not found in the project: " + destNode);
121         }
122
123         log("Porting from '" + srcNode + "' to '" + destNode + "'.");
124
125         AntDataPortDelegate portDelegate = new AntDataPortDelegate(
126                 this,
127                 maps,
128                 includeTables,
129                 excludeTables);
130         DataPort dataPort = new DataPort(portDelegate);
131         dataPort.setEntities(getAllEntities(source, destination));
132         dataPort.setCleaningDestination(cleanDest);
133         dataPort.setSourceNode(source);
134         dataPort.setDestinationNode(destination);
135
136         try {
137             dataPort.execute();
138         }
139         catch (Exception JavaDoc e) {
140             Throwable JavaDoc topOfStack = Util.unwindException(e);
141             throw new BuildException(
142                     "Error porting data: " + topOfStack.getMessage(),
143                     topOfStack);
144         }
145     }
146
147     protected DataNode findNode(Configuration configuration, String JavaDoc name) {
148         Iterator JavaDoc domains = configuration.getDomains().iterator();
149         while (domains.hasNext()) {
150             DataDomain domain = (DataDomain) domains.next();
151             DataNode node = domain.getNode(name);
152             if (node != null) {
153                 return node;
154             }
155         }
156
157         return null;
158     }
159
160     protected Collection JavaDoc getAllEntities(DataNode source, DataNode target) {
161         // use a set to exclude duplicates, though a valid project will probably have
162
// none...
163
Collection JavaDoc allEntities = new HashSet JavaDoc();
164
165         Iterator JavaDoc maps = source.getDataMaps().iterator();
166         while (maps.hasNext()) {
167             DataMap map = (DataMap) maps.next();
168             allEntities.addAll(map.getDbEntities());
169         }
170
171         maps = target.getDataMaps().iterator();
172         while (maps.hasNext()) {
173             DataMap map = (DataMap) maps.next();
174             allEntities.addAll(map.getDbEntities());
175         }
176
177         log("Number of entities: " + allEntities.size(), Project.MSG_VERBOSE);
178
179         if (allEntities.size() == 0) {
180             log("No entities found for either source or target.");
181         }
182         return allEntities;
183     }
184
185     protected void validateParameters() throws BuildException {
186         if (projectFile == null) {
187             throw new BuildException("Required 'projectFile' parameter is missing.");
188         }
189
190         if (!projectFile.exists()) {
191             throw new BuildException("'projectFile' does not exist: " + projectFile);
192         }
193
194         if (srcNode == null) {
195             throw new BuildException("Required 'srcNode' parameter is missing.");
196         }
197
198         if (destNode == null) {
199             throw new BuildException("Required 'destNode' parameter is missing.");
200         }
201     }
202
203     public void setDestNode(String JavaDoc destNode) {
204         this.destNode = destNode;
205     }
206
207     public void setExcludeTables(String JavaDoc excludeTables) {
208         this.excludeTables = excludeTables;
209     }
210
211     public void setIncludeTables(String JavaDoc includeTables) {
212         this.includeTables = includeTables;
213     }
214
215     public void setMaps(String JavaDoc maps) {
216         this.maps = maps;
217     }
218
219     public void setProjectFile(File JavaDoc projectFile) {
220         this.projectFile = projectFile;
221     }
222
223     public void setSrcNode(String JavaDoc srcNode) {
224         this.srcNode = srcNode;
225     }
226
227     public void setCleanDest(boolean flag) {
228         this.cleanDest = flag;
229     }
230 }
Popular Tags