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