KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > ant > WriteDataToDatabaseCommand


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

17
18 import java.io.File JavaDoc;
19 import java.io.FileReader JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.apache.ddlutils.model.Database;
24 import org.apache.ojb.broker.metadata.DescriptorRepository;
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.DirectoryScanner;
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.Task;
29 import org.apache.tools.ant.types.FileSet;
30
31 /**
32  * Command for inserting data XML defined in terms of the repository, into a database.
33  *
34  * @author Thomas Dudziak
35  * @version $Revision: 1.1.2.2 $
36  */

37 public class WriteDataToDatabaseCommand extends Command
38 {
39     /** A single data file to insert. */
40     private File JavaDoc _singleDataFile = null;
41     /** The input files. */
42     private ArrayList JavaDoc _fileSets = new ArrayList JavaDoc();
43     /** Whether we should use batch mode. */
44     private Boolean JavaDoc _useBatchMode;
45     /** The maximum number of objects to insert in one batch. */
46     private Integer JavaDoc _batchSize;
47     
48     /**
49      * Adds a fileset.
50      *
51      * @param fileset The additional input files
52      */

53     public void addConfiguredFileset(FileSet fileset)
54     {
55         _fileSets.add(fileset);
56     }
57
58     /**
59      * Set the xml data file.
60      *
61      * @param dataFile The data file
62      */

63     public void setDataFile(File JavaDoc dataFile)
64     {
65         _singleDataFile = dataFile;
66     }
67
68     /**
69      * Sets the maximum number of objects to insert in one batch.
70      *
71      * @param batchSize The number of objects
72      */

73     public void setBatchSize(int batchSize)
74     {
75         _batchSize = new Integer JavaDoc(batchSize);
76     }
77
78     /**
79      * Specifies whether we shall be using batch mode.
80      *
81      * @param useBatchMode <code>true</code> if we shall use batch mode
82      */

83     public void setUseBatchMode(boolean useBatchMode)
84     {
85         _useBatchMode = Boolean.valueOf(useBatchMode);
86     }
87
88     /**
89      * {@inheritDoc}
90      */

91     public void execute(Task task, Database dbModel, DescriptorRepository objModel) throws BuildException
92     {
93         try
94         {
95             DdlUtilsDataHandling handling = new DdlUtilsDataHandling();
96
97             handling.setModel(dbModel, objModel);
98             handling.setPlatform(getPlatform());
99
100             if (_singleDataFile != null)
101             {
102                 readSingleDataFile(task, handling, _singleDataFile);
103             }
104             else
105             {
106                 for (Iterator JavaDoc it = _fileSets.iterator(); it.hasNext();)
107                 {
108                     FileSet fileSet = (FileSet)it.next();
109                     File JavaDoc fileSetDir = fileSet.getDir(task.getProject());
110                     DirectoryScanner scanner = fileSet.getDirectoryScanner(task.getProject());
111                     String JavaDoc[] files = scanner.getIncludedFiles();
112     
113                     for (int idx = 0; (files != null) && (idx < files.length); idx++)
114                     {
115                         readSingleDataFile(task, handling, new File JavaDoc(fileSetDir, files[idx]));
116                     }
117                 }
118             }
119         }
120         catch (Exception JavaDoc ex)
121         {
122             if (ex instanceof BuildException)
123             {
124                 throw (BuildException)ex;
125             }
126             else
127             {
128                 throw new BuildException(ex);
129             }
130         }
131     }
132
133     /**
134      * Reads a single data file.
135      *
136      * @param task The parent task
137      * @param reader The data reader
138      * @param schemaFile The schema file
139      */

140     private void readSingleDataFile(Task task, DdlUtilsDataHandling handling, File JavaDoc dataFile)
141     {
142         if (!dataFile.exists())
143         {
144             task.log("Could not find data file "+dataFile.getAbsolutePath(), Project.MSG_ERR);
145         }
146         else if (!dataFile.isFile())
147         {
148             task.log("Path "+dataFile.getAbsolutePath()+" does not denote a data file", Project.MSG_ERR);
149         }
150         else if (!dataFile.canRead())
151         {
152             task.log("Could not read data file "+dataFile.getAbsolutePath(), Project.MSG_ERR);
153         }
154         else
155         {
156             int batchSize = 1;
157
158             if ((_useBatchMode != null) && _useBatchMode.booleanValue())
159             {
160                 if (_batchSize != null)
161                 {
162                     batchSize = _batchSize.intValue();
163                 }
164             }
165             try
166             {
167                 handling.insertData(new FileReader JavaDoc(dataFile), batchSize);
168                 task.log("Read data file "+dataFile.getAbsolutePath(), Project.MSG_INFO);
169             }
170             catch (Exception JavaDoc ex)
171             {
172                 if (isFailOnError())
173                 {
174                     throw new BuildException("Could not read data file "+dataFile.getAbsolutePath(), ex);
175                 }
176                 else
177                 {
178                     task.log("Could not read data file "+dataFile.getAbsolutePath(), Project.MSG_ERR);
179                 }
180             }
181         }
182     }
183 }
184
Popular Tags