OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 112 | 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.ResultSet;
4
import java.sql.SQLException;
5
import java.sql.Timestamp;
6
import java.util.ArrayList;
7
import java.util.Arrays;
8
import java.util.Collection;
9
import java.util.Collections;
10
import java.util.Date;
11
import java.util.HashSet;
12
import java.util.List;
13
import java.util.Set;
14
 
15
import org.apache.commons.dbutils.ResultSetHandler;
16
import org.openconcerto.erp.modules.AbstractModule;
17
import org.openconcerto.erp.modules.ModuleElement;
18
import org.openconcerto.sql.element.SQLComponent;
19
import org.openconcerto.sql.element.TreesOfSQLRows;
20
import org.openconcerto.sql.model.DBSystemRoot;
21
import org.openconcerto.sql.model.FieldPath;
22
import org.openconcerto.sql.model.SQLBase;
23
import org.openconcerto.sql.model.SQLField;
24
import org.openconcerto.sql.model.SQLRow;
25
import org.openconcerto.sql.model.SQLRowAccessor;
26
import org.openconcerto.sql.model.SQLRowMode;
27
import org.openconcerto.sql.model.SQLSelect;
28
import org.openconcerto.sql.model.SQLTable;
29
import org.openconcerto.sql.model.Where;
30
import org.openconcerto.sql.model.graph.Path;
31
import org.openconcerto.sql.utils.SQLUtils;
32
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
141 ilm 33
import org.openconcerto.sql.view.list.SQLTableModelSource;
112 ilm 34
import org.openconcerto.utils.RTInterruptedException;
35
 
36
public class OperationSQLElement extends ModuleElement {
37
 
38
    public OperationSQLElement(AbstractModule module) {
39
        super(module, ModuleOperation.TABLE_OPERATION);
40
    }
41
 
42
    @Override
43
    protected List<String> getListFields() {
44
        return Arrays.asList("ID_SITE", "ID_USER_COMMON", "TYPE", "STATUS", "DESCRIPTION");
45
    }
46
 
47
    @Override
48
    protected SQLComponent createComponent() {
49
        return new OperationSQLComponent(this);
50
    }
51
 
52
    @Override
141 ilm 53
    protected void _initTableSource(SQLTableModelSource source) {
54
        super._initTableSource(source);
112 ilm 55
        source.getColumns().add(new BaseSQLTableModelColumn("Date", Timestamp.class) {
56
 
57
            @Override
58
            protected Object show_(SQLRowAccessor r) {
59
                SQLRowAccessor rGroup = r.getForeign("ID_CALENDAR_ITEM_GROUP");
60
                Collection<? extends SQLRowAccessor> l = rGroup.getReferentRows(r.getTable().getTable("CALENDAR_ITEM"));
61
                if (l.isEmpty()) {
62
                    return null;
63
                }
64
                return l.iterator().next().getObject("START");
65
            }
66
 
67
            @Override
68
            public Set<FieldPath> getPaths() {
69
                Path p = new Path(getTable()).add(getTable().getField("ID_CALENDAR_ITEM_GROUP")).add(getTable().getTable("CALENDAR_ITEM"));
70
 
71
                return FieldPath.create(p, Arrays.asList("START"));
72
            }
73
        });
74
    }
75
 
76
    @Override
77
    protected void archive(TreesOfSQLRows trees, boolean cutLinks) throws SQLException {
78
        List<SQLRowAccessor> rows = new ArrayList<SQLRowAccessor>();
79
        for (SQLRow r : trees.getRows()) {
80
            rows.add(r.getForeignRow("ID_CALENDAR_ITEM_GROUP", SQLRowMode.NO_CHECK));
81
        }
82
        super.archive(trees, cutLinks);
83
 
84
        getDirectory().getElement(getTable().getForeignTable("ID_CALENDAR_ITEM_GROUP")).archive(rows);
85
 
86
    }
87
 
88
    /**
89
     * Delete operation, linked calendar groups and items
90
     *
91
     * @throws SQLException
92
     * @throws RTInterruptedException
141 ilm 93
     */
112 ilm 94
    @SuppressWarnings("unchecked")
95
    public void fastDelete(List<Long> operationsIds) throws RTInterruptedException, SQLException {
96
        if (operationsIds.isEmpty()) {
97
            return;
98
        }
99
        // Get calendar groups ids
100
        SQLSelect select = new SQLSelect();
101
        select.addSelect(this.getTable().getField("ID_CALENDAR_ITEM_GROUP"));
102
        select.addGroupBy(this.getTable().getField("ID_CALENDAR_ITEM_GROUP"));
103
        final String pkey = this.getTable().getPKsNames().get(0);
104
        select.setWhere(new Where(this.getTable().getField(pkey), true, operationsIds));
105
        String query = select.asString();
106
        final DBSystemRoot dbSystemRoot = this.getTable().getDBSystemRoot();
107
        List<Number> calendarGroupIds = (List<Number>) dbSystemRoot.getDataSource().executeCol(query);
108
        // Delete items
109
        final SQLField f1 = dbSystemRoot.findTable("CALENDAR_ITEM").getField("ID_CALENDAR_ITEM_GROUP");
110
        String q1 = "update " + SQLBase.quoteIdentifier("CALENDAR_ITEM");
111
        q1 += " set " + SQLBase.quoteIdentifier("ARCHIVE") + " = 1 ";
112
        q1 += " where " + new Where(f1, true, calendarGroupIds);
113
        // Delete groups
114
        final SQLField f2 = this.getTable().getDBSystemRoot().findTable("CALENDAR_ITEM_GROUP").getPrimaryKeys().iterator().next();
115
        String q2 = "update " + SQLBase.quoteIdentifier("CALENDAR_ITEM_GROUP");
116
        q2 += " set " + SQLBase.quoteIdentifier("ARCHIVE") + " = 1 ";
117
        q2 += " where " + new Where(f2, true, calendarGroupIds);
118
        // Delete operations
119
        final SQLField f3 = this.getTable().getField("ID_CALENDAR_ITEM_GROUP");
120
        String q3 = "update " + SQLBase.quoteIdentifier(ModuleOperation.TABLE_OPERATION);
121
        q3 += " set " + SQLBase.quoteIdentifier("ARCHIVE") + " = 1 ";
122
        q3 += " where " + new Where(f3, true, calendarGroupIds);
123
 
124
        // execute in one shot
125
        final List<String> queries = new ArrayList<String>(3);
126
        queries.add(q1);
127
        queries.add(q2);
128
        queries.add(q3);
129
        final List<ResultSetHandler> handlers = new ArrayList<ResultSetHandler>(3);
130
        final ResultSetHandler handler = new ResultSetHandler() {
131
 
132
            @Override
133
            public Object handle(ResultSet r) throws SQLException {
134
                // Nothing to do here
135
                return null;
136
            }
137
        };
138
        handlers.add(handler);
139
        handlers.add(handler);
140
        handlers.add(handler);
141
        SQLUtils.executeMultiple(dbSystemRoot, queries, handlers);
142
 
143
    }
144
 
145
    @SuppressWarnings("unchecked")
146
    public void fastDelete(String uid, Date startDate) throws Exception {
147
        SQLSelect select = new SQLSelect();
148
        select.addSelect(this.getTable().getField("ID_CALENDAR_ITEM_GROUP"));
149
        select.setWhere(new Where(this.getTable().getField("PLANNER_UID"), "=", uid));
150
        String query = select.asString();
151
        final DBSystemRoot dbSystemRoot = this.getTable().getDBSystemRoot();
152
        List<Number> calendarGroupIds = (List<Number>) dbSystemRoot.getDataSource().executeCol(query);
153
 
154
        // Recuperation des groupes correspondants et qui ont des items apres la date
155
        final SQLTable tableCalendarItem = dbSystemRoot.findTable("CALENDAR_ITEM");
156
        final SQLSelect select2 = new SQLSelect();
157
        select2.addSelect(tableCalendarItem.getField("ID_CALENDAR_ITEM_GROUP"));
158
        Where where2 = new Where(tableCalendarItem.getField("ID_CALENDAR_ITEM_GROUP"), true, calendarGroupIds);
159
        where2 = where2.and(new Where(tableCalendarItem.getField("START"), ">=", startDate.getTime()));
160
        select2.setWhere(where2);
161
        String query2 = select2.asString();
162
        List<Number> calendarGroupIdsToDelete = (List<Number>) dbSystemRoot.getDataSource().executeCol(query2);
163
        //
164
        final SQLSelect select3 = new SQLSelect();
165
        select3.addSelect(getTable().getKey());
166
        select3.setWhere(new Where(getTable().getField("ID_CALENDAR_ITEM_GROUP"), true, calendarGroupIdsToDelete));
167
        String query3 = select3.asString();
168
        List<Number> operationIdsToDelete = (List<Number>) dbSystemRoot.getDataSource().executeCol(query3);
169
        Set<Long> oids = new HashSet<Long>();
170
        for (Number n : operationIdsToDelete) {
171
            oids.add(n.longValue());
172
        }
173
        final List<Long> oidsToDelete = new ArrayList<Long>();
174
        oidsToDelete.addAll(oids);
175
        Collections.sort(oidsToDelete);
176
 
177
        fastDelete(oidsToDelete);
178
 
179
    }
180
}