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 @ 44263

History | View | Annotate | Download (5.62 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.complements.Search;
12
import static org.gvsig.fmap.dal.complements.Search.DAL_SEARCH_ATTRIBUTE_PRIORITY;
13
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
14
import org.gvsig.fmap.dal.feature.FeatureType;
15
import org.gvsig.tools.complement.AbstractComplement;
16
import org.gvsig.tools.complement.ComplementFactory;
17
import org.gvsig.tools.util.ChainedIterator;
18
import org.slf4j.Logger;
19
import org.slf4j.LoggerFactory;
20

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

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

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

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

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

    
73
    @Override
74
    public List<FeatureAttributeDescriptor> getOrderedAttributes(
75
            Predicate<FeatureAttributeDescriptor> filter, 
76
            Comparator<FeatureAttributeDescriptor> comparator,
77
            int max
78
        ) {
79
        FeatureType tthis = this.getObject();
80

    
81
        List<FeatureAttributeDescriptor> mostUsed = new ArrayList<>();
82
        List<FeatureAttributeDescriptor> favorites = new ArrayList<>();
83
        List<FeatureAttributeDescriptor> normal = new ArrayList<>();
84

    
85
        for (FeatureAttributeDescriptor attribute : tthis.getRecentUseds()) {
86
            mostUsed.add(attribute);
87
        }
88
        for (FeatureAttributeDescriptor attribute : this.getFavorites()) {
89
            if (contains(mostUsed, attribute.getName())) {
90
                continue;
91
            }
92
            if (filter.test(attribute)) {
93
                favorites.add(attribute);
94
            }
95
        }
96
        for (FeatureAttributeDescriptor attribute : tthis) {
97
            if (contains(mostUsed, attribute.getName())) {
98
                continue;
99
            }
100
            if (contains(favorites, attribute.getName())) {
101
                continue;
102
            }
103
            if (filter.test(attribute)) {
104
                normal.add(attribute);
105
            }
106
        }
107
        mostUsed.sort(comparator);
108
        favorites.sort(comparator);
109
        normal.sort(comparator);
110

    
111
        if (max < 0) {
112
            max = Integer.MAX_VALUE;
113
        }
114
        Iterator<FeatureAttributeDescriptor> it = new ChainedIterator<>(
115
                mostUsed.iterator(),
116
                favorites.iterator(),
117
                normal.iterator()
118
        );
119
        List<FeatureAttributeDescriptor> r = new ArrayList<>();
120
        int n = 0;
121
        while (n < max && it.hasNext()) {
122
            r.add(it.next());
123
            n++;
124
        }
125
        return r;
126
    }
127

    
128
    private static boolean contains(List<FeatureAttributeDescriptor> attributes, String name) {
129
        if (StringUtils.isBlank(name)) {
130
            return false;
131
        }
132
        for (FeatureAttributeDescriptor attribute : attributes) {
133
            if (StringUtils.equalsIgnoreCase(name, attribute.getName())) {
134
                return true;
135
            }
136
        }
137
        return false;
138
    }
139

    
140
    @Override
141
    public int getPriority(FeatureAttributeDescriptor attribute) {
142
        if( attribute==null || attribute.getTags()==null ) {
143
            return 0;
144
        }
145
        int p = attribute.getTags().getInt(DAL_SEARCH_ATTRIBUTE_PRIORITY, 0);
146
        return p;
147
    }
148

    
149
    @Override
150
    public List<FeatureAttributeDescriptor> getFavorites() {
151
        FeatureType featureType= this.getObject();
152
        List<FeatureAttributeDescriptor> attrs = new ArrayList<>();
153
        for (FeatureAttributeDescriptor attribute : featureType) {
154
            if( this.getPriority(attribute)>0 ) {
155
                attrs.add(attribute);
156
            }
157
        }
158
        if( attrs.isEmpty() ) {
159
            return Collections.EMPTY_LIST;
160
        }
161
        attrs.sort(new Comparator<FeatureAttributeDescriptor>() {
162
            @Override
163
            public int compare(FeatureAttributeDescriptor o1, FeatureAttributeDescriptor o2) {
164
                return Integer.compare(getPriority(o1), getPriority(o2));
165
            }
166
        });
167
        return attrs;
168
    }
169

    
170
    
171

    
172
}