001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.service.persistence;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.dao.orm.SQLQuery;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.dao.orm.Type;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.OrderByComparator;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
029    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
030    import com.liferay.portlet.journal.model.JournalStructure;
031    import com.liferay.portlet.journal.model.impl.JournalStructureImpl;
032    import com.liferay.util.dao.orm.CustomSQLUtil;
033    
034    import java.util.Iterator;
035    import java.util.List;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Connor McKay
040     */
041    public class JournalStructureFinderImpl
042            extends BasePersistenceImpl<JournalStructure>
043            implements JournalStructureFinder {
044    
045            public static final String COUNT_BY_C_G_S_N_D =
046                    JournalStructureFinder.class.getName() + ".countByC_G_S_N_D";
047    
048            public static final String FIND_BY_C_G_S_N_D =
049                    JournalStructureFinder.class.getName() + ".findByC_G_S_N_D";
050    
051            @Override
052            public int countByKeywords(long companyId, long[] groupIds, String keywords)
053                    throws SystemException {
054    
055                    String[] structureIds = null;
056                    String[] names = null;
057                    String[] descriptions = null;
058                    boolean andOperator = false;
059    
060                    if (Validator.isNotNull(keywords)) {
061                            structureIds = CustomSQLUtil.keywords(keywords, false);
062                            names = CustomSQLUtil.keywords(keywords);
063                            descriptions = CustomSQLUtil.keywords(keywords);
064                    }
065                    else {
066                            andOperator = true;
067                    }
068    
069                    return countByC_G_S_N_D(
070                            companyId, groupIds, structureIds, names, descriptions,
071                            andOperator);
072            }
073    
074            @Override
075            public int countByC_G_S_N_D(
076                            long companyId, long[] groupIds, String structureId, String name,
077                            String description, boolean andOperator)
078                    throws SystemException {
079    
080                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
081                    String[] names = CustomSQLUtil.keywords(name);
082                    String[] descriptions = CustomSQLUtil.keywords(description);
083    
084                    return countByC_G_S_N_D(
085                            companyId, groupIds, structureIds, names, descriptions,
086                            andOperator);
087            }
088    
089            @Override
090            public int countByC_G_S_N_D(
091                            long companyId, long[] groupIds, String[] structureIds,
092                            String[] names, String[] descriptions, boolean andOperator)
093                    throws SystemException {
094    
095                    return doCountByC_G_S_N_D(
096                            companyId, groupIds, structureIds, names, descriptions, andOperator,
097                            false);
098            }
099    
100            @Override
101            public int filterCountByKeywords(
102                            long companyId, long[] groupIds, String keywords)
103                    throws SystemException {
104    
105                    String[] structureIds = null;
106                    String[] names = null;
107                    String[] descriptions = null;
108                    boolean andOperator = false;
109    
110                    if (Validator.isNotNull(keywords)) {
111                            structureIds = CustomSQLUtil.keywords(keywords, false);
112                            names = CustomSQLUtil.keywords(keywords);
113                            descriptions = CustomSQLUtil.keywords(keywords);
114                    }
115                    else {
116                            andOperator = true;
117                    }
118    
119                    return filterCountByC_G_S_N_D(
120                            companyId, groupIds, structureIds, names, descriptions,
121                            andOperator);
122            }
123    
124            @Override
125            public int filterCountByC_G_S_N_D(
126                            long companyId, long[] groupIds, String structureId, String name,
127                            String description, boolean andOperator)
128                    throws SystemException {
129    
130                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
131                    String[] names = CustomSQLUtil.keywords(name);
132                    String[] descriptions = CustomSQLUtil.keywords(description);
133    
134                    return filterCountByC_G_S_N_D(
135                            companyId, groupIds, structureIds, names, descriptions,
136                            andOperator);
137            }
138    
139            @Override
140            public int filterCountByC_G_S_N_D(
141                            long companyId, long[] groupIds, String[] structureIds,
142                            String[] names, String[] descriptions, boolean andOperator)
143                    throws SystemException {
144    
145                    return doCountByC_G_S_N_D(
146                            companyId, groupIds, structureIds, names, descriptions, andOperator,
147                            true);
148            }
149    
150            @Override
151            public List<JournalStructure> filterFindByKeywords(
152                            long companyId, long[] groupIds, String keywords, int start,
153                            int end, OrderByComparator obc)
154                    throws SystemException {
155    
156                    String[] structureIds = null;
157                    String[] names = null;
158                    String[] descriptions = null;
159                    boolean andOperator = false;
160    
161                    if (Validator.isNotNull(keywords)) {
162                            structureIds = CustomSQLUtil.keywords(keywords, false);
163                            names = CustomSQLUtil.keywords(keywords);
164                            descriptions = CustomSQLUtil.keywords(keywords);
165                    }
166                    else {
167                            andOperator = true;
168                    }
169    
170                    return filterFindByC_G_S_N_D(
171                            companyId, groupIds, structureIds, names, descriptions, andOperator,
172                            start, end, obc);
173            }
174    
175            @Override
176            public List<JournalStructure> filterFindByC_G_S_N_D(
177                            long companyId, long[] groupIds, String structureId, String name,
178                            String description, boolean andOperator, int start, int end,
179                            OrderByComparator obc)
180                    throws SystemException {
181    
182                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
183                    String[] names = CustomSQLUtil.keywords(name);
184                    String[] descriptions = CustomSQLUtil.keywords(description);
185    
186                    return filterFindByC_G_S_N_D(
187                            companyId, groupIds, structureIds, names, descriptions, andOperator,
188                            start, end, obc);
189            }
190    
191            @Override
192            public List<JournalStructure> filterFindByC_G_S_N_D(
193                            long companyId, long[] groupIds, String[] structureIds,
194                            String[] names, String[] descriptions, boolean andOperator,
195                            int start, int end, OrderByComparator obc)
196                    throws SystemException {
197    
198                    return doFindByC_G_S_N_D(
199                            companyId, groupIds, structureIds, names, descriptions, andOperator,
200                            start, end, obc, true);
201            }
202    
203            @Override
204            public List<JournalStructure> findByKeywords(
205                            long companyId, long[] groupIds, String keywords, int start,
206                            int end, OrderByComparator obc)
207                    throws SystemException {
208    
209                    String[] structureIds = null;
210                    String[] names = null;
211                    String[] descriptions = null;
212                    boolean andOperator = false;
213    
214                    if (Validator.isNotNull(keywords)) {
215                            structureIds = CustomSQLUtil.keywords(keywords, false);
216                            names = CustomSQLUtil.keywords(keywords);
217                            descriptions = CustomSQLUtil.keywords(keywords);
218                    }
219                    else {
220                            andOperator = true;
221                    }
222    
223                    return findByC_G_S_N_D(
224                            companyId, groupIds, structureIds, names, descriptions, andOperator,
225                            start, end, obc);
226            }
227    
228            @Override
229            public List<JournalStructure> findByC_G_S_N_D(
230                            long companyId, long[] groupIds, String structureId, String name,
231                            String description, boolean andOperator, int start, int end,
232                            OrderByComparator obc)
233                    throws SystemException {
234    
235                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
236                    String[] names = CustomSQLUtil.keywords(name);
237                    String[] descriptions = CustomSQLUtil.keywords(description);
238    
239                    return findByC_G_S_N_D(
240                            companyId, groupIds, structureIds, names, descriptions, andOperator,
241                            start, end, obc);
242            }
243    
244            @Override
245            public List<JournalStructure> findByC_G_S_N_D(
246                            long companyId, long[] groupIds, String[] structureIds,
247                            String[] names, String[] descriptions, boolean andOperator,
248                            int start, int end, OrderByComparator obc)
249                    throws SystemException {
250    
251                    return doFindByC_G_S_N_D(
252                            companyId, groupIds, structureIds, names, descriptions, andOperator,
253                            start, end, obc, false);
254            }
255    
256            protected int doCountByC_G_S_N_D(
257                            long companyId, long[] groupIds, String[] structureIds,
258                            String[] names, String[] descriptions, boolean andOperator,
259                            boolean inlineSQLHelper)
260                    throws SystemException {
261    
262                    structureIds = CustomSQLUtil.keywords(structureIds, false);
263                    names = CustomSQLUtil.keywords(names);
264                    descriptions = CustomSQLUtil.keywords(descriptions);
265    
266                    Session session = null;
267    
268                    try {
269                            session = openSession();
270    
271                            String sql = CustomSQLUtil.get(COUNT_BY_C_G_S_N_D);
272    
273                            sql = StringUtil.replace(
274                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds));
275                            sql = CustomSQLUtil.replaceKeywords(
276                                    sql, "structureId", StringPool.LIKE, false, structureIds);
277                            sql = CustomSQLUtil.replaceKeywords(
278                                    sql, "lower(name)", StringPool.LIKE, false, names);
279                            sql = CustomSQLUtil.replaceKeywords(
280                                    sql, "lower(description)", StringPool.LIKE, true, descriptions);
281    
282                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
283    
284                            if (inlineSQLHelper) {
285                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
286                                            sql, JournalStructure.class.getName(),
287                                            "JournalStructure.id_", groupIds);
288    
289                                    sql = StringUtil.replace(
290                                            sql, "(companyId", "(JournalStructure.companyId");
291    
292                                    sql = StringUtil.replace(
293                                            sql, "(name", "(JournalStructure.name");
294                            }
295    
296                            SQLQuery q = session.createSQLQuery(sql);
297    
298                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
299    
300                            QueryPos qPos = QueryPos.getInstance(q);
301    
302                            qPos.add(companyId);
303                            qPos.add(groupIds);
304                            qPos.add(structureIds, 2);
305                            qPos.add(names, 2);
306                            qPos.add(descriptions, 2);
307    
308                            Iterator<Long> itr = q.iterate();
309    
310                            if (itr.hasNext()) {
311                                    Long count = itr.next();
312    
313                                    if (count != null) {
314                                            return count.intValue();
315                                    }
316                            }
317    
318                            return 0;
319                    }
320                    catch (Exception e) {
321                            throw new SystemException(e);
322                    }
323                    finally {
324                            closeSession(session);
325                    }
326            }
327    
328            protected List<JournalStructure> doFindByC_G_S_N_D(
329                            long companyId, long[] groupIds, String[] structureIds,
330                            String[] names, String[] descriptions, boolean andOperator,
331                            int start, int end, OrderByComparator obc, boolean inlineSQLHelper)
332                    throws SystemException {
333    
334                    structureIds = CustomSQLUtil.keywords(structureIds, false);
335                    names = CustomSQLUtil.keywords(names);
336                    descriptions = CustomSQLUtil.keywords(descriptions);
337    
338                    Session session = null;
339    
340                    try {
341                            session = openSession();
342    
343                            String sql = CustomSQLUtil.get(FIND_BY_C_G_S_N_D);
344    
345                            sql = StringUtil.replace(
346                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds));
347                            sql = CustomSQLUtil.replaceKeywords(
348                                    sql, "structureId", StringPool.LIKE, false, structureIds);
349                            sql = CustomSQLUtil.replaceKeywords(
350                                    sql, "lower(name)", StringPool.LIKE, false, names);
351                            sql = CustomSQLUtil.replaceKeywords(
352                                    sql, "lower(description)", StringPool.LIKE, true, descriptions);
353    
354                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
355                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
356    
357                            if (inlineSQLHelper) {
358                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
359                                            sql, JournalStructure.class.getName(),
360                                            "JournalStructure.id_", groupIds);
361    
362                                    sql = StringUtil.replace(
363                                            sql, "(companyId", "(JournalStructure.companyId");
364    
365                                    sql = StringUtil.replace(
366                                            sql, "(name", "(JournalStructure.name");
367                            }
368    
369                            SQLQuery q = session.createSQLQuery(sql);
370    
371                            q.addEntity("JournalStructure", JournalStructureImpl.class);
372    
373                            QueryPos qPos = QueryPos.getInstance(q);
374    
375                            qPos.add(companyId);
376                            qPos.add(groupIds);
377                            qPos.add(structureIds, 2);
378                            qPos.add(names, 2);
379                            qPos.add(descriptions, 2);
380    
381                            return (List<JournalStructure>)QueryUtil.list(
382                                    q, getDialect(), start, end);
383                    }
384                    catch (Exception e) {
385                            throw new SystemException(e);
386                    }
387                    finally {
388                            closeSession(session);
389                    }
390            }
391    
392            protected String getGroupIds(long[] groupIds) {
393                    if (groupIds.length == 0) {
394                            return StringPool.BLANK;
395                    }
396    
397                    StringBundler sb = new StringBundler(groupIds.length + 2);
398    
399                    sb.append(" (groupId = ? ");
400    
401                    for (int i = 1; i < groupIds.length; i++) {
402                            sb.append(" OR groupId = ? ");
403                    }
404    
405                    sb.append(") AND ");
406    
407                    return sb.toString();
408            }
409    
410    }