1
22
23 package com.liferay.portlet.tags.service.persistence;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.util.GetterUtil;
27 import com.liferay.portal.kernel.util.StringMaker;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.kernel.util.StringUtil;
30 import com.liferay.portal.spring.hibernate.CustomSQLUtil;
31 import com.liferay.portal.spring.hibernate.HibernateUtil;
32 import com.liferay.portlet.tags.model.TagsEntry;
33 import com.liferay.portlet.tags.model.impl.TagsEntryImpl;
34 import com.liferay.util.dao.hibernate.QueryPos;
35 import com.liferay.util.dao.hibernate.QueryUtil;
36
37 import java.util.Iterator;
38 import java.util.List;
39
40 import org.hibernate.Hibernate;
41 import org.hibernate.SQLQuery;
42 import org.hibernate.Session;
43
44
50 public class TagsEntryFinderImpl implements TagsEntryFinder {
51
52 public static String COUNT_BY_C_N_P =
53 TagsEntryFinder.class.getName() + ".countByC_N_P";
54
55 public static String COUNT_BY_G_C_C_N =
56 TagsEntryFinder.class.getName() + ".countByG_C_C_N";
57
58 public static String FIND_BY_C_N_P =
59 TagsEntryFinder.class.getName() + ".findByC_N_P";
60
61 public static String FIND_BY_G_C_C_N =
62 TagsEntryFinder.class.getName() + ".findByG_C_C_N";
63
64 public int countByC_N_P(long companyId, String name, String[] properties)
65 throws SystemException {
66
67 Session session = null;
68
69 try {
70 session = HibernateUtil.openSession();
71
72 String sql = CustomSQLUtil.get(COUNT_BY_C_N_P);
73
74 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(properties));
75
76 SQLQuery q = session.createSQLQuery(sql);
77
78 q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
79
80 QueryPos qPos = QueryPos.getInstance(q);
81
82 setJoin(qPos, properties);
83 qPos.add(companyId);
84 qPos.add(name);
85 qPos.add(name);
86
87 Iterator<Long> itr = q.list().iterator();
88
89 if (itr.hasNext()) {
90 Long count = itr.next();
91
92 if (count != null) {
93 return count.intValue();
94 }
95 }
96
97 return 0;
98 }
99 catch (Exception e) {
100 throw new SystemException(e);
101 }
102 finally {
103 HibernateUtil.closeSession(session);
104 }
105 }
106
107 public int countByG_C_C_N(
108 long groupId, long companyId, long classNameId, String name)
109 throws SystemException {
110
111 Session session = null;
112
113 try {
114 session = HibernateUtil.openSession();
115
116 String sql = CustomSQLUtil.get(COUNT_BY_G_C_C_N);
117
118 SQLQuery q = session.createSQLQuery(sql);
119
120 q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
121
122 QueryPos qPos = QueryPos.getInstance(q);
123
124 qPos.add(groupId);
125 qPos.add(companyId);
126 qPos.add(classNameId);
127 qPos.add(name);
128 qPos.add(name);
129
130 Iterator<Long> itr = q.list().iterator();
131
132 if (itr.hasNext()) {
133 Long count = itr.next();
134
135 if (count != null) {
136 return count.intValue();
137 }
138 }
139
140 return 0;
141 }
142 catch (Exception e) {
143 throw new SystemException(e);
144 }
145 finally {
146 HibernateUtil.closeSession(session);
147 }
148 }
149
150 public List<TagsEntry> findByC_N_P(
151 long companyId, String name, String[] properties)
152 throws SystemException {
153
154 return findByC_N_P(
155 companyId, name, properties, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
156 }
157
158 public List<TagsEntry> findByC_N_P(
159 long companyId, String name, String[] properties, int begin,
160 int end)
161 throws SystemException {
162
163 Session session = null;
164
165 try {
166 session = HibernateUtil.openSession();
167
168 String sql = CustomSQLUtil.get(FIND_BY_C_N_P);
169
170 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(properties));
171
172 SQLQuery q = session.createSQLQuery(sql);
173
174 q.addEntity("TagsEntry", TagsEntryImpl.class);
175
176 QueryPos qPos = QueryPos.getInstance(q);
177
178 setJoin(qPos, properties);
179 qPos.add(companyId);
180 qPos.add(name);
181 qPos.add(name);
182
183 return (List<TagsEntry>)QueryUtil.list(
184 q, HibernateUtil.getDialect(), begin, end);
185 }
186 catch (Exception e) {
187 throw new SystemException(e);
188 }
189 finally {
190 HibernateUtil.closeSession(session);
191 }
192 }
193
194 public List<TagsEntry> findByG_C_C_N(
195 long groupId, long companyId, long classNameId, String name)
196 throws SystemException {
197
198 return findByG_C_C_N(
199 groupId, companyId, classNameId, name, QueryUtil.ALL_POS,
200 QueryUtil.ALL_POS);
201 }
202
203 public List<TagsEntry> findByG_C_C_N(
204 long groupId, long companyId, long classNameId, String name,
205 int begin, int end)
206 throws SystemException {
207
208 Session session = null;
209
210 try {
211 session = HibernateUtil.openSession();
212
213 String sql = CustomSQLUtil.get(FIND_BY_G_C_C_N);
214
215 SQLQuery q = session.createSQLQuery(sql);
216
217 q.addEntity("TagsEntry", TagsEntryImpl.class);
218
219 QueryPos qPos = QueryPos.getInstance(q);
220
221 qPos.add(groupId);
222 qPos.add(companyId);
223 qPos.add(classNameId);
224 qPos.add(name);
225 qPos.add(name);
226
227 return (List<TagsEntry>)QueryUtil.list(
228 q, HibernateUtil.getDialect(), begin, end);
229 }
230 catch (Exception e) {
231 throw new SystemException(e);
232 }
233 finally {
234 HibernateUtil.closeSession(session);
235 }
236 }
237
238 protected String getJoin(String[] properties) {
239 if (properties.length == 0) {
240 return StringPool.BLANK;
241 }
242 else {
243 StringMaker sm = new StringMaker();
244
245 sm.append(" INNER JOIN TagsProperty ON ");
246 sm.append(" (TagsProperty.entryId = TagsEntry.entryId) AND ");
247
248 for (int i = 0; i < properties.length; i++) {
249 sm.append("(TagsProperty.key_ = ? AND ");
250 sm.append("TagsProperty.value = ?) ");
251
252 if ((i + 1) < properties.length) {
253 sm.append(" AND ");
254 }
255 }
256
257 return sm.toString();
258 }
259 }
260
261 protected void setJoin(QueryPos qPos, String[] properties) {
262 for (int i = 0; i < properties.length; i++) {
263 String[] property = StringUtil.split(
264 properties[i], StringPool.COLON);
265
266 String key = StringPool.BLANK;
267
268 if (property.length > 0) {
269 key = GetterUtil.getString(property[0]);
270 }
271
272 String value = StringPool.BLANK;
273
274 if (property.length > 1) {
275 value = GetterUtil.getString(property[1]);
276 }
277
278 qPos.add(key);
279 qPos.add(value);
280 }
281 }
282
283 }