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 @ 47777
History | View | Annotate | Download (5.39 KB)
1 | 44738 | jjdelcerro | /**
|
---|---|---|---|
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 | 46778 | jjdelcerro | import static org.gvsig.fmap.dal.DataManager.FUNCTION_FOREIGN_VALUE; |
35 | 44748 | jjdelcerro | import static org.gvsig.fmap.dal.DataManager.FUNCTION_FOREING_VALUE; |
36 | 44738 | jjdelcerro | import org.gvsig.fmap.dal.feature.Feature; |
37 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
38 | import org.gvsig.fmap.dal.feature.ForeingKey; |
||
39 | import org.gvsig.tools.util.UnmodifiableBasicList; |
||
40 | import org.gvsig.tools.util.UnmodifiableBasicListAdapter; |
||
41 | 44748 | jjdelcerro | import org.gvsig.tools.util.UnmodifiableBasicListArrayAdapter; |
42 | 44738 | jjdelcerro | |
43 | 44853 | jjdelcerro | public class ForeingValueFunction extends AbstractFeatureFunction { |
44 | 44738 | jjdelcerro | |
45 | public ForeingValueFunction() {
|
||
46 | super(
|
||
47 | DALFunctions.GROUP_DATA_ACCESS, |
||
48 | 46778 | jjdelcerro | FUNCTION_FOREIGN_VALUE, |
49 | 44738 | jjdelcerro | Range.is(1),
|
50 | "Return the value of a field throw a relation from other tables.",
|
||
51 | 46778 | jjdelcerro | "FOREIGN VALUE FROM {{foreign_key_field.field_name}}",
|
52 | 44738 | jjdelcerro | new String[]{ |
53 | 46778 | jjdelcerro | "foreign_key_field - Name of the field in the current table that contains the foreign key of the table that we want to access.",
|
54 | 44925 | jjdelcerro | "field_name - Name of the field of the related table that we want to access."
|
55 | 44738 | jjdelcerro | }, |
56 | 46517 | fdiaz | "OBJECT",
|
57 | true
|
||
58 | 44738 | jjdelcerro | ); |
59 | 46778 | jjdelcerro | this.addAlias(FUNCTION_FOREING_VALUE);
|
60 | 44738 | jjdelcerro | } |
61 | |||
62 | @Override
|
||
63 | public Object call(Interpreter interpreter, Object[] args) throws Exception { |
||
64 | UnmodifiableBasicList<String> fieldNames;
|
||
65 | 44839 | jjdelcerro | switch( args.length) {
|
66 | case 1: |
||
67 | Object fields_o = getObject(args, 0); |
||
68 | 44853 | jjdelcerro | if( fields_o == null ) { |
69 | 47777 | fdiaz | throw new ExpressionRuntimeException("Problems calling '" + DataManager.FUNCTION_FOREIGN_VALUE + "' function, type of parameter 'null' not supported, use string or list."); |
70 | 44853 | jjdelcerro | } |
71 | 44839 | jjdelcerro | if (fields_o instanceof String[]) { |
72 | fieldNames = new UnmodifiableBasicListArrayAdapter<>((String[]) fields_o); |
||
73 | } else 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 | 47777 | fdiaz | throw new ExpressionRuntimeException("Problems calling '" + DataManager.FUNCTION_FOREIGN_VALUE + "' function, type of parameter '" + fields_o.getClass().getSimpleName() + "' not supported, use string or list."); |
81 | 44839 | jjdelcerro | } |
82 | break;
|
||
83 | case 2: |
||
84 | fieldNames = new UnmodifiableBasicListArrayAdapter<>(
|
||
85 | new String[] { |
||
86 | getStr(args, 0),
|
||
87 | getStr(args, 1)
|
||
88 | } |
||
89 | ); |
||
90 | break;
|
||
91 | default:
|
||
92 | 47777 | fdiaz | throw new ExpressionRuntimeException("Problems calling '" + DataManager.FUNCTION_FOREIGN_VALUE + "' function, type of parameter not supported, use string or list."); |
93 | 44738 | jjdelcerro | } |
94 | Feature feature = this.current_row(interpreter);
|
||
95 | Feature currentFeature = feature; |
||
96 | try {
|
||
97 | String fieldName;
|
||
98 | Object value;
|
||
99 | value = feature.getExtraValue(StringUtils.join(fieldNames, "."));
|
||
100 | if (value != null) { |
||
101 | return value;
|
||
102 | } |
||
103 | for (int i = 0; i < fieldNames.size() - 1; i++) { |
||
104 | fieldName = fieldNames.get(i); |
||
105 | value = currentFeature.get(fieldName); |
||
106 | FeatureAttributeDescriptor attrdesc = currentFeature.getType().getAttributeDescriptor(fieldName); |
||
107 | 45294 | omartinez | if (attrdesc.isForeingKey()) {
|
108 | ForeingKey foreingKey = attrdesc.getForeingKey(); |
||
109 | // ContextForeingKey context = foreingKey.createContext();
|
||
110 | currentFeature = foreingKey.getFeature(null, value);
|
||
111 | // DisposeUtils.disposeQuietly(context);
|
||
112 | if (currentFeature == null) { |
||
113 | return null; |
||
114 | } |
||
115 | 44738 | jjdelcerro | } |
116 | } |
||
117 | fieldName = fieldNames.get(fieldNames.size() - 1);
|
||
118 | value = currentFeature.get(fieldName); |
||
119 | return value;
|
||
120 | } catch (ExpressionRuntimeException ex) {
|
||
121 | throw ex;
|
||
122 | } catch (Exception ex) { |
||
123 | 47777 | fdiaz | throw new ExpressionRuntimeException("Problems calling '" + DataManager.FUNCTION_FOREIGN_VALUE + "' function", ex); |
124 | 44738 | jjdelcerro | } |
125 | } |
||
126 | |||
127 | 44748 | jjdelcerro | |
128 | |||
129 | 44738 | jjdelcerro | } |