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.impl.TagsEntryImpl;
33 import com.liferay.util.dao.hibernate.QueryPos;
34 import com.liferay.util.dao.hibernate.QueryUtil;
35
36 import java.util.Iterator;
37 import java.util.List;
38
39 import org.hibernate.Hibernate;
40 import org.hibernate.SQLQuery;
41 import org.hibernate.Session;
42
43
49 public class TagsEntryFinder {
50
51 public static String COUNT_BY_C_N_P =
52 TagsEntryFinder.class.getName() + ".countByC_N_P";
53
54 public static String FIND_BY_C_N_P =
55 TagsEntryFinder.class.getName() + ".findByC_N_P";
56
57 public static int countByC_N_P(
58 long companyId, String name, String[] properties)
59 throws SystemException {
60
61 Session session = null;
62
63 try {
64 session = HibernateUtil.openSession();
65
66 String sql = CustomSQLUtil.get(COUNT_BY_C_N_P);
67
68 sql = StringUtil.replace(sql, "[$JOIN$]", _getJoin(properties));
69
70 SQLQuery q = session.createSQLQuery(sql);
71
72 q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
73
74 QueryPos qPos = QueryPos.getInstance(q);
75
76 _setJoin(qPos, properties);
77 qPos.add(companyId);
78 qPos.add(name);
79 qPos.add(name);
80
81 Iterator itr = q.list().iterator();
82
83 if (itr.hasNext()) {
84 Long count = (Long)itr.next();
85
86 if (count != null) {
87 return count.intValue();
88 }
89 }
90
91 return 0;
92 }
93 catch (Exception e) {
94 throw new SystemException(e);
95 }
96 finally {
97 HibernateUtil.closeSession(session);
98 }
99 }
100
101 public static List findByC_N_P(
102 long companyId, String name, String[] properties)
103 throws SystemException {
104
105 return findByC_N_P(
106 companyId, name, properties, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
107 }
108
109 public static List findByC_N_P(
110 long companyId, String name, String[] properties, int begin,
111 int end)
112 throws SystemException {
113
114 Session session = null;
115
116 try {
117 session = HibernateUtil.openSession();
118
119 String sql = CustomSQLUtil.get(FIND_BY_C_N_P);
120
121 sql = StringUtil.replace(sql, "[$JOIN$]", _getJoin(properties));
122
123 SQLQuery q = session.createSQLQuery(sql);
124
125 q.addEntity("TagsEntry", TagsEntryImpl.class);
126
127 QueryPos qPos = QueryPos.getInstance(q);
128
129 _setJoin(qPos, properties);
130 qPos.add(companyId);
131 qPos.add(name);
132 qPos.add(name);
133
134 return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
135 }
136 catch (Exception e) {
137 throw new SystemException(e);
138 }
139 finally {
140 HibernateUtil.closeSession(session);
141 }
142 }
143
144 private static String _getJoin(String[] properties) {
145 if (properties.length == 0) {
146 return StringPool.BLANK;
147 }
148 else {
149 StringMaker sm = new StringMaker();
150
151 sm.append(" INNER JOIN TagsProperty ON ");
152 sm.append(" (TagsProperty.entryId = TagsEntry.entryId) AND ");
153
154 for (int i = 0; i < properties.length; i++) {
155 sm.append("(TagsProperty.key_ = ? AND ");
156 sm.append("TagsProperty.value = ?) ");
157
158 if ((i + 1) < properties.length) {
159 sm.append(" AND ");
160 }
161 }
162
163 return sm.toString();
164 }
165 }
166
167 private static void _setJoin(QueryPos qPos, String[] properties) {
168 for (int i = 0; i < properties.length; i++) {
169 String[] property = StringUtil.split(
170 properties[i], StringPool.COLON);
171
172 String key = StringPool.BLANK;
173
174 if (property.length > 0) {
175 key = GetterUtil.getString(property[0]);
176 }
177
178 String value = StringPool.BLANK;
179
180 if (property.length > 1) {
181 value = GetterUtil.getString(property[1]);
182 }
183
184 qPos.add(key);
185 qPos.add(value);
186 }
187 }
188
189 }