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 @ 44853
History | View | Annotate | Download (5.02 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.ExpressionRuntimeException; |
31 |
import org.gvsig.expressionevaluator.Interpreter; |
32 |
import org.gvsig.expressionevaluator.impl.DALFunctions; |
33 |
import org.gvsig.fmap.dal.DataManager; |
34 |
import static org.gvsig.fmap.dal.DataManager.FUNCTION_FOREING_VALUE; |
35 |
import org.gvsig.fmap.dal.feature.Feature; |
36 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
37 |
import org.gvsig.fmap.dal.feature.ForeingKey; |
38 |
import org.gvsig.tools.util.UnmodifiableBasicList; |
39 |
import org.gvsig.tools.util.UnmodifiableBasicListAdapter; |
40 |
import org.gvsig.tools.util.UnmodifiableBasicListArrayAdapter; |
41 |
|
42 |
public class ForeingValueFunction extends AbstractFeatureFunction { |
43 |
|
44 |
public ForeingValueFunction() {
|
45 |
super(
|
46 |
DALFunctions.GROUP_DATA_ACCESS, |
47 |
FUNCTION_FOREING_VALUE, |
48 |
Range.is(1),
|
49 |
"Return the value of a field throw a relation from other tables.",
|
50 |
"FOREING VALUE FROM {{field1_name.field2_name}}",
|
51 |
new String[]{ |
52 |
"field_nameX - Name of fields that you want to access."
|
53 |
}, |
54 |
"OBJECT"
|
55 |
); |
56 |
} |
57 |
|
58 |
@Override
|
59 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
60 |
UnmodifiableBasicList<String> fieldNames;
|
61 |
switch( args.length) {
|
62 |
case 1: |
63 |
Object fields_o = getObject(args, 0); |
64 |
if( fields_o == null ) { |
65 |
throw new ExpressionRuntimeException("Problems calling '" + DataManager.FUNCTION_FOREING_VALUE + "' function, type of parameter 'null' not supported, use string or list."); |
66 |
} |
67 |
if (fields_o instanceof String[]) { |
68 |
fieldNames = new UnmodifiableBasicListArrayAdapter<>((String[]) fields_o); |
69 |
} else if (fields_o instanceof List) { |
70 |
fieldNames = new UnmodifiableBasicListAdapter<>((List<String>) fields_o); |
71 |
} else if (fields_o instanceof String) { |
72 |
fieldNames = new UnmodifiableBasicListAdapter<>(Arrays.asList(((String) fields_o).split("[.]"))); |
73 |
} else if (fields_o instanceof UnmodifiableBasicList) { |
74 |
fieldNames = (UnmodifiableBasicList<String>) fields_o;
|
75 |
} else {
|
76 |
throw new ExpressionRuntimeException("Problems calling '" + DataManager.FUNCTION_FOREING_VALUE + "' function, type of parameter '" + fields_o.getClass().getSimpleName() + "' not supported, use string or list."); |
77 |
} |
78 |
break;
|
79 |
case 2: |
80 |
fieldNames = new UnmodifiableBasicListArrayAdapter<>(
|
81 |
new String[] { |
82 |
getStr(args, 0),
|
83 |
getStr(args, 1)
|
84 |
} |
85 |
); |
86 |
break;
|
87 |
default:
|
88 |
throw new ExpressionRuntimeException("Problems calling '" + DataManager.FUNCTION_FOREING_VALUE + "' function, type of parameter not supported, use string or list."); |
89 |
} |
90 |
Feature feature = this.current_row(interpreter);
|
91 |
Feature currentFeature = feature; |
92 |
try {
|
93 |
String fieldName;
|
94 |
Object value;
|
95 |
value = feature.getExtraValue(StringUtils.join(fieldNames, "."));
|
96 |
if (value != null) { |
97 |
return value;
|
98 |
} |
99 |
for (int i = 0; i < fieldNames.size() - 1; i++) { |
100 |
fieldName = fieldNames.get(i); |
101 |
value = currentFeature.get(fieldName); |
102 |
FeatureAttributeDescriptor attrdesc = currentFeature.getType().getAttributeDescriptor(fieldName); |
103 |
ForeingKey foreingKey = attrdesc.getForeingKey(); |
104 |
// ContextForeingKey context = foreingKey.createContext();
|
105 |
currentFeature = foreingKey.getFeature(null, value);
|
106 |
// DisposeUtils.disposeQuietly(context);
|
107 |
if (currentFeature == null) { |
108 |
return null; |
109 |
} |
110 |
} |
111 |
fieldName = fieldNames.get(fieldNames.size() - 1);
|
112 |
value = currentFeature.get(fieldName); |
113 |
return value;
|
114 |
} catch (ExpressionRuntimeException ex) {
|
115 |
throw ex;
|
116 |
} catch (Exception ex) { |
117 |
throw new ExpressionRuntimeException("Problems calling '" + DataManager.FUNCTION_FOREING_VALUE + "' function", ex); |
118 |
} |
119 |
} |
120 |
|
121 |
|
122 |
|
123 |
} |