OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
74 ilm 1
package org.openconcerto.modules.extensionbuilder.list;
2
 
3
import java.util.HashSet;
4
import java.util.List;
5
import java.util.Set;
6
 
7
import javax.swing.tree.DefaultMutableTreeNode;
8
import javax.swing.tree.DefaultTreeModel;
9
 
10
import org.openconcerto.erp.config.ComptaPropsConfiguration;
11
import org.openconcerto.modules.extensionbuilder.Extension;
12
import org.openconcerto.modules.extensionbuilder.table.FieldDescriptor;
13
import org.openconcerto.modules.extensionbuilder.table.TableDescritor;
14
import org.openconcerto.sql.model.SQLField;
15
import org.openconcerto.sql.model.SQLTable;
16
 
17
public class FieldTreeModel extends DefaultTreeModel {
18
 
19
    private Extension extension;
20
    private Set<FieldDescriptor> allFields = new HashSet<FieldDescriptor>();
21
 
22
    public FieldTreeModel(Extension extension) {
23
        super(null, false);
24
        this.extension = extension;
25
    }
26
 
27
    public void fillFromTable(String table) {
28
        System.out.println("FieldTreeModel.fillFromTable():" + table);
29
        final DefaultMutableTreeNode root = new DefaultMutableTreeNode();
30
        if (table == null) {
31
            this.setRoot(root);
32
            return;
33
        }
34
        addToTreeNode(root, table, 0);
35
        this.setRoot(root);
36
    }
37
 
38
    void addToTreeNode(DefaultMutableTreeNode node, String tableName, int depth) {
39
        if (depth > 4) {
40
            return;
41
        }
42
        depth++;
43
        // 1/ On regarde depuis le CreateTable
44
        TableDescritor desc = this.extension.getTableListDescriptor(tableName);
45
        if (desc == null) {
46
            // 2/ On regarde dans la base
47
            try {
48
                SQLTable t = ComptaPropsConfiguration.getInstanceCompta().getRootSociete().getTable(tableName);
49
                if (t != null) {
50
                    desc = new TableDescritor(t.getName());
51
                    Set<SQLField> fields = t.getFields();
52
                    for (SQLField sqlField : fields) {
53
                        String name = sqlField.getName();
54
                        String table = null;
55
                        final SQLTable foreignTable = sqlField.getForeignTable();
56
                        if (foreignTable != null && foreignTable.getDBRoot().equals(t.getDBRoot())) {
57
                            table = foreignTable.getName();
58
                        }
59
                        if (!sqlField.isPrimaryKey() && !name.equals("ORDRE") && !name.equals("ARCHIVE") && !name.startsWith("ID_USER_COMMON")) {
60
                            FieldDescriptor d = new FieldDescriptor(tableName, name, "", "", "", table);
61
                            desc.add(d);
62
                        }
63
                    }
64
                    desc.sortFields();
65
                }
66
            } catch (Exception e) {
67
                desc = null;
68
            }
69
        }
70
        if (desc != null) {
71
            List<FieldDescriptor> fields = desc.getFields();
72
            for (FieldDescriptor fieldDescriptor : fields) {
73
                final DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(fieldDescriptor);
74
                node.add(newChild);
75
                this.allFields.add(fieldDescriptor);
76
                if (fieldDescriptor.getForeignTable() != null) {
77
                    addToTreeNode(newChild, fieldDescriptor.getForeignTable(), depth);
78
                    newChild.setAllowsChildren(true);
79
                } else {
80
                    newChild.setAllowsChildren(false);
81
                }
82
            }
181 ilm 83
            this.setRoot(this.root);
74 ilm 84
        }
85
    }
86
 
87
    public boolean containsFieldDescritor(FieldDescriptor d) {
88
        return this.allFields.contains(d);
89
    }
90
}