OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
112 ilm 1
package org.openconcerto.modules.operation;
2
 
3
import java.sql.SQLException;
4
import java.util.ArrayList;
5
import java.util.Date;
6
import java.util.HashSet;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Set;
10
 
150 ilm 11
import javax.swing.JCheckBox;
112 ilm 12
import javax.swing.JComponent;
150 ilm 13
import javax.swing.JToggleButton;
112 ilm 14
 
15
import org.openconcerto.erp.config.ComptaPropsConfiguration;
150 ilm 16
import org.openconcerto.sql.PropsConfiguration;
112 ilm 17
import org.openconcerto.sql.element.GroupSQLComponent;
18
import org.openconcerto.sql.element.SQLElement;
19
import org.openconcerto.sql.model.DBRoot;
150 ilm 20
import org.openconcerto.sql.model.FieldMapper;
112 ilm 21
import org.openconcerto.sql.model.SQLDataSource;
150 ilm 22
import org.openconcerto.sql.model.SQLField;
112 ilm 23
import org.openconcerto.sql.model.SQLRow;
24
import org.openconcerto.sql.model.SQLRowAccessor;
25
import org.openconcerto.sql.model.SQLRowMode;
26
import org.openconcerto.sql.model.SQLRowValues;
27
import org.openconcerto.sql.model.SQLSelect;
28
import org.openconcerto.sql.model.SQLTable;
29
import org.openconcerto.sql.model.Where;
150 ilm 30
import org.openconcerto.sql.request.ComboSQLRequest;
31
import org.openconcerto.sql.sqlobject.ElementComboBox;
112 ilm 32
import org.openconcerto.sql.sqlobject.SQLTextCombo;
33
import org.openconcerto.ui.JLabelBold;
34
import org.openconcerto.ui.component.ITextArea;
35
import org.openconcerto.ui.date.DateRange;
36
import org.openconcerto.ui.date.DateRangeTable;
150 ilm 37
import org.openconcerto.utils.CollectionUtils;
112 ilm 38
import org.openconcerto.utils.ExceptionHandler;
150 ilm 39
import org.openconcerto.utils.StringUtils;
112 ilm 40
 
41
public class OperationSQLComponent extends GroupSQLComponent {
42
 
43
    public OperationSQLComponent(SQLElement element) {
44
        super(element, new OperationGroup());
45
    }
46
 
47
    @Override
48
    public JComponent createEditor(String id) {
49
        if (id.equals("operation.description")) {
50
            return new ITextArea(15, 3);
150 ilm 51
        } else if (id.equals("operation.freeTime")) {
52
            return new JCheckBox();
112 ilm 53
        } else if (id.equals("operation.dates")) {
54
            return new DateRangeTable(true, false, false);
55
        } else if (id.equals("operation.type") || id.equals("operation.status")) {
150 ilm 56
            return new SQLTextCombo() {
57
                @Override
58
                public boolean canComplete(String originalText) {
59
                    return originalText != null && !originalText.trim().isEmpty();
60
                }
61
            };
62
        } else if (id.equals("operation.user")) {
63
            // do not show desactivated users
64
            final FieldMapper fieldMapper = PropsConfiguration.getInstance().getFieldMapper();
65
            final SQLField field = fieldMapper.getSQLFieldForItem(id);
66
            final SQLElement foreignElement = getElement().getForeignElement(field.getName());
67
            final ElementComboBox comp = new ElementComboBox();
68
            final ComboSQLRequest comboRequest = foreignElement.getComboRequest();
69
            final SQLTable userTable = foreignElement.getTable();
70
            comboRequest.setWhere(new Where(userTable.getField("DISABLED"), "=", false));
71
            comp.init(foreignElement, comboRequest);
72
            comp.setOpaque(false);
73
            return comp;
74
 
112 ilm 75
        }
76
        return super.createEditor(id);
150 ilm 77
 
112 ilm 78
    }
79
 
80
    @Override
81
    protected Set<String> createRequiredNames() {
82
        Set<String> s = new HashSet<String>();
83
        s.add("ID_SITE");
84
        s.add("ID_USER_COMMON");
85
        s.add("TYPE");
86
        s.add("STATUS");
87
        return s;
88
    }
89
 
90
    @Override
91
    protected JComponent createLabel(String id) {
92
        if (id.equals("operation.dates")) {
93
            return new JLabelBold("");
94
        }
95
        return super.createLabel(id);
96
    }
97
 
98
    DateRangeTable getDateRangeTable() {
99
        return (DateRangeTable) getEditor("operation.dates");
100
    }
101
 
102
    @Override
103
    public void select(final SQLRowAccessor r) {
104
        super.select(r);
105
 
150 ilm 106
        final boolean isFreeTime;
112 ilm 107
        if (r != null && r.getID() > this.getTable().getUndefinedID()) {
108
            SQLSelect select = new SQLSelect();
109
            int idGroup = r.getInt("ID_CALENDAR_ITEM_GROUP");
110
            final SQLTable calendarItemTable = r.getTable().getTable("CALENDAR_ITEM");
111
            select.addSelectStar(calendarItemTable);
112
            select.setWhere(calendarItemTable.getField("ID_CALENDAR_ITEM_GROUP"), "=", idGroup);
150 ilm 113
            final SQLDataSource ds = calendarItemTable.getDBSystemRoot().getDataSource();
112 ilm 114
            @SuppressWarnings("unchecked")
115
            final List<Map<String, Object>> result = ds.execute(select.asString());
150 ilm 116
            boolean freeTimeTmp = false;
112 ilm 117
            final List<DateRange> ranges = new ArrayList<DateRange>();
118
            for (Map<String, Object> row : result) {
119
                final DateRange range = new DateRange();
120
                final Date dStart = (Date) row.get("START");
121
                final Date dEnd = (Date) row.get("END");
122
                range.setStart(dStart.getTime());
123
                range.setStop(dEnd.getTime());
124
                ranges.add(range);
150 ilm 125
                freeTimeTmp |= row.get("FLAGS") == null ? false : StringUtils.fastSplit((String) row.get("FLAGS"), ',').contains(ModuleOperation.FREE_TIME_FLAG.getTypeId());
112 ilm 126
            }
150 ilm 127
            isFreeTime = freeTimeTmp;
112 ilm 128
            getDateRangeTable().fillFrom(ranges);
129
        } else {
130
            getDateRangeTable().clear();
150 ilm 131
            isFreeTime = false;
112 ilm 132
        }
150 ilm 133
        ((JToggleButton) getEditor("operation.freeTime")).setSelected(isFreeTime);
112 ilm 134
    }
135
 
136
    @Override
137
    public void update() {
138
        super.update();
139
        updateCalendar(this.getSelectedID());
140
    }
141
 
142
    @Override
143
    public int insert(SQLRow order) {
144
        int id = super.insert(order);
145
        updateCalendar(id);
146
        return id;
147
    }
148
 
141 ilm 149
    private void updateCalendar(int idOperation) {
112 ilm 150
        final DBRoot root = ComptaPropsConfiguration.getInstanceCompta().getRootSociete();
151
        final SQLTable tOperation = root.getTable(ModuleOperation.TABLE_OPERATION);
141 ilm 152
        final SQLRow operationRow = tOperation.getRow(idOperation);
112 ilm 153
        SQLRow calendarGroupRow = operationRow.getForeignRow("ID_CALENDAR_ITEM_GROUP", SQLRowMode.DEFINED);
141 ilm 154
        List<String> oldUIds = new ArrayList<String>();
112 ilm 155
        if (calendarGroupRow != null) {
141 ilm 156
            oldUIds.addAll(getUUidsFromCalendarGroupId(root, calendarGroupRow.getID()));
112 ilm 157
            // Remove associated CalendarItems AND CalendarItemGroups
158
            final SQLTable tItems = root.getTable("CALENDAR_ITEM");
159
            String deleteReq = "DELETE FROM " + tItems.getSQLName().quote() + " WHERE ";
160
            Where where = new Where(tItems.getField("ID_CALENDAR_ITEM_GROUP"), "=", calendarGroupRow.getID());
161
            deleteReq += where.getClause();
162
            root.getDBSystemRoot().getDataSource().execute(deleteReq);
163
            // Archive group
164
            final SQLRowValues asRowValues = calendarGroupRow.asRowValues();
165
            asRowValues.put("ARCHIVE", 1);
166
            try {
167
                asRowValues.commit();
168
            } catch (SQLException e) {
169
                ExceptionHandler.handle("Cannot remove associated Calendar group", e);
170
            }
171
        }
172
        final SQLRowValues rowItemGroup = new SQLRowValues(root.getTable("CALENDAR_ITEM_GROUP"));
173
        rowItemGroup.put("NAME", operationRow.getForeignRow("ID_SITE").getString("NAME") + " " + operationRow.getString("TYPE"));
174
        rowItemGroup.put("DESCRIPTION", operationRow.getString("DESCRIPTION"));
175
        try {
176
            calendarGroupRow = rowItemGroup.commit();
177
            // Update Operation
178
            SQLRowValues operationSQLRowValues = operationRow.asRowValues();
179
            operationSQLRowValues.put("ID_CALENDAR_ITEM_GROUP", calendarGroupRow.getID());
180
            operationSQLRowValues.commit();
181
            // Insert Calendar Items
182
            final List<DateRange> ranges = this.getDateRangeTable().getRanges();
141 ilm 183
            int index = 0;
112 ilm 184
            for (DateRange dateRange : ranges) {
185
                final SQLRowValues rowItem = new SQLRowValues(root.getTable("CALENDAR_ITEM"));
186
                rowItem.put("START", new Date(dateRange.getStart()));
187
                rowItem.put("END", new Date(dateRange.getStop()));
188
                rowItem.put("DURATION_S", (dateRange.getStop() - dateRange.getStart()) / 1000);
189
                rowItem.put("SUMMARY", operationRow.getForeignRow("ID_SITE").getString("NAME") + "\n" + operationRow.getString("TYPE"));
190
                rowItem.put("DESCRIPTION", operationRow.getString("DESCRIPTION"));
150 ilm 191
                final List<String> flags = new ArrayList<>();
192
                final boolean isFreeTime = ((JToggleButton) getEditor("operation.freeTime")).isSelected();
193
                if (isFreeTime)
194
                    flags.add(ModuleOperation.FREE_TIME_FLAG.getTypeId());
195
                if (!StringUtils.isEmpty(operationRow.getString("PLANNER_UID")))
196
                    flags.add("planned");
197
                rowItem.put("FLAGS", CollectionUtils.join(flags, ","));
112 ilm 198
                rowItem.put("STATUS", operationRow.getString("STATUS"));
199
                rowItem.put("ID_CALENDAR_ITEM_GROUP", calendarGroupRow.getID());
141 ilm 200
                rowItem.put("SOURCE_ID", idOperation);
112 ilm 201
                rowItem.put("SOURCE_TABLE", ModuleOperation.TABLE_OPERATION);
141 ilm 202
                // apply old UIDs
203
                if (index < oldUIds.size()) {
204
                    rowItem.put("UID", oldUIds.get(index));
205
                }
112 ilm 206
                rowItem.commit();
141 ilm 207
                index++;
112 ilm 208
            }
209
 
210
        } catch (SQLException e) {
211
            ExceptionHandler.handle("Cannot create Calendar items", e);
212
        }
213
 
214
    }
215
 
141 ilm 216
    @SuppressWarnings("rawtypes")
217
    private List<String> getUUidsFromCalendarGroupId(DBRoot root, int id) {
218
        final List<String> result = new ArrayList<String>();
219
        final SQLTable tCalendarItem = root.getTable("CALENDAR_ITEM");
220
        final SQLSelect s = new SQLSelect();
221
        s.addSelect(tCalendarItem.getField("UID"));
222
        final Where where = new Where(tCalendarItem.getField("ID_CALENDAR_ITEM_GROUP"), "=", id);
223
        s.setWhere(where);
224
 
225
        final List r = root.getDBSystemRoot().getDataSource().execute(s.asString());
226
 
227
        for (Object line : r) {
228
            final String uid = (String) ((Map) line).get("UID");
229
            if (uid != null && !uid.trim().isEmpty()) {
230
                result.add(uid);
231
            }
232
        }
233
        return result;
234
    }
235
 
112 ilm 236
    public void setDate(Date date) {
237
        final List<DateRange> list = new ArrayList<DateRange>();
238
        list.add(new DateRange(date.getTime()));
239
        this.getDateRangeTable().fillFrom(list);
240
    }
241
}