OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 112 | Go to most recent revision | 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
 
11
import javax.swing.JComponent;
12
 
13
import org.openconcerto.erp.config.ComptaPropsConfiguration;
14
import org.openconcerto.sql.element.GroupSQLComponent;
15
import org.openconcerto.sql.element.SQLElement;
16
import org.openconcerto.sql.model.DBRoot;
17
import org.openconcerto.sql.model.SQLDataSource;
18
import org.openconcerto.sql.model.SQLRow;
19
import org.openconcerto.sql.model.SQLRowAccessor;
20
import org.openconcerto.sql.model.SQLRowMode;
21
import org.openconcerto.sql.model.SQLRowValues;
22
import org.openconcerto.sql.model.SQLSelect;
23
import org.openconcerto.sql.model.SQLTable;
24
import org.openconcerto.sql.model.Where;
25
import org.openconcerto.sql.sqlobject.SQLTextCombo;
26
import org.openconcerto.ui.JLabelBold;
27
import org.openconcerto.ui.component.ITextArea;
28
import org.openconcerto.ui.date.DateRange;
29
import org.openconcerto.ui.date.DateRangeTable;
30
import org.openconcerto.utils.ExceptionHandler;
31
 
32
public class OperationSQLComponent extends GroupSQLComponent {
33
 
34
    public OperationSQLComponent(SQLElement element) {
35
        super(element, new OperationGroup());
36
    }
37
 
38
    @Override
39
    public JComponent createEditor(String id) {
40
        if (id.equals("operation.description")) {
41
            return new ITextArea(15, 3);
42
        } else if (id.equals("operation.dates")) {
43
            return new DateRangeTable(true, false, false);
44
        } else if (id.equals("operation.type") || id.equals("operation.status")) {
45
            return new SQLTextCombo();
46
        }
47
        return super.createEditor(id);
48
    }
49
 
50
    @Override
51
    protected Set<String> createRequiredNames() {
52
        Set<String> s = new HashSet<String>();
53
        s.add("ID_SITE");
54
        s.add("ID_USER_COMMON");
55
        s.add("TYPE");
56
        s.add("STATUS");
57
        return s;
58
    }
59
 
60
    @Override
61
    protected JComponent createLabel(String id) {
62
        if (id.equals("operation.dates")) {
63
            return new JLabelBold("");
64
        }
65
        return super.createLabel(id);
66
    }
67
 
68
    DateRangeTable getDateRangeTable() {
69
        return (DateRangeTable) getEditor("operation.dates");
70
    }
71
 
72
    @Override
73
    public void select(final SQLRowAccessor r) {
74
        super.select(r);
75
 
76
        if (r != null && r.getID() > this.getTable().getUndefinedID()) {
77
            SQLSelect select = new SQLSelect();
78
            int idGroup = r.getInt("ID_CALENDAR_ITEM_GROUP");
79
            final SQLTable calendarItemTable = r.getTable().getTable("CALENDAR_ITEM");
80
            select.addSelectStar(calendarItemTable);
81
            select.setWhere(calendarItemTable.getField("ID_CALENDAR_ITEM_GROUP"), "=", idGroup);
82
            final SQLDataSource ds = ComptaPropsConfiguration.getInstanceCompta().getRootSociete().getDBSystemRoot().getDataSource();
83
            @SuppressWarnings("unchecked")
84
            final List<Map<String, Object>> result = ds.execute(select.asString());
85
            final List<DateRange> ranges = new ArrayList<DateRange>();
86
            for (Map<String, Object> row : result) {
87
                final DateRange range = new DateRange();
88
                final Date dStart = (Date) row.get("START");
89
                final Date dEnd = (Date) row.get("END");
90
                range.setStart(dStart.getTime());
91
                range.setStop(dEnd.getTime());
92
                ranges.add(range);
93
            }
94
 
95
            getDateRangeTable().fillFrom(ranges);
96
 
97
        } else {
98
            getDateRangeTable().clear();
99
        }
100
    }
101
 
102
    @Override
103
    public void update() {
104
        super.update();
105
        updateCalendar(this.getSelectedID());
106
    }
107
 
108
    @Override
109
    public int insert(SQLRow order) {
110
        int id = super.insert(order);
111
        updateCalendar(id);
112
        return id;
113
    }
114
 
141 ilm 115
    private void updateCalendar(int idOperation) {
112 ilm 116
        final DBRoot root = ComptaPropsConfiguration.getInstanceCompta().getRootSociete();
117
        final SQLTable tOperation = root.getTable(ModuleOperation.TABLE_OPERATION);
141 ilm 118
        final SQLRow operationRow = tOperation.getRow(idOperation);
112 ilm 119
        SQLRow calendarGroupRow = operationRow.getForeignRow("ID_CALENDAR_ITEM_GROUP", SQLRowMode.DEFINED);
141 ilm 120
        List<String> oldUIds = new ArrayList<String>();
112 ilm 121
        if (calendarGroupRow != null) {
141 ilm 122
            oldUIds.addAll(getUUidsFromCalendarGroupId(root, calendarGroupRow.getID()));
112 ilm 123
            // Remove associated CalendarItems AND CalendarItemGroups
124
            final SQLTable tItems = root.getTable("CALENDAR_ITEM");
125
            String deleteReq = "DELETE FROM " + tItems.getSQLName().quote() + " WHERE ";
126
            Where where = new Where(tItems.getField("ID_CALENDAR_ITEM_GROUP"), "=", calendarGroupRow.getID());
127
            deleteReq += where.getClause();
128
            root.getDBSystemRoot().getDataSource().execute(deleteReq);
129
            // Archive group
130
            final SQLRowValues asRowValues = calendarGroupRow.asRowValues();
131
            asRowValues.put("ARCHIVE", 1);
132
            try {
133
                asRowValues.commit();
134
            } catch (SQLException e) {
135
                ExceptionHandler.handle("Cannot remove associated Calendar group", e);
136
            }
137
        }
138
        final SQLRowValues rowItemGroup = new SQLRowValues(root.getTable("CALENDAR_ITEM_GROUP"));
139
        rowItemGroup.put("NAME", operationRow.getForeignRow("ID_SITE").getString("NAME") + " " + operationRow.getString("TYPE"));
140
        rowItemGroup.put("DESCRIPTION", operationRow.getString("DESCRIPTION"));
141
        try {
142
            calendarGroupRow = rowItemGroup.commit();
143
            // Update Operation
144
            SQLRowValues operationSQLRowValues = operationRow.asRowValues();
145
            operationSQLRowValues.put("ID_CALENDAR_ITEM_GROUP", calendarGroupRow.getID());
146
            operationSQLRowValues.commit();
147
            // Insert Calendar Items
148
            final List<DateRange> ranges = this.getDateRangeTable().getRanges();
141 ilm 149
            int index = 0;
112 ilm 150
            for (DateRange dateRange : ranges) {
151
                final SQLRowValues rowItem = new SQLRowValues(root.getTable("CALENDAR_ITEM"));
152
                rowItem.put("START", new Date(dateRange.getStart()));
153
                rowItem.put("END", new Date(dateRange.getStop()));
154
                rowItem.put("DURATION_S", (dateRange.getStop() - dateRange.getStart()) / 1000);
155
                rowItem.put("SUMMARY", operationRow.getForeignRow("ID_SITE").getString("NAME") + "\n" + operationRow.getString("TYPE"));
156
                rowItem.put("DESCRIPTION", operationRow.getString("DESCRIPTION"));
157
                rowItem.put("FLAGS", "");
158
                rowItem.put("STATUS", operationRow.getString("STATUS"));
159
                rowItem.put("ID_CALENDAR_ITEM_GROUP", calendarGroupRow.getID());
141 ilm 160
                rowItem.put("SOURCE_ID", idOperation);
112 ilm 161
                rowItem.put("SOURCE_TABLE", ModuleOperation.TABLE_OPERATION);
141 ilm 162
                // apply old UIDs
163
                if (index < oldUIds.size()) {
164
                    rowItem.put("UID", oldUIds.get(index));
165
                }
112 ilm 166
                rowItem.commit();
141 ilm 167
                index++;
112 ilm 168
            }
169
 
170
        } catch (SQLException e) {
171
            ExceptionHandler.handle("Cannot create Calendar items", e);
172
        }
173
 
174
    }
175
 
141 ilm 176
    @SuppressWarnings("rawtypes")
177
    private List<String> getUUidsFromCalendarGroupId(DBRoot root, int id) {
178
        final List<String> result = new ArrayList<String>();
179
        final SQLTable tCalendarItem = root.getTable("CALENDAR_ITEM");
180
        final SQLSelect s = new SQLSelect();
181
        s.addSelect(tCalendarItem.getField("UID"));
182
        final Where where = new Where(tCalendarItem.getField("ID_CALENDAR_ITEM_GROUP"), "=", id);
183
        s.setWhere(where);
184
 
185
        final List r = root.getDBSystemRoot().getDataSource().execute(s.asString());
186
 
187
        for (Object line : r) {
188
            final String uid = (String) ((Map) line).get("UID");
189
            if (uid != null && !uid.trim().isEmpty()) {
190
                result.add(uid);
191
            }
192
        }
193
        return result;
194
    }
195
 
112 ilm 196
    public void setDate(Date date) {
197
        final List<DateRange> list = new ArrayList<DateRange>();
198
        list.add(new DateRange(date.getTime()));
199
        this.getDateRangeTable().fillFrom(list);
200
    }
201
}