OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 162 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 162 Rev 181
1
package org.openconcerto.modules.extensionbuilder.table;
1
package org.openconcerto.modules.extensionbuilder.table;
2
 
2
 
3
import java.util.ArrayList;
3
import java.util.ArrayList;
4
import java.util.Collections;
4
import java.util.Collections;
5
import java.util.Comparator;
5
import java.util.Comparator;
6
import java.util.List;
6
import java.util.List;
7
 
7
 
8
import javax.swing.JFrame;
8
import javax.swing.JFrame;
9
import javax.swing.JOptionPane;
9
import javax.swing.JOptionPane;
10
 
10
 
11
import org.openconcerto.erp.config.ComptaPropsConfiguration;
11
import org.openconcerto.erp.config.ComptaPropsConfiguration;
12
import org.openconcerto.erp.config.Log;
12
import org.openconcerto.erp.config.Log;
13
import org.openconcerto.modules.extensionbuilder.Extension;
13
import org.openconcerto.modules.extensionbuilder.Extension;
14
import org.openconcerto.modules.extensionbuilder.ExtensionGroupSQLComponent;
14
import org.openconcerto.modules.extensionbuilder.ExtensionGroupSQLComponent;
15
import org.openconcerto.modules.extensionbuilder.component.ComponentDescritor;
15
import org.openconcerto.modules.extensionbuilder.component.ComponentDescritor;
16
import org.openconcerto.modules.extensionbuilder.list.ColumnDescriptor;
16
import org.openconcerto.modules.extensionbuilder.list.ColumnDescriptor;
17
import org.openconcerto.modules.extensionbuilder.list.ListDescriptor;
17
import org.openconcerto.modules.extensionbuilder.list.ListDescriptor;
18
import org.openconcerto.sql.element.SQLComponent;
18
import org.openconcerto.sql.element.SQLComponent;
19
import org.openconcerto.sql.element.SQLElement;
19
import org.openconcerto.sql.element.SQLElement;
20
import org.openconcerto.sql.model.DBRoot;
20
import org.openconcerto.sql.model.DBRoot;
21
import org.openconcerto.sql.model.SQLField;
21
import org.openconcerto.sql.model.SQLField;
22
import org.openconcerto.sql.model.SQLTable;
22
import org.openconcerto.sql.model.SQLTable;
23
import org.openconcerto.sql.request.ListSQLRequest;
23
import org.openconcerto.sql.request.ListSQLRequest;
24
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
24
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
25
 
25
 
26
public class TableDescritor {
26
public class TableDescritor {
27
    private String name;
27
    private String name;
28
    private List<FieldDescriptor> fields = new ArrayList<FieldDescriptor>();
28
    private List<FieldDescriptor> fields = new ArrayList<FieldDescriptor>();
29
 
29
 
30
    public TableDescritor(String string) {
30
    public TableDescritor(String string) {
31
        this.name = string;
31
        this.name = string;
32
 
32
 
33
    }
33
    }
34
 
34
 
35
    public void createElement(final Extension ext) {
35
    public void createElement(final Extension ext) {
36
        // Create elements
36
        // Create elements
37
        ComptaPropsConfiguration conf = ComptaPropsConfiguration.getInstanceCompta();
37
        ComptaPropsConfiguration conf = ComptaPropsConfiguration.getInstanceCompta();
38
        DBRoot root = conf.getRootSociete();
38
        DBRoot root = conf.getRootSociete();
39
        if (conf.getDirectory().getElement(name) == null) {
39
        if (conf.getDirectory().getElement(this.name) == null) {
40
            final SQLTable table = root.getTable(name);
40
            final SQLTable table = root.getTable(this.name);
41
            final SQLElement e = new SQLElement("ext." + name, "ext." + name, table) {
41
            final SQLElement e = new SQLElement("ext." + this.name, "ext." + this.name, table) {
42
 
42
 
43
                @Override
43
                @Override
44
                protected List<String> getListFields() {
44
                protected List<String> getListFields() {
45
                    return new ArrayList<String>(0);
45
                    return new ArrayList<String>(0);
46
                }
46
                }
47
 
47
 
48
                @Override
48
                @Override
49
                protected List<String> getComboFields() {
49
                protected List<String> getComboFields() {
50
                    List<String> l = new ArrayList<String>();
50
                    List<String> l = new ArrayList<String>();
51
                    int matchCodeNom = 0;
51
                    int matchCodeNom = 0;
52
                    for (SQLField field : getTable().getContentFields()) {
52
                    for (SQLField field : getTable().getContentFields()) {
53
                        l.add(field.getName());
53
                        l.add(field.getName());
54
                        if (field.getName().equalsIgnoreCase("CODE") || field.getName().equalsIgnoreCase("NOM")) {
54
                        if (field.getName().equalsIgnoreCase("CODE") || field.getName().equalsIgnoreCase("NOM")) {
55
                            matchCodeNom++;
55
                            matchCodeNom++;
56
                        }
56
                        }
57
                    }
57
                    }
58
                    if (matchCodeNom == 2) {
58
                    if (matchCodeNom == 2) {
59
                        l.add("CODE");
59
                        l.add("CODE");
60
                        l.add("NOM");
60
                        l.add("NOM");
61
                    } else {
61
                    } else {
62
                        ListDescriptor listDesc = null;
62
                        ListDescriptor listDesc = null;
-
 
63
                        l.clear();
63
                        for (ListDescriptor listDescriptor : ext.getCreateListList()) {
64
                        for (ListDescriptor listDescriptor : ext.getCreateListList()) {
64
                            if (listDescriptor.getMainTable().equals(getTable().getName())) {
65
                            if (listDescriptor.getMainTable().equals(getTable().getName())) {
65
                                listDesc = listDescriptor;
66
                                listDesc = listDescriptor;
66
                                break;
67
                                break;
67
                            }
68
                            }
68
                        }
69
                        }
-
 
70
                        if (listDesc != null) {
69
                        for (ColumnDescriptor string : listDesc.getColumns()) {
71
                            for (ColumnDescriptor string : listDesc.getColumns()) {
70
                            if (!string.getFieldsPaths().contains(".")) {
72
                                if (!string.getFieldsPaths().contains(".")) {
71
                                l.add(string.getFieldsPaths());
73
                                    l.add(string.getFieldsPaths());
72
                                break;
74
                                    break;
73
                            }
75
                                }
74
                        }
76
                            }
75
                    }
77
                        }
-
 
78
                        if (l.isEmpty()) {
-
 
79
                            if (!getTable().getContentFields().isEmpty()) {
-
 
80
                                l.add(new ArrayList<>(getTable().getContentFields()).get(0).getName());
-
 
81
                            }
-
 
82
                        }
-
 
83
                    }
76
                    return l;
84
                    return l;
77
                }
85
                }
78
 
86
 
79
                @Override
87
                @Override
80
                protected SQLTableModelSourceOnline instantiateTableSourceOnline(ListSQLRequest req) {
88
                protected SQLTableModelSourceOnline instantiateTableSourceOnline(ListSQLRequest req) {
81
                    ListDescriptor listDesc = null;
89
                    ListDescriptor listDesc = null;
82
                    for (ListDescriptor listDescriptor : ext.getCreateListList()) {
90
                    for (ListDescriptor listDescriptor : ext.getCreateListList()) {
83
                        if (listDescriptor.getMainTable().equals(getTable().getName())) {
91
                        if (listDescriptor.getMainTable().equals(getTable().getName())) {
84
                            listDesc = listDescriptor;
92
                            listDesc = listDescriptor;
85
                            break;
93
                            break;
86
                        }
94
                        }
87
                    }
95
                    }
88
                    if (listDesc != null) {
96
                    if (listDesc != null) {
89
                        return ext.createSource(this, req, listDesc);
97
                        return ext.createSource(this, req, listDesc);
90
                    } else {
98
                    } else {
91
                        return super.instantiateTableSourceOnline(req);
99
                        return super.instantiateTableSourceOnline(req);
92
                    }
100
                    }
93
 
101
 
94
                }
102
                }
95
 
103
 
96
                @Override
104
                @Override
97
                protected SQLComponent createComponent() {
105
                protected SQLComponent createComponent() {
98
 
106
 
99
                    for (final ComponentDescritor cDescriptor : ext.getCreateComponentList()) {
107
                    for (final ComponentDescritor cDescriptor : ext.getCreateComponentList()) {
100
                        if (cDescriptor.getTable().equals(table.getTable().getName())) {
108
                        if (cDescriptor.getTable().equals(table.getTable().getName())) {
101
                            return new ExtensionGroupSQLComponent(this, cDescriptor.getGroup());
109
                            return new ExtensionGroupSQLComponent(this, cDescriptor.getGroup());
102
                        }
110
                        }
103
                    }
111
                    }
104
                    JOptionPane.showMessageDialog(new JFrame(), "Unable to create default creation component for table " + name);
112
                    JOptionPane.showMessageDialog(new JFrame(), "Unable to create default creation component for table " + TableDescritor.this.name);
105
                    return null;
113
                    return null;
106
                }
114
                }
107
            };
115
            };
108
            conf.getDirectory().addSQLElement(e);
116
            conf.getDirectory().addSQLElement(e);
109
            Log.get().info("Autocreate element for table: " + table.getName());
117
            Log.get().info("Autocreate element for table: " + table.getName());
110
        }
118
        }
111
    }
119
    }
112
 
120
 
113
    public String getName() {
121
    public String getName() {
114
        return name;
122
        return this.name;
115
    }
123
    }
116
 
124
 
117
    public void setName(String name) {
125
    public void setName(String name) {
118
        this.name = name;
126
        this.name = name;
119
    }
127
    }
120
 
128
 
121
    public List<FieldDescriptor> getFields() {
129
    public List<FieldDescriptor> getFields() {
122
        return fields;
130
        return this.fields;
123
    }
131
    }
124
 
132
 
125
    public void add(FieldDescriptor f) {
133
    public void add(FieldDescriptor f) {
126
        fields.add(f);
134
        this.fields.add(f);
127
 
135
 
128
    }
136
    }
129
 
137
 
130
    public void remove(FieldDescriptor field) {
138
    public void remove(FieldDescriptor field) {
131
        fields.remove(field);
139
        this.fields.remove(field);
132
 
140
 
133
    }
141
    }
134
 
142
 
135
    @Override
143
    @Override
136
    public String toString() {
144
    public String toString() {
137
        return name;
145
        return this.name;
138
    }
146
    }
139
 
147
 
140
    public void sortFields() {
148
    public void sortFields() {
141
        Collections.sort(this.fields, new Comparator<FieldDescriptor>() {
149
        Collections.sort(this.fields, new Comparator<FieldDescriptor>() {
142
 
150
 
143
            @Override
151
            @Override
144
            public int compare(FieldDescriptor o1, FieldDescriptor o2) {
152
            public int compare(FieldDescriptor o1, FieldDescriptor o2) {
145
                return o1.getName().compareToIgnoreCase(o2.getName());
153
                return o1.getName().compareToIgnoreCase(o2.getName());
146
            }
154
            }
147
        });
155
        });
148
 
156
 
149
    }
157
    }
150
 
158
 
151
}
159
}