OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | Go to most recent revision | 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.core.finance.payment.element;

import org.openconcerto.erp.config.ComptaPropsConfiguration;
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
import org.openconcerto.sql.element.SQLComponent;
import org.openconcerto.sql.element.SQLElementLink.LinkType;
import org.openconcerto.sql.element.SQLElementLinksSetup;
import org.openconcerto.sql.element.UISQLComponent;
import org.openconcerto.sql.model.DBRoot;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.utils.SQLCreateTable;
import org.openconcerto.ui.component.ITextCombo;
import org.openconcerto.ui.component.ImmutableITextComboCache;
import org.openconcerto.utils.CollectionUtils;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;

public final class SEPAMandateSQLElement extends ComptaSQLConfElement {
    static final String TABLE_NAME = "SEPA_MANDATE";
    static public final String SEQ_FIRST = "FRST";
    static public final String SEQ_RECURRENT = "RCUR";
    static public final String SEQ_FINAL = "FNAL";
    static public final String SEQ_ONEOFF = "OOFF";
    static public final List<String> SEQ_VALUES = Arrays.asList(SEQ_FIRST, SEQ_RECURRENT, SEQ_FINAL, SEQ_ONEOFF);
    private static final int IDENTIFICATION_MAX_LENGTH = 35;

    public static SQLCreateTable getCreateTable(final DBRoot root) {
        if (root.contains(TABLE_NAME))
            return null;
        final SQLCreateTable res = new SQLCreateTable(root, TABLE_NAME);
        res.addForeignColumn(null, root.getTable("CLIENT"));
        res.addVarCharColumn("MandateIdentification", IDENTIFICATION_MAX_LENGTH);
        res.addColumn("DateOfSignature", "date", null, true);
        res.addVarCharColumn("SequenceType", 8);
        res.addBooleanColumn("ACTIVE", Boolean.TRUE, false);
        return res;
    }

    public SEPAMandateSQLElement(final ComptaPropsConfiguration conf) {
        super(conf.getRootSociete().findTable(TABLE_NAME, true));
    }

    @Override
    protected void setupLinks(SQLElementLinksSetup links) {
        super.setupLinks(links);
        links.get("ID_CLIENT").setType(LinkType.PARENT);
    }

    @Override
    protected List<String> getListFields() {
        final List<String> l = new ArrayList<String>();
        l.add("ID_CLIENT");
        l.add("MandateIdentification");
        l.add("DateOfSignature");
        l.add("SequenceType");
        l.add("ACTIVE");
        return l;
    }

    @Override
    protected List<String> getComboFields() {
        final List<String> l = new ArrayList<String>();
        l.add("MandateIdentification");
        l.add("DateOfSignature");
        l.add("SequenceType");
        return l;
    }

    @Override
    public Set<String> getReadOnlyFields() {
        return CollectionUtils.createSet("ID_CLIENT", "MandateIdentification");
    }

    @Override
    protected SQLComponent createComponent() {
        return new UISQLComponent(this) {
            @Override
            protected void addViews() {
                this.addView("ID_CLIENT");
                this.addView("MandateIdentification");
                this.addView("DateOfSignature");
                final ITextCombo seqTypeCombo = new ITextCombo(true);
                seqTypeCombo.initCache(new ImmutableITextComboCache(SEQ_VALUES));
                this.addView(seqTypeCombo, "SequenceType");
                this.addView("ACTIVE");
            }
        };
    }

    @Override
    protected String createCode() {
        // TODO rename createCodeFromPackage() to createCodeOfPackage() and change createCode()
        // implementation to use a new createCodeFromPackage() which uses the class name (w/o
        // SQLElement suffix)
        return this.createCodeOfPackage() + ".SEPAMandate";
    }

    public final SQLRowValues createRecurrent(final Number idClient, final String mandateIdent, final Date dateOfSignature) {
        final SQLRowValues res = new SQLRowValues(getTable());
        res.put("ID_CLIENT", Objects.requireNonNull(idClient));
        if (mandateIdent.length() > IDENTIFICATION_MAX_LENGTH)
            throw new IllegalArgumentException("Identification too long (>" + IDENTIFICATION_MAX_LENGTH + ") : " + mandateIdent);
        res.put("MandateIdentification", mandateIdent);
        res.put("DateOfSignature", Objects.requireNonNull(dateOfSignature));
        res.put("SequenceType", SEPAMandateSQLElement.SEQ_FIRST);
        res.put("ACTIVE", Boolean.TRUE);
        return res;
    }

    public String generateMandateIdentification(String label, final char pad, final boolean padStart, final boolean truncateStart) {
        final UUID uuid = UUID.randomUUID();
        final ByteBuffer byteBuffer = ByteBuffer.allocate(16);
        byteBuffer.putLong(uuid.getMostSignificantBits());
        byteBuffer.putLong(uuid.getLeastSignificantBits());
        // slash should be legal but don't take any chances
        final String uuidS = Base64.getEncoder().withoutPadding().encodeToString(byteBuffer.array()).replace('/', '.');
        assert uuidS.length() == 22;

        // don't add space
        if (Character.isSpaceChar(pad))
            throw new IllegalArgumentException("Invalid pad : spaces can be hard to debug : '" + pad + "'");
        // don't accept space
        label = label.trim();
        final int numberL = 10;
        final int zeroesToAdd = numberL - label.length();
        final String fixedLNumber;
        if (zeroesToAdd == 0) {
            fixedLNumber = label;
        } else if (zeroesToAdd > 0) {
            final StringBuilder sb = new StringBuilder(numberL);
            if (!padStart)
                sb.append(label);
            for (int i = 0; i < zeroesToAdd; i++) {
                sb.append(pad);
            }
            if (padStart)
                sb.append(label);
            fixedLNumber = sb.toString();
        } else {
            fixedLNumber = truncateStart ? label.substring(-zeroesToAdd, label.length()) : label.substring(0, numberL);
        }
        assert fixedLNumber.length() == numberL;

        // use both UUID and a meaningful label
        final String res = uuidS + '-' + fixedLNumber;
        assert res.length() <= IDENTIFICATION_MAX_LENGTH;
        return res;
    }
}