OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 73 | Rev 91 | 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;
65 ilm 20
import org.openconcerto.erp.core.finance.accounting.report.JournauxSheetXML;
25 ilm 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.ListeVenteXmlSheet;
25
import org.openconcerto.erp.core.sales.invoice.report.VenteComptoirSheet;
26
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureXmlSheet;
27
import org.openconcerto.erp.core.sales.order.report.CommandeClientXmlSheet;
28
import org.openconcerto.erp.core.sales.quote.report.DevisXmlSheet;
29
import org.openconcerto.erp.core.sales.shipment.report.BonLivraisonXmlSheet;
61 ilm 30
import org.openconcerto.erp.generationDoc.AbstractLocalTemplateProvider;
31
import org.openconcerto.erp.generationDoc.DefaultCloudTemplateProvider;
25 ilm 32
import org.openconcerto.erp.generationDoc.DefaultLocalTemplateProvider;
33
import org.openconcerto.erp.generationDoc.DocumentLocalStorageManager;
34
import org.openconcerto.erp.generationDoc.SheetXml;
35
import org.openconcerto.erp.generationDoc.TemplateManager;
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");
61 ilm 69
            if (fConf != null) {
18 ilm 70
                try {
71
                    StreamUtils.copy(fConf, f2);
72
                    fConf.close();
73
                } catch (IOException e) {
74
                    JOptionPane.showMessageDialog(null,
75
                            "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.");
76
                    e.printStackTrace();
77
                }
78
            }
79
        }
80
 
81
        if (f2.exists()) {
82
            return f2.getAbsolutePath();
83
        } else {
84
            try {
85
                f2.getParentFile().mkdirs();
86
                f2.createNewFile();
87
            } catch (IOException e) {
88
                System.err.println(f2);
89
                e.printStackTrace();
90
            }
91
            return f2.getAbsolutePath();
92
        }
93
    }
94
 
95
    @Override
96
    public String getPropertySuffix() {
97
        return societeBaseName;
98
    }
99
 
100
    @Override
101
    public String getDefaultStringValue() {
19 ilm 102
 
18 ilm 103
        final Configuration conf = ComptaPropsConfiguration.getInstance();
104
        final SQLRow rowSociete = ((ComptaPropsConfiguration) conf).getRowSociete();
105
        return conf.getWD().getAbsolutePath() + File.separator + rowSociete.getString("NOM") + "-" + rowSociete.getID();
106
    }
107
 
25 ilm 108
    public void initDocumentLocalStorage() {
109
        final DocumentLocalStorageManager storage = DocumentLocalStorageManager.getInstance();
110
        String propertyDefaultDirectory = getProperty(SheetXml.DEFAULT_PROPERTY_NAME + "OO");
111
        if (propertyDefaultDirectory == null) {
112
            System.out.println("Warning: no default directory stored for document output");
113
            propertyDefaultDirectory = getDefaultStringValue();
114
        }
115
        storage.setDocumentDefaultDirectory(new File(propertyDefaultDirectory));
116
        String propertyDefaultPDFDirectory = getProperty(SheetXml.DEFAULT_PROPERTY_NAME + "PDF");
117
        if (propertyDefaultPDFDirectory == null) {
118
            System.out.println("Warning: no default directory stored for PFD output");
119
            propertyDefaultPDFDirectory = propertyDefaultDirectory;
120
        }
121
 
122
        storage.setPDFDefaultDirectory(new File(propertyDefaultPDFDirectory));
83 ilm 123
 
124
 
125
            register(DevisXmlSheet.TEMPLATE_ID, DevisXmlSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("DEVIS"));
126
            register(VenteFactureXmlSheet.TEMPLATE_ID, VenteFactureXmlSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("SAISIE_VENTE_FACTURE"));
127
            register(CommandeClientXmlSheet.TEMPLATE_ID, CommandeClientXmlSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("COMMANDE_CLIENT"));
128
            register(BonLivraisonXmlSheet.TEMPLATE_ID, BonLivraisonXmlSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("BON_DE_LIVRAISON"));
129
            register(AvoirClientXmlSheet.TEMPLATE_ID, AvoirClientXmlSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("AVOIR_CLIENT"));
130
            register(AvoirFournisseurXmlSheet.TEMPLATE_ID, AvoirFournisseurXmlSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("AVOIR_FOURNISSEUR"));
131
            register(CommandeXmlSheet.TEMPLATE_ID, CommandeXmlSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("COMMANDE"));
132
            register(EtatVentesXmlSheet.TEMPLATE_ID, EtatVentesXmlSheet.TEMPLATE_PROPERTY_NAME, null);
133
            register(FicheClientXmlSheet.TEMPLATE_ID, FicheClientXmlSheet.TEMPLATE_PROPERTY_NAME, null);
134
            register(FicheRelanceSheet.TEMPLATE_ID, FicheRelanceSheet.TEMPLATE_PROPERTY_NAME, null);
135
            register(ReleveChequeSheet.TEMPLATE_ID, ReleveChequeSheet.TEMPLATE_PROPERTY_NAME, null);
136
            register(ListeVenteXmlSheet.TEMPLATE_ID, ListeVenteXmlSheet.TEMPLATE_PROPERTY_NAME, null);
137
            register(BalanceSheet.TEMPLATE_ID, BalanceSheet.TEMPLATE_PROPERTY_NAME, BalanceSheet.TEMPLATE_ID);
138
            register(GrandLivreSheet.TEMPLATE_ID, GrandLivreSheet.TEMPLATE_PROPERTY_NAME, GrandLivreSheet.TEMPLATE_ID);
139
            register(JournauxSheetXML.TEMPLATE_ID, JournauxSheetXML.TEMPLATE_PROPERTY_NAME, JournauxSheetXML.TEMPLATE_ID);
140
            register(EtatChargesPayeSheet.TEMPLATE_ID, EtatChargesPayeSheet.TEMPLATE_PROPERTY_NAME, "Etat des charges");
141
            register(FichePayeSheet.TEMPLATE_ID, FichePayeSheet.TEMPLATE_PROPERTY_NAME, "Fiche paye");
142
            register(LivrePayeSheet.TEMPLATE_ID, LivrePayeSheet.TEMPLATE_PROPERTY_NAME, "Livre paye");
143
            register(CourrierClientSheet.TEMPLATE_ID, CourrierClientSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("COMMANDE"));
144
            register(PointageXmlSheet.TEMPLATE_ID, PointageXmlSheet.TEMPLATE_PROPERTY_NAME, null);
145
            register(RelanceSheet.TEMPLATE_ID, RelanceSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("RELANCE"));
146
            register(VenteComptoirSheet.TEMPLATE_ID, VenteComptoirSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("SAISIE_VENTE_COMPTOIR"));
147
            register(ReleveChequeEmisSheet.TEMPLATE_ID, ReleveChequeEmisSheet.TEMPLATE_PROPERTY_NAME, null);
25 ilm 148
        storage.dump();
149
 
150
    }
151
 
83 ilm 152
    public void preventecRegister(String path, String templateId, String defaultSubFolder) {
153
        if (templateId == null) {
154
            throw new IllegalArgumentException("null template id");
155
        }
156
 
157
        if (TemplateManager.getInstance().isKnwonTemplate(templateId)) {
158
            System.err.println("Warning: registering known template id : " + templateId);
159
        }
160
        final DocumentLocalStorageManager storage = DocumentLocalStorageManager.getInstance();
161
        if (defaultSubFolder != null) {
162
            storage.addDocumentDirectory(templateId, new File(path, defaultSubFolder));
163
            storage.addPDFDirectory(templateId, new File(path, defaultSubFolder));
164
        } else {
165
            storage.addDocumentDirectory(templateId, new File(path));
166
            storage.addPDFDirectory(templateId, new File(path));
167
        }
168
    }
169
 
61 ilm 170
    public void register(String templateId, String propertyBaseName, String defaultSubFolder) {
25 ilm 171
        if (templateId == null) {
172
            throw new IllegalArgumentException("null template id");
173
        }
174
        if (propertyBaseName == null) {
175
            throw new IllegalArgumentException("null propertyBaseName");
176
        }
177
        if (TemplateManager.getInstance().isKnwonTemplate(templateId)) {
178
            System.err.println("Warning: registering known template id : " + templateId + " with property base name: " + propertyBaseName);
179
        }
180
        final DocumentLocalStorageManager storage = DocumentLocalStorageManager.getInstance();
181
        final String propertyOO = getProperty(propertyBaseName + "OO");
182
        if (propertyOO != null) {
183
            storage.addDocumentDirectory(templateId, new File(propertyOO));
28 ilm 184
        } else {
185
            if (defaultSubFolder != null && defaultSubFolder.trim().length() > 0) {
186
                storage.addDocumentDirectory(templateId, new File(storage.getDocumentOutputDirectory("default"), defaultSubFolder));
187
            }
25 ilm 188
        }
189
        final String propertyPDF = getProperty(propertyBaseName + "PDF");
190
        if (propertyPDF != null) {
191
            storage.addPDFDirectory(templateId, new File(propertyPDF));
28 ilm 192
        } else {
193
            if (defaultSubFolder != null && defaultSubFolder.trim().length() > 0) {
194
                storage.addDocumentDirectory(templateId, new File(storage.getPDFOutputDirectory("default"), defaultSubFolder));
195
            }
25 ilm 196
        }
197
    }
198
 
18 ilm 199
    synchronized public static TemplateProps getInstance() {
200
        if (instance == null) {
201
            instance = new TemplateNXProps();
25 ilm 202
            ((TemplateNXProps) instance).initDocumentLocalStorage();
203
            ((TemplateNXProps) instance).initDefaulTemplateProvider();
18 ilm 204
        }
205
        return instance;
206
    }
25 ilm 207
 
208
    private void initDefaulTemplateProvider() {
209
        final String property = getProperty("LocationTemplate");
61 ilm 210
        final AbstractLocalTemplateProvider provider;
211
 
212
        final ComptaPropsConfiguration configuration = (ComptaPropsConfiguration) ComptaPropsConfiguration.getInstance();
213
        if (!configuration.isOnCloud()) {
214
            provider = new DefaultLocalTemplateProvider();
215
            if (property != null) {
83 ilm 216
                File storage = new File(property);
217
                ((DefaultLocalTemplateProvider) provider).setBaseDirectory(storage);
61 ilm 218
            }
219
        } else {
220
            provider = new DefaultCloudTemplateProvider(configuration.getSocieteID());
25 ilm 221
        }
61 ilm 222
 
25 ilm 223
        TemplateManager.getInstance().setDefaultProvider(provider);
224
        TemplateManager.getInstance().dump();
225
    }
18 ilm 226
}