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

import java.util.*;

import org.litesoft.core.simpletypes.nonpublic.*;
import org.litesoft.orsup.base.*;
import org.litesoft.orsup.otherattributeaccessors.*;

public class MetaDataRegistry implements MetaDataStore
{
    private Map<String, MetaDataForPO> mMetaDataByPOtablename = new HashMap<String, MetaDataForPO>();
    private Map<String, MetaDataForPO> mMetaDataByPOregistrationname = new HashMap<String, MetaDataForPO>();
    private Map<String, MetaDataForPO> mMetaDataByPOidentifierName = new HashMap<String, MetaDataForPO>();
    private MetaDataForPO[] mAllMetaDataForPOs;

    public MetaDataRegistry( MetaDataForPOinternalExtension... pMDs )
    {
        if ( pMDs != null )
        {
            for ( MetaDataForPOinternalExtension zMD : pMDs )
            {
                if ( zMD != null )
                {
                    add( zMD );
                }
            }
        }
        mAllMetaDataForPOs = mMetaDataByPOregistrationname.values().toArray( new MetaDataForPO[mMetaDataByPOregistrationname.size()] );
    }

    @Override
    public MetaDataForPO[] getAllMetaDataForPOs()
    {
        return mAllMetaDataForPOs;
    }

    @Override
    public MetaDataForPO getMetaDataOptionallyByIdentifier( String pIdentifierName )
    {
        return mMetaDataByPOidentifierName.get( pIdentifierName );
    }

    @Override
    public MetaDataForPO getMetaDataOptional( String pPersistedObjectRegistrationName )
    {
        return mMetaDataByPOregistrationname.get( pPersistedObjectRegistrationName );
    }

    @Override
    public MetaDataForPO getMetaDataOptional( Class<? extends PersistentObject> pPersistedObjectClass )
    {
        return getMetaDataOptional( pPersistedObjectClass.getName() );
    }

    @Override
    public MetaDataForPO getMetaDataRequired( String pPersistedObjectRegistrationName )
    {
        MetaDataForPO zMD = getMetaDataOptional( pPersistedObjectRegistrationName );
        if ( zMD != null )
        {
            return zMD;
        }
        throw new MetaDataNotFoundException( pPersistedObjectRegistrationName );
    }

    @Override
    public MetaDataForPO getMetaDataRequired( Class<? extends PersistentObject> pPersistedObjectClass )
    {
        return getMetaDataRequired( pPersistedObjectClass.getName() );
    }

    private void add( MetaDataForPOinternalExtension pMetaDataForPO )
    {
        String zRegistrationName = pMetaDataForPO.getPOregistrationName();
        if ( zRegistrationName.equals( Parts4Blobs.MyMetaData.INSTANCE.getPOregistrationName() ) )
        {
            throw new IllegalArgumentException( "Underlying Blob support PO (Parts4Blobs) Automatically added" );
        }
        addNonNull( pMetaDataForPO );
        if ( !mBlobSupportAdded )
        {
            if ( zRegistrationName.equals( Blob.MyMetaData.INSTANCE.getPOregistrationName() ) )
            {
                addBlobSupport();
            }
            else if ( usesBlobs( pMetaDataForPO ) )
            {
                addNonNull( Blob.MyMetaData.INSTANCE );
                addBlobSupport();
            }
        }
    }

    private boolean usesBlobs( MetaDataForPO pMetaDataForPO )
    {
        for ( AttributeAccessorSCD zSCD : pMetaDataForPO.getAccessorSCDs() )
        {
            if ( zSCD instanceof AttributeAccessorSCDtoBlob )
            {
                return true;
            }
        }
        return false;
    }

    private void addBlobSupport()
    {
        addNonNull( Parts4Blobs.MyMetaData.INSTANCE );
        mBlobSupportAdded = true;
    }

    private boolean mBlobSupportAdded = false;

    private void addNonNull( MetaDataForPOinternalExtension pMetaDataForPO )
    {
        String zIdentifierName = pMetaDataForPO.getIdentifierName();
        String zRegistrationName = pMetaDataForPO.getPOregistrationName();
        String zTableName = pMetaDataForPO.getTableName();
        if ( mMetaDataByPOtablename.containsKey( zTableName ) )
        {
            throw new IllegalArgumentException( "Duplicate Persistable PO Table " + zTableName );
        }
        if ( mMetaDataByPOregistrationname.containsKey( zRegistrationName ) )
        {
            throw new IllegalArgumentException( "Duplicate Persistable PO Registration Name " + zRegistrationName );
        }
        if ( mMetaDataByPOidentifierName.containsKey( zRegistrationName ) )
        {
            throw new IllegalArgumentException( "Duplicate Persistable PO Identifier Name " + zRegistrationName );
        }
        pMetaDataForPO.LLset( this );
        mMetaDataByPOtablename.put( zTableName, pMetaDataForPO );
        mMetaDataByPOregistrationname.put( zRegistrationName, pMetaDataForPO );
        mMetaDataByPOidentifierName.put( zIdentifierName, pMetaDataForPO );
    }

    public MetaDataForPO[] getInReducingDependancyOrderAllMetaDataForPO()
    {
        MetaDataForPO[] zAllMDs = getAllMetaDataForPOs();
        Map<String, RefsAndMDs> zRefs = new HashMap<String, RefsAndMDs>( zAllMDs.length );
        for ( MetaDataForPO md : zAllMDs )
        {
            zRefs.put( md.getPOregistrationName(), new RefsAndMDs( md ) );
        }
        for ( MetaDataForPO md : zAllMDs )
        {
            AttributeAccessorSCD[] aas = md.getPersistingAccessorSCDs();
            for ( AttributeAccessorSCD aa : aas )
            {
                if ( aa instanceof AbstractAttributeAccessorSCDtoOne )
                {
                    String zToThemName = ((AbstractAttributeAccessorSCDtoOne) aa).getToThemName();
                    if ( zToThemName != null )
                    {
                        RefsAndMDs zRefsAndMDs = zRefs.get( zToThemName );
                        if ( zRefsAndMDs == null )
                        {
                            throw new IllegalStateException( "No Refs for (" + aa.getName() + "): " + zToThemName );
                        }
                        zRefsAndMDs.incRef();
                    }
                }
            }
        }
        List<RefsAndMDs> zRefsAndMDs = new ArrayList<RefsAndMDs>( zRefs.values() );
        Collections.sort( zRefsAndMDs );
        MetaDataForPO[] rv = new MetaDataForPO[zRefsAndMDs.size()];
        for ( int i = 0; i < zRefsAndMDs.size(); i++ )
        {
            rv[i] = zRefsAndMDs.get( i ).getPMetaDataForPO();
        }
        return rv;
    }

    private static class RefsAndMDs implements Comparable<RefsAndMDs>
    {
        private int mRefs = 0;
        private MetaDataForPO mMetaDataForPO;

        public RefsAndMDs( MetaDataForPO pMetaDataForPO )
        {
            mMetaDataForPO = pMetaDataForPO;
        }

        public void incRef()
        {
            mRefs++;
        }

        public MetaDataForPO getPMetaDataForPO()
        {
            return mMetaDataForPO;
        }

        @Override
        public int compareTo( RefsAndMDs them )
        {
            return CompareSupport.compareEm( them.mRefs - this.mRefs, //
                                             them.mMetaDataForPO.getPOregistrationName().compareTo( this.mMetaDataForPO.getPOregistrationName() ) );
        }
    }
}

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

Diff revisions: vs.
Revision Author Commited Message
151 Diff Diff GeorgeS picture GeorgeS Thu 17 Mar, 2011 04:16:22 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

24 Diff Diff GeorgeS picture GeorgeS Wed 24 Feb, 2010 01:51:38 +0000
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000