OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
151 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.core.finance.payment.element;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
18
import org.openconcerto.sql.element.SQLComponent;
19
import org.openconcerto.sql.element.SQLElementLink.LinkType;
20
import org.openconcerto.sql.element.SQLElementLinksSetup;
21
import org.openconcerto.sql.element.UISQLComponent;
22
import org.openconcerto.sql.model.DBRoot;
23
import org.openconcerto.sql.model.SQLRowValues;
24
import org.openconcerto.sql.utils.SQLCreateTable;
25
import org.openconcerto.ui.component.ITextCombo;
26
import org.openconcerto.ui.component.ImmutableITextComboCache;
27
import org.openconcerto.utils.CollectionUtils;
28
 
29
import java.nio.ByteBuffer;
30
import java.util.ArrayList;
31
import java.util.Arrays;
32
import java.util.Base64;
33
import java.util.Date;
34
import java.util.List;
35
import java.util.Objects;
36
import java.util.Set;
37
import java.util.UUID;
38
 
39
public final class SEPAMandateSQLElement extends ComptaSQLConfElement {
40
    static final String TABLE_NAME = "SEPA_MANDATE";
41
    static public final String SEQ_FIRST = "FRST";
42
    static public final String SEQ_RECURRENT = "RCUR";
43
    static public final String SEQ_FINAL = "FNAL";
44
    static public final String SEQ_ONEOFF = "OOFF";
45
    static public final List<String> SEQ_VALUES = Arrays.asList(SEQ_FIRST, SEQ_RECURRENT, SEQ_FINAL, SEQ_ONEOFF);
46
    private static final int IDENTIFICATION_MAX_LENGTH = 35;
47
 
48
    public static SQLCreateTable getCreateTable(final DBRoot root) {
49
        if (root.contains(TABLE_NAME))
50
            return null;
51
        final SQLCreateTable res = new SQLCreateTable(root, TABLE_NAME);
52
        res.addForeignColumn(null, root.getTable("CLIENT"));
53
        res.addVarCharColumn("MandateIdentification", IDENTIFICATION_MAX_LENGTH);
54
        res.addColumn("DateOfSignature", "date", null, true);
55
        res.addVarCharColumn("SequenceType", 8);
56
        res.addBooleanColumn("ACTIVE", Boolean.TRUE, false);
57
        return res;
58
    }
59
 
60
    public SEPAMandateSQLElement(final ComptaPropsConfiguration conf) {
61
        super(conf.getRootSociete().findTable(TABLE_NAME, true));
62
    }
63
 
64
    @Override
65
    protected void setupLinks(SQLElementLinksSetup links) {
66
        super.setupLinks(links);
67
        links.get("ID_CLIENT").setType(LinkType.PARENT);
68
    }
69
 
70
    @Override
71
    protected List<String> getListFields() {
72
        final List<String> l = new ArrayList<String>();
73
        l.add("ID_CLIENT");
74
        l.add("MandateIdentification");
75
        l.add("DateOfSignature");
76
        l.add("SequenceType");
77
        l.add("ACTIVE");
78
        return l;
79
    }
80
 
81
    @Override
82
    protected List<String> getComboFields() {
83
        final List<String> l = new ArrayList<String>();
84
        l.add("MandateIdentification");
85
        l.add("DateOfSignature");
86
        l.add("SequenceType");
87
        return l;
88
    }
89
 
90
    @Override
91
    public Set<String> getReadOnlyFields() {
92
        return CollectionUtils.createSet("ID_CLIENT", "MandateIdentification");
93
    }
94
 
95
    @Override
96
    protected SQLComponent createComponent() {
97
        return new UISQLComponent(this) {
98
            @Override
99
            protected void addViews() {
100
                this.addView("ID_CLIENT");
101
                this.addView("MandateIdentification");
102
                this.addView("DateOfSignature");
103
                final ITextCombo seqTypeCombo = new ITextCombo(true);
104
                seqTypeCombo.initCache(new ImmutableITextComboCache(SEQ_VALUES));
105
                this.addView(seqTypeCombo, "SequenceType");
106
                this.addView("ACTIVE");
107
            }
108
        };
109
    }
110
 
111
    @Override
112
    protected String createCode() {
113
        // TODO rename createCodeFromPackage() to createCodeOfPackage() and change createCode()
114
        // implementation to use a new createCodeFromPackage() which uses the class name (w/o
115
        // SQLElement suffix)
156 ilm 116
        return this.createCodeOfPackage() + ".SEPAMandate";
151 ilm 117
    }
118
 
119
    public final SQLRowValues createRecurrent(final Number idClient, final String mandateIdent, final Date dateOfSignature) {
120
        final SQLRowValues res = new SQLRowValues(getTable());
121
        res.put("ID_CLIENT", Objects.requireNonNull(idClient));
122
        if (mandateIdent.length() > IDENTIFICATION_MAX_LENGTH)
123
            throw new IllegalArgumentException("Identification too long (>" + IDENTIFICATION_MAX_LENGTH + ") : " + mandateIdent);
124
        res.put("MandateIdentification", mandateIdent);
125
        res.put("DateOfSignature", Objects.requireNonNull(dateOfSignature));
126
        res.put("SequenceType", SEPAMandateSQLElement.SEQ_FIRST);
127
        res.put("ACTIVE", Boolean.TRUE);
128
        return res;
129
    }
130
 
131
    public String generateMandateIdentification(String label, final char pad, final boolean padStart, final boolean truncateStart) {
132
        final UUID uuid = UUID.randomUUID();
133
        final ByteBuffer byteBuffer = ByteBuffer.allocate(16);
134
        byteBuffer.putLong(uuid.getMostSignificantBits());
135
        byteBuffer.putLong(uuid.getLeastSignificantBits());
136
        // slash should be legal but don't take any chances
137
        final String uuidS = Base64.getEncoder().withoutPadding().encodeToString(byteBuffer.array()).replace('/', '.');
138
        assert uuidS.length() == 22;
139
 
140
        // don't add space
141
        if (Character.isSpaceChar(pad))
142
            throw new IllegalArgumentException("Invalid pad : spaces can be hard to debug : '" + pad + "'");
143
        // don't accept space
144
        label = label.trim();
145
        final int numberL = 10;
146
        final int zeroesToAdd = numberL - label.length();
147
        final String fixedLNumber;
148
        if (zeroesToAdd == 0) {
149
            fixedLNumber = label;
150
        } else if (zeroesToAdd > 0) {
151
            final StringBuilder sb = new StringBuilder(numberL);
152
            if (!padStart)
153
                sb.append(label);
154
            for (int i = 0; i < zeroesToAdd; i++) {
155
                sb.append(pad);
156
            }
157
            if (padStart)
158
                sb.append(label);
159
            fixedLNumber = sb.toString();
160
        } else {
161
            fixedLNumber = truncateStart ? label.substring(-zeroesToAdd, label.length()) : label.substring(0, numberL);
162
        }
163
        assert fixedLNumber.length() == numberL;
164
 
165
        // use both UUID and a meaningful label
166
        final String res = uuidS + '-' + fixedLNumber;
167
        assert res.length() <= IDENTIFICATION_MAX_LENGTH;
168
        return res;
169
    }
170
}