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 / fmap / dal / feature / impl / featurereference / FeatureReferenceFactory.java @ 47078

History | View | Annotate | Download (3.51 KB)

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.util.Base64;
9
import javax.json.JsonObject;
10
import org.gvsig.fmap.dal.feature.Feature;
11
import org.gvsig.fmap.dal.feature.FeatureReference;
12
import org.gvsig.fmap.dal.feature.FeatureStore;
13
import org.gvsig.fmap.dal.feature.impl.DefaultFeature;
14
import static org.gvsig.fmap.dal.feature.impl.featurereference.FeatureReferenceCounter.COUNTER_TYPE;
15
import static org.gvsig.fmap.dal.feature.impl.featurereference.FeatureReferenceOID.OID_TYPE;
16
import static org.gvsig.fmap.dal.feature.impl.featurereference.FeatureReferencePK.PK_TYPE;
17
import static org.gvsig.fmap.dal.feature.impl.featurereference.FeatureReferenceUID.UID_TYPE;
18
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
19
import org.gvsig.json.Json;
20

    
21
/**
22
 *
23
 * @author fdiaz
24
 */
25
public class FeatureReferenceFactory {
26
    
27
    private FeatureReferenceFactory() {
28
        
29
    }
30
    
31
    public static FeatureReference createFromFeature(Feature feature){
32
        FeatureProvider fdata = ((DefaultFeature) feature).getData();
33
        FeatureStore store = feature.getStore();
34
        return createFromFeatureProvider(store, fdata);
35
    }
36
    
37
    public static FeatureReference createFromOID(FeatureStore store, Object oid){
38
        if(FeatureReferenceCounter.canUse(oid)){
39
            FeatureReference ref = new FeatureReferenceCounter(store, oid);
40
            return ref;
41
        }
42
        FeatureReference ref = new FeatureReferenceOID(store, oid);
43
        return ref;
44
    }
45
    
46
    public static FeatureReference createFromCode(FeatureStore store, String code){
47
        String json = new String(Base64.getDecoder().decode(code.getBytes()));
48

    
49
        JsonObject x = Json.createObject(json);
50
        final String type = x.getString("type",null);
51
        switch (type){
52
            case OID_TYPE:
53
                return new FeatureReferenceOID(store, x);
54
            case COUNTER_TYPE:
55
                return new FeatureReferenceCounter(store, x);
56
            case UID_TYPE:
57
                return new FeatureReferenceUID(store, x);
58
            case PK_TYPE:
59
                return new FeatureReferencePK(store, x);
60
            default:
61
                throw new IllegalArgumentException("Unsupported feature reference type"+type);
62
        }
63
            
64
    }
65
    
66
    public static FeatureReference createReferencePKFromFeature(Feature feature){
67
        // If store from feature does not support pk's throws IllegalArgumentException
68
        FeatureProvider fdata = ((DefaultFeature) feature).getData();
69
        FeatureStore store = feature.getStore();
70
        return new FeatureReferencePK(store, fdata);
71
    }
72

    
73
    
74
    public static FeatureReference createFromFeatureProvider(FeatureStore store, FeatureProvider fdata) {
75
        FeatureReference ref = null;
76
        boolean isNewFeature = fdata.isNew();
77

    
78
        if(isNewFeature){
79
            ref = new FeatureReferenceCounter(store);            
80
        } else if (fdata.getType().hasOID()) {
81
            if (fdata.getOID() != null) {
82
                ref = new FeatureReferenceOID(store, fdata.getOID());
83
            } else {
84
                throw new RuntimeException("Missing OID");
85
            }
86
        } else {
87
            ref = new FeatureReferencePK(store, fdata);
88
        }
89
        return ref;
90
    }
91
    
92
    public static void registerPersistent() {
93
        FeatureReferencePersistenceFactory.selfRegister();
94
    }
95

    
96
}
97