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

History | View | Annotate | Download (5.28 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
            true
57
    );
58
  }
59

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

    
126
  
127
}