Statistics
| Revision:

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 @ 44839

History | View | Annotate | Download (5.12 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.expressionevaluator.spi.AbstractFunction;
34
import org.gvsig.fmap.dal.DataManager;
35
import static org.gvsig.fmap.dal.DataManager.FUNCTION_CURRENT_ROW;
36
import static org.gvsig.fmap.dal.DataManager.FUNCTION_FOREING_VALUE;
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
import org.gvsig.tools.util.UnmodifiableBasicListArrayAdapter;
43

    
44
public class ForeingValueFunction extends AbstractFunction {
45

    
46
  public ForeingValueFunction() {
47
    super(
48
            DALFunctions.GROUP_DATA_ACCESS, 
49
            FUNCTION_FOREING_VALUE, 
50
            Range.is(1), 
51
            "Return the value of a field throw a relation from other tables.", 
52
            "FOREING VALUE FROM {{field1_name.field2_name}}", 
53
            new String[]{
54
              "field_nameX - Name of fields that you want to access."
55
            }, 
56
            "OBJECT"
57
    );
58
  }
59

    
60
  private Feature current_row(Interpreter interpreter) {
61
    try {
62
      Feature f = (Feature) interpreter.call(FUNCTION_CURRENT_ROW);
63
      return f;
64
    } catch (Exception ex) {
65
      return null;
66
    }
67
  }
68
  
69
  @Override
70
  public Object call(Interpreter interpreter, Object[] args) throws Exception {
71
    UnmodifiableBasicList<String> fieldNames;
72
    switch( args.length) {
73
      case 1:
74
        Object fields_o = getObject(args, 0);
75
        if (fields_o instanceof String[]) {
76
          fieldNames = new UnmodifiableBasicListArrayAdapter<>((String[]) fields_o);
77
        } else if (fields_o instanceof List) {
78
          fieldNames = new UnmodifiableBasicListAdapter<>((List<String>) fields_o);
79
        } else if (fields_o instanceof String) {
80
          fieldNames = new UnmodifiableBasicListAdapter<>(Arrays.asList(((String) fields_o).split("[.]")));
81
        } else if (fields_o instanceof UnmodifiableBasicList) {
82
          fieldNames = (UnmodifiableBasicList<String>) fields_o;
83
        } else {
84
          throw new ExpressionRuntimeException("Problems calling '" + DataManager.FUNCTION_FOREING_VALUE + "' function, type of parameter '" + fields_o.getClass().getSimpleName() + "' not supported, use string or list.");
85
        }
86
        break;
87
      case 2:
88
        fieldNames = new UnmodifiableBasicListArrayAdapter<>(
89
                new String[] {
90
                  getStr(args, 0),
91
                  getStr(args, 1)
92
                }
93
        );
94
        break;
95
      default:
96
          throw new ExpressionRuntimeException("Problems calling '" + DataManager.FUNCTION_FOREING_VALUE + "' function, type of parameter  not supported, use string or list.");
97
    }
98
    Feature feature = this.current_row(interpreter);
99
    Feature currentFeature = feature;
100
    try {
101
      String fieldName;
102
      Object value;
103
      value = feature.getExtraValue(StringUtils.join(fieldNames, "."));
104
      if (value != null) {
105
        return value;
106
      }
107
      for (int i = 0; i < fieldNames.size() - 1; i++) {
108
        fieldName = fieldNames.get(i);
109
        value = currentFeature.get(fieldName);
110
        FeatureAttributeDescriptor attrdesc = currentFeature.getType().getAttributeDescriptor(fieldName);
111
        ForeingKey foreingKey = attrdesc.getForeingKey();
112
        // ContextForeingKey context = foreingKey.createContext();
113
        currentFeature = foreingKey.getFeature(null, value);
114
        // DisposeUtils.disposeQuietly(context);
115
        if (currentFeature == null) {
116
          return null;
117
        }
118
      }
119
      fieldName = fieldNames.get(fieldNames.size() - 1);
120
      value = currentFeature.get(fieldName);
121
      return value;
122
    } catch (ExpressionRuntimeException ex) {
123
      throw ex;
124
    } catch (Exception ex) {
125
      throw new ExpressionRuntimeException("Problems calling '" + DataManager.FUNCTION_FOREING_VALUE + "' function", ex);
126
    }
127
  }
128
  
129

    
130
  
131
}