80 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
182 |
ilm |
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
80 |
ilm |
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 |
|
|
|
16 |
import org.openconcerto.erp.config.Gestion;
|
|
|
17 |
import org.openconcerto.erp.core.common.ui.AbstractArticleItemTable;
|
|
|
18 |
import org.openconcerto.sql.Configuration;
|
|
|
19 |
import org.openconcerto.sql.element.GroupSQLComponent;
|
|
|
20 |
import org.openconcerto.sql.element.SQLComponent;
|
|
|
21 |
import org.openconcerto.sql.element.SQLElement;
|
|
|
22 |
import org.openconcerto.sql.model.SQLField;
|
|
|
23 |
import org.openconcerto.sql.model.SQLInjector;
|
|
|
24 |
import org.openconcerto.sql.model.SQLRow;
|
|
|
25 |
import org.openconcerto.sql.model.SQLRowAccessor;
|
|
|
26 |
import org.openconcerto.sql.model.SQLRowValues;
|
|
|
27 |
import org.openconcerto.sql.model.SQLRowValues.ForeignCopyMode;
|
|
|
28 |
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
|
|
|
29 |
import org.openconcerto.sql.model.SQLSelect;
|
|
|
30 |
import org.openconcerto.sql.model.Where;
|
|
|
31 |
import org.openconcerto.sql.view.EditFrame;
|
|
|
32 |
import org.openconcerto.sql.view.EditPanel.EditMode;
|
|
|
33 |
import org.openconcerto.sql.view.list.RowValuesTable;
|
|
|
34 |
import org.openconcerto.ui.FrameUtil;
|
|
|
35 |
import org.openconcerto.ui.group.Group;
|
|
|
36 |
import org.openconcerto.utils.ExceptionHandler;
|
|
|
37 |
import org.openconcerto.utils.cc.ITransformer;
|
|
|
38 |
|
|
|
39 |
import java.math.BigDecimal;
|
|
|
40 |
import java.util.ArrayList;
|
|
|
41 |
import java.util.HashSet;
|
|
|
42 |
import java.util.Iterator;
|
|
|
43 |
import java.util.List;
|
|
|
44 |
import java.util.Set;
|
|
|
45 |
|
|
|
46 |
import javax.swing.ImageIcon;
|
|
|
47 |
|
|
|
48 |
public abstract class TransfertGroupSQLComponent extends GroupSQLComponent {
|
|
|
49 |
protected SQLRowAccessor selectedRow;
|
|
|
50 |
private List<SQLRowValues> sourceRows;
|
|
|
51 |
|
|
|
52 |
public TransfertGroupSQLComponent(SQLElement element, Group group) {
|
|
|
53 |
super(element, group);
|
|
|
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) {
|
|
|
72 |
SQLInjector injector = SQLInjector.getInjector(itemsElt.getTable(), table.getSQLElement().getTable());
|
|
|
73 |
if (clear) {
|
|
|
74 |
table.getModel().clearRows();
|
|
|
75 |
}
|
|
|
76 |
for (SQLRow rowElt : myListItem) {
|
|
|
77 |
|
|
|
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 |
}
|
182 |
ilm |
84 |
table.getModel().addRow(createRowValuesFrom, false);
|
80 |
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 |
}
|
|
|
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
|
|
|
131 |
final SQLRowValues singleRowValues = new SQLRowValues(r.asRowValues(), ForeignCopyMode.COPY_ID_OR_RM);
|
|
|
132 |
super.select(singleRowValues);
|
|
|
133 |
final RowValuesTable table = this.getRowValuesTable();
|
|
|
134 |
if (table != null) {
|
|
|
135 |
table.clear();
|
|
|
136 |
table.insertFrom(r);
|
|
|
137 |
}
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
protected RowValuesTable getRowValuesTable() {
|
|
|
141 |
return null;
|
|
|
142 |
}
|
|
|
143 |
|
144 |
ilm |
144 |
public static EditFrame openTransfertFrame(List<SQLRowValues> sourceRows, String destTableName, String groupID) {
|
80 |
ilm |
145 |
final SQLElement elt = Configuration.getInstance().getDirectory().getElement(destTableName);
|
|
|
146 |
final EditFrame editFrame = new EditFrame(elt.createComponent(groupID), EditMode.CREATION);
|
|
|
147 |
editFrame.setIconImage(new ImageIcon(Gestion.class.getResource("frameicon.png")).getImage());
|
|
|
148 |
final SQLComponent sqlComponent = editFrame.getSQLComponent();
|
|
|
149 |
if (sqlComponent instanceof TransfertGroupSQLComponent) {
|
|
|
150 |
final TransfertGroupSQLComponent comp = (TransfertGroupSQLComponent) sqlComponent;
|
|
|
151 |
|
|
|
152 |
if (!sourceRows.isEmpty()) {
|
|
|
153 |
// fetch all fields of all table to avoid 1 request by referent row
|
|
|
154 |
final List<Number> ids = new ArrayList<Number>(sourceRows.size());
|
|
|
155 |
for (SQLRowValues sqlRowValues : sourceRows) {
|
|
|
156 |
ids.add(sqlRowValues.getIDNumber());
|
|
|
157 |
}
|
|
|
158 |
final SQLRowValues row = sourceRows.get(0).deepCopy();
|
|
|
159 |
// FIXME don't work in the general case
|
|
|
160 |
for (final SQLField rk : row.getTable().getDBSystemRoot().getGraph().getReferentKeys(row.getTable())) {
|
|
|
161 |
final Set<SQLRowValues> referentRows = row.getReferentRows(rk);
|
|
|
162 |
if (referentRows.size() > 1) {
|
|
|
163 |
final Iterator<SQLRowValues> iter = new ArrayList<SQLRowValues>(referentRows).iterator();
|
|
|
164 |
// keep the first
|
|
|
165 |
iter.next();
|
|
|
166 |
while (iter.hasNext()) {
|
|
|
167 |
final SQLRowValues ref = iter.next();
|
|
|
168 |
ref.remove(rk.getName());
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
for (SQLRowValues r : row.getGraph().getItems()) {
|
|
|
173 |
final Set<String> fields = new HashSet<String>(r.getTable().getFieldsName());
|
|
|
174 |
fields.removeAll(r.getFields());
|
|
|
175 |
r.putNulls(fields, false);
|
|
|
176 |
}
|
|
|
177 |
final SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(row);
|
|
|
178 |
fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
|
|
|
179 |
|
|
|
180 |
@Override
|
|
|
181 |
public SQLSelect transformChecked(SQLSelect input) {
|
|
|
182 |
input.setWhere(new Where(row.getTable().getKey(), ids));
|
|
|
183 |
return input;
|
|
|
184 |
}
|
|
|
185 |
});
|
|
|
186 |
|
|
|
187 |
final List<SQLRowValues> result = fetcher.fetch();
|
|
|
188 |
comp.importFrom(result);
|
|
|
189 |
FrameUtil.show(editFrame);
|
|
|
190 |
}
|
144 |
ilm |
191 |
return editFrame;
|
80 |
ilm |
192 |
} else {
|
|
|
193 |
throw new IllegalArgumentException("Table " + destTableName + " SQLComponent is not a TransfertBaseSQLComponent");
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
}
|