OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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