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
package org.litesoft.bo.views;

import java.util.*;

import org.litesoft.bo.*;
import org.litesoft.core.simpletypes.*;
import org.litesoft.core.util.*;

public class FilterUtil
{
    private enum MapForm
    {
        ALL
                {
                    public Boolean wouldImplicitlyProducesSuperSet( MapForm pCurrent )
                    {
                        return false; // We are ALL!  Nothing can produce more than us!
                    }
                },
        REGULAR
                {
                    public Boolean wouldImplicitlyProducesSuperSet( MapForm pCurrent )
                    {
                        switch ( pCurrent )
                        {
                            case ALL:
                                return true;
                            case REGULAR:
                                return null; // Indeterminate
                            case EQUALS:
                                return true; // Too different, assume the worst!
                            case NONE:
                                return false;
                        }
                        return true;  // Assume the worst!
                    }
                },
        EQUALS
                {
                    public Boolean wouldImplicitlyProducesSuperSet( MapForm pCurrent )
                    {
                        switch ( pCurrent )
                        {
                            case ALL:
                                return true;
                            case REGULAR:
                                return true; // Too different, assume the worst!
                            case EQUALS:
                                return null; // Indeterminate
                            case NONE:
                                return false;
                        }
                        return true;  // Assume the worst!
                    }
                },
        NONE
                {
                    public Boolean wouldImplicitlyProducesSuperSet( MapForm pCurrent )
                    {
                        return NONE != pCurrent; // if we are None then anything other than NONE can produce more than us!
                    }
                };

        abstract public Boolean wouldImplicitlyProducesSuperSet( MapForm pCurrent );
    }

    /**
     * Tests if the <code>pCurrent</code> filter would NOT produce a sub-set of the set that <code>pBase</code> would produce.
     *
     * @param pMetaData non-null. The meta data for the view object being filtered.
     * @param pBase     non-null
     * @param pCurrent  non-null
     *
     * @return <code>true</code> if the <code>pCurrent</code> filter would NOT produce a sub-set of the set that <code>pBase</code> would produce.
     */
    @SuppressWarnings({"SimplifiableIfStatement"})
    public static boolean needsRefresh( BoMetaData pMetaData, BoAccessorFilters pBase, BoAccessorFilters pCurrent )
    {
        UtilsCommon.assertNotNull( "pMetaData", pMetaData );
        UtilsCommon.assertNotNull( "pBase", pBase );
        UtilsCommon.assertNotNull( "pCurrent", pCurrent );

        SSMap zBaseFilters = pBase.getUniqueFilters();
        SSMap zCurrentFilters = pCurrent.getUniqueFilters();

        if ( !zBaseFilters.isEmpty() )
        {
            return needsRefresh( pMetaData, zBaseFilters, zCurrentFilters );
        }
        if ( !zCurrentFilters.isEmpty() )
        {
            return true;
        }

        return needsRefresh( pMetaData, pBase.getNonUniqueFilters(), pCurrent.getNonUniqueFilters() );
    }

    private static MapForm determineForm( SSMap pFilters )
    {
        if ( pFilters instanceof NoRowsMatchingFilterMap )
        {
            return MapForm.NONE;
        }
        if ( (pFilters == null) || pFilters.isEmpty() )
        {
            return MapForm.ALL;
        }
        return (pFilters instanceof EqualsMatchingFilterMap) ? MapForm.EQUALS : MapForm.REGULAR;
    }

    /**
     * Tests if the <code>pCurrentFilters</code> filter would NOT produce a sub-set of the set that <code>pBaseFilters</code> would produce.
     * <p/>
     * Note: two special types of SSMap: NoRowsMatchingFilterMap & EqualsMatchingFilterMap
     *
     * @param pMetaData       non-null. The meta data for the view object being filtered.
     * @param pBaseFilters    null / empty means ALL
     * @param pCurrentFilters null / empty means ALL
     *
     * @return <code>true</code> if the <code>pCurrentFilters</code> filter would NOT produce a sub-set of the set that <code>pBaseFilters</code> would produce.
     */
    public static boolean needsRefresh( BoMetaData pMetaData, SSMap pBaseFilters, SSMap pCurrentFilters )
    {
        MapForm zBaseForm = determineForm( pBaseFilters );

        Boolean zWouldImplicitlyProducesSuperSet = zBaseForm.wouldImplicitlyProducesSuperSet( determineForm( pCurrentFilters ) );

        if ( zWouldImplicitlyProducesSuperSet != null )
        {
            return zWouldImplicitlyProducesSuperSet;
        }

        if ( pBaseFilters.size() > pCurrentFilters.size() )
        {
            return true;
        }

        boolean zEquals = MapForm.EQUALS == zBaseForm;

        /*
         * Iterate over the base filter set, checking that a filter exists in
         * the current set which is the same or more restrictive.
         */
        for ( String zAttributeName : pBaseFilters.keySet() )
        {
            BoAttribute zAttribute;
            try
            {
                zAttribute = pMetaData.getBoAttribute( zAttributeName ); // todo: Custom Fields
            }
            catch ( NoSuchElementException e )
            {
                return true; // This is a simple coding Error!
            }
            String zBaseValue = pBaseFilters.get( zAttributeName );
            String zCurrentValue = pCurrentFilters.get( zAttributeName );
            if ( (zCurrentValue == null) && !pCurrentFilters.containsKey( zAttributeName ) ) // support 'null' values!
            {
                return true;
            }

            if ( isCurrentLessRestrictive( zEquals, zAttribute, zBaseValue, zCurrentValue ) )
            {
                return true;
            }
        }
        return false;
    }

    public static Filter createFilter( BoMetaData pMetaData, SSMap[] pORed_ANDFiltersNotNullNotEmpty )
    {
        if ( pORed_ANDFiltersNotNullNotEmpty.length == 0 )
        {
            return Filter.ALL;
        }
        Filter result = Filter.NONE;
        for ( SSMap zMap : pORed_ANDFiltersNotNullNotEmpty )
        {
            result = Filter.Or( result, createFilter( pMetaData, zMap ) );
            if ( result.getType() == Filter.Type.ALL )
            {
                break;
            }
        }
        return result;
    }

    public static Filter createFilter( BoMetaData pMetaData, SSMap pFilters )
    {
        if ( pFilters instanceof NoRowsMatchingFilterMap )
        {
            return Filter.NONE;
        }
        Filter result = Filter.ALL;
        if ( (pFilters != null) && !pFilters.isEmpty() )
        {
            boolean zEquals = (pFilters instanceof EqualsMatchingFilterMap);
            for ( String zAttributeName : pFilters.keySet() )
            {
                BoAttribute zAttribute;
                try
                {
                    zAttribute = pMetaData.getBoAttribute( zAttributeName ); // todo: Custom Fields
                }
                catch ( NoSuchElementException e )
                {
                    return Filter.NONE;
                }
                Filter zFilter = createFilter( zEquals, zAttribute, UtilsCommon.noEmpty( pFilters.get( zAttributeName ) ) ); // != Filter.Type.NONE
                result = Filter.And( result, zFilter );
            }
        }
        return result;
    }

    private static Filter createFilter( boolean pEquals, BoAttribute pAttribute, String pAttributeValue )
    {
        Filter result;
        if ( pEquals || pAttribute.getType() != BoAttribute.AttributeType.String ) // todo: Custom Fields
        {
            result = new Filter.ValueEqualsFilter( pAttribute.getName(), pAttributeValue );
        }
        else
        {
            result = new Filter.StringExpressionFilter( pAttribute.getName(), pAttributeValue, false ); // Case Sensitive
        }
        return result;
    }

    private static boolean isCurrentLessRestrictive( boolean pEquals, BoAttribute pAttribute, String pBaseValue, String pCurrentValue )
    {
        if ( UtilsCommon.areNonArraysEqual( pBaseValue, pCurrentValue ) ) // handle both null
        {
            return false; // equal implicitly means NOT Less Restrictive
        }
        if ( pEquals || pAttribute.getType() != BoAttribute.AttributeType.String ) // todo: Custom Fields
        {
            return true; // !equal means different, which means implicitly Less Restrictive
        }
        // Wild Carding...
        if ( (pBaseValue == null) || (pCurrentValue == null) )
        {
            return true; // if one is null then won't produce same results, which means implicitly Less Restrictive
        }
        String[] zBaseParts = createParts( pBaseValue );
        String[] zCurrentParts = createParts( pCurrentValue );

        int zBaseLast = zBaseParts.length - 1;
        int zCurrentLast = zCurrentParts.length - 1;

        if ( zBaseLast > zCurrentLast )
        {
            return true; // Current can NOT produce a sub-set
        }

        if ( !zCurrentParts[0].startsWith( zBaseParts[0] ) || //. . . . . . . . StartsWith
             !zCurrentParts[zCurrentLast].endsWith( zBaseParts[zBaseLast] ) )// EndsWith
        {
            return true;
        }

        int zCurrentIndex = 1;
        for ( int zBaseIndex = 1; zBaseIndex < zBaseLast; zBaseIndex++ )
        {
            String zBasePart = zBaseParts[zBaseIndex];
            do
            {
                if ( zCurrentIndex == zCurrentLast )
                {
                    return true; // Current did not contain a Base part, therefor it potentially wil produce a super-set
                }
            }
            while ( !zCurrentParts[zCurrentIndex++].contains( zBasePart ) );
        }
        return false;
    }

    private static String[] createParts( String pValue )
    {
        if ( pValue.indexOf( '*' ) == -1 )
        {
            pValue += '*';
        }
        return UtilsCommon.parseChar( pValue.toLowerCase(), '*' );
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/bo/views/FilterUtil.java

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