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 @ 44340
History | View | Annotate | Download (6.64 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 |
FeatureType tthis = this.getObject();
|
102 |
|
103 |
List<OrderedAttribute> mostUsed = new ArrayList<>(); |
104 |
List<OrderedAttribute> favorites = new ArrayList<>(); |
105 |
List<OrderedAttribute> normal = new ArrayList<>(); |
106 |
|
107 |
for (FeatureAttributeDescriptor attribute : tthis.getRecentUseds()) {
|
108 |
mostUsed.add(new AnOrderedAttribute(attribute, OrderedAttribute.TYPE_RECENT));
|
109 |
} |
110 |
for (FeatureAttributeDescriptor attribute : this.getFavorites()) { |
111 |
if (contains(mostUsed, attribute.getName())) {
|
112 |
continue;
|
113 |
} |
114 |
if (filter.test(attribute)) {
|
115 |
favorites.add(new AnOrderedAttribute(attribute, OrderedAttribute.TYPE_FAVORITE));
|
116 |
} |
117 |
} |
118 |
for (FeatureAttributeDescriptor attribute : tthis) {
|
119 |
if (contains(mostUsed, attribute.getName())) {
|
120 |
continue;
|
121 |
} |
122 |
if (contains(favorites, attribute.getName())) {
|
123 |
continue;
|
124 |
} |
125 |
if (filter.test(attribute)) {
|
126 |
normal.add(new AnOrderedAttribute(attribute, OrderedAttribute.TYPE_REGURAL));
|
127 |
} |
128 |
} |
129 |
Comparator<OrderedAttribute> comparatorAdapter = new Comparator<OrderedAttribute>() { |
130 |
@Override
|
131 |
public int compare(OrderedAttribute o1, OrderedAttribute o2) { |
132 |
return comparator.compare(o1.getDescriptor(), o2.getDescriptor());
|
133 |
} |
134 |
}; |
135 |
mostUsed.sort(comparatorAdapter); |
136 |
favorites.sort(comparatorAdapter); |
137 |
normal.sort(comparatorAdapter); |
138 |
|
139 |
if (max < 0) { |
140 |
max = Integer.MAX_VALUE;
|
141 |
} |
142 |
Iterator<OrderedAttribute> it = new ChainedIterator<>( |
143 |
mostUsed.iterator(), |
144 |
favorites.iterator(), |
145 |
normal.iterator() |
146 |
); |
147 |
List<OrderedAttribute> r = new ArrayList<>(); |
148 |
int n = 0; |
149 |
while (n < max && it.hasNext()) {
|
150 |
r.add(it.next()); |
151 |
n++; |
152 |
} |
153 |
return r;
|
154 |
} |
155 |
|
156 |
private static boolean contains(List<OrderedAttribute> attributes, String name) { |
157 |
if (StringUtils.isBlank(name)) {
|
158 |
return false; |
159 |
} |
160 |
for (OrderedAttribute attribute : attributes) {
|
161 |
if (StringUtils.equalsIgnoreCase(name, attribute.getDescriptor().getName())) {
|
162 |
return true; |
163 |
} |
164 |
} |
165 |
return false; |
166 |
} |
167 |
|
168 |
@Override
|
169 |
public int getPriority(FeatureAttributeDescriptor attribute) { |
170 |
if( attribute==null || attribute.getTags()==null ) { |
171 |
return 0; |
172 |
} |
173 |
int p = attribute.getTags().getInt(DAL_SEARCH_ATTRIBUTE_PRIORITY, 0); |
174 |
return p;
|
175 |
} |
176 |
|
177 |
@Override
|
178 |
public List<FeatureAttributeDescriptor> getFavorites() { |
179 |
FeatureType featureType= this.getObject();
|
180 |
List<FeatureAttributeDescriptor> attrs = new ArrayList<>(); |
181 |
for (FeatureAttributeDescriptor attribute : featureType) {
|
182 |
if( this.getPriority(attribute)>0 ) { |
183 |
attrs.add(attribute); |
184 |
} |
185 |
} |
186 |
if( attrs.isEmpty() ) {
|
187 |
return Collections.EMPTY_LIST; |
188 |
} |
189 |
attrs.sort(new Comparator<FeatureAttributeDescriptor>() { |
190 |
@Override
|
191 |
public int compare(FeatureAttributeDescriptor o1, FeatureAttributeDescriptor o2) { |
192 |
return Integer.compare(getPriority(o1), getPriority(o2)); |
193 |
} |
194 |
}); |
195 |
return attrs;
|
196 |
} |
197 |
|
198 |
|
199 |
|
200 |
} |