Subversion Repository Public Repository

litesoft

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

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