OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 18 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 18 Rev 25
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
9
 * language governing permissions and limitations under the License.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.erp.utils;
14
 package org.openconcerto.erp.utils;
15
 
15
 
16
import org.openconcerto.map.model.DatabaseAccessor;
16
import org.openconcerto.map.model.DatabaseAccessor;
17
import org.openconcerto.map.model.Ville;
17
import org.openconcerto.map.model.Ville;
18
import org.openconcerto.sql.Configuration;
18
import org.openconcerto.sql.Configuration;
19
import org.openconcerto.sql.model.SQLRow;
19
import org.openconcerto.sql.model.SQLRow;
20
import org.openconcerto.sql.model.SQLRowListRSH;
20
import org.openconcerto.sql.model.SQLRowListRSH;
21
import org.openconcerto.sql.model.SQLRowValues;
21
import org.openconcerto.sql.model.SQLRowValues;
22
import org.openconcerto.sql.model.SQLSelect;
22
import org.openconcerto.sql.model.SQLSelect;
23
import org.openconcerto.sql.model.SQLTable;
23
import org.openconcerto.sql.model.SQLTable;
-
 
24
import org.openconcerto.sql.model.Where;
24
 
25
 
25
import java.sql.SQLException;
26
import java.sql.SQLException;
26
import java.util.ArrayList;
27
import java.util.ArrayList;
27
import java.util.List;
28
import java.util.List;
28
 
29
 
29
 
-
 
-
 
30
// TODO use the one from Nego
30
public class NXDatabaseAccessor implements DatabaseAccessor {
31
public class NXDatabaseAccessor implements DatabaseAccessor {
31
    @SuppressWarnings("unchecked")
32
    @SuppressWarnings("unchecked")
32
    public List<Ville> read() {
33
    public List<Ville> read() {
33
        SQLTable ville = Configuration.getInstance().getBase().getTable("VILLE");
34
        SQLTable ville = Configuration.getInstance().getBase().getTable("VILLE");
34
        SQLSelect sel = new SQLSelect(Configuration.getInstance().getBase());
35
        SQLSelect sel = new SQLSelect(Configuration.getInstance().getBase());
35
        sel.addSelectStar(ville);
36
        sel.addSelectStar(ville);
36
        List<SQLRow> l = (List<SQLRow>) Configuration.getInstance().getBase().getDataSource().execute(sel.asString(), SQLRowListRSH.createFromSelect(sel));
37
        List<SQLRow> l = (List<SQLRow>) Configuration.getInstance().getBase().getDataSource().execute(sel.asString(), SQLRowListRSH.createFromSelect(sel));
37
 
38
 
38
        List<Ville> lResult = new ArrayList<Ville>();
39
        List<Ville> lResult = new ArrayList<Ville>();
39
        for (SQLRow row : l) {
40
        for (SQLRow row : l) {
40
            final String nom = row.getString("NOM");
41
            final String nom = row.getString("NOM");
41
            final String cp = row.getString("CODE_POSTAL");
42
            final String cp = row.getString("CODE_POSTAL");
42
            long pop = ((Number) row.getObject("POPULATION")).longValue();
43
            long pop = ((Number) row.getObject("POPULATION")).longValue();
43
            long x = ((Number) row.getObject("X_LAMBERT")).longValue();
44
            long x = ((Number) row.getObject("X_LAMBERT")).longValue();
44
            long y = ((Number) row.getObject("Y_LAMBERT")).longValue();
45
            long y = ((Number) row.getObject("Y_LAMBERT")).longValue();
45
            Ville v = new Ville(nom, pop, x, y, cp);
46
            Ville v = new Ville(nom, pop, x, y, cp);
46
            lResult.add(v);
47
            lResult.add(v);
47
        }
48
        }
48
 
49
 
49
        return lResult;
50
        return lResult;
50
    }
51
    }
51
 
52
 
52
    public void store(Ville v) {
53
    public void store(Ville v) {
53
        SQLTable ville = Configuration.getInstance().getBase().getTable("VILLE");
54
        SQLTable ville = Configuration.getInstance().getBase().getTable("VILLE");
54
        SQLRowValues rowVals = new SQLRowValues(ville);
55
        SQLRowValues rowVals = new SQLRowValues(ville);
55
        rowVals.put("NOM", v.getName());
56
        rowVals.put("NOM", v.getName());
56
        rowVals.put("CODE_POSTAL", v.getCodepostal());
57
        rowVals.put("CODE_POSTAL", v.getCodepostal());
57
        rowVals.put("X_LAMBERT", v.getXLambert());
58
        rowVals.put("X_LAMBERT", v.getXLambert());
58
        rowVals.put("Y_LAMBERT", v.getYLambert());
59
        rowVals.put("Y_LAMBERT", v.getYLambert());
59
        rowVals.put("POPULATION", v.getPopulation());
60
        rowVals.put("POPULATION", v.getPopulation());
60
        try {
61
        try {
61
            rowVals.insert();
62
            rowVals.insert();
62
        } catch (SQLException e) {
63
        } catch (SQLException e) {
63
            // TODO Auto-generated catch block
64
            // TODO Auto-generated catch block
64
            e.printStackTrace();
65
            e.printStackTrace();
65
        }
66
        }
-
 
67
    }
66
 
68
 
-
 
69
    @Override
-
 
70
    public void delete(Ville v) {
-
 
71
        SQLTable villeT = Configuration.getInstance().getBase().getTable("VILLE");
-
 
72
        final Where w = new Where(villeT.getField("NOM"), "=", v.getName()).and(new Where(villeT.getField("CODE_POSTAL"), "=", v.getCodepostal()));
-
 
73
        villeT.getDBSystemRoot().getDataSource().execute("DELETE FROM " + villeT.getSQLName().quote() + " WHERE " + w);
67
    }
74
    }
68
}
75
}