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 $
036     */
037    public class WriteDataToDatabaseCommand extends Command
038    {
039        /** A single data file to insert. */
040        private File      _singleDataFile = null;
041        /** The input files. */
042        private ArrayList _fileSets = new ArrayList();
043        /** Whether we should use batch mode. */
044        private Boolean _useBatchMode;
045        /** The maximum number of objects to insert in one batch. */
046        private Integer _batchSize;
047        
048        /**
049         * Adds a fileset.
050         * 
051         * @param fileset The additional input files
052         */
053        public void addConfiguredFileset(FileSet fileset)
054        {
055            _fileSets.add(fileset);
056        }
057    
058        /**
059         * Set the xml data file.
060         *
061         * @param dataFile The data file
062         */
063        public void setDataFile(File dataFile)
064        {
065            _singleDataFile = dataFile;
066        }
067    
068        /**
069         * Sets the maximum number of objects to insert in one batch.
070         *
071         * @param batchSize The number of objects
072         */
073        public void setBatchSize(int batchSize)
074        {
075            _batchSize = new Integer(batchSize);
076        }
077    
078        /**
079         * Specifies whether we shall be using batch mode.
080         *
081         * @param useBatchMode <code>true</code> if we shall use batch mode
082         */
083        public void setUseBatchMode(boolean useBatchMode)
084        {
085            _useBatchMode = Boolean.valueOf(useBatchMode);
086        }
087    
088        /**
089         * {@inheritDoc}
090         */
091        public void execute(Task task, Database dbModel, DescriptorRepository objModel) throws BuildException
092        {
093            try
094            {
095                DdlUtilsDataHandling handling = new DdlUtilsDataHandling();
096    
097                handling.setModel(dbModel, objModel);
098                handling.setPlatform(getPlatform());
099    
100                if (_singleDataFile != null)
101                {
102                    readSingleDataFile(task, handling, _singleDataFile);
103                }
104                else
105                {
106                    for (Iterator it = _fileSets.iterator(); it.hasNext();)
107                    {
108                        FileSet          fileSet    = (FileSet)it.next();
109                        File             fileSetDir = fileSet.getDir(task.getProject());
110                        DirectoryScanner scanner    = fileSet.getDirectoryScanner(task.getProject());
111                        String[]         files      = scanner.getIncludedFiles();
112        
113                        for (int idx = 0; (files != null) && (idx < files.length); idx++)
114                        {
115                            readSingleDataFile(task, handling, new File(fileSetDir, files[idx]));
116                        }
117                    }
118                }
119            }
120            catch (Exception 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 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(dataFile), batchSize);
168                    task.log("Read data file "+dataFile.getAbsolutePath(), Project.MSG_INFO);
169                }
170                catch (Exception 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    }