OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.modules;
15
 
19 ilm 16
import org.openconcerto.sql.element.SQLComponent;
25 ilm 17
import org.openconcerto.sql.element.SQLElement;
19 ilm 18
import org.openconcerto.sql.element.SQLElementDirectory;
25 ilm 19
import org.openconcerto.sql.model.DBRoot;
19 ilm 20
 
80 ilm 21
import java.io.File;
18 ilm 22
import java.io.IOException;
151 ilm 23
import java.sql.SQLException;
19 ilm 24
import java.util.ArrayList;
25
import java.util.Collections;
26
import java.util.HashMap;
27
import java.util.List;
28
import java.util.Map;
18 ilm 29
 
30
public abstract class AbstractModule {
31
 
32
    private final ModuleFactory factory;
80 ilm 33
    private File localDir;
18 ilm 34
 
35
    public AbstractModule(final ModuleFactory f) throws IOException {
36
        this.factory = f;
80 ilm 37
        this.localDir = null;
18 ilm 38
    }
39
 
40
    public final ModuleFactory getFactory() {
41
        return this.factory;
42
    }
43
 
44
    /**
45
     * The name presented in the UI.
46
     *
47
     * @return an arbitrary name to display.
48
     */
49
    public final String getName() {
50
        return this.getFactory().getName();
51
    }
52
 
53
    public final String getDescription() {
54
        return this.getFactory().getDescription();
55
    }
56
 
57
    public final int getVersion() {
58
        return this.getFactory().getMajorVersion();
59
    }
60
 
80 ilm 61
    final void setLocalDirectory(final File f) {
62
        if (f == null)
63
            throw new NullPointerException("Null dir");
64
        if (this.localDir != null)
65
            throw new IllegalStateException("Already set to " + this.localDir);
66
        this.localDir = f;
67
    }
68
 
19 ilm 69
    /**
80 ilm 70
     * The directory this module should use while running. During installation use
71
     * {@link DBContext#getLocalDirectory()}.
19 ilm 72
     *
80 ilm 73
     * @return the directory for this module.
25 ilm 74
     */
80 ilm 75
    protected final File getLocalDirectory() {
76
        return this.localDir;
25 ilm 77
    }
78
 
79
    /**
80
     * Should create permanent items. NOTE: all structure items created through <code>ctxt</code>
81
     * will be dropped automatically, and similarly all files created in
82
     * {@link DBContext#getLocalDirectory()} will be deleted automatically, i.e. no action is
83
     * necessary in {@link #uninstall(DBRoot)}.
84
     *
19 ilm 85
     * @param ctxt to create database objects.
151 ilm 86
     * @throws SQLException if a DB error occurs.
87
     * @throws IOException if a local error occurs.
19 ilm 88
     */
151 ilm 89
    protected void install(DBContext ctxt) throws SQLException, IOException {
18 ilm 90
 
91
    }
92
 
19 ilm 93
    /**
94
     * Should add elements for the tables of this module. It's also the place to
25 ilm 95
     * {@link SQLElement#setAction(String, SQLElement.ReferenceAction) set actions} for foreign keys
96
     * of this module. NOTE: this method is called as long as the module is installed in the
97
     * database, even if it is stopped.
19 ilm 98
     *
99
     * @param dir the directory where to add elements.
100
     */
101
    protected void setupElements(SQLElementDirectory dir) {
102
    }
103
 
73 ilm 104
    protected void setupMenu(MenuContext menuContext) {
105
    }
106
 
19 ilm 107
    /**
108
     * Called before start() to add fields to {@link SQLComponent}.
109
     *
110
     * @param ctxt context to modify sql components.
111
     */
112
    protected void setupComponents(ComponentsContext ctxt) {
113
    }
114
 
18 ilm 115
    protected abstract void start();
116
 
156 ilm 117
    public List<ModulePreferencePanelDesc> getPrefDescriptors(final DBRoot root) {
19 ilm 118
        return Collections.emptyList();
119
    }
120
 
156 ilm 121
    public final Map<Boolean, List<ModulePreferencePanelDesc>> getPrefDescriptorsByLocation(final DBRoot root) {
19 ilm 122
        final Map<Boolean, List<ModulePreferencePanelDesc>> res = new HashMap<Boolean, List<ModulePreferencePanelDesc>>();
156 ilm 123
        for (final ModulePreferencePanelDesc desc : getPrefDescriptors(root)) {
19 ilm 124
            final Boolean key = desc.isLocal();
125
            final List<ModulePreferencePanelDesc> l;
126
            if (!res.containsKey(key)) {
127
                l = new ArrayList<ModulePreferencePanelDesc>();
128
                res.put(key, l);
129
            } else {
130
                l = res.get(key);
131
            }
132
            l.add(desc);
133
        }
134
        return res;
135
    }
136
 
18 ilm 137
    protected abstract void stop();
138
 
25 ilm 139
    protected void uninstall(DBRoot root) {
18 ilm 140
 
141
    }
156 ilm 142
 
143
    @Override
144
    public String toString() {
145
        return this.getClass().getName() + " created from " + this.getFactory();
146
    }
18 ilm 147
}