Subversion Repository Public Repository

litesoft

Diff Revisions 947 vs 948 for /trunk/Java/core/Server/src/org/litesoft/orsup/DB_Admin.java

Diff revisions: vs.
  @@ -1,13 +1,8 @@
1 1 // This Source Code is in the Public Domain per: http://unlicense.org
2 2 package org.litesoft.orsup;
3 3
4 - import java.io.*;
5 -
6 4 import org.litesoft.commonfoundation.iterators.*;
7 5 import org.litesoft.commonfoundation.typeutils.*;
8 -
9 - import java.util.*;
10 -
11 6 import org.litesoft.configuration.*;
12 7 import org.litesoft.core.*;
13 8 import org.litesoft.logger.*;
  @@ -17,8 +12,10 @@
17 12 import org.litesoft.orsup.nonpublic.*;
18 13 import org.litesoft.sql.*;
19 14
20 - public abstract class DB_Admin
21 - {
15 + import java.io.*;
16 + import java.util.*;
17 +
18 + public abstract class DB_Admin {
22 19 private CmdLineProcessor[] CMDS = //
23 20 { //
24 21 new Create( "create" ), //
  @@ -32,8 +29,7 @@
32 29 // new ReCreateIndices( "reCreateIndices" ), //
33 30 };
34 31
35 - public DB_Admin( MetaDataRegistry pRegistry, ConfigDataAccessorFactory pConfigDataAccessorFactory )
36 - {
32 + public DB_Admin( MetaDataRegistry pRegistry, ConfigDataAccessorFactory pConfigDataAccessorFactory ) {
37 33 ConfigDataAccessor zAccessor = pConfigDataAccessorFactory.createConfigDataAccessor();
38 34 zAccessor.overrideAllStartingWith( LoggerLevel.CONFIGURATION_BASE + ".", "Warn" );
39 35 zAccessor.setKeyValue( ConfigDataAccessor.Level.RUN, LoggerLevel.CONFIGURATION_BASE + ".org.litesoft.orsup.DB_Import", "Info" );
  @@ -51,10 +47,8 @@
51 47 new ServerContext( zContextID, new SimpleMapServerSession() ).set();
52 48 }
53 49
54 - private String help( String pProblem )
55 - {
56 - if ( Strings.isNotNullOrEmpty( pProblem ) )
57 - {
50 + private String help( String pProblem ) {
51 + if ( Strings.isNotNullOrEmpty( pProblem ) ) {
58 52 System.out.println();
59 53 System.out.println( pProblem );
60 54 }
  @@ -64,11 +58,9 @@
64 58 System.out.println( "Where command(s) are one or more of the following:" );
65 59 System.out.println();
66 60
67 - for ( CmdLineProcessor zProcessor : CMDS )
68 - {
61 + for ( CmdLineProcessor zProcessor : CMDS ) {
69 62 System.out.println( " " + zProcessor.helpCMD() );
70 - if ( Strings.isNotNullOrEmpty( zProcessor.getDescription() ) )
71 - {
63 + if ( Strings.isNotNullOrEmpty( zProcessor.getDescription() ) ) {
72 64 System.out.println( " " + zProcessor.getDescription() );
73 65 }
74 66 System.out.println();
  @@ -77,12 +69,9 @@
77 69 return null;
78 70 }
79 71
80 - public CmdLineProcessor getProcessor( String pOperation )
81 - {
82 - for ( CmdLineProcessor processor : CMDS )
83 - {
84 - if ( processor.getCommand().equalsIgnoreCase( pOperation ) )
85 - {
72 + public CmdLineProcessor getProcessor( String pOperation ) {
73 + for ( CmdLineProcessor processor : CMDS ) {
74 + if ( processor.getCommand().equalsIgnoreCase( pOperation ) ) {
86 75 return processor;
87 76 }
88 77 }
  @@ -90,10 +79,8 @@
90 79 }
91 80
92 81 protected void processMainArgs( String argv[] )
93 - throws Exception
94 - {
95 - if ( argv.length >= 1 )
96 - {
82 + throws Exception {
83 + if ( argv.length >= 1 ) {
97 84 processArgs( new ArrayIterator<String>( argv ) );
98 85 return;
99 86 }
  @@ -101,15 +88,12 @@
101 88 }
102 89
103 90 private void processArgs( Iterator<String> pArgs )
104 - throws Exception
105 - {
106 - while ( pArgs.hasNext() )
107 - {
91 + throws Exception {
92 + while ( pArgs.hasNext() ) {
108 93 String zCommand = pArgs.next();
109 94
110 95 CmdLineProcessor processor = getProcessor( zCommand );
111 - if ( processor != null )
112 - {
96 + if ( processor != null ) {
113 97 processor.callExecute( pArgs );
114 98 continue;
115 99 }
  @@ -117,8 +101,7 @@
117 101 }
118 102 }
119 103
120 - abstract class CmdLineProcessor
121 - {
104 + abstract class CmdLineProcessor {
122 105 private String mCommand;
123 106 private String mDescription;
124 107 private String[] mParams;
  @@ -126,27 +109,23 @@
126 109 protected int mIndexParams = 0;
127 110 private boolean mIsVarArgs;
128 111
129 - protected CmdLineProcessor( String pCommand, String pDescription, boolean pIsVarArgs, String... pParams )
130 - {
112 + protected CmdLineProcessor( String pCommand, String pDescription, boolean pIsVarArgs, String... pParams ) {
131 113 mCommand = pCommand;
132 114 mDescription = pDescription;
133 115 mIsVarArgs = pIsVarArgs;
134 116 mParams = (pParams != null) ? pParams : new String[0];
135 117 }
136 118
137 - public String getCommand()
138 - {
119 + public String getCommand() {
139 120 return mCommand;
140 121 }
141 122
142 - public String getDescription()
143 - {
123 + public String getDescription() {
144 124 return mDescription;
145 125 }
146 126
147 127 public final void callExecute( Iterator<String> pArgs )
148 - throws Exception
149 - {
128 + throws Exception {
150 129 System.out.println( "executing " + getClass().getSimpleName() );
151 130 mArgs = pArgs;
152 131 mIndexParams = 0;
  @@ -156,168 +135,137 @@
156 135 abstract protected void execute()
157 136 throws Exception;
158 137
159 - protected boolean hasAnotherArg()
160 - {
138 + protected boolean hasAnotherArg() {
161 139 return mArgs.hasNext();
162 140 }
163 141
164 - protected String getNextArg()
165 - {
166 - if ( mIndexParams < mParams.length )
167 - {
142 + protected String getNextArg() {
143 + if ( mIndexParams < mParams.length ) {
168 144 return getNextArg( mParams[mIndexParams++] );
169 145 }
170 - if ( mIsVarArgs )
171 - {
146 + if ( mIsVarArgs ) {
172 147 return getNextArg( null );
173 148 }
174 149 return help( "Attempt to read more parameters than specified" );
175 150 }
176 151
177 - protected String getNextArg( String pWhat )
178 - {
179 - if ( !mIsVarArgs && !mArgs.hasNext() )
180 - {
152 + protected String getNextArg( String pWhat ) {
153 + if ( !mIsVarArgs && !mArgs.hasNext() ) {
181 154 help( "Command '" + getCommand() + "' Missing: " + pWhat );
182 155 }
183 156 return mArgs.next();
184 157 }
185 158
186 - public String helpCMD()
187 - {
159 + public String helpCMD() {
188 160 StringBuilder sb = new StringBuilder( mCommand ).append( ' ' );
189 - for ( String param : mParams )
190 - {
161 + for ( String param : mParams ) {
191 162 sb.append( param ).append( ' ' );
192 163 }
193 164 return sb.toString();
194 165 }
195 166 }
196 167
197 - private class Create extends CmdLineProcessor
198 - {
199 - public Create( String pCommand )
200 - {
168 + private class Create extends CmdLineProcessor {
169 + public Create( String pCommand ) {
201 170 super( pCommand, "create all tables", false );
202 171 }
203 172
204 173 @Override
205 174 protected final void execute()
206 - throws Exception
207 - {
175 + throws Exception {
208 176 DataStoreLocator.get().createAllTables();
209 177 }
210 178 }
211 179
212 - private class Populate extends CmdLineProcessor
213 - {
214 - public Populate( String pCommand )
215 - {
180 + private class Populate extends CmdLineProcessor {
181 + public Populate( String pCommand ) {
216 182 super( pCommand, "Load the DB via the DataLoader", false );
217 183 }
218 184
219 185 @Override
220 186 protected final void execute()
221 - throws Exception
222 - {
187 + throws Exception {
223 188 DataLoader.INSTANCE.loadData();
224 189 }
225 190 }
226 191
227 - private class ImportCSV extends CmdLineProcessor
228 - {
229 - public ImportCSV( String pCommand )
230 - {
192 + private class ImportCSV extends CmdLineProcessor {
193 + public ImportCSV( String pCommand ) {
231 194 super( pCommand, "Import CSV file(s) from Dir", false, "LoadDataDir" );
232 195 }
233 196
234 197 @Override
235 198 protected final void execute()
236 - throws Exception
237 - {
199 + throws Exception {
238 200 DataLoader.INSTANCE.loadDataFrom( Strings.assertNotNullNotEmpty( "LoadDataDir", getNextArg() ) );
239 201 }
240 202 }
241 203
242 - private class ExportCSV extends CmdLineProcessor
243 - {
204 + private class ExportCSV extends CmdLineProcessor {
244 205 private static final String DESC = "writes all POs in csv format appropriate for re-importing";
245 206
246 - public ExportCSV( String pCommand )
247 - {
207 + public ExportCSV( String pCommand ) {
248 208 super( pCommand, DESC, false, "ExportFileName" );
249 209 }
250 210
251 211 @Override
252 212 public void execute()
253 - throws Exception
254 - {
213 + throws Exception {
255 214 DB_Export.exportCSV( Strings.assertNotNullNotEmpty( "ExportFileName", getNextArg() ) );
256 215 }
257 216 }
258 217
259 - private class Drop extends CmdLineProcessor
260 - {
261 - public Drop( String pCommand )
262 - {
218 + private class Drop extends CmdLineProcessor {
219 + public Drop( String pCommand ) {
263 220 super( pCommand, "drop all tables", false );
264 221 }
265 222
266 223 @Override
267 224 protected final void execute()
268 - throws Exception
269 - {
225 + throws Exception {
270 226 DataStoreLocator.get().dropAllTables();
271 227 }
272 228 }
273 229
274 - private class Fresh extends CmdLineProcessor
275 - {
276 - public Fresh( String pCommand )
277 - {
230 + private class Fresh extends CmdLineProcessor {
231 + public Fresh( String pCommand ) {
278 232 super( pCommand, "drop and create all tables", false );
279 233 }
280 234
281 235 @Override
282 236 protected final void execute()
283 - throws Exception
284 - {
237 + throws Exception {
285 238 DataStoreLocator.get().dropAllTables();
286 239 DataStoreLocator.get().createAllTables();
287 240 }
288 241 }
289 242
290 - private class Pause extends CmdLineProcessor
291 - {
243 + private class Pause extends CmdLineProcessor {
292 244 private static final String DESC = "wait to receive <enter> from console ";
293 245
294 - public Pause( String pCommand )
295 - {
246 + public Pause( String pCommand ) {
296 247 super( pCommand, DESC, true );
297 248 }
298 249
299 - @SuppressWarnings({"ResultOfMethodCallIgnored"}) @Override
250 + @SuppressWarnings({"ResultOfMethodCallIgnored"})
251 + @Override
300 252 public void execute()
301 - throws Exception
302 - {
253 + throws Exception {
303 254 System.out.print( "press <enter> to continue: " );
304 255 System.out.flush();
305 256 System.in.read();
306 257 }
307 258 }
308 259
309 - private class ExportObject extends CmdLineProcessor
310 - {
311 - public ExportObject( String pCommand )
312 - {
260 + private class ExportObject extends CmdLineProcessor {
261 + public ExportObject( String pCommand ) {
313 262 super( pCommand, "writes all objects of specified name(s) in csv format appropriate for re-importing to the specified ExportFileName", //
314 263 true, "ExportFileName", "ObjectName(s)" );
315 264 }
316 265
317 266 @Override
318 267 public void execute()
319 - throws Exception
320 - {
268 + throws Exception {
321 269 String zExportFileName = Strings.assertNotNullNotEmpty( "ExportFileName", getNextArg() );
322 270 String zObjectName = Strings.assertNotNullNotEmpty( "ObjectName", getNextArg() );
323 271
  @@ -327,8 +275,7 @@
327 275
328 276 List<MetaDataForPO> zMDs = new ArrayList<MetaDataForPO>();
329 277 zMDs.add( verifyMD( zMDstore, zObjectName ) );
330 - while ( hasAnotherArg() )
331 - {
278 + while ( hasAnotherArg() ) {
332 279 zMDs.add( verifyMD( zMDstore, Strings.assertNotNullNotEmpty( "ObjectName", getNextArg() ) ) );
333 280 }
334 281
  @@ -337,19 +284,16 @@
337 284 System.out.println( "Writing to file: " + zExportFileName );
338 285 FileWriter w = new FileWriter( zExportFileName );
339 286
340 - for ( MetaDataForPO zMD : zMDs )
341 - {
287 + for ( MetaDataForPO zMD : zMDs ) {
342 288 DB_Export.printObjects( w, zFinder.findAllCursored( zMD.getPOregistrationName(), null ) );
343 289 }
344 290 w.flush();
345 291 w.close();
346 292 }
347 293
348 - private MetaDataForPO verifyMD( MetaDataStore pMDstore, String pObjectName )
349 - {
294 + private MetaDataForPO verifyMD( MetaDataStore pMDstore, String pObjectName ) {
350 295 MetaDataForPO zMetaDataForPO = pMDstore.getMetaDataOptionallyByIdentifier( pObjectName );
351 - if ( zMetaDataForPO == null )
352 - {
296 + if ( zMetaDataForPO == null ) {
353 297 help( "No Object known by: " + pObjectName );
354 298 }
355 299 return zMetaDataForPO;