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  import java.net.*;
24  
25  import org.caleigo.core.*;
26  import org.caleigo.toolkit.log.*;
27  
28  /*** TextFileDataService provides a simple extension of the MemoryDataService
29   * that stores the data entities in text files in a database file-catalog.
30   *
31   * Note that the class is not suitable to store sensitive data. The class
32   * does not verify foreign keys and should only be used for temporary
33   * storage and/or for test or demonstration purposes.
34   *
35   * @author  Dennis Zikovic
36   * @version 1.00
37   * 
38   *//* 
39   *
40   * WHEN        WHO               WHY & WHAT
41   * -----------------------------------------------------------------------------
42   * 2003-01-07  Dennis Zikovic    Creation
43   */
44  public class TextURLDataService extends MemoryDataService
45  {
46      // Constants ---------------------------------------------------------------
47      private static final String NULL = "<NULL>";
48      
49      // Data members ------------------------------------------------------------
50      private URL mRootURL;
51      
52      private int mFieldSeparator = ';';
53      private int mRecordSeparator = '\n';
54      private int mStringDelimiter = '"';
55      
56      // Constructors ------------------------------------------------------------
57  	public TextURLDataService(IDataSourceDescriptor dataSourceDescriptor, URL dbRootURL) 
58  	{
59  		this(dataSourceDescriptor, dataSourceDescriptor.getSourceName(), dbRootURL);
60  	}
61      
62      public TextURLDataService(IDataSourceDescriptor dataSourceDescriptor, Object serviceIdentity, URL dbRootURL) 
63      {
64          super(dataSourceDescriptor, serviceIdentity);
65          mRootURL = dbRootURL;
66      }
67      
68      // Superclass overrides ----------------------------------------------------
69      public boolean ping()
70      {
71          return mRootURL!=null;
72      }
73      
74      protected ISelection loadTableSelection(IEntityDescriptor entityDescriptor)
75      {
76          ISelection dbSelection = null;
77  
78          try
79          {            
80              URL entityURL = new URL(mRootURL, entityDescriptor.getCodeName()+".tdb");
81              Log.print(this, "Loading table selection from: \""+ entityURL.toString()+"\"");
82  
83              // Create buffered file reader.
84              Reader fileReader = new InputStreamReader(entityURL.openStream());
85              fileReader = new BufferedReader(fileReader, 10000);
86              
87              // Wrap file reader in entity reader.
88              EntityReader entityReader = new EntityReader(fileReader, mFieldSeparator, mRecordSeparator);
89              entityReader.setStringDelimiter(mStringDelimiter);
90              
91              // Read table selection.
92              dbSelection = entityReader.readMappedSelection(entityDescriptor);  
93              
94              // Close reader.
95              fileReader.close();    
96  
97              Log.print(this, "Loading completed. "+(dbSelection!=null ? dbSelection.size() : 0)+" entities loaded.");
98          }
99          catch(Exception e)
100         {
101             Log.printError(this, "Failed to load complete table selection: "+ entityDescriptor, e);
102             dbSelection = new Selection(entityDescriptor);
103         }
104         
105         return dbSelection;
106     }
107     
108     protected void storeTableSelection(ISelection dbSelection)
109     {
110     }    
111         
112     // Access methods ----------------------------------------------------------
113     
114     public int getFieldSeparator()
115     {
116         return mFieldSeparator;
117     }
118     
119     public void setFieldSeparator(int character)
120     {
121         mFieldSeparator = character;
122     }
123     
124     public int getRecordSeparator()
125     {
126         return mRecordSeparator;
127     }
128     
129     public void setRecordSeparator(int character)
130     {
131         mRecordSeparator = character;
132     }
133     
134     public int getStringDelimiter()
135     {
136         return mStringDelimiter;
137     }
138     
139     public void setStringDelimiter(int character)
140     {
141         mStringDelimiter = character;
142     }
143     
144     public URL getRootURL()
145     {
146         return mRootURL;
147     }
148 }