Coverage Report - org.apache.ojb.broker.ant.WriteDataToDatabaseCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
WriteDataToDatabaseCommand
N/A
N/A
3.833
 
 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;
 19  
 import java.io.FileReader;
 20  
 import java.util.ArrayList;
 21  
 import java.util.Iterator;
 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 $
 36  
  */
 37  
 public class WriteDataToDatabaseCommand extends Command
 38  
 {
 39  
     /** A single data file to insert. */
 40  
     private File      _singleDataFile = null;
 41  
     /** The input files. */
 42  
     private ArrayList _fileSets = new ArrayList();
 43  
     /** Whether we should use batch mode. */
 44  
     private Boolean _useBatchMode;
 45  
     /** The maximum number of objects to insert in one batch. */
 46  
     private Integer _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 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(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 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  
 }