1
22
23 package com.liferay.portlet.journal.service.persistence;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.util.OrderByComparator;
27 import com.liferay.portal.kernel.util.StringPool;
28 import com.liferay.portal.kernel.util.StringUtil;
29 import com.liferay.portal.kernel.util.Validator;
30 import com.liferay.portal.spring.hibernate.CustomSQLUtil;
31 import com.liferay.portal.spring.hibernate.HibernateUtil;
32 import com.liferay.portlet.journal.model.impl.JournalStructureImpl;
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 JournalStructureFinder {
50
51 public static String COUNT_BY_C_G_S_N_D =
52 JournalStructureFinder.class.getName() + ".countByC_G_S_N_D";
53
54 public static String FIND_BY_C_G_S_N_D =
55 JournalStructureFinder.class.getName() + ".findByC_G_S_N_D";
56
57 public static int countByKeywords(
58 long companyId, long groupId, String keywords)
59 throws SystemException {
60
61 String[] structureIds = null;
62 String[] names = null;
63 String[] descriptions = null;
64 boolean andOperator = false;
65
66 if (Validator.isNotNull(keywords)) {
67 structureIds = CustomSQLUtil.keywords(keywords, false);
68 names = CustomSQLUtil.keywords(keywords);
69 descriptions = CustomSQLUtil.keywords(keywords);
70 }
71 else {
72 andOperator = true;
73 }
74
75 return countByC_G_S_N_D(
76 companyId, groupId, structureIds, names, descriptions, andOperator);
77 }
78
79 public static int countByC_G_S_N_D(
80 long companyId, long groupId, String structureId, String name,
81 String description, boolean andOperator)
82 throws SystemException {
83
84 return countByC_G_S_N_D(
85 companyId, groupId, new String[] {structureId}, new String[] {name},
86 new String[] {description}, andOperator);
87 }
88
89 public static int countByC_G_S_N_D(
90 long companyId, long groupId, String[] structureIds, String[] names,
91 String[] descriptions, boolean andOperator)
92 throws SystemException {
93
94 structureIds = CustomSQLUtil.keywords(structureIds, false);
95 names = CustomSQLUtil.keywords(names);
96 descriptions = CustomSQLUtil.keywords(descriptions);
97
98 Session session = null;
99
100 try {
101 session = HibernateUtil.openSession();
102
103 String sql = CustomSQLUtil.get(COUNT_BY_C_G_S_N_D);
104
105 if (groupId <= 0) {
106 sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
107 }
108
109 sql = CustomSQLUtil.replaceKeywords(
110 sql, "structureId", StringPool.LIKE, false, structureIds);
111 sql = CustomSQLUtil.replaceKeywords(
112 sql, "lower(name)", StringPool.LIKE, false, names);
113 sql = CustomSQLUtil.replaceKeywords(
114 sql, "lower(description)", StringPool.LIKE, true, descriptions);
115
116 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
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(companyId);
125
126 if (groupId > 0) {
127 qPos.add(groupId);
128 }
129
130 qPos.add(structureIds, 2);
131 qPos.add(names, 2);
132 qPos.add(descriptions, 2);
133
134 Iterator itr = q.list().iterator();
135
136 if (itr.hasNext()) {
137 Long count = (Long)itr.next();
138
139 if (count != null) {
140 return count.intValue();
141 }
142 }
143
144 return 0;
145 }
146 catch (Exception e) {
147 throw new SystemException(e);
148 }
149 finally {
150 HibernateUtil.closeSession(session);
151 }
152 }
153
154 public static List findByKeywords(
155 long companyId, long groupId, String keywords, int begin, int end,
156 OrderByComparator obc)
157 throws SystemException {
158
159 String[] structureIds = null;
160 String[] names = null;
161 String[] descriptions = null;
162 boolean andOperator = false;
163
164 if (Validator.isNotNull(keywords)) {
165 structureIds = CustomSQLUtil.keywords(keywords, false);
166 names = CustomSQLUtil.keywords(keywords);
167 descriptions = CustomSQLUtil.keywords(keywords);
168 }
169 else {
170 andOperator = true;
171 }
172
173 return findByC_G_S_N_D(
174 companyId, groupId, structureIds, names, descriptions, andOperator,
175 begin, end, obc);
176 }
177
178 public static List findByC_G_S_N_D(
179 long companyId, long groupId, String structureId, String name,
180 String description, boolean andOperator, int begin, int end,
181 OrderByComparator obc)
182 throws SystemException {
183
184 return findByC_G_S_N_D(
185 companyId, groupId, new String[] {structureId}, new String[] {name},
186 new String[] {description}, andOperator, begin, end, obc);
187 }
188
189 public static List findByC_G_S_N_D(
190 long companyId, long groupId, String[] structureIds, String[] names,
191 String[] descriptions, boolean andOperator, int begin, int end,
192 OrderByComparator obc)
193 throws SystemException {
194
195 structureIds = CustomSQLUtil.keywords(structureIds, false);
196 names = CustomSQLUtil.keywords(names);
197 descriptions = CustomSQLUtil.keywords(descriptions);
198
199 Session session = null;
200
201 try {
202 session = HibernateUtil.openSession();
203
204 String sql = CustomSQLUtil.get(FIND_BY_C_G_S_N_D);
205
206 if (groupId <= 0) {
207 sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
208 }
209
210 sql = CustomSQLUtil.replaceKeywords(
211 sql, "structureId", StringPool.LIKE, false, structureIds);
212 sql = CustomSQLUtil.replaceKeywords(
213 sql, "lower(name)", StringPool.LIKE, false, names);
214 sql = CustomSQLUtil.replaceKeywords(
215 sql, "lower(description)", StringPool.LIKE, true, descriptions);
216
217 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
218 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
219
220 SQLQuery q = session.createSQLQuery(sql);
221
222 q.addEntity("JournalStructure", JournalStructureImpl.class);
223
224 QueryPos qPos = QueryPos.getInstance(q);
225
226 qPos.add(companyId);
227
228 if (groupId > 0) {
229 qPos.add(groupId);
230 }
231
232 qPos.add(structureIds, 2);
233 qPos.add(names, 2);
234 qPos.add(descriptions, 2);
235
236 return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
237 }
238 catch (Exception e) {
239 throw new SystemException(e);
240 }
241 finally {
242 HibernateUtil.closeSession(session);
243 }
244 }
245
246 }