View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   *  
17   */
18  
19  package org.caleigo.core.service;
20  
21  
22  import java.io.*;
23  
24  import org.caleigo.core.*;
25  import org.caleigo.toolkit.log.*;
26  
27  /*** TextFileDataService provides a simple extension of the MemoryDataService
28   * that stores the data entities in text files in a database file-catalog.
29   *
30   * Note that the class is not suitable to store sensitive data. The class
31   * does not verify foreign keys and should only be used for temporary
32   * storage and/or for test or demonstration purposes.
33   *
34   * @author  Dennis Zikovic
35   * @version 1.00
36   * 
37   *//* 
38   *
39   * WHEN        WHO               WHY & WHAT
40   * -----------------------------------------------------------------------------
41   * 2003-01-07  Dennis Zikovic    Creation
42   */
43  public class TextFileDataService extends MemoryDataService
44  {
45      // Constants ---------------------------------------------------------------
46      private static final String NULL = "<NULL>";
47      
48      // Data members ------------------------------------------------------------
49      private File mDirectoryFile;
50      
51      private int mFieldSeparator = ';';
52      private int mRecordSeparator = '\n';
53      private int mStringDelimiter = '"';
54      
55      // Constructors ------------------------------------------------------------
56  	public TextFileDataService(IDataSourceDescriptor dataSourceDescriptor, String dbDirectoryPath) 
57  	{
58  		this(dataSourceDescriptor, dataSourceDescriptor.getSourceName(), dbDirectoryPath);
59  	}
60      
61      public TextFileDataService(IDataSourceDescriptor dataSourceDescriptor, Object serviceIdentity, String dbDirectoryPath) 
62      {
63          super(dataSourceDescriptor, serviceIdentity);
64          
65          // Create root directory file.
66          try
67          {
68              mDirectoryFile = new File(dbDirectoryPath);
69              
70              // Try to create directory if it doesn't exist.
71              if(!mDirectoryFile.exists())
72                  mDirectoryFile.mkdir();
73              
74              // Check read/write access.
75              if(mDirectoryFile==null)
76                  Log.printError(this, "Failed to initialize FileDataService, path not found \""+dbDirectoryPath+"\"!");
77              else if(!mDirectoryFile.canRead() || !mDirectoryFile.canWrite())
78              {
79                  Log.printError(this, "Failed to initialize FileDataService, no read/write access on \""+dbDirectoryPath+"\"!");
80                  mDirectoryFile = null;
81              }
82              else
83                  Log.print(this, "FileDataService initialized on path \""+dbDirectoryPath+"\".");
84          }
85          catch(Exception e)
86          {
87              Log.printError(this, "Failed to initialize FileDataService for path \""+dbDirectoryPath+"\"!", e);
88              mDirectoryFile = null;
89          }        
90      }
91      
92      // Superclass overrides ----------------------------------------------------
93      public boolean ping()
94      {
95          return mDirectoryFile!=null;
96      }
97      
98      protected ISelection loadTableSelection(IEntityDescriptor entityDescriptor)
99      {
100         ISelection dbSelection = null;
101 
102         try
103         {            
104             File entityFile = new File(mDirectoryFile, entityDescriptor.getCodeName()+".tdb");
105             Log.print(this, "Loading table selection from: \""+ entityFile.getAbsolutePath()+"\"");
106             if(entityFile.exists())
107             {
108                 // Create buffered file reader.
109                 Reader fileReader = new FileReader(entityFile);
110                 fileReader = new BufferedReader(fileReader, 10000);
111                 
112                 // Wrap file reader in entity reader.
113                 EntityReader entityReader = new EntityReader(fileReader, mFieldSeparator, mRecordSeparator);
114                 entityReader.setStringDelimiter(mStringDelimiter);
115                 
116                 // Read table selection.
117                 dbSelection = entityReader.readMappedSelection(entityDescriptor);  
118                 
119                 // Close reader.
120                 fileReader.close();    
121             }   
122             else
123                 dbSelection = new Selection(entityDescriptor);
124             Log.print(this, "Loading completed. "+(dbSelection!=null ? dbSelection.size() : 0)+" entities loaded.");
125         }
126         catch(Exception e)
127         {
128             Log.printError(this, "Failed to load complete table selection: "+ entityDescriptor, e);
129         }
130         
131         return dbSelection;
132     }
133     
134     protected void storeTableSelection(ISelection dbSelection)
135     {
136         try
137         {
138             // Create buffered file writer.
139             File entityFile = new File(mDirectoryFile, dbSelection.getEntityDescriptor().getCodeName()+".tdb");
140             Log.print(this, "Storing table selection to: \""+ entityFile.getAbsolutePath()+"\"");
141             Writer fileWriter = new FileWriter(entityFile);
142             fileWriter = new BufferedWriter(fileWriter, 10000);
143             
144             // Wrap file reader in entity writer.
145             EntityWriter entityWriter = new EntityWriter(fileWriter, mFieldSeparator, mRecordSeparator);
146             entityWriter.setStringDelimiter(mStringDelimiter);
147 
148             // Write selection.
149             entityWriter.writeMappedSelection(dbSelection);
150             
151             // Flush and close writers.
152             entityWriter.flush();
153             entityWriter.close();
154             
155             Log.print(this, "Store completed. "+(dbSelection!=null ? dbSelection.size() : 0)+" entities stored.");
156         }
157         catch(Exception e)
158         {
159             Log.printError(this, "Failed to store table selection: "+ dbSelection.getEntityDescriptor(), e);
160         }
161     }    
162         
163     // Access methods ----------------------------------------------------------
164     
165     public int getFieldSeparator()
166     {
167         return mFieldSeparator;
168     }
169     
170     public void setFieldSeparator(int character)
171     {
172         mFieldSeparator = character;
173     }
174     
175     public int getRecordSeparator()
176     {
177         return mRecordSeparator;
178     }
179     
180     public void setRecordSeparator(int character)
181     {
182         mRecordSeparator = character;
183     }
184     
185     public int getStringDelimiter()
186     {
187         return mStringDelimiter;
188     }
189     
190     public void setStringDelimiter(int character)
191     {
192         mStringDelimiter = character;
193     }
194     
195     public File getDirectoryFile()
196     {
197         return mDirectoryFile;
198     }
199 }