OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | 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.core.common.component;
15
 
73 ilm 16
import org.openconcerto.erp.config.Gestion;
18 ilm 17
import org.openconcerto.erp.core.common.ui.AbstractArticleItemTable;
73 ilm 18
import org.openconcerto.sql.Configuration;
18 ilm 19
import org.openconcerto.sql.element.BaseSQLComponent;
73 ilm 20
import org.openconcerto.sql.element.SQLComponent;
18 ilm 21
import org.openconcerto.sql.element.SQLElement;
73 ilm 22
import org.openconcerto.sql.model.SQLField;
18 ilm 23
import org.openconcerto.sql.model.SQLInjector;
24
import org.openconcerto.sql.model.SQLRow;
73 ilm 25
import org.openconcerto.sql.model.SQLRowAccessor;
28 ilm 26
import org.openconcerto.sql.model.SQLRowValues;
73 ilm 27
import org.openconcerto.sql.model.SQLRowValues.ForeignCopyMode;
28
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
29
import org.openconcerto.sql.model.SQLSelect;
174 ilm 30
import org.openconcerto.sql.model.SQLTable;
73 ilm 31
import org.openconcerto.sql.model.Where;
32
import org.openconcerto.sql.view.EditFrame;
94 ilm 33
import org.openconcerto.sql.view.EditPanel.EditMode;
73 ilm 34
import org.openconcerto.sql.view.list.RowValuesTable;
35
import org.openconcerto.ui.FrameUtil;
36
import org.openconcerto.utils.ExceptionHandler;
37
import org.openconcerto.utils.cc.ITransformer;
18 ilm 38
 
28 ilm 39
import java.math.BigDecimal;
73 ilm 40
import java.util.ArrayList;
41
import java.util.HashSet;
42
import java.util.Iterator;
18 ilm 43
import java.util.List;
73 ilm 44
import java.util.Set;
18 ilm 45
 
73 ilm 46
import javax.swing.ImageIcon;
47
 
18 ilm 48
public abstract class TransfertBaseSQLComponent extends BaseSQLComponent {
73 ilm 49
    protected SQLRowAccessor selectedRow;
50
    private List<SQLRowValues> sourceRows;
18 ilm 51
 
52
    public TransfertBaseSQLComponent(SQLElement element) {
53
        super(element);
54
    }
55
 
56
    /**
57
     * Chargement d'élément à partir d'une autre table ex : les éléments d'un BL dans une facture
58
     *
59
     * @param table ItemTable du component de destination (ex : tableFacture)
60
     * @param elt element source (ex : BL)
61
     * @param id id de la row source
62
     * @param itemsElt elements des items de la source (ex : BL_ELEMENT)
63
     */
64
    public void loadItem(AbstractArticleItemTable table, SQLElement elt, int id, SQLElement itemsElt) {
65
        loadItem(table, elt, id, itemsElt, true);
66
    }
67
 
68
    public void loadItem(AbstractArticleItemTable table, SQLElement elt, int id, SQLElement itemsElt, boolean clear) {
69
        List<SQLRow> myListItem = elt.getTable().getRow(id).getReferentRows(itemsElt.getTable());
70
 
71
        if (myListItem.size() != 0) {
73 ilm 72
            SQLInjector injector = SQLInjector.getInjector(itemsElt.getTable(), table.getSQLElement().getTable());
18 ilm 73
            if (clear) {
74
                table.getModel().clearRows();
75
            }
76
            for (SQLRow rowElt : myListItem) {
77
 
28 ilm 78
                SQLRowValues createRowValuesFrom = injector.createRowValuesFrom(rowElt);
79
                if (createRowValuesFrom.getTable().getFieldsName().contains("POURCENT_ACOMPTE")) {
80
                    if (createRowValuesFrom.getObject("POURCENT_ACOMPTE") == null) {
81
                        createRowValuesFrom.put("POURCENT_ACOMPTE", new BigDecimal(100.0));
82
                    }
83
                }
84
                table.getModel().addRow(createRowValuesFrom);
18 ilm 85
                int rowIndex = table.getModel().getRowCount() - 1;
86
                table.getModel().fireTableModelModified(rowIndex);
87
            }
88
        } else {
89
            if (clear) {
90
                table.getModel().clearRows();
91
                table.getModel().addNewRowAt(0);
92
            }
93
        }
94
        table.getModel().fireTableDataChanged();
95
        table.repaint();
96
    }
73 ilm 97
 
98
    public void importFrom(List<SQLRowValues> rows) {
99
        this.sourceRows = rows;
100
        if (rows.size() > 0) {
101
            final SQLInjector injector = SQLInjector.getInjector(rows.get(0).getTable(), this.getTable());
102
            final SQLRowValues rValues = injector.createRowValuesFrom(rows);
103
            select(rValues);
104
        } else {
105
            select(null);
106
        }
107
    }
108
 
109
    @Override
110
    public int insert(SQLRow order) {
111
        // TODO: Pour l'instant appelé dans Swing, mais cela va changer...
112
        final int insertedId = super.insert(order);
113
        if (insertedId != SQLRow.NONEXISTANT_ID && sourceRows != null && !sourceRows.isEmpty()) {
114
            final SQLInjector injector = SQLInjector.getInjector(sourceRows.get(0).getTable(), this.getTable());
115
            try {
116
                injector.commitTransfert(sourceRows, insertedId);
117
            } catch (Exception e) {
118
                ExceptionHandler.handle("Unable to insert transfert", e);
119
            }
120
        }
121
        return insertedId;
122
    }
123
 
124
    @Override
125
    public void select(SQLRowAccessor r) {
126
        if (r == null) {
127
            super.select(null);
128
            return;
129
        }
130
        // remove foreign and replace rowvalues by id
132 ilm 131
        super.select(r);
73 ilm 132
        final RowValuesTable table = this.getRowValuesTable();
133
        if (table != null) {
134
            table.clear();
135
            table.insertFrom(r);
136
        }
93 ilm 137
        refreshAfterSelect(r);
73 ilm 138
    }
139
 
93 ilm 140
    protected abstract void refreshAfterSelect(SQLRowAccessor rSource);
141
 
73 ilm 142
    protected RowValuesTable getRowValuesTable() {
143
        return null;
144
    }
145
 
94 ilm 146
    public static EditFrame openTransfertFrame(List<SQLRowValues> sourceRows, String destTableName) {
147
        return openTransfertFrame(sourceRows, destTableName, null);
148
    }
149
 
150
    public static EditFrame openTransfertFrame(List<SQLRowValues> sourceRows, String destTableName, String compID) {
73 ilm 151
        final SQLElement elt = Configuration.getInstance().getDirectory().getElement(destTableName);
94 ilm 152
        final EditFrame editFrame;
153
        if (compID == null || compID.trim().length() == 0) {
154
            editFrame = new EditFrame(elt);
155
        } else {
156
            editFrame = new EditFrame(elt.createComponent(compID), EditMode.CREATION);
157
        }
73 ilm 158
        editFrame.setIconImage(new ImageIcon(Gestion.class.getResource("frameicon.png")).getImage());
159
        final SQLComponent sqlComponent = editFrame.getSQLComponent();
160
        if (sqlComponent instanceof TransfertBaseSQLComponent) {
161
            final TransfertBaseSQLComponent comp = (TransfertBaseSQLComponent) sqlComponent;
162
 
163
            if (!sourceRows.isEmpty()) {
164
                // fetch all fields of all table to avoid 1 request by referent row
165
                final List<Number> ids = new ArrayList<Number>(sourceRows.size());
166
                for (SQLRowValues sqlRowValues : sourceRows) {
167
                    ids.add(sqlRowValues.getIDNumber());
168
                }
174 ilm 169
 
170
                final SQLRowValues row = new SQLRowValues(sourceRows.get(0).getTable());
73 ilm 171
                // FIXME don't work in the general case
172
                for (final SQLField rk : row.getTable().getDBSystemRoot().getGraph().getReferentKeys(row.getTable())) {
173
                    final Set<SQLRowValues> referentRows = row.getReferentRows(rk);
174
                    if (referentRows.size() > 1) {
175
                        final Iterator<SQLRowValues> iter = new ArrayList<SQLRowValues>(referentRows).iterator();
176
                        // keep the first
177
                        iter.next();
178
                        while (iter.hasNext()) {
179
                            final SQLRowValues ref = iter.next();
180
                            ref.remove(rk.getName());
181
                        }
182
                    }
183
                }
184
                for (SQLRowValues r : row.getGraph().getItems()) {
185
                    final Set<String> fields = new HashSet<String>(r.getTable().getFieldsName());
186
                    fields.removeAll(r.getFields());
187
                    r.putNulls(fields, false);
188
                }
189
                final SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(row);
190
                fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
191
 
192
                    @Override
193
                    public SQLSelect transformChecked(SQLSelect input) {
194
                        input.setWhere(new Where(row.getTable().getKey(), ids));
195
                        return input;
196
                    }
197
                });
198
 
199
                final List<SQLRowValues> result = fetcher.fetch();
200
                comp.importFrom(result);
201
                FrameUtil.show(editFrame);
202
            }
94 ilm 203
            return editFrame;
73 ilm 204
        } else {
205
            throw new IllegalArgumentException("Table " + destTableName + " SQLComponent is not a TransfertBaseSQLComponent");
206
        }
207
    }
174 ilm 208
 
209
    public static boolean isAlreadyAllTransfert(final List<SQLRowValues> selectedRows, final SQLTable from, final SQLTable to, final String fieldTotalFrom, final String fieldTotalTo) {
210
        final SQLTable tableTransfert = from.getTable("TR_" + from.getName());
211
        SQLRowValues rowVals = new SQLRowValues(tableTransfert);
212
        rowVals.put("ID_" + from.getName(), new SQLRowValues(from).put("NUMERO", null).put(fieldTotalFrom, null));
213
        rowVals.putRowValues("ID_" + to.getName()).put("NUMERO", null).put(fieldTotalTo, null);
214
        rowVals.put("ID", null);
215
 
216
        final List<Number> lID = new ArrayList<>();
217
        for (SQLRowValues sqlRowValues : selectedRows) {
218
            lID.add(sqlRowValues.getID());
219
        }
220
 
221
        SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(rowVals);
222
        fetch.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
223
 
224
            @Override
225
            public SQLSelect transformChecked(SQLSelect input) {
226
                Where w = new Where(tableTransfert.getField("ID_" + from.getName()), lID);
227
                w = w.and(new Where(tableTransfert.getField("ID_" + to.getName()), "IS NOT", (Object) null));
228
                input.setWhere(w);
229
                return input;
230
            }
231
        });
232
 
233
        List<SQLRowValues> rows = fetch.fetch();
234
        if (rows != null && rows.size() > 0) {
235
            String numero = "";
236
 
237
            long totalBR = 0;
238
            long totalFact = 0;
239
            Set<String> idsAdded = new HashSet<>();
240
            for (SQLRowValues sqlRow : rows) {
241
                final SQLRowAccessor foreignBR = sqlRow.getForeign("ID_" + from.getName());
242
                final SQLRowAccessor foreignFact = sqlRow.getForeign("ID_" + to.getName());
243
                numero += foreignBR.getString("NUMERO") + " ,";
244
                String fromKey = from.getName() + "-" + foreignBR.getID();
245
                if (!idsAdded.contains(fromKey)) {
246
                    totalBR += foreignBR.getLong(fieldTotalFrom);
247
                    idsAdded.add(fromKey);
248
                }
249
                String toKey = to.getName() + "-" + foreignFact.getID();
250
                if (!idsAdded.contains(toKey)) {
251
                    totalFact += foreignFact.getLong(fieldTotalTo);
252
                    idsAdded.add(toKey);
253
                }
254
            }
255
 
256
            numero = numero.substring(0, numero.length() - 2);
257
            return totalBR <= totalFact;
258
        }
259
        return Boolean.FALSE;
260
    }
18 ilm 261
}