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 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
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
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.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.utils;
15
 
16
import org.openconcerto.map.model.DatabaseAccessor;
17
import org.openconcerto.map.model.Ville;
18
import org.openconcerto.sql.Configuration;
19
import org.openconcerto.sql.model.SQLRow;
20
import org.openconcerto.sql.model.SQLRowListRSH;
21
import org.openconcerto.sql.model.SQLRowValues;
22
import org.openconcerto.sql.model.SQLSelect;
23
import org.openconcerto.sql.model.SQLTable;
25 ilm 24
import org.openconcerto.sql.model.Where;
18 ilm 25
 
26
import java.sql.SQLException;
27
import java.util.ArrayList;
28
import java.util.List;
29
 
25 ilm 30
// TODO use the one from Nego
18 ilm 31
public class NXDatabaseAccessor implements DatabaseAccessor {
32
    @SuppressWarnings("unchecked")
33
    public List<Ville> read() {
34
        SQLTable ville = Configuration.getInstance().getBase().getTable("VILLE");
35
        SQLSelect sel = new SQLSelect(Configuration.getInstance().getBase());
36
        sel.addSelectStar(ville);
37
        List<SQLRow> l = (List<SQLRow>) Configuration.getInstance().getBase().getDataSource().execute(sel.asString(), SQLRowListRSH.createFromSelect(sel));
38
 
39
        List<Ville> lResult = new ArrayList<Ville>();
40
        for (SQLRow row : l) {
41
            final String nom = row.getString("NOM");
42
            final String cp = row.getString("CODE_POSTAL");
43
            long pop = ((Number) row.getObject("POPULATION")).longValue();
44
            long x = ((Number) row.getObject("X_LAMBERT")).longValue();
45
            long y = ((Number) row.getObject("Y_LAMBERT")).longValue();
46
            Ville v = new Ville(nom, pop, x, y, cp);
47
            lResult.add(v);
48
        }
49
 
50
        return lResult;
51
    }
52
 
53
    public void store(Ville v) {
54
        SQLTable ville = Configuration.getInstance().getBase().getTable("VILLE");
55
        SQLRowValues rowVals = new SQLRowValues(ville);
56
        rowVals.put("NOM", v.getName());
57
        rowVals.put("CODE_POSTAL", v.getCodepostal());
58
        rowVals.put("X_LAMBERT", v.getXLambert());
59
        rowVals.put("Y_LAMBERT", v.getYLambert());
60
        rowVals.put("POPULATION", v.getPopulation());
61
        try {
62
            rowVals.insert();
63
        } catch (SQLException e) {
64
            // TODO Auto-generated catch block
65
            e.printStackTrace();
66
        }
25 ilm 67
    }
18 ilm 68
 
25 ilm 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);
18 ilm 74
    }
75
}