OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 121 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
99 ilm 1
package org.openconcerto.modules.card;
2
 
3
import java.io.IOException;
153 ilm 4
import java.sql.SQLException;
99 ilm 5
import java.util.ArrayList;
6
import java.util.Arrays;
7
import java.util.List;
8
 
9
import org.openconcerto.erp.config.MainFrame;
10
import org.openconcerto.erp.modules.AbstractModule;
11
import org.openconcerto.erp.modules.AlterTableRestricted;
12
import org.openconcerto.erp.modules.ComponentsContext;
13
import org.openconcerto.erp.modules.DBContext;
14
import org.openconcerto.erp.modules.MenuContext;
15
import org.openconcerto.erp.modules.ModuleElement;
16
import org.openconcerto.erp.modules.ModuleFactory;
17
import org.openconcerto.erp.modules.ModulePreferencePanel;
18
import org.openconcerto.erp.modules.ModulePreferencePanelDesc;
19
import org.openconcerto.sql.element.SQLComponent;
20
import org.openconcerto.sql.element.SQLElement.ReferenceAction;
21
import org.openconcerto.sql.element.SQLElementDirectory;
22
import org.openconcerto.sql.element.UISQLComponent;
23
import org.openconcerto.sql.model.SQLTable;
24
import org.openconcerto.sql.utils.SQLCreateTable;
121 ilm 25
import org.openconcerto.utils.ListMap;
99 ilm 26
import org.openconcerto.utils.PrefType;
27
 
28
public final class ModuleCard extends AbstractModule {
29
 
30
    private static final String TABLE_NAME = "FIDELITY_CARD";
31
 
32
    public ModuleCard(ModuleFactory f) throws IOException {
33
        super(f);
34
 
35
    }
36
 
37
    @Override
153 ilm 38
    protected void install(DBContext ctxt) throws SQLException, IOException {
99 ilm 39
        super.install(ctxt);
40
        // TODO use version to upgrade
41
        if (!ctxt.getTablesPreviouslyCreated().contains(TABLE_NAME)) {
42
            final SQLCreateTable createTable = ctxt.getCreateTable(TABLE_NAME);
43
            createTable.addVarCharColumn("SERIAL", 64);
44
            createTable.addColumn("POINTS", "int NOT NULL DEFAULT 0");
45
            createTable.addDateAndTimeColumn("EXPIRATION_DATE");
46
 
47
            final AlterTableRestricted alterTable = ctxt.getAlterTable("CLIENT");
48
            alterTable.addForeignColumn("ID_FIDELITY_CARD", createTable);
49
        }
50
    }
51
 
52
    @Override
53
    protected void setupElements(SQLElementDirectory dir) {
54
        super.setupElements(dir);
55
        final ModuleElement fidCardElement = new ModuleElement(this, TABLE_NAME) {
56
 
57
            @Override
58
            protected List<String> getListFields() {
59
                final List<String> l = new ArrayList<String>();
60
                l.add("SERIAL");
61
                l.add("POINTS");
62
                l.add("EXPIRATION_DATE");
63
                return l;
64
            }
65
 
66
            @Override
67
            protected List<String> getComboFields() {
68
                final List<String> l = new ArrayList<String>();
69
                l.add("SERIAL");
70
                l.add("POINTS");
71
                return l;
72
            }
73
 
74
            @Override
121 ilm 75
            public ListMap<String, String> getShowAs() {
76
                return ListMap.singleton(null, getComboFields());
99 ilm 77
            }
78
 
79
            @Override
80
            protected SQLComponent createComponent() {
81
                return new UISQLComponent(this) {
82
                    @Override
83
                    protected void addViews() {
84
                        this.addView("SERIAL");
85
                        this.addView("POINTS");
86
                        this.addView("EXPIRATION_DATE");
87
                    }
88
                };
89
            }
90
        };
91
 
92
        dir.addSQLElement(fidCardElement);
93
        final SQLTable clientT = fidCardElement.getTable().findReferentTable("CLIENT");
94
        // do not delete a client
95
        dir.getElement(clientT).setAction("ID_FIDELITY_CARD", ReferenceAction.SET_EMPTY);
96
    }
97
 
98
    @Override
99
    protected void setupComponents(ComponentsContext ctxt) {
100
        ctxt.putAdditionalField("CLIENT", "ID_FIDELITY_CARD");
101
    }
102
 
103
    @Override
104
    protected void setupMenu(MenuContext ctxt) {
105
        ctxt.addMenuItem(ctxt.createListAction(TABLE_NAME), MainFrame.LIST_MENU);
106
    }
107
 
108
    @Override
109
    protected void start() {
110
    }
111
 
112
    @Override
113
    public List<ModulePreferencePanelDesc> getPrefDescriptors() {
114
        return Arrays.<ModulePreferencePanelDesc> asList(new ModulePreferencePanelDesc("Préf") {
115
            @Override
116
            protected ModulePreferencePanel createPanel() {
117
                return new ModulePreferencePanel("Mon super titre") {
118
                    @Override
119
                    protected void addViews() {
120
                        this.addView(new SQLPrefView<Boolean>(PrefType.BOOLEAN_TYPE, "Un booléen", "boolPref").setDefaultValue(Boolean.TRUE));
121
                        this.addView(new SQLPrefView<String>(PrefType.STRING_TYPE, 12, "Du texte", "textPref").setDefaultValue("court"));
122
                        this.addView(new SQLPrefView<Double>(PrefType.DOUBLE_TYPE, "Un double", "doublePref"));
123
                    }
124
                };
125
            }
126
        }, new ModulePreferencePanelDesc("Préf2") {
127
            @Override
128
            protected ModulePreferencePanel createPanel() {
129
                return new ModulePreferencePanel("Mon super titre") {
130
                    @Override
131
                    protected void addViews() {
132
                        this.addView(new SQLPrefView<String>(PrefType.STRING_TYPE, 512, "Du texte", "textPref").setDefaultValue("long texte"));
133
                        this.addView(new SQLPrefView<Long>(PrefType.LONG_TYPE, "Un long", "longPref"));
134
                    }
135
                };
136
            }
137
        }.setLocal(false).setKeywords("toto", "vélo"));
138
    }
139
 
140
    @Override
141
    protected void stop() {
142
    }
143
}