Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / complements / search / SearchImpl.java @ 46505

History | View | Annotate | Download (7.53 KB)

1
package org.gvsig.fmap.dal.complements.search;
2

    
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.Collections;
6
import java.util.Comparator;
7
import java.util.Iterator;
8
import java.util.List;
9
import java.util.function.Predicate;
10
import org.apache.commons.lang3.StringUtils;
11
import org.gvsig.fmap.dal.DataManager;
12
import org.gvsig.fmap.dal.complements.Search;
13
import static org.gvsig.fmap.dal.complements.Search.DAL_SEARCH_ATTRIBUTE_PRIORITY;
14
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
15
import org.gvsig.fmap.dal.feature.FeatureType;
16
import org.gvsig.tools.complement.AbstractComplement;
17
import org.gvsig.tools.complement.ComplementFactory;
18
import org.gvsig.tools.util.ChainedIterator;
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
21

    
22
/**
23
 *
24
 * @author jjdelcerro
25
 */
26
@SuppressWarnings("UseSpecificCatch")
27
public class SearchImpl
28
        extends AbstractComplement<FeatureType>
29
        implements Search {
30

    
31
    protected static final Logger LOGGER = LoggerFactory.getLogger(SearchImpl.class);
32

    
33
    public SearchImpl(ComplementFactory<FeatureType> factory, FeatureType object) {
34
        super(factory, object);
35
    }
36

    
37
    @Override
38
    public List<String> getResultColumnNames() {
39
        FeatureType featureType = this.getObject();
40
        List<String> columnNames;
41
        String columns = featureType.getTags().getString(DataManager.DAL_PREFERRED_COLUMNS, null);
42
        if( StringUtils.isBlank(columns) ) {
43
            List<FeatureAttributeDescriptor> attributes = featureType.getFilteredAttributes(
44
                    FeatureType.BASIC_TYPES_FILTER,
45
                    12
46
            );
47
            attributes.sort(Search.STR_INT_LONG_LABEL_ORDER);
48
            columnNames = new ArrayList<>();
49
            for (FeatureAttributeDescriptor attr : attributes) {
50
                columnNames.add(attr.getName());
51
            }
52
        } else {
53
            columnNames = Arrays.asList(split(columns, ":/;,|-"));
54
        }
55
        return columnNames;
56
    }
57

    
58
    private String[] split(String value, String separators) {
59
        int firstSeparatorPosition = 1000000;
60
        Character sep = null;
61
        for (char ch : separators.toCharArray()) {
62
            int pos = value.indexOf(ch);
63
            if( pos>0 && pos<firstSeparatorPosition ) {
64
                sep = ch;
65
                firstSeparatorPosition = pos;
66
            }
67
        }
68
        if( sep == null ) {
69
            return new String[] { value };
70
        }
71
        return value.split("["+sep+"]");
72
    }
73

    
74
    private static class AnOrderedAttribute implements OrderedAttribute {
75
        private final FeatureAttributeDescriptor descriptor;
76
        private final int type;
77
        
78
        public AnOrderedAttribute(FeatureAttributeDescriptor descriptor, int type) {
79
            this.descriptor = descriptor;
80
            this.type = type;
81
        }
82

    
83
        @Override
84
        public FeatureAttributeDescriptor getDescriptor() {
85
            return descriptor;
86
        }
87

    
88
        @Override
89
        public int getType() {
90
            return type;
91
        }
92
        
93
    }
94
    
95
    @Override
96
    public List<OrderedAttribute> getOrderedAttributes(
97
            Predicate<FeatureAttributeDescriptor> filter, 
98
            final Comparator<FeatureAttributeDescriptor> comparator,
99
            int max
100
        ) {
101
        return this.getOrderedAttributes(filter, comparator, max, null);
102
    }
103
    
104
    @Override
105
    public List<OrderedAttribute> getOrderedAttributes(
106
            Predicate<FeatureAttributeDescriptor> filter, 
107
            final Comparator<FeatureAttributeDescriptor> comparator,
108
            int max,
109
            List<FeatureAttributeDescriptor> extra
110
        ) {
111
        FeatureType tthis = this.getObject();
112

    
113
        List<OrderedAttribute> mostUsed = new ArrayList<>();
114
        List<OrderedAttribute> favorites = new ArrayList<>();
115
        List<OrderedAttribute> normal = new ArrayList<>();
116

    
117
        for (FeatureAttributeDescriptor attribute : tthis.getRecentUseds()) {
118
            mostUsed.add(new AnOrderedAttribute(attribute, OrderedAttribute.TYPE_RECENT));
119
        }
120
        for (FeatureAttributeDescriptor attribute : this.getFavorites()) {
121
            if (contains(mostUsed, attribute.getName())) {
122
                continue;
123
            }
124
            if (filter.test(attribute)) {
125
                favorites.add(new AnOrderedAttribute(attribute, OrderedAttribute.TYPE_FAVORITE));
126
            }
127
        }
128
        for (FeatureAttributeDescriptor attribute : tthis.getAllAttributeDescriptors()) {
129
            if (contains(mostUsed, attribute.getName())) {
130
                continue;
131
            }
132
            if (contains(favorites, attribute.getName())) {
133
                continue;
134
            }
135
            if (filter.test(attribute)) {
136
                normal.add(new AnOrderedAttribute(attribute, OrderedAttribute.TYPE_REGURAL));
137
            }
138
        }
139
        
140
        if(extra != null){
141
            for (FeatureAttributeDescriptor attribute : extra) {
142
                if (contains(mostUsed, attribute.getName())) {
143
                    continue;
144
                }
145
                if (contains(favorites, attribute.getName())) {
146
                    continue;
147
                }
148
                if (filter.test(attribute)) {
149
                    normal.add(new AnOrderedAttribute(attribute, OrderedAttribute.TYPE_EXTRA));
150
                }
151
            }
152
        }
153
        
154
        Comparator<OrderedAttribute> comparatorAdapter = new Comparator<OrderedAttribute>() {
155
            @Override
156
            public int compare(OrderedAttribute o1, OrderedAttribute o2) {
157
                return comparator.compare(o1.getDescriptor(), o2.getDescriptor());
158
            }
159
        };
160
        mostUsed.sort(comparatorAdapter);
161
        favorites.sort(comparatorAdapter);
162
        normal.sort(comparatorAdapter);
163

    
164
        if (max < 0) {
165
            max = Integer.MAX_VALUE;
166
        }
167
        Iterator<OrderedAttribute> it = new ChainedIterator<>(
168
                mostUsed.iterator(),
169
                favorites.iterator(),
170
                normal.iterator()
171
        );
172
        List<OrderedAttribute> r = new ArrayList<>();
173
        int n = 0;
174
        while (n < max && it.hasNext()) {
175
            r.add(it.next());
176
            n++;
177
        }
178
        return r;
179
    }
180

    
181
    private static boolean contains(List<OrderedAttribute> attributes, String name) {
182
        if (StringUtils.isBlank(name)) {
183
            return false;
184
        }
185
        for (OrderedAttribute attribute : attributes) {
186
            if (StringUtils.equalsIgnoreCase(name, attribute.getDescriptor().getName())) {
187
                return true;
188
            }
189
        }
190
        return false;
191
    }
192

    
193
    @Override
194
    public int getPriority(FeatureAttributeDescriptor attribute) {
195
        if( attribute==null || attribute.getTags()==null ) {
196
            return 0;
197
        }
198
        int p = attribute.getTags().getInt(DAL_SEARCH_ATTRIBUTE_PRIORITY, 0);
199
        return p;
200
    }
201

    
202
    @Override
203
    public List<FeatureAttributeDescriptor> getFavorites() {
204
        FeatureType featureType= this.getObject();
205
        List<FeatureAttributeDescriptor> attrs = new ArrayList<>();
206
        for (FeatureAttributeDescriptor attribute : featureType) {
207
            if( this.getPriority(attribute)>0 ) {
208
                attrs.add(attribute);
209
            }
210
        }
211
        if( attrs.isEmpty() ) {
212
            return Collections.EMPTY_LIST;
213
        }
214
        attrs.sort(new Comparator<FeatureAttributeDescriptor>() {
215
            @Override
216
            public int compare(FeatureAttributeDescriptor o1, FeatureAttributeDescriptor o2) {
217
                return Integer.compare(getPriority(o1), getPriority(o2));
218
            }
219
        });
220
        return attrs;
221
    }
222

    
223
    
224

    
225
}