Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.spi / src / main / java / org / gvsig / fmap / dal / feature / spi / LocalTransaction.java @ 47606

History | View | Annotate | Download (3.93 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.spi;
7

    
8
import org.apache.commons.lang3.ArrayUtils;
9
import org.gvsig.fmap.dal.DALLocator;
10
import org.gvsig.fmap.dal.DataManager;
11
import org.gvsig.fmap.dal.DataServerExplorer;
12
import org.gvsig.fmap.dal.DataStore;
13
import org.gvsig.fmap.dal.DataTransaction;
14
import org.gvsig.fmap.dal.SupportTransactions;
15
import org.gvsig.fmap.dal.exception.DataException;
16
import org.slf4j.Logger;
17
import org.slf4j.LoggerFactory;
18

    
19
/**
20
 *
21
 * @author fdiaz
22
 */
23
public class LocalTransaction {
24

    
25
    protected static final Logger LOGGER = LoggerFactory.getLogger(LocalTransaction.class);
26
    
27
    boolean local;
28
    DataTransaction trans;
29
    DataManager dataManager;
30

    
31
    public LocalTransaction(DataManager dataManager, DataTransaction... transaction) {
32
        this.dataManager = dataManager;
33
        if(this.dataManager == null){
34
            this.dataManager = DALLocator.getDataManager();
35
        }
36
        if(transaction == null) {
37
            this.trans = null;
38
        } else if (ArrayUtils.isEmpty(transaction)){
39
            this.trans = null;
40
        } else {
41
            DataTransaction t = null;
42
            for (DataTransaction current : transaction) {
43
                if(current != null) {
44
                    if(t == null){
45
                        t = current;
46
                        continue;
47
                    }
48
                    if(current != t) {
49
                        throw new IllegalArgumentException("Transactions conflict.");
50
                    }
51
                }
52
            }
53
            this.trans = t;
54
        }
55
        this.local = false;
56
    }
57

    
58
    public void add(DataStore store) throws DataException {
59
        if (trans == null) {
60
            trans = this.dataManager.createTransaction();
61
            this.local = true;
62
        }
63
        this.trans.add(store, false);
64
    }
65

    
66
    public void add(DataServerExplorer explorer) throws DataException {
67
        if (trans == null) {
68
            trans = this.dataManager.createTransaction();
69
            this.local = true;
70
        }
71
        this.trans.add(explorer, false);
72
    }
73

    
74
    public void add(Object obj) throws DataException {
75
        if(obj == null){
76
            return;
77
        }
78
        if(obj instanceof DataStore){
79
            this.add((DataStore)obj);
80
            return;
81
        }
82
        if(obj instanceof DataServerExplorer){
83
            this.add((DataServerExplorer)obj);
84
            return;
85
        }
86
        if(obj instanceof SupportTransactions){
87
            this.add((SupportTransactions)obj);
88
            return;
89
        }
90
        throw new IllegalArgumentException("Can't add "+obj.getClass().getName());
91
    }
92
    
93
    private void add(SupportTransactions obj) throws DataException {
94
        if (trans == null) {
95
            trans = this.dataManager.createTransaction();
96
            this.local = true;
97
        }
98
        this.trans.add(obj, false);
99
    }
100

    
101
    public void begin() throws DataException {
102
        if (trans == null) {
103
            trans = this.dataManager.createTransaction();
104
            this.local = true;
105
        }
106
        if (!this.trans.isInProgress()) {
107
            trans.begin();
108
        }
109
    }
110

    
111
    public void commit() throws DataException {
112
        if (this.local) {
113
            trans.commit();
114
        }
115
    }
116

    
117
    public void close() throws Exception {
118
        if (this.local) {
119
            trans.close();
120
        }
121
    }
122

    
123
    public void closeQuietly() {
124
        if (this.local) {
125
            try {
126
                trans.close();
127
            } catch (Exception ex) {
128
                LOGGER.debug("", ex);
129
            }
130
        }
131
    }
132

    
133
    public void abortQuietly() {
134
        if (this.local) {
135
            try {
136
                trans.rollback();
137
                trans.close();
138
            } catch (Exception ex) {
139
                LOGGER.debug("", ex);
140
            }
141
        }
142
    }
143
    
144
}