1
22
23 package com.liferay.portlet.journal.service.persistence;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.dao.orm.QueryPos;
27 import com.liferay.portal.kernel.dao.orm.QueryUtil;
28 import com.liferay.portal.kernel.dao.orm.SQLQuery;
29 import com.liferay.portal.kernel.dao.orm.Session;
30 import com.liferay.portal.kernel.dao.orm.Type;
31 import com.liferay.portal.kernel.util.OrderByComparator;
32 import com.liferay.portal.kernel.util.StringPool;
33 import com.liferay.portal.kernel.util.StringUtil;
34 import com.liferay.portal.kernel.util.Validator;
35 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
36 import com.liferay.portlet.journal.model.JournalStructure;
37 import com.liferay.portlet.journal.model.impl.JournalStructureImpl;
38 import com.liferay.util.dao.orm.CustomSQLUtil;
39
40 import java.util.Iterator;
41 import java.util.List;
42
43
49 public class JournalStructureFinderImpl
50 extends BasePersistenceImpl implements JournalStructureFinder {
51
52 public static String COUNT_BY_C_G_S_N_D =
53 JournalStructureFinder.class.getName() + ".countByC_G_S_N_D";
54
55 public static String FIND_BY_C_G_S_N_D =
56 JournalStructureFinder.class.getName() + ".findByC_G_S_N_D";
57
58 public int countByKeywords(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 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 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 = 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(COUNT_COLUMN_NAME, Type.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<Long> itr = q.list().iterator();
135
136 if (itr.hasNext()) {
137 Long count = 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 closeSession(session);
151 }
152 }
153
154 public List<JournalStructure> findByKeywords(
155 long companyId, long groupId, String keywords, int start, 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 start, end, obc);
176 }
177
178 public List<JournalStructure> findByC_G_S_N_D(
179 long companyId, long groupId, String structureId, String name,
180 String description, boolean andOperator, int start, 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, start, end, obc);
187 }
188
189 public List<JournalStructure> findByC_G_S_N_D(
190 long companyId, long groupId, String[] structureIds, String[] names,
191 String[] descriptions, boolean andOperator, int start, 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 = 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 (List<JournalStructure>)QueryUtil.list(
237 q, getDialect(), start, end);
238 }
239 catch (Exception e) {
240 throw new SystemException(e);
241 }
242 finally {
243 closeSession(session);
244 }
245 }
246
247 }