OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

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