001: package org.apache.ojb.broker.ant;
002:
003: /* Copyright 2005 The Apache Software Foundation.
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.io.File;
019: import java.io.FileReader;
020: import java.util.ArrayList;
021: import java.util.Iterator;
022:
023: import org.apache.ddlutils.model.Database;
024: import org.apache.ojb.broker.metadata.DescriptorRepository;
025: import org.apache.tools.ant.BuildException;
026: import org.apache.tools.ant.DirectoryScanner;
027: import org.apache.tools.ant.Project;
028: import org.apache.tools.ant.Task;
029: import org.apache.tools.ant.types.FileSet;
030:
031: /**
032: * Command for inserting data XML defined in terms of the repository, into a database.
033: *
034: * @author Thomas Dudziak
035: * @version $Revision: 1.1.2.2 $
036: */
037: public class WriteDataToDatabaseCommand extends Command {
038: /** A single data file to insert. */
039: private File _singleDataFile = null;
040: /** The input files. */
041: private ArrayList _fileSets = new ArrayList();
042: /** Whether we should use batch mode. */
043: private Boolean _useBatchMode;
044: /** The maximum number of objects to insert in one batch. */
045: private Integer _batchSize;
046:
047: /**
048: * Adds a fileset.
049: *
050: * @param fileset The additional input files
051: */
052: public void addConfiguredFileset(FileSet fileset) {
053: _fileSets.add(fileset);
054: }
055:
056: /**
057: * Set the xml data file.
058: *
059: * @param dataFile The data file
060: */
061: public void setDataFile(File dataFile) {
062: _singleDataFile = dataFile;
063: }
064:
065: /**
066: * Sets the maximum number of objects to insert in one batch.
067: *
068: * @param batchSize The number of objects
069: */
070: public void setBatchSize(int batchSize) {
071: _batchSize = new Integer(batchSize);
072: }
073:
074: /**
075: * Specifies whether we shall be using batch mode.
076: *
077: * @param useBatchMode <code>true</code> if we shall use batch mode
078: */
079: public void setUseBatchMode(boolean useBatchMode) {
080: _useBatchMode = Boolean.valueOf(useBatchMode);
081: }
082:
083: /**
084: * {@inheritDoc}
085: */
086: public void execute(Task task, Database dbModel,
087: DescriptorRepository objModel) throws BuildException {
088: try {
089: DdlUtilsDataHandling handling = new DdlUtilsDataHandling();
090:
091: handling.setModel(dbModel, objModel);
092: handling.setPlatform(getPlatform());
093:
094: if (_singleDataFile != null) {
095: readSingleDataFile(task, handling, _singleDataFile);
096: } else {
097: for (Iterator it = _fileSets.iterator(); it.hasNext();) {
098: FileSet fileSet = (FileSet) it.next();
099: File fileSetDir = fileSet.getDir(task.getProject());
100: DirectoryScanner scanner = fileSet
101: .getDirectoryScanner(task.getProject());
102: String[] files = scanner.getIncludedFiles();
103:
104: for (int idx = 0; (files != null)
105: && (idx < files.length); idx++) {
106: readSingleDataFile(task, handling, new File(
107: fileSetDir, files[idx]));
108: }
109: }
110: }
111: } catch (Exception ex) {
112: if (ex instanceof BuildException) {
113: throw (BuildException) ex;
114: } else {
115: throw new BuildException(ex);
116: }
117: }
118: }
119:
120: /**
121: * Reads a single data file.
122: *
123: * @param task The parent task
124: * @param reader The data reader
125: * @param schemaFile The schema file
126: */
127: private void readSingleDataFile(Task task,
128: DdlUtilsDataHandling handling, File dataFile) {
129: if (!dataFile.exists()) {
130: task.log("Could not find data file "
131: + dataFile.getAbsolutePath(), Project.MSG_ERR);
132: } else if (!dataFile.isFile()) {
133: task.log("Path " + dataFile.getAbsolutePath()
134: + " does not denote a data file", Project.MSG_ERR);
135: } else if (!dataFile.canRead()) {
136: task.log("Could not read data file "
137: + dataFile.getAbsolutePath(), Project.MSG_ERR);
138: } else {
139: int batchSize = 1;
140:
141: if ((_useBatchMode != null) && _useBatchMode.booleanValue()) {
142: if (_batchSize != null) {
143: batchSize = _batchSize.intValue();
144: }
145: }
146: try {
147: handling
148: .insertData(new FileReader(dataFile), batchSize);
149: task.log(
150: "Read data file " + dataFile.getAbsolutePath(),
151: Project.MSG_INFO);
152: } catch (Exception ex) {
153: if (isFailOnError()) {
154: throw new BuildException(
155: "Could not read data file "
156: + dataFile.getAbsolutePath(), ex);
157: } else {
158: task.log("Could not read data file "
159: + dataFile.getAbsolutePath(),
160: Project.MSG_ERR);
161: }
162: }
163: }
164: }
165: }
|