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 / expressionevaluator / impl / function / dataaccess / ForeingValueFunction.java @ 44738
History | View | Annotate | Download (4.52 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.expressionevaluator.impl.function.dataaccess; |
25 |
|
26 |
import java.util.Arrays; |
27 |
import java.util.List; |
28 |
import org.apache.commons.lang3.Range; |
29 |
import org.apache.commons.lang3.StringUtils; |
30 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
31 |
import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
32 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
33 |
import org.gvsig.expressionevaluator.Interpreter; |
34 |
import org.gvsig.expressionevaluator.impl.DALFunctions; |
35 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
36 |
import org.gvsig.fmap.dal.DataManager; |
37 |
import org.gvsig.fmap.dal.feature.Feature; |
38 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
39 |
import org.gvsig.fmap.dal.feature.ForeingKey; |
40 |
import org.gvsig.tools.util.UnmodifiableBasicList; |
41 |
import org.gvsig.tools.util.UnmodifiableBasicListAdapter; |
42 |
|
43 |
public class ForeingValueFunction extends AbstractFunction { |
44 |
|
45 |
public ForeingValueFunction() {
|
46 |
super(
|
47 |
DALFunctions.GROUP_DATA_ACCESS, |
48 |
DALFunctions.FUNCTION_FOREING_VALUE, |
49 |
Range.is(1),
|
50 |
"Return the value of a field throw a relation from other tables.",
|
51 |
DALFunctions.FUNCTION_FOREING_VALUE + "({{field_name}})",
|
52 |
new String[]{ |
53 |
"fieldName - dot separated field path or list of strings with field names."
|
54 |
}, |
55 |
"OBJECT"
|
56 |
); |
57 |
} |
58 |
|
59 |
private Feature current_row(Interpreter interpreter) {
|
60 |
try {
|
61 |
Feature f = (Feature) interpreter.call(DALFunctions.FUNCTION_CURRENT_ROW); |
62 |
return f;
|
63 |
} catch (Exception ex) { |
64 |
return null; |
65 |
} |
66 |
} |
67 |
|
68 |
@Override
|
69 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
70 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
71 |
UnmodifiableBasicList<String> fieldNames;
|
72 |
Object fields_o = getObject(args, 0); |
73 |
if (fields_o instanceof List) { |
74 |
fieldNames = new UnmodifiableBasicListAdapter<>((List<String>) fields_o); |
75 |
} else if (fields_o instanceof String) { |
76 |
fieldNames = new UnmodifiableBasicListAdapter<>(Arrays.asList(((String) fields_o).split("[.]"))); |
77 |
} else if (fields_o instanceof UnmodifiableBasicList) { |
78 |
fieldNames = (UnmodifiableBasicList<String>) fields_o;
|
79 |
} else {
|
80 |
throw new ExpressionRuntimeException("Problems calling '" + DataManager.FUNCTION_FOREING_VALUE + "' function, type of parameter '" + fields_o.getClass().getSimpleName() + "' not supported, use string or list."); |
81 |
} |
82 |
Feature feature = this.current_row(interpreter);
|
83 |
Feature currentFeature = feature; |
84 |
try {
|
85 |
String fieldName;
|
86 |
Object value;
|
87 |
value = feature.getExtraValue(StringUtils.join(fieldNames, "."));
|
88 |
if (value != null) { |
89 |
return value;
|
90 |
} |
91 |
for (int i = 0; i < fieldNames.size() - 1; i++) { |
92 |
fieldName = fieldNames.get(i); |
93 |
value = currentFeature.get(fieldName); |
94 |
FeatureAttributeDescriptor attrdesc = currentFeature.getType().getAttributeDescriptor(fieldName); |
95 |
ForeingKey foreingKey = attrdesc.getForeingKey(); |
96 |
// ContextForeingKey context = foreingKey.createContext();
|
97 |
currentFeature = foreingKey.getFeature(null, value);
|
98 |
// DisposeUtils.disposeQuietly(context);
|
99 |
if (currentFeature == null) { |
100 |
return null; |
101 |
} |
102 |
} |
103 |
fieldName = fieldNames.get(fieldNames.size() - 1);
|
104 |
value = currentFeature.get(fieldName); |
105 |
return value;
|
106 |
} catch (ExpressionRuntimeException ex) {
|
107 |
throw ex;
|
108 |
} catch (Exception ex) { |
109 |
throw new ExpressionRuntimeException("Problems calling '" + DataManager.FUNCTION_FOREING_VALUE + "' function", ex); |
110 |
} |
111 |
} |
112 |
|
113 |
} |