OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 181 | 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 static org.openconcerto.sql.TM.getTM;
4
 
5
import java.awt.event.ActionEvent;
6
import java.io.IOException;
153 ilm 7
import java.sql.SQLException;
112 ilm 8
import java.util.ArrayList;
9
import java.util.HashSet;
10
import java.util.List;
11
import java.util.Set;
12
 
13
import javax.swing.AbstractAction;
14
import javax.swing.ImageIcon;
15
import javax.swing.JFrame;
16
import javax.swing.SwingUtilities;
17
 
18
import org.jopencalendar.model.Flag;
19
import org.jopencalendar.model.JCalendarItem;
20
import org.jopencalendar.model.JCalendarItemPart;
21
import org.jopencalendar.ui.DayView;
22
import org.openconcerto.erp.config.ComptaPropsConfiguration;
23
import org.openconcerto.erp.config.MainFrame;
24
import org.openconcerto.erp.generationDoc.TemplateManager;
25
import org.openconcerto.erp.modules.AbstractModule;
26
import org.openconcerto.erp.modules.ComponentsContext;
27
import org.openconcerto.erp.modules.DBContext;
28
import org.openconcerto.erp.modules.MenuContext;
29
import org.openconcerto.erp.modules.ModuleFactory;
30
import org.openconcerto.modules.operation.action.LockAction;
31
import org.openconcerto.sql.element.SQLElement;
32
import org.openconcerto.sql.element.SQLElementDirectory;
33
import org.openconcerto.sql.model.SQLRowValues;
34
import org.openconcerto.sql.utils.SQLCreateTable;
35
import org.openconcerto.sql.view.list.IListe;
36
import org.openconcerto.sql.view.list.RowAction;
183 ilm 37
import org.openconcerto.sql.view.list.action.ListEvent;
112 ilm 38
import org.openconcerto.ui.FrameUtil;
39
import org.openconcerto.utils.i18n.TranslationManager;
40
 
41
public final class ModuleOperation extends AbstractModule {
42
    public static final String OPERATIONS_REPORT_TEMPLATE_ID = "Operations Report";
43
    public static final String OPERATIONS_REPORT_TEMPLATE2_ID = "Operations Report2";
44
    // -Dmodule.dir=../"Module Operation" -Dgestion.confFile=Configuration/main.properties -ea
45
    public static final String TABLE_SITE = "SITE";
46
    public static final String TABLE_OPERATION = "OPERATION";
47
 
150 ilm 48
    public static final Flag FREE_TIME_FLAG = new Flag("freeTime", null, "Non travaillé", "");
49
 
112 ilm 50
    public ModuleOperation(ModuleFactory f) throws IOException {
51
        super(f);
52
    }
53
 
54
    @Override
55
    protected void setupElements(SQLElementDirectory dir) {
56
        final ComptaPropsConfiguration conf = ComptaPropsConfiguration.getInstanceCompta();
57
        conf.getFieldMapper().addMapperStreamFromClass(ModuleOperation.class);
58
 
59
        dir.addSQLElement(new SiteSQLElement(this));
60
        dir.addSQLElement(new OperationSQLElement(this));
61
 
62
        // SQLRequestLog.setEnabled(true);
63
        // SQLRequestLog.showFrame();
64
        TemplateManager.getInstance().register(OPERATIONS_REPORT_TEMPLATE_ID);
65
        TemplateManager.getInstance().register(OPERATIONS_REPORT_TEMPLATE2_ID);
66
    }
67
 
68
    @Override
69
    protected void setupMenu(MenuContext ctxt) {
70
        ctxt.addMenuItem(ctxt.createListAction(TABLE_SITE), "operation");
150 ilm 71
        final SQLElement element = ctxt.getElement(TABLE_OPERATION);
112 ilm 72
        final AbstractAction aOperations = new AbstractAction(getTM().trM("listAction.name", "element", element.getName())) {
73
 
74
            @Override
75
            public void actionPerformed(ActionEvent e) {
76
                JFrame f = new JFrame("Gérer les interventions");
77
                f.setContentPane(new OperationHistoryPanel());
78
                f.pack();
79
                f.setLocationRelativeTo(null);
80
                FrameUtil.showPacked(f);
81
 
82
            }
83
        };
84
        ctxt.addMenuItem(aOperations, "operation");
85
 
86
        // final AbstractAction aExport = new
87
        // AbstractAction(TranslationManager.getInstance().getTranslationForMenu("operation.export"))
88
        // {
89
        //
90
        // @Override
91
        // public void actionPerformed(ActionEvent e) {
92
        // JFrame f = new JFrame("Export");
93
        // f.setContentPane(new OperationExportPanel(new OperationCalendarManager("all")));
94
        // f.pack();
95
        // f.setLocationRelativeTo(null);
96
        // FrameUtil.showPacked(f);
97
        //
98
        // }
99
        // };
100
        // ctxt.addMenuItem(aExport, "operation");
101
 
102
        final SQLElement elementSite = ctxt.getElement(TABLE_SITE);
103
        final RowAction editAction = new RowAction(new AbstractAction(TranslationManager.getInstance().getTranslationForMenu("operation.export")) {
104
 
105
            @Override
106
            public void actionPerformed(ActionEvent e) {
107
                final List<SQLRowValues> rows = IListe.get(e).getSelectedRows();
108
                JFrame f = new JFrame("Export");
109
                f.setContentPane(new OperationExportPanel(new OperationCalendarManager("all"), rows));
110
                f.pack();
111
                f.setLocationRelativeTo(null);
112
                FrameUtil.showPacked(f);
113
            }
114
        }, true) {
115
            @Override
183 ilm 116
            public boolean enabledFor(List<SQLRowValues> values) {
112 ilm 117
                return true;
118
            }
119
 
120
        };
121
        // editAction.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
122
        elementSite.getRowActions().add(editAction);
123
 
124
    }
125
 
126
    @Override
127
    protected void setupComponents(ComponentsContext ctxt) {
128
 
129
    }
130
 
131
    @Override
153 ilm 132
    protected void install(DBContext ctxt) throws SQLException, IOException {
112 ilm 133
        super.install(ctxt);
134
        if (ctxt.getRoot().getTable(TABLE_SITE) == null) {
135
            final SQLCreateTable createTableSite = ctxt.getCreateTable(TABLE_SITE);
136
            createTableSite.addVarCharColumn("NAME", 500);
137
            createTableSite.addForeignColumn("CLIENT");
138
            createTableSite.addForeignColumn("ADRESSE");
139
            createTableSite.addVarCharColumn("COMMENT", 10000);
140
            createTableSite.addVarCharColumn("INFO", 10000);
141
            final SQLCreateTable createTableOperation = ctxt.getCreateTable(TABLE_OPERATION);
142
            createTableOperation.addForeignColumn(createTableSite);
143
            createTableOperation.addForeignColumn("ID_USER_COMMON", ctxt.getRoot().findTable("USER_COMMON"));
144
            createTableOperation.addVarCharColumn("TYPE", 200);
145
            createTableOperation.addVarCharColumn("STATUS", 200);
146
            createTableOperation.addForeignColumn("CALENDAR_ITEM_GROUP");
147
            createTableOperation.addVarCharColumn("DESCRIPTION", 10000);
148
            createTableOperation.addVarCharColumn("PLANNER_UID", 2048);
149
            createTableOperation.addVarCharColumn("PLANNER_XML", 2048);
181 ilm 150
 
151
            ctxt.executeSQL();
152
            // SQLTable.setUndefID(ctxt.getRoot().getSchema(), TABLE_SITE, null);
153
            // SQLTable.setUndefID(ctxt.getRoot().getSchema(), TABLE_OPERATION, null);
112 ilm 154
        }
155
    }
156
 
157
    static OperationCalendarPanel comp;
158
    static DailyOperationCalendarPanel comp2;
159
 
160
    @Override
161
    protected void start() {
162
        ImageIcon icon = new ImageIcon(DayView.class.getResource("auto.png"));
163
        Flag.register(new Flag("planned", icon, "Planifié", ""));
164
        icon = new ImageIcon(LockAction.class.getResource("locked.png"));
165
        Flag.register(new Flag("locked", icon, "Verrouillé", ""));
150 ilm 166
        Flag.register(FREE_TIME_FLAG);
112 ilm 167
 
150 ilm 168
        final ComptaPropsConfiguration conf = ComptaPropsConfiguration.getInstanceCompta();
169
        comp = new OperationCalendarPanel(conf);
170
        comp2 = new DailyOperationCalendarPanel(conf);
112 ilm 171
 
172
        MainFrame.getInstance().getTabbedPane().addTab("Planning", comp);
173
        MainFrame.getInstance().getTabbedPane().addTab("Planning journalier", comp2);
141 ilm 174
        MainFrame.getInstance().getTabbedPane().addTab("Chantiers", new GanttChantierPanel());
112 ilm 175
        MainFrame.getInstance().getTabbedPane().setSelectedIndex(1);
176
    }
177
 
178
    public static void reloadCalendars() {
179
        if (!SwingUtilities.isEventDispatchThread()) {
180
            throw new IllegalAccessError("Must be called in EDT");
181
        }
182
        comp.reload();
183
        comp2.reload();
184
    }
185
 
186
    @Override
187
    protected void stop() {
188
    }
189
 
190
    public static List<Long> getOperationIdsFrom(Set<JCalendarItemDB> toDelete) {
191
        final List<Long> ids = new ArrayList<Long>();
192
        for (JCalendarItemDB jCalendarItemDB : toDelete) {
150 ilm 193
            if (jCalendarItemDB.getSourceElement().getClass() != OperationSQLElement.class) {
194
                throw new IllegalStateException("Table is not " + ModuleOperation.TABLE_OPERATION + " but " + jCalendarItemDB.getSource());
112 ilm 195
 
196
            }
150 ilm 197
            final long sourceId = jCalendarItemDB.getSource().getID();
112 ilm 198
            if (!ids.contains(sourceId)) {
199
                ids.add(sourceId);
200
            }
201
        }
202
        return ids;
203
    }
204
 
205
    public static Set<JCalendarItemDB> getItemDB(List<JCalendarItemPart> parts) {
206
        Set<JCalendarItemDB> result = new HashSet<JCalendarItemDB>();
207
        for (JCalendarItemPart part : parts) {
208
            JCalendarItem item = part.getItem();
209
            if (item instanceof JCalendarItemDB) {
210
                result.add((JCalendarItemDB) item);
211
            }
212
        }
213
        return result;
214
    }
215
}