OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.erp.preferences;

import org.openconcerto.erp.config.ComptaPropsConfiguration;
import org.openconcerto.erp.core.customerrelationship.customer.report.FicheClientXmlSheet;
import org.openconcerto.erp.core.edm.AttachmentSQLElement;
import org.openconcerto.erp.core.finance.accounting.report.BalanceSheet;
import org.openconcerto.erp.core.finance.accounting.report.GrandLivreSheetXML;
import org.openconcerto.erp.core.finance.accounting.report.JournauxSheetXML;
import org.openconcerto.erp.core.humanresources.payroll.report.EtatChargesPayeSheet;
import org.openconcerto.erp.core.humanresources.payroll.report.FichePayeSheetXML;
import org.openconcerto.erp.core.humanresources.payroll.report.LivrePayeSheet;
import org.openconcerto.erp.core.sales.invoice.report.ListeVenteXmlSheet;
import org.openconcerto.erp.core.sales.invoice.report.VenteComptoirSheet;
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureXmlSheet;
import org.openconcerto.erp.core.sales.order.report.CommandeClientXmlSheet;
import org.openconcerto.erp.core.sales.quote.report.DevisXmlSheet;
import org.openconcerto.erp.core.sales.shipment.report.BonLivraisonXmlSheet;
import org.openconcerto.erp.core.supplychain.order.element.DemandePrixSheetXML;
import org.openconcerto.erp.generationDoc.AbstractLocalTemplateProvider;
import org.openconcerto.erp.generationDoc.DefaultCloudTemplateProvider;
import org.openconcerto.erp.generationDoc.DefaultLocalTemplateProvider;
import org.openconcerto.erp.generationDoc.DocumentLocalStorageManager;
import org.openconcerto.erp.generationDoc.SheetXml;
import org.openconcerto.erp.generationDoc.TemplateManager;
import org.openconcerto.erp.generationDoc.gestcomm.AvoirClientXmlSheet;
import org.openconcerto.erp.generationDoc.gestcomm.AvoirFournisseurXmlSheet;
import org.openconcerto.erp.generationDoc.gestcomm.BonReceptionXmlSheet;
import org.openconcerto.erp.generationDoc.gestcomm.CommandeXmlSheet;
import org.openconcerto.erp.generationDoc.gestcomm.CourrierClientSheet;
import org.openconcerto.erp.generationDoc.gestcomm.EtatVentesXmlSheet;
import org.openconcerto.erp.generationDoc.gestcomm.FactureFournisseurXmlSheet;
import org.openconcerto.erp.generationDoc.gestcomm.FicheArticleXmlSheet;
import org.openconcerto.erp.generationDoc.gestcomm.FicheRelanceSheet;
import org.openconcerto.erp.generationDoc.gestcomm.PointageXmlSheet;
import org.openconcerto.erp.generationDoc.gestcomm.RelanceSheet;
import org.openconcerto.erp.generationDoc.gestcomm.ReleveChequeEmisSheet;
import org.openconcerto.erp.generationDoc.gestcomm.ReleveChequeSheet;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.task.config.ComptaBasePropsConfiguration;
import org.openconcerto.ui.preferences.TemplateProps;
import org.openconcerto.utils.StreamUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.JOptionPane;

public class TemplateNXProps extends TemplateProps {

    private static final String societeBaseName = ((ComptaPropsConfiguration) Configuration.getInstance()).getSocieteBaseName();

    @Override
    protected String getPropsFileName() {
        final File f = Configuration.getInstance().getConfDir();
        final File f2 = new File(f, "Configuration" + File.separator + "Template.properties");

        if (!f2.exists()) {
            final InputStream fConf = ComptaBasePropsConfiguration.getStreamStatic("/Configuration/Template.properties");
            if (fConf != null) {
                try {
                    StreamUtils.copy(fConf, f2);
                    fConf.close();
                } catch (IOException e) {
                    JOptionPane.showMessageDialog(null,
                            "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.");
                    e.printStackTrace();
                }
            }
        }

        if (f2.exists()) {
            return f2.getAbsolutePath();
        } else {
            try {
                f2.getParentFile().mkdirs();
                f2.createNewFile();
            } catch (IOException e) {
                System.err.println(f2);
                e.printStackTrace();
            }
            return f2.getAbsolutePath();
        }
    }

    @Override
    public String getPropertySuffix() {
        return societeBaseName;
    }

    @Override
    public String getDefaultStringValue() {

        final Configuration conf = ComptaPropsConfiguration.getInstance();
        final SQLRow rowSociete = ((ComptaPropsConfiguration) conf).getRowSociete();
        return conf.getWD().getAbsolutePath() + File.separator + rowSociete.getString("NOM") + "-" + rowSociete.getID();
    }

    public void initDocumentLocalStorage() {
        final DocumentLocalStorageManager storage = DocumentLocalStorageManager.getInstance();
        String propertyDefaultDirectory = getProperty(SheetXml.DEFAULT_PROPERTY_NAME + "OO");
        if (propertyDefaultDirectory == null) {
            System.out.println("Warning: no default directory stored for document output");
            propertyDefaultDirectory = getDefaultStringValue();
        }
        storage.setDocumentDefaultDirectory(new File(propertyDefaultDirectory));
        String propertyDefaultPDFDirectory = getProperty(SheetXml.DEFAULT_PROPERTY_NAME + "PDF");
        if (propertyDefaultPDFDirectory == null) {
            System.out.println("Warning: no default directory stored for PFD output");
            propertyDefaultPDFDirectory = propertyDefaultDirectory;
        }

        final String propertyAtt = getProperty(AttachmentSQLElement.DIRECTORY_PREFS);
        if (propertyAtt != null && propertyAtt.trim().length() > 0) {
            storage.addDocumentDirectory(AttachmentSQLElement.DIRECTORY_PREFS, new File(propertyAtt));
        }

        storage.setPDFDefaultDirectory(new File(propertyDefaultPDFDirectory));

            register(DemandePrixSheetXML.TEMPLATE_ID, DemandePrixSheetXML.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("DEMANDE_PRIX"));
            register(DevisXmlSheet.TEMPLATE_ID, DevisXmlSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("DEVIS"));
            register(VenteFactureXmlSheet.TEMPLATE_ID, VenteFactureXmlSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("SAISIE_VENTE_FACTURE"));
            register(CommandeClientXmlSheet.TEMPLATE_ID, CommandeClientXmlSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("COMMANDE_CLIENT"));
            register(BonLivraisonXmlSheet.TEMPLATE_ID, BonLivraisonXmlSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("BON_DE_LIVRAISON"));
            register(AvoirClientXmlSheet.TEMPLATE_ID, AvoirClientXmlSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("AVOIR_CLIENT"));
            register(AvoirFournisseurXmlSheet.TEMPLATE_ID, AvoirFournisseurXmlSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("AVOIR_FOURNISSEUR"));
            register(BonReceptionXmlSheet.TEMPLATE_ID, BonReceptionXmlSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("BON_RECEPTION"));
            register(FactureFournisseurXmlSheet.TEMPLATE_ID, FactureFournisseurXmlSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("FACTURE_FOURNISSEUR"));
            register(CommandeXmlSheet.TEMPLATE_ID, CommandeXmlSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("COMMANDE"));
            register(FicheArticleXmlSheet.TEMPLATE_ID, FicheArticleXmlSheet.TEMPLATE_PROPERTY_NAME, "Fiche article");
            register(EtatVentesXmlSheet.TEMPLATE_ID, EtatVentesXmlSheet.TEMPLATE_PROPERTY_NAME, null);
            register(FicheClientXmlSheet.TEMPLATE_ID, FicheClientXmlSheet.TEMPLATE_PROPERTY_NAME, "Fiche client");
            register(FicheRelanceSheet.TEMPLATE_ID, FicheRelanceSheet.TEMPLATE_PROPERTY_NAME, null);
            register(ReleveChequeSheet.TEMPLATE_ID, ReleveChequeSheet.TEMPLATE_PROPERTY_NAME, null);
            register(ListeVenteXmlSheet.TEMPLATE_ID, ListeVenteXmlSheet.TEMPLATE_PROPERTY_NAME, null);
            register(BalanceSheet.TEMPLATE_ID, BalanceSheet.TEMPLATE_PROPERTY_NAME, BalanceSheet.TEMPLATE_ID);
            register(GrandLivreSheetXML.TEMPLATE_ID, GrandLivreSheetXML.TEMPLATE_PROPERTY_NAME, GrandLivreSheetXML.TEMPLATE_ID);
            register(JournauxSheetXML.TEMPLATE_ID, JournauxSheetXML.TEMPLATE_PROPERTY_NAME, JournauxSheetXML.TEMPLATE_ID);
            register(EtatChargesPayeSheet.TEMPLATE_ID, EtatChargesPayeSheet.TEMPLATE_PROPERTY_NAME, "Etat des charges");
            register(FichePayeSheetXML.TEMPLATE_ID, FichePayeSheetXML.TEMPLATE_PROPERTY_NAME, "Fiche paye");
            register(LivrePayeSheet.TEMPLATE_ID, LivrePayeSheet.TEMPLATE_PROPERTY_NAME, "Livre paye");
            register(CourrierClientSheet.TEMPLATE_ID, CourrierClientSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("COMMANDE"));
            register(PointageXmlSheet.TEMPLATE_ID, PointageXmlSheet.TEMPLATE_PROPERTY_NAME, null);
            register(RelanceSheet.TEMPLATE_ID, RelanceSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("RELANCE"));
            register(VenteComptoirSheet.TEMPLATE_ID, VenteComptoirSheet.TEMPLATE_PROPERTY_NAME, AbstractGenerationDocumentPreferencePanel.getLabelFromTable("SAISIE_VENTE_COMPTOIR"));
            register(ReleveChequeEmisSheet.TEMPLATE_ID, ReleveChequeEmisSheet.TEMPLATE_PROPERTY_NAME, null);
        storage.dump();

    }

    public void preventecRegister(String path, String templateId, String defaultSubFolder) {
        if (templateId == null) {
            throw new IllegalArgumentException("null template id");
        }

        if (TemplateManager.getInstance().isKnwonTemplate(templateId)) {
            System.err.println("Warning: registering known template id : " + templateId);
        }
        final DocumentLocalStorageManager storage = DocumentLocalStorageManager.getInstance();
        if (defaultSubFolder != null) {
            storage.addDocumentDirectory(templateId, new File(path, defaultSubFolder));
            storage.addPDFDirectory(templateId, new File(path, defaultSubFolder));
        } else {
            storage.addDocumentDirectory(templateId, new File(path));
            storage.addPDFDirectory(templateId, new File(path));
        }
    }

    public void register(String templateId, String propertyBaseName, String defaultSubFolder) {
        if (templateId == null) {
            throw new IllegalArgumentException("null template id");
        }
        if (propertyBaseName == null) {
            throw new IllegalArgumentException("null propertyBaseName");
        }
        if (TemplateManager.getInstance().isKnwonTemplate(templateId)) {
            System.err.println("Warning: registering known template id : " + templateId + " with property base name: " + propertyBaseName);
        }
        final DocumentLocalStorageManager storage = DocumentLocalStorageManager.getInstance();

        final String propertyOO = getProperty(propertyBaseName + "OO");
        if (propertyOO != null) {
            storage.addDocumentDirectory(templateId, new File(propertyOO));
        } else {
            if (defaultSubFolder != null && defaultSubFolder.trim().length() > 0) {
                storage.addDocumentDirectory(templateId, new File(storage.getDocumentOutputDirectory("default"), defaultSubFolder));
            }
        }
        final String propertyPDF = getProperty(propertyBaseName + "PDF");
        if (propertyPDF != null) {
            storage.addPDFDirectory(templateId, new File(propertyPDF));
        } else {
            if (defaultSubFolder != null && defaultSubFolder.trim().length() > 0) {
                storage.addDocumentDirectory(templateId, new File(storage.getPDFOutputDirectory("default"), defaultSubFolder));
            }
        }
    }

    synchronized public static TemplateProps getInstance() {
        if (instance == null) {
            instance = new TemplateNXProps();
            ((TemplateNXProps) instance).initDocumentLocalStorage();
            ((TemplateNXProps) instance).initDefaulTemplateProvider();
        }
        return instance;
    }

    private void initDefaulTemplateProvider() {
        final String property = getProperty("LocationTemplate");
        final AbstractLocalTemplateProvider provider;

        final ComptaPropsConfiguration configuration = (ComptaPropsConfiguration) ComptaPropsConfiguration.getInstance();
        if (!configuration.isOnCloud()) {
            provider = new DefaultLocalTemplateProvider();
            if (property != null) {
                File storage = new File(property);
                ((DefaultLocalTemplateProvider) provider).setBaseDirectory(storage);
            }
        } else {
            provider = new DefaultCloudTemplateProvider(configuration.getSocieteID());
        }

        TemplateManager.getInstance().setDefaultProvider(provider);
        TemplateManager.getInstance().dump();
    }
}