1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
47 private static final String NULL = "<NULL>";
48
49
50 private URL mRootURL;
51
52 private int mFieldSeparator = ';';
53 private int mRecordSeparator = '\n';
54 private int mStringDelimiter = '"';
55
56
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
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
84 Reader fileReader = new InputStreamReader(entityURL.openStream());
85 fileReader = new BufferedReader(fileReader, 10000);
86
87
88 EntityReader entityReader = new EntityReader(fileReader, mFieldSeparator, mRecordSeparator);
89 entityReader.setStringDelimiter(mStringDelimiter);
90
91
92 dbSelection = entityReader.readMappedSelection(entityDescriptor);
93
94
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
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 }