1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.efaps.db.print;
23
24 import org.efaps.admin.datamodel.Type;
25 import org.efaps.db.GeneralInstance;
26 import org.efaps.db.wrapper.SQLPart;
27 import org.efaps.db.wrapper.SQLSelect;
28 import org.efaps.util.cache.CacheReloadException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33
34
35
36
37
38
39 public class GenInstSelectPart
40 extends AbstractSelectPart
41 {
42
43
44
45 private static final Logger LOG = LoggerFactory.getLogger(GenInstSelectPart.class);
46
47
48
49
50 private final Type type;
51
52
53
54
55 private Integer tableIdx;
56
57
58
59
60 private boolean addTypeClause = false;
61
62
63
64
65 public GenInstSelectPart(final Type _type)
66 {
67 this.type = _type;
68 }
69
70
71
72
73 @Override
74 public int join(final OneSelect _oneSelect,
75 final SQLSelect _select,
76 final int _relIndex)
77 {
78 this.tableIdx = _oneSelect.getTableIndex(GeneralInstance.TABLENAME,
79 GeneralInstance.ISIDCOLUMN + "_" + GeneralInstance.ISTYPECOLUMN, _relIndex, null);
80 if (this.tableIdx == null) {
81 this.tableIdx = _oneSelect.getNewTableIndex(GeneralInstance.TABLENAME,
82 GeneralInstance.ISIDCOLUMN + "_" + GeneralInstance.ISTYPECOLUMN, _relIndex, null);
83 if (getType().getMainTable().getSqlColType() != null) {
84 _select.leftJoin(GeneralInstance.TABLENAME, this.tableIdx,
85 new String[] {GeneralInstance.ISIDCOLUMN, GeneralInstance.ISTYPECOLUMN},
86 _relIndex, new String[] {"ID", getType().getMainTable().getSqlColType()});
87 } else {
88 _select.leftJoin(GeneralInstance.TABLENAME, this.tableIdx, GeneralInstance.ISIDCOLUMN, _relIndex, "ID");
89 this.addTypeClause = true;
90 }
91 }
92 _select.column(this.tableIdx, "ID");
93 return this.tableIdx;
94 }
95
96
97
98
99 @Override
100 public Type getType()
101 {
102 return this.type;
103 }
104
105
106
107
108 @Override
109 public void add2Where(final OneSelect _oneselect,
110 final SQLSelect _select)
111 {
112 try {
113 if (this.addTypeClause) {
114 _select.addPart(SQLPart.AND).addColumnPart(this.tableIdx, GeneralInstance.ISTYPECOLUMN)
115 .addPart(SQLPart.IN).addPart(SQLPart.PARENTHESIS_OPEN);
116 _select.addValuePart(this.type.getId());
117 for (final Type childType : this.type.getChildTypes()) {
118 _select.addPart(SQLPart.COMMA);
119 _select.addValuePart(childType.getId());
120 }
121 _select.addPart(SQLPart.PARENTHESIS_CLOSE);
122 }
123 } catch (final CacheReloadException e) {
124 GenInstSelectPart.LOG.error("Could not build where part for General Instance: {}", this);
125 }
126 }
127 }