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
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.orsup;

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

import org.litesoft.bo.attributes.*;
import org.litesoft.core.typeutils.*;
import org.litesoft.core.util.*;
import org.litesoft.db.*;
import org.litesoft.logger.*;
import org.litesoft.orsup.base.*;
import org.litesoft.orsup.nonpublic.*;
import org.litesoft.orsup.otherattributeaccessors.*;

public class DB_Export
{
    static Logger sLogger = LoggerFactory.getLogger( DB_Export.class );

    public static final char NEW_LINE_CHAR = '\n';

    /**
     * Exports current database to given file in csv format
     */
    public static void exportCSV( String fileName )
            throws IOException
    {
        FileWriter zWriter = new FileWriter( fileName );
        exportCSV( zWriter );
        zWriter.flush();
        zWriter.close();
    }

    /**
     * Exports current database to given file in csv format
     */
    public static void exportCSV( Writer pWriter )
            throws IOException
    {
        DataStore ds = DataStoreLocator.get();

        UnfilteringFinder zFinder = ds.getUnfilteredFinder();

        MetaDataStore zMetaDataStore = ds.getMetaDataStore();
        if ( !(zMetaDataStore instanceof MetaDataRegistry) )
        {
            throw new Error( "MetaDataStore not a MetaDataRegistry" );
        }
        MetaDataForPO[] zPO_MDs = reverseAndFilter( ((MetaDataRegistry) zMetaDataStore).getInReducingDependancyOrderAllMetaDataForPO() );

        for ( MetaDataForPO md : zPO_MDs )
        {
            exportByType( pWriter, zFinder, md );
        }
    }

    public static void printObjects( Writer pWriter, POQueryIterator pPOs )
            throws IOException
    {
        if ( pPOs.hasNext() )
        {
            PersistentObject zPO = pPOs.next();
            AttributeAccessorSCD[] zSCDs = zPO.getMetaDataForPO().getAccessorSCDs(); // PersistingAccessorSCDs();
            printObject( pWriter, zPO, zSCDs );
            while ( pPOs.hasNext() )
            {
                printObject( pWriter, pPOs.next(), zSCDs );
            }
        }
    }

    private static void exportByType( Writer pWriter, UnfilteringFinder pFinder, MetaDataForPO md )
            throws IOException
    {
        POQueryIterator zPOs;
        try
        {
            zPOs = pFinder.findAllCursored( md.getPOregistrationName(), null );
        }
        catch ( DBException e )
        {
            pWriter.write( "# *** Error *** " + md.getIdentifierName() + ": " + e.getMessage() + NEW_LINE_CHAR );
            sLogger.error.log( e, md.getIdentifierName() );
            return;
        }
        try
        {
            if ( zPOs.hasNext() )
            {
                pWriter.write( "# " + md.getIdentifierName() + NEW_LINE_CHAR );
                printObjects( pWriter, zPOs );
                pWriter.write( NEW_LINE_CHAR );
            }
        }
        finally
        {
            zPOs.releaseResources();
        }
    }

    private static MetaDataForPO[] reverseAndFilter( MetaDataForPO[] pPOmds )
    {
        if ( Objects.isNullOrEmpty( pPOmds ) )
        {
            return pPOmds;
        }
        List<MetaDataForPO> zPOmds = new ArrayList<MetaDataForPO>( pPOmds.length );
        for ( int zSrcNdx = pPOmds.length; --zSrcNdx >= 0; )
        {
            MetaDataForPO md = pPOmds[zSrcNdx];
            if ( md.isImportable() )
            {
                zPOmds.add( md );
            }
        }
        return zPOmds.toArray( new MetaDataForPO[zPOmds.size()] );
    }

    private static void printObject( Writer pWriter, PersistentObject<?> pPO, AttributeAccessorSCD... pSCDs )
            throws IOException
    {
        Vector<String> unencodedData = new Vector<String>();
        unencodedData.add( new PersistentObjectURL( pPO.getObjectName(), pPO.getPersistentObjectUniqueKey() ).toString() );
        for ( SupplimentedSCD attr : pSCDs )
        {
            if ( isExportable( attr ) )
            {
                String attributeName = attr.getName();
                Object value = pPO.getAttributeValue( attributeName );
                if ( value != null )
                {
                    if ( SupplimentedSCD.Form.ToOne.equals( attr.getForm() ) )
                    {
                        if ( attr instanceof AttributeAccessorSCDtoOneVariable )
                        {
                            AttributeAccessorSCDtoOneVariable zVariableOne = (AttributeAccessorSCDtoOneVariable) attr;
                            value = getVariableToOneDBvalue( pPO, zVariableOne );
                        }
                        else
                        {
                            PersistentObject zThem = (PersistentObject) value;
                            value = new PersistentObjectURL( zThem.getObjectName(), zThem.getPersistentObjectUniqueKey() );
                        }
                    }
                    if ( value != null )
                    {
                        String strValue = value.toString().trim();
                        if ( strValue.length() != 0 )
                        {
                            unencodedData.add( attributeName + "=" + strValue );
                        }
                    }
                }
            }
        }
        String encodedData = new CsvSupport().encode( unencodedData.toArray( new String[unencodedData.size()] ) );

        pWriter.append( encodedData );
        pWriter.append( NEW_LINE_CHAR );
        pWriter.flush();
    }

    private static boolean isExportable( SupplimentedSCD attr )
    {
        return !Mutability.RO.equals( attr.getMutability() ) && //
               !(attr instanceof NonImportableFeature) && //
               !AttributeMetaData.Type.Password.equals( attr.getAMD().getType() ) && //
               !SupplimentedSCD.Form.ToMany.equals( attr.getForm() );
    }

    @SuppressWarnings({"unchecked"})
    private static Object getVariableToOneDBvalue( PersistentObject<?> pPO, AttributeAccessorSCDtoOneVariable pVariableOne )
    {
        return pVariableOne.db_getValueOnPO( pPO );
    }
}

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

Diff revisions: vs.
Revision Author Commited Message
809 Diff Diff GeorgeS picture GeorgeS Thu 16 Aug, 2012 04:10:46 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

21 Diff Diff GeorgeS picture GeorgeS Tue 23 Feb, 2010 21:11:25 +0000
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000