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