Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package org.litesoft.orsup;

import java.io.*;
import java.util.*;

import org.litesoft.configuration.*;
import org.litesoft.core.*;
import org.litesoft.core.util.*;
import org.litesoft.logger.*;
import org.litesoft.loggerconfig.*;
import org.litesoft.orsup.base.*;
import org.litesoft.orsup.nonpublic.*;
import org.litesoft.sql.*;
import org.litesoft.util.*;

public abstract class DB_Admin
{
    private CmdLineProcessor[] CMDS = //
            { //
              new Create( "create" ), //
              new Drop( "drop" ), //
              new Fresh( "fresh" ), //
              new Populate( "populate" ), //
              new ExportObject( "exportObject" ), //
              new Pause( "pause" ), //
              new ImportCSV( "importCSV" ), //
              new ExportCSV( "exportCSV" ), //
              // new ReCreateIndices( "reCreateIndices" ), //
            };

    public DB_Admin( MetaDataRegistry pRegistry, ConfigDataAccessorFactory pConfigDataAccessorFactory )
    {
        new ServerConfiguration( pConfigDataAccessorFactory ); // force creation & self registration of Configuration
        LoggerFactory.init( new ConfigurationLoggerLevel() );

        DataStore zDataStore = new DataStoreBaseSQL( pRegistry );

        DataStoreLocator.initialize( zDataStore );

        ServerContext.clearAllServerStores();

        ContextID zContextID = new ContextID( "Admin", "Admin" );
        new ServerContext( zContextID, new SimpleMapServerSession() ).set();
    }

    private String help( String pProblem )
    {
        if ( Utils.isNotNullOrEmpty( pProblem ) )
        {
            System.out.println();
            System.out.println( pProblem );
        }
        System.out.println();
        System.out.println( "Usage: java " + getClass().getSimpleName() + " command(s) ..." );
        System.out.println();
        System.out.println( "Where command(s) are one or more of the following:" );
        System.out.println();

        for ( CmdLineProcessor zProcessor : CMDS )
        {
            System.out.println( "   " + zProcessor.helpCMD() );
            if ( Utils.isNotNullOrEmpty( zProcessor.getDescription() ) )
            {
                System.out.println( "       " + zProcessor.getDescription() );
            }
            System.out.println();
        }
        System.exit( -1 );
        return null;
    }

    public CmdLineProcessor getProcessor( String pOperation )
    {
        for ( CmdLineProcessor processor : CMDS )
        {
            if ( processor.getCommand().equalsIgnoreCase( pOperation ) )
            {
                return processor;
            }
        }
        return null;
    }

    protected void processMainArgs( String argv[] )
            throws Exception
    {
        if ( argv.length >= 1 )
        {
            processArgs( new ArrayIterator<String>( argv ) );
            return;
        }
        help( "" );
    }

    private void processArgs( Iterator<String> pArgs )
            throws Exception
    {
        while ( pArgs.hasNext() )
        {
            String zCommand = pArgs.next();

            CmdLineProcessor processor = getProcessor( zCommand );
            if ( processor != null )
            {
                processor.callExecute( pArgs );
                continue;
            }
            help( "Unknown operation - " + zCommand );
        }
    }

    abstract class CmdLineProcessor
    {
        private String mCommand;
        private String mDescription;
        private String[] mParams;
        private Iterator<String> mArgs = null;
        protected int mIndexParams = 0;
        private boolean mIsVarArgs;

        protected CmdLineProcessor( String pCommand, String pDescription, boolean pIsVarArgs, String... pParams )
        {
            mCommand = pCommand;
            mDescription = pDescription;
            mIsVarArgs = pIsVarArgs;
            mParams = (pParams != null) ? pParams : new String[0];
        }

        public String getCommand()
        {
            return mCommand;
        }

        public String getDescription()
        {
            return mDescription;
        }

        public String[] getParams()
        {
            return mParams;
        }

        public final void callExecute( Iterator<String> pArgs )
                throws Exception
        {
            System.out.println( "executing " + getClass().getSimpleName() );
            mArgs = pArgs;
            mIndexParams = 0;
            execute();
        }

        abstract protected void execute()
                throws Exception;

        protected boolean hasAnotherArg()
        {
            return mArgs.hasNext();
        }

        protected String getNextArg()
        {
            if ( mIndexParams < mParams.length )
            {
                return getNextArg( mParams[mIndexParams++] );
            }
            if ( mIsVarArgs )
            {
                return getNextArg( null );
            }
            return help( "Attempt to read more parameters than specified" );
        }

        protected String getNextArg( String pWhat )
        {
            if ( !mIsVarArgs && !mArgs.hasNext() )
            {
                help( "Command '" + getCommand() + "' Missing: " + pWhat );
            }
            return mArgs.next();
        }

        public String helpCMD()
        {
            StringBuilder sb = new StringBuilder( mCommand ).append( ' ' );
            for ( String param : mParams )
            {
                sb.append( param ).append( ' ' );
            }
            return sb.toString();
        }
    }

    private class Create extends CmdLineProcessor
    {
        public Create( String pCommand )
        {
            super( pCommand, "create all tables", false );
        }

        protected final void execute()
                throws Exception
        {
            DataStoreLocator.get().createAllTables();
        }
    }

    private class Populate extends CmdLineProcessor
    {
        public Populate( String pCommand )
        {
            super( pCommand, "Load the DB via the DataLoader", false );
        }

        protected final void execute()
                throws Exception
        {
            DataLoader.INSTANCE.loadData();
        }
    }

    private class ImportCSV extends CmdLineProcessor
    {
        public ImportCSV( String pCommand )
        {
            super( pCommand, "Import CSV file(s) from Dir", false, "LoadDataDir" );
        }

        protected final void execute()
                throws Exception
        {
            DataLoader.INSTANCE.loadDataFrom( Utils.assertNotNullNotEmpty( "LoadDataDir", getNextArg() ) );
        }
    }

    private class ExportCSV extends CmdLineProcessor
    {
        private static final String DESC = "writes all POs in csv format appropriate for re-importing";

        public ExportCSV( String pCommand )
        {
            super( pCommand, DESC, false, "ExportFileName" );
        }

        public void execute()
                throws Exception
        {
            DB_Export.exportCSV( Utils.assertNotNullNotEmpty( "ExportFileName", getNextArg() ) );
        }
    }

    private class Drop extends CmdLineProcessor
    {
        public Drop( String pCommand )
        {
            super( pCommand, "drop all tables", false );
        }

        protected final void execute()
                throws Exception
        {
            DataStoreLocator.get().dropAllTables();
        }
    }

    private class Fresh extends CmdLineProcessor
    {
        public Fresh( String pCommand )
        {
            super( pCommand, "drop and create all tables", false );
        }

        protected final void execute()
                throws Exception
        {
            DataStoreLocator.get().dropAllTables();
            DataStoreLocator.get().createAllTables();
        }
    }

    private class Pause extends CmdLineProcessor
    {
        private static final String DESC = "wait to receive <enter> from console ";

        public Pause( String pCommand )
        {
            super( pCommand, DESC, true );
        }

        @SuppressWarnings({"ResultOfMethodCallIgnored"})
        public void execute()
                throws Exception
        {
            System.out.print( "press <enter> to continue: " );
            System.out.flush();
            System.in.read();
        }
    }

    private class ExportObject extends CmdLineProcessor
    {
        public ExportObject( String pCommand )
        {
            super( pCommand, "writes all objects of specified name(s) in csv format appropriate for re-importing to the specified ExportFileName", //
                   true, "ExportFileName", "ObjectName(s)" );
        }

        public void execute()
                throws Exception
        {
            String zExportFileName = Utils.assertNotNullNotEmpty( "ExportFileName", getNextArg() );
            String zObjectName = Utils.assertNotNullNotEmpty( "ObjectName", getNextArg() );

            DataStore zDS = DataStoreLocator.get();

            MetaDataStore zMDstore = zDS.getMetaDataStore();

            List<MetaDataForPO> zMDs = new ArrayList<MetaDataForPO>();
            zMDs.add( verifyMD( zMDstore, zObjectName ) );
            while ( hasAnotherArg() )
            {
                zMDs.add( verifyMD( zMDstore, Utils.assertNotNullNotEmpty( "ObjectName", getNextArg() ) ) );
            }

            UnfilteringFinder zFinder = zDS.getUnfilteredFinder();

            System.out.println( "Writing to file: " + zExportFileName );
            FileWriter w = new FileWriter( zExportFileName );

            for ( MetaDataForPO zMD : zMDs )
            {
                DB_Export.printObjects( w, zFinder.findAllCursored( zMD.getPOregistrationName(), null ) );
            }
            w.flush();
            w.close();
        }

        private MetaDataForPO verifyMD( MetaDataStore pMDstore, String pObjectName )
        {
            MetaDataForPO zMetaDataForPO = pMDstore.getMetaDataOptionallyByIdentifier( pObjectName );
            if ( zMetaDataForPO == null )
            {
                help( "No Object known by: " + pObjectName );
            }
            return zMetaDataForPO;
        }
    }

//    private class ReCreateIndices extends CmdLineProcessor
//    {
//        public ReCreateIndices( String pCommand )
//        {
//            super( pCommand, "Recreate Database Table Indices", false );
//        }
//
//        protected final void execute()
//                throws Exception
//        {
//            DataStoreLocator.get().reCreateAllTablesIndices();
//        }
//    }
}

Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/orsup/DB_Admin.java

Diff revisions: vs.
Revision Author Commited Message
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000