OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 19 | Rev 28 | 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.preferences;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
25 ilm 17
import org.openconcerto.erp.core.customerrelationship.customer.report.FicheClientXmlSheet;
18
import org.openconcerto.erp.core.finance.accounting.report.BalanceSheet;
19
import org.openconcerto.erp.core.finance.accounting.report.GrandLivreSheet;
20
import org.openconcerto.erp.core.finance.accounting.report.JournauxSheet;
21
import org.openconcerto.erp.core.humanresources.payroll.report.EtatChargesPayeSheet;
22
import org.openconcerto.erp.core.humanresources.payroll.report.FichePayeSheet;
23
import org.openconcerto.erp.core.humanresources.payroll.report.LivrePayeSheet;
24
import org.openconcerto.erp.core.sales.invoice.report.ListeFactureXmlSheet;
25
import org.openconcerto.erp.core.sales.invoice.report.ListeVenteXmlSheet;
26
import org.openconcerto.erp.core.sales.invoice.report.VenteComptoirSheet;
27
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureXmlSheet;
28
import org.openconcerto.erp.core.sales.order.report.CommandeClientXmlSheet;
29
import org.openconcerto.erp.core.sales.quote.report.DevisXmlSheet;
30
import org.openconcerto.erp.core.sales.shipment.report.BonLivraisonXmlSheet;
31
import org.openconcerto.erp.generationDoc.DefaultLocalTemplateProvider;
32
import org.openconcerto.erp.generationDoc.DocumentLocalStorageManager;
33
import org.openconcerto.erp.generationDoc.SheetXml;
34
import org.openconcerto.erp.generationDoc.TemplateManager;
35
import org.openconcerto.erp.generationDoc.TemplateProvider;
36
import org.openconcerto.erp.generationDoc.gestcomm.AvoirClientXmlSheet;
37
import org.openconcerto.erp.generationDoc.gestcomm.AvoirFournisseurXmlSheet;
38
import org.openconcerto.erp.generationDoc.gestcomm.CommandeXmlSheet;
39
import org.openconcerto.erp.generationDoc.gestcomm.CourrierClientSheet;
40
import org.openconcerto.erp.generationDoc.gestcomm.EtatVentesXmlSheet;
41
import org.openconcerto.erp.generationDoc.gestcomm.FicheRelanceSheet;
42
import org.openconcerto.erp.generationDoc.gestcomm.PointageXmlSheet;
43
import org.openconcerto.erp.generationDoc.gestcomm.RelanceSheet;
44
import org.openconcerto.erp.generationDoc.gestcomm.ReleveChequeEmisSheet;
45
import org.openconcerto.erp.generationDoc.gestcomm.ReleveChequeSheet;
18 ilm 46
import org.openconcerto.sql.Configuration;
47
import org.openconcerto.sql.model.SQLRow;
48
import org.openconcerto.task.config.ComptaBasePropsConfiguration;
49
import org.openconcerto.ui.preferences.TemplateProps;
50
import org.openconcerto.utils.StreamUtils;
51
 
52
import java.io.File;
53
import java.io.IOException;
54
import java.io.InputStream;
55
 
56
import javax.swing.JOptionPane;
57
 
58
public class TemplateNXProps extends TemplateProps {
59
 
60
    private static final String societeBaseName = ((ComptaPropsConfiguration) Configuration.getInstance()).getSocieteBaseName();
61
 
62
    @Override
63
    protected String getPropsFileName() {
64
        final File f = Configuration.getInstance().getConfDir();
65
        final File f2 = new File(f, "Configuration" + File.separator + "Template.properties");
66
 
67
        if (!f2.exists()) {
68
            final InputStream fConf = ComptaBasePropsConfiguration.getStreamStatic("/Configuration/Template.properties");
69
            if (fConf == null) {
70
                JOptionPane.showMessageDialog(null, "L'emplacement des modéles n'est pas défini.");
71
            } else {
72
                try {
73
                    StreamUtils.copy(fConf, f2);
74
                    fConf.close();
75
                } catch (IOException e) {
76
                    JOptionPane.showMessageDialog(null,
77
                            "Impossible de copier le fichier de configuration de l'emplacement des modéles\ndepuis le serveur, veuillez définir l'emplacement des modéles manuellement.");
78
                    e.printStackTrace();
79
                }
80
            }
81
        }
82
 
83
        if (f2.exists()) {
84
            return f2.getAbsolutePath();
85
        } else {
86
            try {
87
                f2.getParentFile().mkdirs();
88
                f2.createNewFile();
89
            } catch (IOException e) {
90
                System.err.println(f2);
91
                e.printStackTrace();
92
            }
93
            return f2.getAbsolutePath();
94
        }
95
    }
96
 
97
    @Override
98
    public String getPropertySuffix() {
99
 
100
        return societeBaseName;
101
    }
102
 
103
    @Override
104
    public String getDefaultStringValue() {
19 ilm 105
 
18 ilm 106
        final Configuration conf = ComptaPropsConfiguration.getInstance();
107
        final SQLRow rowSociete = ((ComptaPropsConfiguration) conf).getRowSociete();
108
        return conf.getWD().getAbsolutePath() + File.separator + rowSociete.getString("NOM") + "-" + rowSociete.getID();
109
    }
110
 
25 ilm 111
    public void initDocumentLocalStorage() {
112
        final DocumentLocalStorageManager storage = DocumentLocalStorageManager.getInstance();
113
        String propertyDefaultDirectory = getProperty(SheetXml.DEFAULT_PROPERTY_NAME + "OO");
114
        if (propertyDefaultDirectory == null) {
115
            System.out.println("Warning: no default directory stored for document output");
116
            propertyDefaultDirectory = getDefaultStringValue();
117
        }
118
        storage.setDocumentDefaultDirectory(new File(propertyDefaultDirectory));
119
        String propertyDefaultPDFDirectory = getProperty(SheetXml.DEFAULT_PROPERTY_NAME + "PDF");
120
        if (propertyDefaultPDFDirectory == null) {
121
            System.out.println("Warning: no default directory stored for PFD output");
122
            propertyDefaultPDFDirectory = propertyDefaultDirectory;
123
        }
124
 
125
        storage.setPDFDefaultDirectory(new File(propertyDefaultPDFDirectory));
126
        register(DevisXmlSheet.TEMPLATE_ID, DevisXmlSheet.TEMPLATE_PROPERTY_NAME);
127
        register(VenteFactureXmlSheet.TEMPLATE_ID, VenteFactureXmlSheet.TEMPLATE_PROPERTY_NAME);
128
        register(CommandeClientXmlSheet.TEMPLATE_ID, CommandeClientXmlSheet.TEMPLATE_PROPERTY_NAME);
129
        register(BonLivraisonXmlSheet.TEMPLATE_ID, BonLivraisonXmlSheet.TEMPLATE_PROPERTY_NAME);
130
        register(AvoirClientXmlSheet.TEMPLATE_ID, AvoirClientXmlSheet.TEMPLATE_PROPERTY_NAME);
131
        register(AvoirFournisseurXmlSheet.TEMPLATE_ID, AvoirFournisseurXmlSheet.TEMPLATE_PROPERTY_NAME);
132
        register(CommandeXmlSheet.TEMPLATE_ID, CommandeXmlSheet.TEMPLATE_PROPERTY_NAME);
133
        register(EtatVentesXmlSheet.TEMPLATE_ID, EtatVentesXmlSheet.TEMPLATE_PROPERTY_NAME);
134
        register(FicheClientXmlSheet.TEMPLATE_ID, FicheClientXmlSheet.TEMPLATE_PROPERTY_NAME);
135
        register(FicheRelanceSheet.TEMPLATE_ID, FicheRelanceSheet.TEMPLATE_PROPERTY_NAME);
136
        register(ReleveChequeSheet.TEMPLATE_ID, ReleveChequeSheet.TEMPLATE_PROPERTY_NAME);
137
        register(ListeFactureXmlSheet.TEMPLATE_ID, ListeFactureXmlSheet.TEMPLATE_PROPERTY_NAME);
138
        register(ListeVenteXmlSheet.TEMPLATE_ID, ListeVenteXmlSheet.TEMPLATE_PROPERTY_NAME);
139
        register(BalanceSheet.TEMPLATE_ID, BalanceSheet.TEMPLATE_PROPERTY_NAME);
140
        register(GrandLivreSheet.TEMPLATE_ID, GrandLivreSheet.TEMPLATE_PROPERTY_NAME);
141
        register(JournauxSheet.TEMPLATE_ID, JournauxSheet.TEMPLATE_PROPERTY_NAME);
142
        register(EtatChargesPayeSheet.TEMPLATE_ID, EtatChargesPayeSheet.TEMPLATE_PROPERTY_NAME);
143
        register(FichePayeSheet.TEMPLATE_ID, FichePayeSheet.TEMPLATE_PROPERTY_NAME);
144
        register(LivrePayeSheet.TEMPLATE_ID, LivrePayeSheet.TEMPLATE_PROPERTY_NAME);
145
        register(CourrierClientSheet.TEMPLATE_ID, CourrierClientSheet.TEMPLATE_PROPERTY_NAME);
146
        register(PointageXmlSheet.TEMPLATE_ID, PointageXmlSheet.TEMPLATE_PROPERTY_NAME);
147
        register(RelanceSheet.TEMPLATE_ID, RelanceSheet.TEMPLATE_PROPERTY_NAME);
148
        register(VenteComptoirSheet.TEMPLATE_ID, VenteComptoirSheet.TEMPLATE_PROPERTY_NAME);
149
        register(ReleveChequeEmisSheet.TEMPLATE_ID, ReleveChequeEmisSheet.TEMPLATE_PROPERTY_NAME);
150
        storage.dump();
151
 
152
    }
153
 
154
    private void register(String templateId, String propertyBaseName) {
155
        if (templateId == null) {
156
            throw new IllegalArgumentException("null template id");
157
        }
158
        if (propertyBaseName == null) {
159
            throw new IllegalArgumentException("null propertyBaseName");
160
        }
161
        if (TemplateManager.getInstance().isKnwonTemplate(templateId)) {
162
            System.err.println("Warning: registering known template id : " + templateId + " with property base name: " + propertyBaseName);
163
        }
164
        final DocumentLocalStorageManager storage = DocumentLocalStorageManager.getInstance();
165
        final String propertyOO = getProperty(propertyBaseName + "OO");
166
        if (propertyOO != null) {
167
 
168
            storage.addDocumentDirectory(templateId, new File(propertyOO));
169
        }
170
        final String propertyPDF = getProperty(propertyBaseName + "PDF");
171
        if (propertyPDF != null) {
172
            storage.addPDFDirectory(templateId, new File(propertyPDF));
173
        }
174
    }
175
 
18 ilm 176
    synchronized public static TemplateProps getInstance() {
177
        if (instance == null) {
178
            instance = new TemplateNXProps();
25 ilm 179
            ((TemplateNXProps) instance).initDocumentLocalStorage();
180
            ((TemplateNXProps) instance).initDefaulTemplateProvider();
18 ilm 181
        }
182
        return instance;
183
    }
25 ilm 184
 
185
    private void initDefaulTemplateProvider() {
186
        final String property = getProperty("LocationTemplate");
187
        final DefaultLocalTemplateProvider provider = new DefaultLocalTemplateProvider();
188
        if (property != null) {
189
            provider.setBaseDirectory(new File(property));
190
        }
191
        TemplateManager.getInstance().setDefaultProvider(provider);
192
        TemplateManager.getInstance().dump();
193
    }
18 ilm 194
}