1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.efaps.db.search;
22
23 import org.apache.commons.lang3.builder.ToStringBuilder;
24 import org.efaps.admin.datamodel.Attribute;
25 import org.efaps.db.AbstractObjectQuery;
26 import org.efaps.db.search.compare.AbstractQAttrCompare;
27 import org.efaps.db.wrapper.SQLPart;
28 import org.efaps.db.wrapper.SQLSelect;
29 import org.efaps.util.EFapsException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35
36
37
38
39 public class QAttribute
40 extends AbstractQPart
41 {
42
43
44
45
46 private static final Logger LOG = LoggerFactory.getLogger(QAttribute.class);
47
48
49
50
51 private Attribute attribute;
52
53
54
55
56 private final String attributeName;
57
58
59
60
61 private Integer tableIndex;
62
63
64
65
66 private boolean ignoreCase = false;
67
68
69
70
71 public QAttribute(final Attribute _attribute)
72 {
73 this.attribute = _attribute;
74 this.attributeName = this.attribute.getName();
75 }
76
77
78
79
80 public QAttribute(final String _attributeName)
81 {
82 this.attributeName = _attributeName;
83 }
84
85
86
87
88 @Override
89 public AbstractQPart prepare(final AbstractObjectQuery<?> _query,
90 final AbstractQPart _part)
91 throws EFapsException
92 {
93 if (_part instanceof AbstractQAttrCompare) {
94 this.ignoreCase = ((AbstractQAttrCompare) _part).isIgnoreCase();
95 }
96 if (this.attribute == null) {
97 if (_query.getBaseType().getAttributes().containsKey(this.attributeName)) {
98 this.attribute = _query.getBaseType().getAttribute(this.attributeName);
99 } else {
100 QAttribute.LOG.error("Could not get attribute with Name '{}' for type: '{}'", this.attributeName,
101 _query.getBaseType());
102 throw new EFapsException(getClass(), "prepare", this.attributeName);
103 }
104 }
105 this.tableIndex = _query.getIndex4SqlTable(this.attribute.getTable());
106 return this;
107 }
108
109
110
111
112 @Override
113 public AbstractQPart appendSQL(final SQLSelect _sql)
114 {
115 if (this.ignoreCase) {
116 _sql.addPart(SQLPart.UPPER).addPart(SQLPart.PARENTHESIS_OPEN);
117 }
118 _sql.addColumnPart(this.tableIndex, this.attribute.getSqlColNames().get(0));
119 if (this.ignoreCase) {
120 _sql.addPart(SQLPart.PARENTHESIS_CLOSE);
121 }
122 return this;
123 }
124
125
126
127
128
129
130 public Attribute getAttribute()
131 {
132 return this.attribute;
133 }
134
135 @Override
136 public String toString()
137 {
138 return new ToStringBuilder(this)
139 .append("attributeName", this.attributeName)
140 .append("attribute", this.attribute).toString();
141 }
142 }