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

History | View | Annotate | Download (4.61 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
    Object fields_o = getObject(args, 0);
73
    if (fields_o instanceof String[]) {
74
      fieldNames = new UnmodifiableBasicListArrayAdapter<>((String[]) fields_o);
75
    } else if (fields_o instanceof List) {
76
      fieldNames = new UnmodifiableBasicListAdapter<>((List<String>) fields_o);
77
    } else if (fields_o instanceof String) {
78
      fieldNames = new UnmodifiableBasicListAdapter<>(Arrays.asList(((String) fields_o).split("[.]")));
79
    } else if (fields_o instanceof UnmodifiableBasicList) {
80
      fieldNames = (UnmodifiableBasicList<String>) fields_o;
81
    } else {
82
      throw new ExpressionRuntimeException("Problems calling '" + DataManager.FUNCTION_FOREING_VALUE + "' function, type of parameter '" + fields_o.getClass().getSimpleName() + "' not supported, use string or list.");
83
    }
84
    Feature feature = this.current_row(interpreter);
85
    Feature currentFeature = feature;
86
    try {
87
      String fieldName;
88
      Object value;
89
      value = feature.getExtraValue(StringUtils.join(fieldNames, "."));
90
      if (value != null) {
91
        return value;
92
      }
93
      for (int i = 0; i < fieldNames.size() - 1; i++) {
94
        fieldName = fieldNames.get(i);
95
        value = currentFeature.get(fieldName);
96
        FeatureAttributeDescriptor attrdesc = currentFeature.getType().getAttributeDescriptor(fieldName);
97
        ForeingKey foreingKey = attrdesc.getForeingKey();
98
        // ContextForeingKey context = foreingKey.createContext();
99
        currentFeature = foreingKey.getFeature(null, value);
100
        // DisposeUtils.disposeQuietly(context);
101
        if (currentFeature == null) {
102
          return null;
103
        }
104
      }
105
      fieldName = fieldNames.get(fieldNames.size() - 1);
106
      value = currentFeature.get(fieldName);
107
      return value;
108
    } catch (ExpressionRuntimeException ex) {
109
      throw ex;
110
    } catch (Exception ex) {
111
      throw new ExpressionRuntimeException("Problems calling '" + DataManager.FUNCTION_FOREING_VALUE + "' function", ex);
112
    }
113
  }
114
  
115

    
116
  
117
}