OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
80 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.panel.compta;
15
 
16
import org.openconcerto.sql.model.DBRoot;
17
import org.openconcerto.sql.model.SQLDataSource;
18
import org.openconcerto.sql.model.SQLRow;
19
import org.openconcerto.sql.model.SQLSelect;
20
import org.openconcerto.sql.model.SQLSelectJoin;
21
import org.openconcerto.sql.model.SQLTable;
22
import org.openconcerto.sql.model.Where;
23
import org.openconcerto.sql.request.UpdateBuilder;
24
import org.openconcerto.utils.Tuple2;
25
 
26
import java.io.BufferedOutputStream;
27
import java.io.File;
28
import java.io.FileOutputStream;
29
import java.io.IOException;
30
import java.io.OutputStream;
31
import java.text.DateFormat;
180 ilm 32
import java.text.ParseException;
80 ilm 33
import java.text.SimpleDateFormat;
34
import java.util.Date;
35
 
90 ilm 36
public abstract class AbstractExport {
80 ilm 37
 
38
    private final DBRoot rootSociete;
39
    private final String type;
40
    private final String extension;
41
    private boolean used;
90 ilm 42
    private int nbCharCpt = 0;
43
    private int nbCharLimitCpt = 0;
80 ilm 44
 
45
    protected AbstractExport(final DBRoot rootSociete, final String type, final String extension) {
46
        this.rootSociete = rootSociete;
47
        this.type = type;
48
        this.extension = extension;
49
        this.used = false;
50
    }
51
 
90 ilm 52
    public String getFormattedCompte(String s) {
53
        String result = s;
54
        if (s != null) {
55
            if (this.nbCharCpt > 0 && s.trim().length() > 0) {
149 ilm 56
                final StringBuilder res = new StringBuilder(this.nbCharCpt);
90 ilm 57
                for (int i = 0; i < Math.max(this.nbCharCpt, s.length()); i++) {
58
                    if (i < s.length()) {
59
                        res.append(s.charAt(i));
60
                    } else {
61
                        res.append("0");
62
                    }
63
                }
64
                result = res.toString();
65
            }
66
            if (this.nbCharLimitCpt > 0 && result.length() > this.nbCharLimitCpt) {
67
                result = result.substring(0, this.nbCharLimitCpt);
68
            }
69
        }
70
        return result;
71
    }
72
 
73
    public void setNbCharCpt(int nbCharCpt) {
74
        this.nbCharCpt = nbCharCpt;
75
    }
76
 
77
    public void setNbCharLimitCpt(int nbCharLimitCpt) {
78
        this.nbCharLimitCpt = nbCharLimitCpt;
79
    }
80
 
81
    public int getNbCharCpt() {
82
        return nbCharCpt;
83
    }
84
 
85
    public int getNbCharLimitCpt() {
86
        return nbCharLimitCpt;
87
    }
88
 
80 ilm 89
    protected final DBRoot getRootSociete() {
90
        return this.rootSociete;
91
    }
92
 
93
    protected final SQLDataSource getDS() {
94
        return this.getRootSociete().getDBSystemRoot().getDataSource();
95
    }
96
 
97
    protected final SQLTable getEcritureT() {
98
        return this.getRootSociete().getTable("ECRITURE");
99
    }
100
 
101
    protected final Where getWhere(Date from, Date to, SQLRow selectedJournal, boolean onlyNew) {
102
        final SQLTable tableEcriture = getEcritureT();
103
        Where w = new Where(tableEcriture.getField("DATE"), from, to);
104
 
105
        if (selectedJournal != null && !selectedJournal.isUndefined()) {
106
            w = w.and(new Where(tableEcriture.getField("ID_JOURNAL"), "=", selectedJournal.getID()));
107
        }
108
 
109
        if (onlyNew) {
110
            w = w.and(Where.isNull(tableEcriture.getField("DATE_EXPORT")));
111
        }
112
        return w;
113
    }
114
 
115
    protected SQLSelect createSelect(Date from, Date to, SQLRow selectedJournal, boolean onlyNew) {
116
        final SQLSelect sel = new SQLSelect();
117
 
118
        final SQLTable tableEcriture = this.getRootSociete().getTable("ECRITURE");
119
        sel.addFrom(tableEcriture);
120
        sel.addJoin("LEFT", tableEcriture.getField("ID_JOURNAL"));
121
        sel.addJoin("LEFT", tableEcriture.getField("ID_COMPTE_PCE"));
122
        final SQLSelectJoin mvtJoin = sel.addJoin("LEFT", tableEcriture.getField("ID_MOUVEMENT"));
123
        sel.addJoin("LEFT", mvtJoin.getJoinedTable().getField("ID_PIECE"));
124
        sel.setWhere(getWhere(from, to, selectedJournal, onlyNew));
125
 
126
        return sel;
127
    }
128
 
129
    public final Tuple2<File, Number> export(final File selectedFile, Date from, Date to, SQLRow selectedJournal, boolean onlyNew) throws Exception {
130
        synchronized (this) {
131
            if (this.used)
132
                throw new IllegalStateException("Already used");
133
            this.used = true;
134
        }
149 ilm 135
        File directory = selectedFile;
136
        if (directory == null) {
80 ilm 137
            throw new IllegalArgumentException("Dossier sélectionné incorrect");
149 ilm 138
        }
139
        if (!directory.isDirectory()) {
140
            directory = directory.getParentFile();
141
        }
142
        if (!directory.canWrite()) {
143
            throw new IllegalArgumentException("Vous n'avez pas les droits pour écrire dans le dossier " + directory.getAbsolutePath());
144
        }
80 ilm 145
 
146
        final int count = this.fetchData(from, to, selectedJournal, onlyNew);
149 ilm 147
        if (count == 0) {
80 ilm 148
            return Tuple2.<File, Number> create(null, count);
149
        }
150
        final Date now = new Date();
151
        // ExportOC_20131101-20131127_koala.144356.txt
152
        // MAYBE only include generation date if necessary
93 ilm 153
        final DateFormat fileDateFormat = new SimpleDateFormat("yyyyMMdd");
154
        final DateFormat uniqueDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
149 ilm 155
        final File fOut = new File(directory, "ExportOC_" + fileDateFormat.format(from) + "-" + fileDateFormat.format(to) + "_" + this.type + "." + uniqueDateFormat.format(now) + this.extension);
80 ilm 156
        final BufferedOutputStream bufOut = new BufferedOutputStream(new FileOutputStream(fOut.getAbsolutePath()));
157
        try {
158
            this.export(bufOut);
159
        } finally {
160
            bufOut.close();
161
        }
162
        // Store export date
163
        final UpdateBuilder update = new UpdateBuilder(getEcritureT());
164
        update.set("DATE_EXPORT", getEcritureT().getField("DATE_EXPORT").getType().toString(now));
165
        // onlyNew=true to not overwrite already exported
166
        update.setWhere(getWhere(from, to, selectedJournal, true));
167
        getDS().execute(update.asString());
168
 
169
        return Tuple2.<File, Number> create(fOut, count);
170
    }
171
 
172
    protected abstract int fetchData(Date from, Date to, SQLRow selectedJournal, boolean onlyNew);
173
 
180 ilm 174
    protected abstract void export(final OutputStream out) throws IOException, ParseException;
149 ilm 175
 
176
    public static final String fixedLengthLeftAlign(final String str, final int fixedWidth) {
177
        if (str.length() >= fixedWidth) {
178
            return str.substring(0, fixedWidth);
179
        }
180
        StringBuilder b = new StringBuilder();
181
        b.append(str);
182
        int paddingSize = fixedWidth - str.length();
183
        for (int i = 0; i < paddingSize; i++) {
184
            b.append(' ');
185
        }
186
        return b.toString();
187
    }
188
 
189
    public static final String fixedLengthRightAlign(final String str, final int fixedWidth) {
190
        if (str.length() >= fixedWidth) {
191
            return str.substring(0, fixedWidth);
192
        }
193
        final StringBuilder b = new StringBuilder();
194
        int paddingSize = fixedWidth - str.length();
195
        for (int i = 0; i < paddingSize; i++) {
196
            b.append(' ');
197
        }
198
        b.append(str);
199
        return b.toString();
200
    }
201
 
80 ilm 202
}