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