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 @ 44262
History | View | Annotate | Download (5.68 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 |
if (filter.test(attribute)) {
|
87 |
mostUsed.add(attribute); |
88 |
} |
89 |
} |
90 |
for (FeatureAttributeDescriptor attribute : this.getFavorites()) { |
91 |
if (contains(mostUsed, attribute.getName())) {
|
92 |
continue;
|
93 |
} |
94 |
if (filter.test(attribute)) {
|
95 |
favorites.add(attribute); |
96 |
} |
97 |
} |
98 |
for (FeatureAttributeDescriptor attribute : tthis) {
|
99 |
if (contains(mostUsed, attribute.getName())) {
|
100 |
continue;
|
101 |
} |
102 |
if (contains(favorites, attribute.getName())) {
|
103 |
continue;
|
104 |
} |
105 |
if (filter.test(attribute)) {
|
106 |
normal.add(attribute); |
107 |
} |
108 |
} |
109 |
mostUsed.sort(comparator); |
110 |
favorites.sort(comparator); |
111 |
normal.sort(comparator); |
112 |
|
113 |
if (max < 0) { |
114 |
max = Integer.MAX_VALUE;
|
115 |
} |
116 |
Iterator<FeatureAttributeDescriptor> it = new ChainedIterator<>( |
117 |
mostUsed.iterator(), |
118 |
favorites.iterator(), |
119 |
normal.iterator() |
120 |
); |
121 |
List<FeatureAttributeDescriptor> r = new ArrayList<>(); |
122 |
int n = 0; |
123 |
while (n < max && it.hasNext()) {
|
124 |
r.add(it.next()); |
125 |
n++; |
126 |
} |
127 |
return r;
|
128 |
} |
129 |
|
130 |
private static boolean contains(List<FeatureAttributeDescriptor> attributes, String name) { |
131 |
if (StringUtils.isBlank(name)) {
|
132 |
return false; |
133 |
} |
134 |
for (FeatureAttributeDescriptor attribute : attributes) {
|
135 |
if (StringUtils.equalsIgnoreCase(name, attribute.getName())) {
|
136 |
return true; |
137 |
} |
138 |
} |
139 |
return false; |
140 |
} |
141 |
|
142 |
@Override
|
143 |
public int getPriority(FeatureAttributeDescriptor attribute) { |
144 |
if( attribute==null || attribute.getTags()==null ) { |
145 |
return 0; |
146 |
} |
147 |
int p = attribute.getTags().getInt(DAL_SEARCH_ATTRIBUTE_PRIORITY, 0); |
148 |
return p;
|
149 |
} |
150 |
|
151 |
@Override
|
152 |
public List<FeatureAttributeDescriptor> getFavorites() { |
153 |
FeatureType featureType= this.getObject();
|
154 |
List<FeatureAttributeDescriptor> attrs = new ArrayList<>(); |
155 |
for (FeatureAttributeDescriptor attribute : featureType) {
|
156 |
if( this.getPriority(attribute)>0 ) { |
157 |
attrs.add(attribute); |
158 |
} |
159 |
} |
160 |
if( attrs.isEmpty() ) {
|
161 |
return Collections.EMPTY_LIST; |
162 |
} |
163 |
attrs.sort(new Comparator<FeatureAttributeDescriptor>() { |
164 |
@Override
|
165 |
public int compare(FeatureAttributeDescriptor o1, FeatureAttributeDescriptor o2) { |
166 |
return Integer.compare(getPriority(o1), getPriority(o2)); |
167 |
} |
168 |
}); |
169 |
return attrs;
|
170 |
} |
171 |
|
172 |
|
173 |
|
174 |
} |