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

History | View | Annotate | Download (5.18 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 {{foreing_key_field.field_name}}", 
51
            new String[]{
52
              "foreing_key_field - Name of the field in the current table that contains the foreign key of the table that we want to access.",
53
              "field_name - Name of the field of the related table that we want to access."
54
            }, 
55
            "OBJECT"
56
    );
57
  }
58

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

    
123
  
124
}