Revision 45647

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/test/java/org/gvsig/fmap/dal/store/jdbc2/SQLBuilderTest.java
19 19
import org.gvsig.fmap.dal.feature.EditableFeatureType;
20 20
import org.gvsig.fmap.dal.feature.EditableForeingKey;
21 21
import org.gvsig.fmap.dal.feature.spi.SQLBuilderBase;
22
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
22 23
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCHelperBase;
23 24
import org.gvsig.fmap.geom.DataTypes;
24 25
import org.gvsig.fmap.geom.Geometry;
......
523 524

  
524 525
    public void testForeingValue() throws Exception {
525 526
        DataManager dataManager = DALLocator.getDataManager();
526
        JDBCHelperBase helper = new JDBCHelperBase(null);
527
        JDBCHelper helper = createJDBCHelper();
527 528
        
528 529
        SQLBuilder sqlbuilder = createSQLBuilder();
529 530
        ExpressionBuilder expbuilder = sqlbuilder.expression();
......
606 607
        );
607 608
    }
608 609
    
610
    private JDBCHelper createJDBCHelper(){
611
        
612
        JDBCStoreParameters params = new JDBCStoreParameters();
613
        JDBCHelperBase helper = new JDBCHelperBase(params);
614
        return helper;
615
        
616
    }
609 617

  
610 618
}
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/java/org/gvsig/fmap/dal/store/jdbc2/spi/JDBCHelperBase.java
117 117
  protected FeatureType providerFeatureType = null;
118 118

  
119 119
  public JDBCHelperBase(JDBCConnectionParameters connectionParameters) {
120
    this.connectionParameters = connectionParameters;
120
        if (connectionParameters == null) {
121
            throw new IllegalArgumentException("Connection parameters can't be null.");
122
        }
123
        this.connectionParameters = connectionParameters;
121 124

  
122
    // If a particular treatment is required to convert SRS to the 
123
    // BBDD format, overwrite JDBCSRSsBase and use it instead of JDBCSRSsDumb.
124
    this.srssolver = new SRSSolverDumb(this);
125
  }
125
        // If a particular treatment is required to convert SRS to the 
126
        // BBDD format, overwrite JDBCSRSsBase and use it instead of JDBCSRSsDumb.
127
        this.srssolver = new SRSSolverDumb(this);
128
    }
126 129

  
127 130
  protected void initialize(
128 131
          ResourceConsumer helperClient,
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.mdb/src/test/java/org/gvsig/fmap/dal/store/mdb/TestUtils.java
17 17
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
18 18
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
19 19
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
20
import org.gvsig.fmap.dal.store.jdbc2.spi.FakeConnectionProvider;
20 21
import org.slf4j.Logger;
21 22
import org.slf4j.LoggerFactory;
22 23

  
......
120 121
      return ((DefaultFeature)feature).getData();
121 122
    }
122 123

  
123
    public static JDBCHelper getJDBCHelper() {
124
      MDBHelper helper = new MDBHelper();
125
      return helper;
124
    public static JDBCHelper getJDBCHelper() throws Exception {
125
        MDBConnectionParameters params = buildDBConnection("fake");
126
        MDBHelper helper = new MDBHelper(params, new FakeConnectionProvider());
127
        return helper;
126 128
    }
127
    
128 129

  
130

  
129 131
}
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.mdb/src/test/java/org/gvsig/fmap/dal/store/mdb/operations/sql/TestFetchFeatureProviderByReference.java
7 7
import org.gvsig.fmap.dal.feature.FeatureStore;
8 8
import org.gvsig.fmap.dal.feature.FeatureType;
9 9
import org.gvsig.fmap.dal.feature.impl.DefaultFeature;
10
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureReference;
10
import org.gvsig.fmap.dal.feature.impl.featurereference.DefaultFeatureReference;
11 11
import org.gvsig.fmap.dal.store.mdb.TestUtils;
12 12
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
13 13
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory;
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.mdb/src/main/java/org/gvsig/fmap/dal/store/mdb/MDBHelper.java
43 43
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCCantFetchValueException;
44 44
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCDriverClassNotFoundException;
45 45
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
46
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
46 47
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory;
48
import org.gvsig.fmap.dal.store.jdbc2.spi.ConnectionProvider;
47 49
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCHelperBase;
48 50
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
49 51
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCServerExplorerBase;
......
106 108
        return connectionURL;
107 109
    }
108 110

  
109
    public static class ConnectionProvider {
111
    public static class ConnectionProviderImpl implements ConnectionProvider {
110 112

  
111 113
        private static boolean needRegisterDriver = true;
112 114

  
......
118 120
        private static String server = null;
119 121
        private static boolean startServer = true;
120 122

  
121
        public ConnectionProvider(MDBConnectionParameters connectionParameters) {
123
        public ConnectionProviderImpl(MDBConnectionParameters connectionParameters) {
122 124
            this.connectionParameters = connectionParameters;
123 125
        }
124 126
        
127
        @Override
128
        public String getStatus() {
129
            StringBuilder builder = new StringBuilder();
130
            builder.append("Pool: ");
131
            builder.append(JDBCUtils.getHexId(dataSource));
132
            builder.append(" Actives: ");
133
            builder.append(dataSource.getNumActive());
134
            builder.append("/");
135
            builder.append(dataSource.getMaxActive());
136
            builder.append(" idle: ");
137
            builder.append(dataSource.getNumIdle());
138
            builder.append("/");
139
            builder.append(dataSource.getMinIdle());
140
            builder.append(":");
141
            builder.append(dataSource.getMaxIdle());
142
            return builder.toString();
143
        }
144

  
145
        
125 146
        public static void stopServer() {
126 147
          try {
127 148
//            server.shutdown();
......
247 268
    }
248 269

  
249 270
    private ConnectionProvider connectionProvider = null;
250
 
251
    /**
271
    
272
        /**
252 273
     * Constructor for use only for testing purposes.
274
     * 
275
     * @param connectionParameters
276
     * @param connectionProvider
253 277
     */
254
    public MDBHelper() { 
255
        super(null);
278
    public MDBHelper(JDBCConnectionParameters connectionParameters, ConnectionProvider connectionProvider) { 
279
        super(connectionParameters);
280
        this.srssolver = new SRSSolverDumb(this);
281
        this.connectionProvider = connectionProvider;
256 282
    }
257
  
283
 
258 284
    public MDBHelper(JDBCConnectionParameters connectionParameters) {
259 285
        super(connectionParameters);
260 286
        this.srssolver = new SRSSolverDumb(this);
......
268 294
              if( connectionParameters==null ) {
269 295
                return null; // Testing mode?
270 296
              }
271
              this.connectionProvider = new ConnectionProvider(connectionParameters);
297
              this.connectionProvider = new ConnectionProviderImpl(connectionParameters);
272 298
            }
273 299
            Connection connection = this.connectionProvider.getConnection();
274 300
            if( LOGGER.isDebugEnabled() ) {
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.mdb/src/main/java/org/gvsig/fmap/dal/store/mdb/MDBExplorer.java
43 43
    @Override
44 44
    public Object execute(String sql) {
45 45
      if( StringUtils.equalsIgnoreCase(sql, "!SHUTDOWN") ) {
46
        MDBHelper.ConnectionProvider.stopServer();
46
        MDBHelper.ConnectionProviderImpl.stopServer();
47 47
        return null;
48 48
      }
49 49
      return super.execute(sql);
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/impl/DALDefaultImplLibrary.java
29 29
import java.util.Map;
30 30
import org.gvsig.expressionevaluator.ExpressionEvaluatorLibrary;
31 31
import org.gvsig.expressionevaluator.impl.DALFunctions;
32

  
33 32
import org.gvsig.fmap.dal.DALLibrary;
34 33
import org.gvsig.fmap.dal.DALLocator;
35 34
import org.gvsig.fmap.dal.DataManager;
......
41 40
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureExtraColumns;
42 41
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureQuery;
43 42
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureQueryOrder;
44
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureReference;
45 43
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureReferenceSelection;
46 44
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureSelection;
47 45
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore;
......
50 48
import org.gvsig.fmap.dal.feature.impl.DefaultForeingKey;
51 49
import org.gvsig.fmap.dal.feature.impl.LargeFeatureReferenceSelection;
52 50
import org.gvsig.fmap.dal.feature.impl.LargeFeatureSelection;
53
import org.gvsig.fmap.dal.feature.impl.indexes.jsir.JSIRSpatialIndexProviderFactory;
54
import org.gvsig.fmap.dal.feature.impl.indexes.memoryspatial.MemorySpatialIndexProviderFactory;
55 51
import org.gvsig.fmap.dal.feature.impl.dynobjectutils.ComputeAvailableValuesFromTable;
56 52
import org.gvsig.fmap.dal.feature.impl.dynobjectutils.DumbFeatureTypeDefinitionsManager;
57 53
import org.gvsig.fmap.dal.feature.impl.dynobjectutils.relations.DALLink_getRelatedFeature;
58 54
import org.gvsig.fmap.dal.feature.impl.dynobjectutils.relations.DALLink_getRelatedFeatures;
55
import org.gvsig.fmap.dal.feature.impl.featurereference.FeatureReferenceFactory;
56
import org.gvsig.fmap.dal.feature.impl.indexes.jsir.JSIRSpatialIndexProviderFactory;
59 57
import org.gvsig.fmap.dal.feature.impl.indexes.memorybasictypes.MemoryBigDecimalIndexProviderFactory;
60 58
import org.gvsig.fmap.dal.feature.impl.indexes.memorybasictypes.MemoryDoubleIndexProviderFactory;
61 59
import org.gvsig.fmap.dal.feature.impl.indexes.memorybasictypes.MemoryFloatIndexProviderFactory;
62 60
import org.gvsig.fmap.dal.feature.impl.indexes.memorybasictypes.MemoryIntIndexProviderFactory;
63 61
import org.gvsig.fmap.dal.feature.impl.indexes.memorybasictypes.MemoryLongIndexProviderFactory;
64 62
import org.gvsig.fmap.dal.feature.impl.indexes.memorybasictypes.MemoryStringIndexProviderFactory;
63
import org.gvsig.fmap.dal.feature.impl.indexes.memoryspatial.MemorySpatialIndexProviderFactory;
65 64
import org.gvsig.fmap.dal.feature.spi.memory.MemoryResource;
66 65
import org.gvsig.fmap.dal.feature.spi.memory.MemoryResourceParameters;
67 66
import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultFeatureAttributeEmulatorExpression;
......
74 73
import org.gvsig.fmap.dal.spi.DataServerExplorerPoolImpl;
75 74
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
76 75
import org.gvsig.i18n.Messages;
77
import org.gvsig.json.Json;
78 76
import org.gvsig.metadata.exceptions.MetadataException;
79 77
import org.gvsig.srs.SRSJsonSerializer;
80 78
import org.gvsig.tools.ToolsLocator;
......
133 131
        
134 132
        DefaultFeatureQuery.registerPersistent();
135 133
        DefaultFeatureQueryOrder.registerPersistent();
136
        DefaultFeatureReference.registerPersistent();
134
        FeatureReferenceFactory.registerPersistent();
137 135
        DefaultFeatureStoreTransforms.registerPersistent();
138 136
        AbstractFeatureStoreTransform.registerPersistent();
139 137
        DefaultFeatureReferenceSelection.registerPersistent();
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/DefaultFeatureReference.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 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.fmap.dal.feature.impl;
25

  
26
import java.lang.ref.WeakReference;
27
import java.util.Arrays;
28
import java.util.Base64;
29
import java.util.List;
30
import java.util.Objects;
31
import org.apache.commons.lang3.ArrayUtils;
32

  
33
import org.gvsig.fmap.dal.exception.DataException;
34
import org.gvsig.fmap.dal.feature.Feature;
35
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
36
import org.gvsig.fmap.dal.feature.FeatureStore;
37
import org.gvsig.fmap.dal.feature.FeatureType;
38
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
39
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
40
import org.gvsig.tools.ToolsLocator;
41
import org.gvsig.tools.dynobject.DynStruct;
42
import org.gvsig.tools.persistence.Persistent;
43
import org.gvsig.tools.persistence.PersistentState;
44
import org.gvsig.tools.persistence.exception.PersistenceException;
45
import org.json.JSONArray;
46
import org.json.JSONObject;
47

  
48
@SuppressWarnings("UseSpecificCatch")
49
public class DefaultFeatureReference implements
50
		FeatureReferenceProviderServices, Persistent {
51

  
52
	private Object oid;
53
	private Integer myHashCode = null;
54
	private Object[] pk;
55
	private String[] pkNames;
56
	private WeakReference storeRef;
57
	private boolean isNewFeature;
58
	private String featureTypeId;
59
	 
60
        
61
        private static final int OID_INDEX = 0;
62
        private static final int MYHASHCODE_INDEX = 1;
63
        private static final int ISNEWFEATURE_INDEX = 2;
64
        private static final int FEATURETYPEID_INDEX = 3;
65
        private static final int PKNAMES_SIZE_INDEX = 4;
66
        private static final int PKNAMES_INDEX = 5;
67
        private static final int PK_INDEX = 6;
68
        
69
	/**
70
     * Constructor used by the persistence manager. Don't use directly.
71
     * After to invoke this method, the persistence manager calls 
72
     * the the method {@link #loadFromState(PersistentState)} to set 
73
     * the values of the internal attributes that this class needs to work.
74
     */
75
	public DefaultFeatureReference() {
76
		super();		
77
	}
78
	
79
	public DefaultFeatureReference(DefaultFeature feature) {
80
		this(feature.getStore(), feature.getData());
81
	}
82

  
83
	public DefaultFeatureReference(FeatureStore store,
84
			FeatureProvider fdata) {
85
		this.isNewFeature = fdata.isNew();
86
		this.oid = null;
87
		this.pk = null;
88
		this.storeRef = new WeakReference(store);
89
		this.featureTypeId = fdata.getType().getId();
90

  
91
		if (fdata.getType().hasOID() || isNewFeature) {
92
			this.oid = fdata.getOID();
93
			if (this.oid == null) {
94
				// FIXME Exception
95
				throw new RuntimeException("Missing OID");
96
			}
97
		} else {
98
			this.calculatePK(fdata);
99
			if (this.pk == null) {
100
				// FIXME Exception
101
				throw new RuntimeException("Missing pk attributes");
102
			}
103
		}
104

  
105
	}
106

  
107
	/*
108
	 * Use only for Persistent.setState
109
	 */
110
	public DefaultFeatureReference(FeatureStore store) {
111
		this.isNewFeature = false;
112
		this.oid = null;
113
		this.pk = null;
114
		this.storeRef = new WeakReference(store);
115
	}
116

  
117
	public DefaultFeatureReference(FeatureStore store, Object oid) {
118
		// FIXME featureTypeId is needed !!!
119
		this.isNewFeature = false;
120
		this.oid = oid;
121
		this.pk = null;
122
		this.storeRef = new WeakReference(store);
123
	}
124

  
125
	public DefaultFeatureReference(FeatureStore store, String code) {
126
            this.storeRef = new WeakReference(store);
127
            
128
            String json = new String(Base64.getDecoder().decode(code.getBytes()));
129
            
130
            JSONArray x = new JSONArray(json);
131
            this.oid = x.get(OID_INDEX);
132
            if( x.get(MYHASHCODE_INDEX)==JSONObject.NULL ) {
133
                this.myHashCode = null;
134
            } else {
135
                this.myHashCode = x.getInt(MYHASHCODE_INDEX);
136
            }
137
            this.isNewFeature = x.getBoolean(ISNEWFEATURE_INDEX);
138
            this.featureTypeId = x.getString(FEATURETYPEID_INDEX);
139
            int pkNames_size = x.getInt(PKNAMES_SIZE_INDEX);
140
            if( pkNames_size<0 ) {
141
                this.pk = null;
142
                this.pkNames = null;
143
            } else {
144
                this.pk = new Object[pkNames_size];
145
                this.pkNames = new String[pkNames_size];
146
                JSONArray xx = x.getJSONArray(PKNAMES_INDEX);
147
                for( int i=0; i<xx.length(); i++ ) {
148
                    this.pkNames[i] = xx.getString(i);
149
                }
150
                xx = x.getJSONArray(PKNAMES_INDEX);
151
                for( int i=0; i<xx.length(); i++ ) {
152
                    this.pk[i] = xx.get(i);
153
                }
154
            }
155
            
156
//            JSONObject x = new JSONObject(json);
157
//            this.oid = x.get("oid");
158
//            this.myHashCode = x.getInt("myHashCode");
159
//            this.isNewFeature = x.getBoolean("isNewFeature");
160
//            this.featureTypeId = x.getString("featureTypeId");
161
//            int pkNames_size = x.getInt("pkNames_size");
162
//            if( pkNames_size<0 ) {
163
//                this.pk = null;
164
//                this.pkNames = null;
165
//            } else {
166
//                this.pk = new Object[pkNames_size];
167
//                this.pkNames = new String[pkNames_size];
168
//                JSONArray xx = x.getJSONArray("pkNames");
169
//                for( int i=0; i<xx.length(); i++ ) {
170
//                    this.pkNames[i] = xx.getString(i);
171
//                }
172
//                xx = x.getJSONArray("pk");
173
//                for( int i=0; i<xx.length(); i++ ) {
174
//                    this.pk[i] = xx.get(i);
175
//                }
176
//            }
177
        }
178
        
179
        private DefaultFeatureStore getStore() {
180
		return (DefaultFeatureStore) this.storeRef.get();
181
	}
182

  
183
	public void calculatePK(FeatureProvider fdata) { // Usado en los test de BBDD
184
            FeatureAttributeDescriptor[] pkattrs = fdata.getType().getPrimaryKey();
185
            if( ArrayUtils.isEmpty(pkattrs) ) {
186
                this.pk = null;
187
                this.pkNames = null;
188
                return ;
189
            }
190
            this.pk = new Object[pkattrs.length];
191
            this.pkNames = new String[pkattrs.length];
192
            int n = 0;
193
            for (FeatureAttributeDescriptor pkattr : pkattrs) {
194
                if( pkattr.isAutomatic() ) {
195
                    this.pk = null;
196
                    this.pkNames = null;
197
                    return ;
198
                }
199
                this.pk[n] = fdata.get(pkattr.getIndex());
200
                this.pkNames[n] = pkattr.getName();
201
                n++;
202
            }
203
    }
204

  
205
    @Override
206
    public Feature getFeature() throws DataException {
207
	return this.getStore().getFeatureByReference(this);
208
    }
209

  
210
    @Override
211
    public Feature getFeatureQuietly() {
212
        try {
213
            return this.getFeature();
214
        } catch(Exception ex) {
215
            return null;
216
        }
217
    }
218

  
219
    @Override
220
	public Feature getFeature(FeatureType featureType) throws DataException {
221
		return this.getStore().getFeatureByReference(this, featureType);
222
	}
223

  
224
    @Override
225
	public Object getOID() {
226
		return this.oid;
227
	}
228

  
229
    @Override
230
	public boolean isNewFeature() {
231
		return this.isNewFeature;
232
	}
233

  
234

  
235
    @Override
236
	public boolean equals(Object obj) {
237
		if (!(obj instanceof DefaultFeatureReference)) {
238
			return false;
239
		}
240
		DefaultFeatureReference other = (DefaultFeatureReference) obj;
241

  
242
		FeatureStore otherStore = (FeatureStore) other.storeRef.get();
243
		FeatureStore myrStore = (FeatureStore) this.storeRef.get();
244
		if (otherStore == null || myrStore == null) {
245
			return false;
246
		}
247
		if (!myrStore.equals(otherStore)) {
248
			return false;
249
		}
250
		if (myHashCode != null && other.myHashCode != null) {
251
			return myHashCode.equals(other.myHashCode);
252
		}
253
		if (this.oid != null) {
254
			return this.oid.equals(other.oid);
255
		}
256
		if(pk != null) {
257
			if(other.pk == null) {
258
				return false;
259
			}
260
    		for (int i = 0; i < this.pk.length; i++) {
261
    			if (!this.pk[i].equals(other.pk[i])) {
262
    				return false;
263
    			}
264
    		}
265
		}
266
		return true;
267
	}
268

  
269
    @Override
270
	public int hashCode() {
271
		if (this.oid != null) {
272
			return this.oid.hashCode();
273
		}
274
		if (myHashCode == null) {
275
			StringBuilder buff = new StringBuilder();
276

  
277
			for (int i = 0; i < this.pk.length; i++) {
278
				buff.append(Objects.hashCode(this.pk[i]));
279
				buff.append("##");
280
			}
281
			myHashCode = buff.toString().hashCode();
282
		}
283
		return myHashCode;
284
	}
285

  
286
    @Override
287
	public String[] getKeyNames() {
288
		return pkNames;
289
	}
290

  
291
    @Override
292
	public Object getKeyValue(String name) {
293
		for (int i = 0; i < pkNames.length; i++) {
294
			if (pkNames[i].equalsIgnoreCase(name)) {
295
				return pk[i];
296
			}
297
		}
298
		// FIXME exception????
299
		return null;
300
	}
301

  
302
    @Override
303
	public String getFeatureTypeId() {
304
		return featureTypeId;
305
	}
306

  
307
	// *** Persistence ***
308

  
309
    @Override
310
	public void loadFromState(PersistentState state)
311
			throws PersistenceException {
312
		this.oid = state.get("oid");
313
		this.myHashCode = (Integer) state.get("myHashCode");
314
		this.storeRef = new WeakReference(state.get("store"));
315
		this.isNewFeature = state.getBoolean("isNewFeature");
316
		this.featureTypeId = state.getString("featureTypeId");
317
		List pkList = (List) state.get("pk");
318
		if (pkList != null) {
319
			List pkNamesList = (List) state.get("pkNames");
320
			if (pkNamesList == null || pkList.size() != pkNamesList.size()) {
321
				throw new PersistenceException("bad pkNames value");
322
			}
323
			this.pk = pkList.toArray();
324
            this.pkNames =
325
                (String[]) pkNamesList.toArray(new String[pkList.size()]);
326
		} else {
327
			this.pk = null;
328
			this.pkNames = null;
329
		}
330
	}
331

  
332
    @Override
333
	public void saveToState(PersistentState state) throws PersistenceException {
334
		state.set("oid", oid);
335
		state.set("myHashCode", myHashCode);
336
		state.set("isNewFeature", isNewFeature);
337
		state.set("store", (Persistent) storeRef.get());
338
		state.set("featureTypeId", featureTypeId);
339
		if (pk == null) {
340
			state.setNull("pk");
341
			state.setNull("pkNames");
342
		} else {
343
			state.set("pk", pk);
344
			state.set("pkNames", pkNames);
345
		}
346

  
347
	}
348
        
349
        @Override
350
        public String getCode() {
351
//            JSONObject x = new JSONObject();
352
////            x.put("store", (Persistent) storeRef.get());
353
//            x.put("oid", oid);
354
//            x.put("myHashCode", myHashCode);
355
//            x.put("isNewFeature", isNewFeature);
356
//            x.put("featureTypeId", featureTypeId);
357
//            if( this.pk == null ) {
358
//                x.put("pkNames_size", -1);
359
//                x.put("pkNames", JSONObject.NULL);
360
//                x.put("pk", JSONObject.NULL);
361
//            } else {
362
//                x.put("pkNames_size", pkNames.length);
363
//                x.put("pkNames", new JSONArray(this.pkNames));
364
//                x.put("pk", new JSONArray(this.pk));
365
//            }
366
//
367
            Object[] data = new Object[7];
368
            data[OID_INDEX] = this.oid;
369
            data[MYHASHCODE_INDEX] = this.myHashCode;
370
            data[ISNEWFEATURE_INDEX] = this.isNewFeature;
371
            data[FEATURETYPEID_INDEX] = this.featureTypeId;
372
            if( this.pk == null ) {
373
                data[PKNAMES_SIZE_INDEX] = -1;
374
                data[PKNAMES_INDEX] = JSONObject.NULL;
375
                data[PK_INDEX] = JSONObject.NULL;
376
            } else {
377
                data[PKNAMES_SIZE_INDEX] = pkNames.length;
378
                data[PKNAMES_INDEX] = new JSONArray(this.pkNames);
379
                data[PK_INDEX] = new JSONArray(this.pk);            
380
            }            
381
            String s = new JSONArray(data).toString();
382
            String r = Base64.getEncoder().encodeToString(s.getBytes());
383
            
384
            return r;
385
        }
386

  
387
	public static void registerPersistent() {
388
		DynStruct definition = ToolsLocator.getPersistenceManager().addDefinition(
389
				DefaultFeatureReference.class, 
390
				"Reference", 
391
				"DefaultFeatureReference Persistent definition", 
392
				null, 
393
				null
394
			);
395

  
396
		definition.addDynFieldObject("oid")
397
			.setClassOfValue(Object.class)
398
			.setMandatory(false)
399
			.setPersistent(true);
400
			
401
		definition.addDynFieldBoolean("isNewFeature")
402
			.setMandatory(true)
403
			.setPersistent(true);
404
	
405
		definition.addDynFieldObject("store")
406
			.setClassOfValue(FeatureStore.class)
407
			.setMandatory(true)
408
			.setPersistent(true);
409

  
410
		definition.addDynFieldInt("myHashCode")
411
			.setMandatory(false)
412
			.setPersistent(true);
413
	
414
		definition.addDynFieldString("featureTypeId")
415
			.setMandatory(true)
416
			.setPersistent(true);
417

  
418
		definition.addDynFieldArray("pk")
419
			.setClassOfItems(Object.class)
420
			.setMandatory(false)
421
			.setPersistent(true);
422

  
423
        definition.addDynFieldArray("pkNames")
424
			.setClassOfItems(String.class)
425
			.setMandatory(false)
426
			.setPersistent(true);
427

  
428
	}
429

  
430
    @Override
431
	public String toString() {
432
        StringBuilder builder = new StringBuilder();
433
        builder.append("FeatureReference: oid = ");
434
        builder.append(oid);
435
        if( myHashCode!=null ) {
436
            builder.append(", myHashCode = ");
437
            builder.append(myHashCode);
438
        } 
439
        if( pk!=null ) {
440
            builder.append(", pks = ");
441
            builder.append(Arrays.asList(pk));
442
        }
443
        if( pkNames!=null ) {
444
            builder.append(", pkNames = ");
445
            builder.append(Arrays.asList(pkNames));
446
        }
447
        if( isNewFeature ) {
448
            builder.append(", isNew = ");
449
            builder.append(isNewFeature);
450
        }
451
        if( featureTypeId!=null ) {
452
            builder.append(", featureTypeId = ");
453
            builder.append(featureTypeId);
454
        }
455
        return builder.toString();
456
	}
457

  
458
}
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/DefaultFeatureReferenceSelection.java
27 27
import java.util.HashSet;
28 28
import java.util.Iterator;
29 29
import java.util.Set;
30

  
31 30
import org.gvsig.fmap.dal.DataStore;
32 31
import org.gvsig.fmap.dal.DataStoreNotification;
33 32
import org.gvsig.fmap.dal.exception.DataException;
34
import org.gvsig.fmap.dal.feature.Feature;
35 33
import org.gvsig.fmap.dal.feature.FeatureReference;
36 34
import org.gvsig.fmap.dal.feature.FeatureReferenceSelection;
37 35
import org.gvsig.fmap.dal.feature.FeatureStore;
......
493 491
        data.setTotalSize(state.getLong("totalSize"));
494 492
        Iterator it = state.getIterator("selected");
495 493
        while (it.hasNext()) {
496
            DefaultFeatureReference ref = (DefaultFeatureReference) it.next();
494
            FeatureReference ref = (FeatureReference) it.next();
497 495
            data.add(ref);
498 496
        }
499 497

  
......
522 520
        definition.addDynFieldObject("store").setClassOfValue(FeatureStore.class).setMandatory(true);
523 521
        definition.addDynFieldBoolean("reversed").setMandatory(true);
524 522
        definition.addDynFieldLong("totalSize").setMandatory(true);
525
        definition.addDynFieldList("selected").setClassOfItems(DefaultFeatureReference.class).setMandatory(true);
523
        definition.addDynFieldList("selected").setClassOfItems(FeatureReference.class).setMandatory(true);
526 524

  
527 525
    }
528 526

  
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/featureset/FastEditedIterator.java
25 25

  
26 26
import org.gvsig.fmap.dal.exception.DataException;
27 27
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
28
import org.gvsig.fmap.dal.feature.FeatureReference;
29 28
import org.gvsig.fmap.dal.feature.FeatureType;
30 29
import org.gvsig.fmap.dal.feature.impl.DefaultEditableFeature;
31 30
import org.gvsig.fmap.dal.feature.impl.DefaultFeature;
32
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureReference;
33 31
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
34 32
import org.gvsig.tools.exception.BaseException;
35 33

  
......
52 50
    @Override
53 51
    protected DefaultFeature createFeature(FeatureProvider data) throws DataException {
54 52

  
55
        DefaultFeature f = null;
56
        try {
57
            FeatureReference ref = new DefaultFeatureReference(fset.store, data);
58
            f = (DefaultFeature) featureManager.get(ref, fset.store); 
59
        } catch (DataException e) {
60
            RuntimeException ex = new RuntimeException();
61
            e.initCause(e);
62
            throw ex;
63
        }
53
        DefaultFeature f = this.featureManager.get(data);
54
//        DefaultFeature f = null;
55
//        try {
56
//            FeatureReference ref = new DefaultFeatureReference(fset.store, data);
57
//            f = (DefaultFeature) featureManager.get(ref, fset.store); 
58
//        } catch (DataException e) {
59
//            RuntimeException ex = new RuntimeException();
60
//            e.initCause(e);
61
//            throw ex;
62
//        }
64 63
        if (f == null) {
65 64
            this.myFeature.setData(data);
66 65
        } else {
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/featureset/EditedIterator.java
25 25

  
26 26
import java.util.Iterator;
27 27
import org.gvsig.fmap.dal.exception.DataException;
28
import org.gvsig.fmap.dal.feature.FeatureReference;
29 28
import org.gvsig.fmap.dal.feature.exception.ConcurrentDataModificationException;
30 29
import org.gvsig.fmap.dal.feature.impl.DefaultFeature;
31
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureReference;
32 30
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore;
33 31
import org.gvsig.fmap.dal.feature.impl.editing.memory.FeatureManager;
34 32
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
......
113 111
    @Override
114 112
    protected DefaultFeature createFeature(FeatureProvider data) throws DataException {
115 113

  
116
        DefaultFeature f = null;
117
        try {
118
            FeatureReference ref = new DefaultFeatureReference(store, data);
119
            f= (DefaultFeature) this.featureManager.get(ref, store, data.getType());
120
        } catch (DataException e) {
121
            RuntimeException ex = new RuntimeException();
122
            e.initCause(e);
123
            throw ex;
124
        }
114
        DefaultFeature f = this.featureManager.get(data);
115
//        DefaultFeature f = null;
116
//        try {
117
//            FeatureReference ref = new DefaultFeatureReference(store, data);
118
//            f= (DefaultFeature) this.featureManager.get(ref, store, data.getType());
119
//        } catch (DataException e) {
120
//            RuntimeException ex = new RuntimeException();
121
//            e.initCause(e);
122
//            throw ex;
123
//        }
125 124
        if (f == null) {
126 125
            // La feature no se ha editado.
127 126
            f = new DefaultFeature(store, data);
......
136 135
    
137 136
    @Override
138 137
    protected boolean skipFeature(FeatureProvider data) {
139

  
140 138
        try {
141 139
            if (getIterator().getCurrent() == 0) {
142
                FeatureReference ref = new DefaultFeatureReference(store, data);
143
                if (this.featureManager.isDeleted(ref)) {
140
//                FeatureReference ref = new DefaultFeatureReference(store, data);
141
                if (this.featureManager.isDeleted(data)) {
144 142
                    return true;
145 143
                };
146 144
//                if (this.featureManager.get(ref, store) != null) {
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/featurereference/EditingFeatureReference.java
1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.fmap.dal.feature.impl.featurereference;
7

  
8
import java.lang.ref.WeakReference;
9
import java.util.Base64;
10
import java.util.UUID;
11
import org.gvsig.fmap.dal.exception.DataException;
12
import org.gvsig.fmap.dal.feature.Feature;
13
import org.gvsig.fmap.dal.feature.FeatureStore;
14
import org.gvsig.fmap.dal.feature.FeatureType;
15
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore;
16
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
17
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
18
import org.gvsig.tools.persistence.Persistent;
19
import org.gvsig.tools.persistence.PersistentState;
20
import org.gvsig.tools.persistence.exception.PersistenceException;
21
import org.json.JSONArray;
22

  
23
/**
24
 *
25
 * @author fdiaz
26
 */
27
public class EditingFeatureReference implements FeatureReferenceProviderServices, Persistent {
28

  
29
    private String uid;
30
    private WeakReference storeRef;
31
    private String featureTypeId;
32

  
33
    /*friend*/
34
    EditingFeatureReference(FeatureStore store, FeatureProvider data) {
35
    	this.storeRef = new WeakReference(store);
36
        this.featureTypeId = store.getDefaultFeatureTypeQuietly().getId();
37
        this.generateUUID();
38
    }
39
    
40
    private String generateUUID() {
41
        UUID x = UUID.randomUUID();
42
        String s = x.toString().replace("-", "");
43
        return s;
44
    }
45

  
46
    @Override
47
    public Object getOID() {
48
        return null;
49
    }
50

  
51
    @Override
52
    public String[] getKeyNames() {
53
        return null;
54
    }
55

  
56
    @Override
57
    public Object getKeyValue(String name) {
58
        return null;
59
    }
60

  
61
    @Override
62
    public String getFeatureTypeId() {
63
        return featureTypeId;
64
    }
65

  
66
    @Override
67
    public Feature getFeature() throws DataException {
68
	return this.getStore().getFeatureByReference(this);
69
    }
70

  
71
    @Override
72
    public Feature getFeatureQuietly() {
73
        try {
74
            return this.getFeature();
75
        } catch (Exception e) {
76
            return null;
77
        }
78
    }
79

  
80
    @Override
81
    public Feature getFeature(FeatureType featureType) throws DataException {
82
        return this.getStore().getFeatureByReference(this, featureType);
83
    }
84
    
85
    private DefaultFeatureStore getStore() {
86
        return (DefaultFeatureStore) this.storeRef.get();
87
    }
88

  
89
    @Override
90
    public boolean isNewFeature() {
91
        return true;
92
    }
93

  
94
    @Override
95
    public String getCode() {
96
        Object[] data = new Object[7];
97
        data[0] = "uid";
98
        data[1] = this.uid;
99

  
100
        String s = new JSONArray(data).toString();
101
        String r = Base64.getEncoder().encodeToString(s.getBytes());
102
        return r;
103
    }
104

  
105
    @Override
106
    public void saveToState(PersistentState state) throws PersistenceException {
107
        state.set("uid", uid);
108
        state.set("store", (Persistent) storeRef.get());
109
        state.set("featureTypeId", featureTypeId);
110
    }
111

  
112
    @Override
113
    public void loadFromState(PersistentState state) throws PersistenceException {
114
        this.uid =  state.getString("uuid");
115
        this.storeRef = new WeakReference(state.get("store"));
116
        this.featureTypeId = state.getString("featureTypeId");
117
    }
118

  
119
}
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/featurereference/DefaultFeatureReference.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 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.fmap.dal.feature.impl.featurereference;
25

  
26
import java.lang.ref.WeakReference;
27
import java.util.Arrays;
28
import java.util.Base64;
29
import java.util.List;
30
import java.util.Objects;
31
import org.apache.commons.lang3.ArrayUtils;
32
import org.gvsig.fmap.dal.exception.DataException;
33
import org.gvsig.fmap.dal.feature.Feature;
34
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
36
import org.gvsig.fmap.dal.feature.FeatureType;
37
import org.gvsig.fmap.dal.feature.impl.DefaultFeature;
38
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore;
39
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
40
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
41
import org.gvsig.tools.ToolsLocator;
42
import org.gvsig.tools.dynobject.DynStruct;
43
import org.gvsig.tools.persistence.Persistent;
44
import org.gvsig.tools.persistence.PersistentState;
45
import org.gvsig.tools.persistence.exception.PersistenceException;
46
import org.json.JSONArray;
47
import org.json.JSONObject;
48

  
49
@SuppressWarnings("UseSpecificCatch")
50
public class DefaultFeatureReference implements
51
		FeatureReferenceProviderServices, Persistent {
52

  
53
	private Object oid;
54
	private Integer myHashCode = null;
55
	private Object[] pk;
56
	private String[] pkNames;
57
	private WeakReference storeRef;
58
	private boolean isNewFeature;
59
	private String featureTypeId;
60
	 
61
        
62
        private static final int OID_INDEX = 0;
63
        private static final int MYHASHCODE_INDEX = 1;
64
        private static final int ISNEWFEATURE_INDEX = 2;
65
        private static final int FEATURETYPEID_INDEX = 3;
66
        private static final int PKNAMES_SIZE_INDEX = 4;
67
        private static final int PKNAMES_INDEX = 5;
68
        private static final int PK_INDEX = 6;
69
        
70
	/**
71
     * Constructor used by the persistence manager. Don't use directly.
72
     * After to invoke this method, the persistence manager calls 
73
     * the the method {@link #loadFromState(PersistentState)} to set 
74
     * the values of the internal attributes that this class needs to work.
75
     */
76
	public DefaultFeatureReference() {
77
		super();		
78
	}
79
	
80
	public DefaultFeatureReference(DefaultFeature feature) {
81
		this(feature.getStore(), feature.getData());
82
	}
83

  
84
	public DefaultFeatureReference(FeatureStore store,
85
			FeatureProvider fdata) {
86
		this.isNewFeature = fdata.isNew();
87
		this.oid = null;
88
		this.pk = null;
89
		this.storeRef = new WeakReference(store);
90
		this.featureTypeId = fdata.getType().getId();
91

  
92
		if (fdata.getType().hasOID() || isNewFeature) {
93
			this.oid = fdata.getOID();
94
			if (this.oid == null) {
95
				// FIXME Exception
96
				throw new RuntimeException("Missing OID");
97
			}
98
		} else {
99
			this.calculatePK(fdata);
100
			if (this.pk == null) {
101
				// FIXME Exception
102
				throw new RuntimeException("Missing pk attributes");
103
			}
104
		}
105

  
106
	}
107

  
108
	/*
109
	 * Use only for Persistent.setState
110
	 */
111
//	public DefaultFeatureReference(FeatureStore store) {
112
//		this.isNewFeature = false;
113
//		this.oid = null;
114
//		this.pk = null;
115
//		this.storeRef = new WeakReference(store);
116
//	}
117

  
118
	public DefaultFeatureReference(FeatureStore store, Object oid) {
119
		// FIXME featureTypeId is needed !!!
120
		this.isNewFeature = false;
121
		this.oid = oid;
122
		this.pk = null;
123
		this.storeRef = new WeakReference(store);
124
	}
125

  
126
	public DefaultFeatureReference(FeatureStore store, String code) {
127
            this.storeRef = new WeakReference(store);
128
            
129
            String json = new String(Base64.getDecoder().decode(code.getBytes()));
130
            
131
            JSONArray x = new JSONArray(json);
132
            this.oid = x.get(OID_INDEX);
133
            if( x.get(MYHASHCODE_INDEX)==JSONObject.NULL ) {
134
                this.myHashCode = null;
135
            } else {
136
                this.myHashCode = x.getInt(MYHASHCODE_INDEX);
137
            }
138
            this.isNewFeature = x.getBoolean(ISNEWFEATURE_INDEX);
139
            this.featureTypeId = x.getString(FEATURETYPEID_INDEX);
140
            int pkNames_size = x.getInt(PKNAMES_SIZE_INDEX);
141
            if( pkNames_size<0 ) {
142
                this.pk = null;
143
                this.pkNames = null;
144
            } else {
145
                this.pk = new Object[pkNames_size];
146
                this.pkNames = new String[pkNames_size];
147
                JSONArray xx = x.getJSONArray(PKNAMES_INDEX);
148
                for( int i=0; i<xx.length(); i++ ) {
149
                    this.pkNames[i] = xx.getString(i);
150
                }
151
                xx = x.getJSONArray(PKNAMES_INDEX);
152
                for( int i=0; i<xx.length(); i++ ) {
153
                    this.pk[i] = xx.get(i);
154
                }
155
            }
156
            
157
//            JSONObject x = new JSONObject(json);
158
//            this.oid = x.get("oid");
159
//            this.myHashCode = x.getInt("myHashCode");
160
//            this.isNewFeature = x.getBoolean("isNewFeature");
161
//            this.featureTypeId = x.getString("featureTypeId");
162
//            int pkNames_size = x.getInt("pkNames_size");
163
//            if( pkNames_size<0 ) {
164
//                this.pk = null;
165
//                this.pkNames = null;
166
//            } else {
167
//                this.pk = new Object[pkNames_size];
168
//                this.pkNames = new String[pkNames_size];
169
//                JSONArray xx = x.getJSONArray("pkNames");
170
//                for( int i=0; i<xx.length(); i++ ) {
171
//                    this.pkNames[i] = xx.getString(i);
172
//                }
173
//                xx = x.getJSONArray("pk");
174
//                for( int i=0; i<xx.length(); i++ ) {
175
//                    this.pk[i] = xx.get(i);
176
//                }
177
//            }
178
        }
179
        
180
        private DefaultFeatureStore getStore() {
181
		return (DefaultFeatureStore) this.storeRef.get();
182
	}
183

  
184
	public void calculatePK(FeatureProvider fdata) { // Usado en los test de BBDD
185
            FeatureAttributeDescriptor[] pkattrs = fdata.getType().getPrimaryKey();
186
            if( ArrayUtils.isEmpty(pkattrs) ) {
187
                this.pk = null;
188
                this.pkNames = null;
189
                return ;
190
            }
191
            this.pk = new Object[pkattrs.length];
192
            this.pkNames = new String[pkattrs.length];
193
            int n = 0;
194
            for (FeatureAttributeDescriptor pkattr : pkattrs) {
195
//                if( pkattr.isAutomatic() ) {
196
//                    this.pk = null;
197
//                    this.pkNames = null;
198
//                    return ;
199
//                }
200
                this.pk[n] = fdata.get(pkattr.getIndex());
201
                this.pkNames[n] = pkattr.getName();
202
                n++;
203
            }
204
    }
205

  
206
    @Override
207
    public Feature getFeature() throws DataException {
208
	return this.getStore().getFeatureByReference(this);
209
    }
210

  
211
    @Override
212
    public Feature getFeatureQuietly() {
213
        try {
214
            return this.getFeature();
215
        } catch(Exception ex) {
216
            return null;
217
        }
218
    }
219

  
220
    @Override
221
	public Feature getFeature(FeatureType featureType) throws DataException {
222
		return this.getStore().getFeatureByReference(this, featureType);
223
	}
224

  
225
    @Override
226
	public Object getOID() {
227
		return this.oid;
228
	}
229

  
230
    @Override
231
	public boolean isNewFeature() {
232
		return this.isNewFeature;
233
	}
234

  
235

  
236
    @Override
237
	public boolean equals(Object obj) {
238
		if (!(obj instanceof DefaultFeatureReference)) {
239
			return false;
240
		}
241
		DefaultFeatureReference other = (DefaultFeatureReference) obj;
242

  
243
		FeatureStore otherStore = (FeatureStore) other.storeRef.get();
244
		FeatureStore myrStore = (FeatureStore) this.storeRef.get();
245
		if (otherStore == null || myrStore == null) {
246
			return false;
247
		}
248
		if (!myrStore.equals(otherStore)) {
249
			return false;
250
		}
251
		if (myHashCode != null && other.myHashCode != null) {
252
			return myHashCode.equals(other.myHashCode);
253
		}
254
		if (this.oid != null) {
255
			return this.oid.equals(other.oid);
256
		}
257
		if(pk != null) {
258
			if(other.pk == null) {
259
				return false;
260
			}
261
    		for (int i = 0; i < this.pk.length; i++) {
262
    			if (!this.pk[i].equals(other.pk[i])) {
263
    				return false;
264
    			}
265
    		}
266
		}
267
		return true;
268
	}
269

  
270
    @Override
271
	public int hashCode() {
272
		if (this.oid != null) {
273
			return this.oid.hashCode();
274
		}
275
		if (myHashCode == null) {
276
			StringBuilder buff = new StringBuilder();
277

  
278
			for (int i = 0; i < this.pk.length; i++) {
279
				buff.append(Objects.hashCode(this.pk[i]));
280
				buff.append("##");
281
			}
282
			myHashCode = buff.toString().hashCode();
283
		}
284
		return myHashCode;
285
	}
286

  
287
    @Override
288
	public String[] getKeyNames() {
289
		return pkNames;
290
	}
291

  
292
    @Override
293
	public Object getKeyValue(String name) {
294
		for (int i = 0; i < pkNames.length; i++) {
295
			if (pkNames[i].equalsIgnoreCase(name)) {
296
				return pk[i];
297
			}
298
		}
299
		// FIXME exception????
300
		return null;
301
	}
302

  
303
    @Override
304
	public String getFeatureTypeId() {
305
		return featureTypeId;
306
	}
307

  
308
	// *** Persistence ***
309

  
310
    @Override
311
	public void loadFromState(PersistentState state)
312
			throws PersistenceException {
313
		this.oid = state.get("oid");
314
		this.myHashCode = (Integer) state.get("myHashCode");
315
		this.storeRef = new WeakReference(state.get("store"));
316
		this.isNewFeature = state.getBoolean("isNewFeature");
317
		this.featureTypeId = state.getString("featureTypeId");
318
		List pkList = (List) state.get("pk");
319
		if (pkList != null) {
320
			List pkNamesList = (List) state.get("pkNames");
321
			if (pkNamesList == null || pkList.size() != pkNamesList.size()) {
322
				throw new PersistenceException("bad pkNames value");
323
			}
324
			this.pk = pkList.toArray();
325
            this.pkNames =
326
                (String[]) pkNamesList.toArray(new String[pkList.size()]);
327
		} else {
328
			this.pk = null;
329
			this.pkNames = null;
330
		}
331
	}
332

  
333
    @Override
334
	public void saveToState(PersistentState state) throws PersistenceException {
335
		state.set("oid", oid);
336
		state.set("myHashCode", myHashCode);
337
		state.set("isNewFeature", isNewFeature);
338
		state.set("store", (Persistent) storeRef.get());
339
		state.set("featureTypeId", featureTypeId);
340
		if (pk == null) {
341
			state.setNull("pk");
342
			state.setNull("pkNames");
343
		} else {
344
			state.set("pk", pk);
345
			state.set("pkNames", pkNames);
346
		}
347

  
348
	}
349
        
350
        @Override
351
        public String getCode() {
352
//            JSONObject x = new JSONObject();
353
////            x.put("store", (Persistent) storeRef.get());
354
//            x.put("oid", oid);
355
//            x.put("myHashCode", myHashCode);
356
//            x.put("isNewFeature", isNewFeature);
357
//            x.put("featureTypeId", featureTypeId);
358
//            if( this.pk == null ) {
359
//                x.put("pkNames_size", -1);
360
//                x.put("pkNames", JSONObject.NULL);
361
//                x.put("pk", JSONObject.NULL);
362
//            } else {
363
//                x.put("pkNames_size", pkNames.length);
364
//                x.put("pkNames", new JSONArray(this.pkNames));
365
//                x.put("pk", new JSONArray(this.pk));
366
//            }
367
//
368
            Object[] data = new Object[7];
369
            data[OID_INDEX] = this.oid;
370
            data[MYHASHCODE_INDEX] = this.myHashCode;
371
            data[ISNEWFEATURE_INDEX] = this.isNewFeature;
372
            data[FEATURETYPEID_INDEX] = this.featureTypeId;
373
            if( this.pk == null ) {
374
                data[PKNAMES_SIZE_INDEX] = -1;
375
                data[PKNAMES_INDEX] = JSONObject.NULL;
376
                data[PK_INDEX] = JSONObject.NULL;
377
            } else {
378
                data[PKNAMES_SIZE_INDEX] = pkNames.length;
379
                data[PKNAMES_INDEX] = new JSONArray(this.pkNames);
380
                data[PK_INDEX] = new JSONArray(this.pk);            
381
            }            
382
            String s = new JSONArray(data).toString();
383
            String r = Base64.getEncoder().encodeToString(s.getBytes());
384
            
385
            return r;
386
        }
387

  
388
	public static void registerPersistent() {
389
		DynStruct definition = ToolsLocator.getPersistenceManager().addDefinition(
390
				DefaultFeatureReference.class, 
391
				"Reference", 
392
				"DefaultFeatureReference Persistent definition", 
393
				null, 
394
				null
395
			);
396

  
397
		definition.addDynFieldObject("oid")
398
			.setClassOfValue(Object.class)
399
			.setMandatory(false)
400
			.setPersistent(true);
401
			
402
		definition.addDynFieldBoolean("isNewFeature")
403
			.setMandatory(true)
404
			.setPersistent(true);
405
	
406
		definition.addDynFieldObject("store")
407
			.setClassOfValue(FeatureStore.class)
408
			.setMandatory(true)
409
			.setPersistent(true);
410

  
411
		definition.addDynFieldInt("myHashCode")
412
			.setMandatory(false)
413
			.setPersistent(true);
414
	
415
		definition.addDynFieldString("featureTypeId")
416
			.setMandatory(true)
417
			.setPersistent(true);
418

  
419
		definition.addDynFieldArray("pk")
420
			.setClassOfItems(Object.class)
421
			.setMandatory(false)
422
			.setPersistent(true);
423

  
424
        definition.addDynFieldArray("pkNames")
425
			.setClassOfItems(String.class)
426
			.setMandatory(false)
427
			.setPersistent(true);
428

  
429
	}
430

  
431
    @Override
432
	public String toString() {
433
        StringBuilder builder = new StringBuilder();
434
        builder.append("FeatureReference: oid = ");
435
        builder.append(oid);
436
        if( myHashCode!=null ) {
437
            builder.append(", myHashCode = ");
438
            builder.append(myHashCode);
439
        } 
440
        if( pk!=null ) {
441
            builder.append(", pks = ");
442
            builder.append(Arrays.asList(pk));
443
        }
444
        if( pkNames!=null ) {
445
            builder.append(", pkNames = ");
446
            builder.append(Arrays.asList(pkNames));
447
        }
448
        if( isNewFeature ) {
449
            builder.append(", isNew = ");
450
            builder.append(isNewFeature);
451
        }
452
        if( featureTypeId!=null ) {
453
            builder.append(", featureTypeId = ");
454
            builder.append(featureTypeId);
455
        }
456
        return builder.toString();
457
	}
458

  
459
}
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/featurereference/FeatureReferenceFactory.java
1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.fmap.dal.feature.impl.featurereference;
7

  
8
import org.gvsig.fmap.dal.feature.Feature;
9
import org.gvsig.fmap.dal.feature.FeatureReference;
10
import org.gvsig.fmap.dal.feature.FeatureStore;
11
import org.gvsig.fmap.dal.feature.impl.DefaultFeature;
12
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
13

  
14
/**
15
 *
16
 * @author fdiaz
17
 */
18
public class FeatureReferenceFactory {
19

  
20
    private FeatureReferenceFactory() {
21
        
22
    }
23
    
24
    public static FeatureReference createFromFeature(Feature feature){
25
        DefaultFeatureReference ref = new DefaultFeatureReference(feature.getStore(), ((DefaultFeature)feature).getData());
26
        return ref;
27
    }
28
    
29
    public static FeatureReference createFromOID(FeatureStore store, Object oid){
30
        FeatureReference ref = new DefaultFeatureReference(store, oid);
31
//        FeatureReference ref = new FeatureReferenceOID(store, oid);
32
        return ref;
33
    }
34
    
35
    public static FeatureReference createFromCode(FeatureStore store, String code){
36
        DefaultFeatureReference ref = new DefaultFeatureReference(store, code);
37
        return ref;
38
    }
39
    
40
    public static FeatureReference createFromFeatureProvider(FeatureStore store, FeatureProvider data) {
41
        DefaultFeatureReference ref = new DefaultFeatureReference(store, data);
42
        return ref;
43
    }
44
    
45
    public static void registerPersistent() {
46
//        DefaultFeatureReference.registerPersistent();
47
        FeatureReferencePersistenceFactory.selfRegister();
48
    }
49

  
50
}
51

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff