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 @ 46309
History | View | Annotate | Download (3.51 KB)
1 | 45647 | fdiaz | /*
|
---|---|---|---|
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 | 45688 | fdiaz | import java.util.Base64; |
9 | import javax.json.JsonObject; |
||
10 | 45647 | fdiaz | 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 | 45898 | fdiaz | 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 | 45647 | fdiaz | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
19 | 45688 | fdiaz | import org.gvsig.json.Json; |
20 | 45647 | fdiaz | |
21 | /**
|
||
22 | *
|
||
23 | * @author fdiaz
|
||
24 | */
|
||
25 | public class FeatureReferenceFactory { |
||
26 | 45688 | fdiaz | |
27 | 45647 | fdiaz | private FeatureReferenceFactory() {
|
28 | |||
29 | } |
||
30 | |||
31 | public static FeatureReference createFromFeature(Feature feature){ |
||
32 | 45688 | fdiaz | FeatureProvider fdata = ((DefaultFeature) feature).getData(); |
33 | FeatureStore store = feature.getStore(); |
||
34 | return createFromFeatureProvider(store, fdata);
|
||
35 | 45647 | fdiaz | } |
36 | |||
37 | public static FeatureReference createFromOID(FeatureStore store, Object oid){ |
||
38 | 45898 | fdiaz | if(FeatureReferenceCounter.canUse(oid)){
|
39 | FeatureReference ref = new FeatureReferenceCounter(store, oid);
|
||
40 | return ref;
|
||
41 | } |
||
42 | 45688 | fdiaz | FeatureReference ref = new FeatureReferenceOID(store, oid);
|
43 | 45647 | fdiaz | return ref;
|
44 | } |
||
45 | |||
46 | public static FeatureReference createFromCode(FeatureStore store, String code){ |
||
47 | 45688 | fdiaz | 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 | 45898 | fdiaz | case COUNTER_TYPE:
|
55 | return new FeatureReferenceCounter(store, x); |
||
56 | 45688 | fdiaz | 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 | 45647 | fdiaz | } |
65 | |||
66 | 45688 | fdiaz | 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 | 45898 | fdiaz | ref = new FeatureReferenceCounter(store);
|
80 | 45688 | fdiaz | } 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 | 45647 | fdiaz | return ref;
|
90 | } |
||
91 | |||
92 | public static void registerPersistent() { |
||
93 | FeatureReferencePersistenceFactory.selfRegister(); |
||
94 | } |
||
95 | |||
96 | } |