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.dynamicdatamapping.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.ArrayUtil;
024    import com.liferay.portal.kernel.util.OrderByComparator;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
030    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
031    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
032    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
033    import com.liferay.portlet.dynamicdatamapping.model.impl.DDMStructureImpl;
034    import com.liferay.util.dao.orm.CustomSQLUtil;
035    
036    import java.util.Iterator;
037    import java.util.List;
038    
039    /**
040     * @author Eduardo Lundgren
041     * @author Connor McKay
042     * @author Marcellus Tavares
043     */
044    public class DDMStructureFinderImpl
045            extends BasePersistenceImpl<DDMStructure> implements DDMStructureFinder {
046    
047            public static final String COUNT_BY_C_G_C_N_D_S_T =
048                    DDMStructureFinder.class.getName() + ".countByC_G_C_N_D_S_T";
049    
050            public static final String FIND_BY_C_G_C_N_D_S_T =
051                    DDMStructureFinder.class.getName() + ".findByC_G_C_N_D_S_T";
052    
053            @Override
054            public int countByKeywords(
055                            long companyId, long[] groupIds, long[] classNameIds,
056                            String keywords)
057                    throws SystemException {
058    
059                    String[] names = null;
060                    String[] descriptions = null;
061                    boolean andOperator = false;
062    
063                    if (Validator.isNotNull(keywords)) {
064                            names = CustomSQLUtil.keywords(keywords);
065                            descriptions = CustomSQLUtil.keywords(keywords, false);
066                    }
067                    else {
068                            andOperator = true;
069                    }
070    
071                    return countByC_G_C_N_D_S_T(
072                            companyId, groupIds, classNameIds, names, descriptions, null,
073                            DDMStructureConstants.TYPE_DEFAULT, andOperator);
074            }
075    
076            @Override
077            public int countByC_G_C_N_D_S_T(
078                            long companyId, long[] groupIds, long[] classNameIds, String name,
079                            String description, String storageType, int type,
080                            boolean andOperator)
081                    throws SystemException {
082    
083                    String[] names = CustomSQLUtil.keywords(name);
084                    String[] descriptions = CustomSQLUtil.keywords(description, false);
085                    String[] storageTypes = CustomSQLUtil.keywords(storageType, false);
086    
087                    return countByC_G_C_N_D_S_T(
088                            companyId, groupIds, classNameIds, names, descriptions,
089                            storageTypes, type, andOperator);
090            }
091    
092            @Override
093            public int countByC_G_C_N_D_S_T(
094                            long companyId, long[] groupIds, long[] classNameIds,
095                            String[] names, String[] descriptions, String[] storageTypes,
096                            int type, boolean andOperator)
097                    throws SystemException {
098    
099                    return doCountByC_G_C_N_D_S_T(
100                            companyId, groupIds, classNameIds, names, descriptions,
101                            storageTypes, type, andOperator, false);
102            }
103    
104            @Override
105            public int filterCountByKeywords(
106                            long companyId, long[] groupIds, long[] classNameIds,
107                            String keywords)
108                    throws SystemException {
109    
110                    String[] names = null;
111                    String[] descriptions = null;
112                    boolean andOperator = false;
113    
114                    if (Validator.isNotNull(keywords)) {
115                            names = CustomSQLUtil.keywords(keywords);
116                            descriptions = CustomSQLUtil.keywords(keywords, false);
117                    }
118                    else {
119                            andOperator = true;
120                    }
121    
122                    return filterCountByC_G_C_N_D_S_T(
123                            companyId, groupIds, classNameIds, names, descriptions, null,
124                            DDMStructureConstants.TYPE_DEFAULT, andOperator);
125            }
126    
127            @Override
128            public int filterCountByC_G_C_N_D_S_T(
129                            long companyId, long[] groupIds, long[] classNameIds, String name,
130                            String description, String storageType, int type,
131                            boolean andOperator)
132                    throws SystemException {
133    
134                    String[] names = CustomSQLUtil.keywords(name);
135                    String[] descriptions = CustomSQLUtil.keywords(description, false);
136                    String[] storageTypes = CustomSQLUtil.keywords(storageType, false);
137    
138                    return filterCountByC_G_C_N_D_S_T(
139                            companyId, groupIds, classNameIds, names, descriptions,
140                            storageTypes, type, andOperator);
141            }
142    
143            @Override
144            public int filterCountByC_G_C_N_D_S_T(
145                            long companyId, long[] groupIds, long[] classNameIds,
146                            String[] names, String[] descriptions, String[] storageTypes,
147                            int type, boolean andOperator)
148                    throws SystemException {
149    
150                    return doCountByC_G_C_N_D_S_T(
151                            companyId, groupIds, classNameIds, names, descriptions,
152                            storageTypes, type, andOperator, true);
153            }
154    
155            @Override
156            public List<DDMStructure> filterFindByKeywords(
157                            long companyId, long[] groupIds, long[] classNameIds,
158                            String keywords, int start, int end,
159                            OrderByComparator orderByComparator)
160                    throws SystemException {
161    
162                    String[] names = null;
163                    String[] descriptions = null;
164                    boolean andOperator = false;
165    
166                    if (Validator.isNotNull(keywords)) {
167                            names = CustomSQLUtil.keywords(keywords);
168                            descriptions = CustomSQLUtil.keywords(keywords, false);
169                    }
170                    else {
171                            andOperator = true;
172                    }
173    
174                    return filterFindByC_G_C_N_D_S_T(
175                            companyId, groupIds, classNameIds, names, descriptions, null,
176                            DDMStructureConstants.TYPE_DEFAULT, andOperator, start, end,
177                            orderByComparator);
178            }
179    
180            @Override
181            public List<DDMStructure> filterFindByC_G_C_N_D_S_T(
182                            long companyId, long[] groupIds, long[] classNameIds, String name,
183                            String description, String storageType, int type,
184                            boolean andOperator, int start, int end,
185                            OrderByComparator orderByComparator)
186                    throws SystemException {
187    
188                    String[] names = CustomSQLUtil.keywords(name);
189                    String[] descriptions = CustomSQLUtil.keywords(description, false);
190                    String[] storageTypes = CustomSQLUtil.keywords(storageType, false);
191    
192                    return filterFindByC_G_C_N_D_S_T(
193                            companyId, groupIds, classNameIds, names, descriptions,
194                            storageTypes, type, andOperator, start, end, orderByComparator);
195            }
196    
197            @Override
198            public List<DDMStructure> filterFindByC_G_C_N_D_S_T(
199                            long companyId, long[] groupIds, long[] classNameIds,
200                            String[] names, String[] descriptions, String[] storageTypes,
201                            int type, boolean andOperator, int start, int end,
202                            OrderByComparator orderByComparator)
203                    throws SystemException {
204    
205                    return doFindByC_G_C_N_D_S_T(
206                            companyId, groupIds, classNameIds, names, descriptions,
207                            storageTypes, type, andOperator, start, end, orderByComparator,
208                            true);
209            }
210    
211            @Override
212            public List<DDMStructure> findByKeywords(
213                            long companyId, long[] groupIds, long[] classNameIds,
214                            String keywords, int start, int end,
215                            OrderByComparator orderByComparator)
216                    throws SystemException {
217    
218                    String[] names = null;
219                    String[] descriptions = null;
220                    boolean andOperator = false;
221    
222                    if (Validator.isNotNull(keywords)) {
223                            names = CustomSQLUtil.keywords(keywords);
224                            descriptions = CustomSQLUtil.keywords(keywords, false);
225                    }
226                    else {
227                            andOperator = true;
228                    }
229    
230                    return findByC_G_C_N_D_S_T(
231                            companyId, groupIds, classNameIds, names, descriptions, null,
232                            DDMStructureConstants.TYPE_DEFAULT, andOperator, start, end,
233                            orderByComparator);
234            }
235    
236            @Override
237            public List<DDMStructure> findByC_G_C_N_D_S_T(
238                            long companyId, long[] groupIds, long[] classNameIds, String name,
239                            String description, String storageType, int type,
240                            boolean andOperator, int start, int end,
241                            OrderByComparator orderByComparator)
242                    throws SystemException {
243    
244                    String[] names = CustomSQLUtil.keywords(name);
245                    String[] descriptions = CustomSQLUtil.keywords(description, false);
246                    String[] storageTypes = CustomSQLUtil.keywords(storageType, false);
247    
248                    return findByC_G_C_N_D_S_T(
249                            companyId, groupIds, classNameIds, names, descriptions,
250                            storageTypes, type, andOperator, start, end, orderByComparator);
251            }
252    
253            @Override
254            public List<DDMStructure> findByC_G_C_N_D_S_T(
255                            long companyId, long[] groupIds, long[] classNameIds,
256                            String[] names, String[] descriptions, String[] storageTypes,
257                            int type, boolean andOperator, int start, int end,
258                            OrderByComparator orderByComparator)
259                    throws SystemException {
260    
261                    return doFindByC_G_C_N_D_S_T(
262                            companyId, groupIds, classNameIds, names, descriptions,
263                            storageTypes, type, andOperator, start, end, orderByComparator,
264                            false);
265            }
266    
267            protected int doCountByC_G_C_N_D_S_T(
268                            long companyId, long[] groupIds, long[] classNameIds,
269                            String[] names, String[] descriptions, String[] storageTypes,
270                            int type, boolean andOperator, boolean inlineSQLHelper)
271                    throws SystemException {
272    
273                    String[] classNameIdsString = null;
274    
275                    if (classNameIds == null) {
276                            classNameIdsString = new String[] {null};
277                    }
278                    else {
279                            classNameIdsString = ArrayUtil.toStringArray(classNameIds);
280                    }
281    
282                    names = CustomSQLUtil.keywords(names);
283                    descriptions = CustomSQLUtil.keywords(descriptions, false);
284                    storageTypes = CustomSQLUtil.keywords(storageTypes, false);
285    
286                    Session session = null;
287    
288                    try {
289                            session = openSession();
290    
291                            String sql = CustomSQLUtil.get(COUNT_BY_C_G_C_N_D_S_T);
292    
293                            if (inlineSQLHelper) {
294                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
295                                            sql, DDMStructure.class.getName(),
296                                            "DDMStructure.structureId", groupIds);
297                            }
298    
299                            sql = StringUtil.replace(
300                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds));
301                            sql = CustomSQLUtil.replaceKeywords(
302                                    sql, "classNameId", StringPool.EQUAL, false,
303                                    classNameIdsString);
304                            sql = CustomSQLUtil.replaceKeywords(
305                                    sql, "lower(name)", StringPool.LIKE, false, names);
306                            sql = CustomSQLUtil.replaceKeywords(
307                                    sql, "description", StringPool.LIKE, false, descriptions);
308                            sql = CustomSQLUtil.replaceKeywords(
309                                    sql, "storageType", StringPool.LIKE, true, storageTypes);
310                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
311    
312                            SQLQuery q = session.createSQLQuery(sql);
313    
314                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
315    
316                            QueryPos qPos = QueryPos.getInstance(q);
317    
318                            qPos.add(companyId);
319                            qPos.add(groupIds);
320                            qPos.add(classNameIds, 2);
321                            qPos.add(names, 2);
322                            qPos.add(descriptions, 2);
323                            qPos.add(storageTypes, 2);
324                            qPos.add(type);
325    
326                            Iterator<Long> itr = q.iterate();
327    
328                            if (itr.hasNext()) {
329                                    Long count = itr.next();
330    
331                                    if (count != null) {
332                                            return count.intValue();
333                                    }
334                            }
335    
336                            return 0;
337                    }
338                    catch (Exception e) {
339                            throw new SystemException(e);
340                    }
341                    finally {
342                            closeSession(session);
343                    }
344            }
345    
346            protected List<DDMStructure> doFindByC_G_C_N_D_S_T(
347                            long companyId, long[] groupIds, long[] classNameIds,
348                            String[] names, String[] descriptions, String[] storageTypes,
349                            int type, boolean andOperator, int start, int end,
350                            OrderByComparator orderByComparator, boolean inlineSQLHelper)
351                    throws SystemException {
352    
353                    String[] classNameIdsString = null;
354    
355                    if (classNameIds == null) {
356                            classNameIdsString = new String[] {null};
357                    }
358                    else {
359                            classNameIdsString = ArrayUtil.toStringArray(classNameIds);
360                    }
361    
362                    names = CustomSQLUtil.keywords(names);
363                    descriptions = CustomSQLUtil.keywords(descriptions, false);
364                    storageTypes = CustomSQLUtil.keywords(storageTypes, false);
365    
366                    Session session = null;
367    
368                    try {
369                            session = openSession();
370    
371                            String sql = CustomSQLUtil.get(FIND_BY_C_G_C_N_D_S_T);
372    
373                            if (inlineSQLHelper) {
374                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
375                                            sql, DDMStructure.class.getName(),
376                                            "DDMStructure.structureId", groupIds);
377                            }
378    
379                            sql = StringUtil.replace(
380                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds));
381                            sql = CustomSQLUtil.replaceKeywords(
382                                    sql, "classNameId", StringPool.EQUAL, false,
383                                    classNameIdsString);
384                            sql = CustomSQLUtil.replaceKeywords(
385                                    sql, "lower(name)", StringPool.LIKE, false, names);
386                            sql = CustomSQLUtil.replaceKeywords(
387                                    sql, "description", StringPool.LIKE, false, descriptions);
388                            sql = CustomSQLUtil.replaceKeywords(
389                                    sql, "storageType", StringPool.LIKE, true, storageTypes);
390                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
391    
392                            if (orderByComparator != null) {
393                                    String orderByFields = StringUtil.merge(
394                                            orderByComparator.getOrderByFields(), StringPool.COMMA);
395    
396                                    sql = StringUtil.replace(
397                                            sql, "structureId DESC", orderByFields.concat(" DESC"));
398                            }
399    
400                            SQLQuery q = session.createSQLQuery(sql);
401    
402                            q.addEntity("DDMStructure", DDMStructureImpl.class);
403    
404                            QueryPos qPos = QueryPos.getInstance(q);
405    
406                            qPos.add(companyId);
407                            qPos.add(groupIds);
408                            qPos.add(classNameIds, 2);
409                            qPos.add(names, 2);
410                            qPos.add(descriptions, 2);
411                            qPos.add(storageTypes, 2);
412                            qPos.add(type);
413    
414                            return (List<DDMStructure>)QueryUtil.list(
415                                    q, getDialect(), start, end);
416                    }
417                    catch (Exception e) {
418                            throw new SystemException(e);
419                    }
420                    finally {
421                            closeSession(session);
422                    }
423            }
424    
425            protected String getGroupIds(long[] groupIds) {
426                    if (groupIds.length == 0) {
427                            return StringPool.BLANK;
428                    }
429    
430                    StringBundler sb = new StringBundler(groupIds.length * 2);
431    
432                    sb.append("(");
433    
434                    for (int i = 0; i < groupIds.length; i++) {
435                            sb.append("groupId = ?");
436    
437                            if ((i + 1) < groupIds.length) {
438                                    sb.append(" OR ");
439                            }
440                    }
441    
442                    sb.append(") AND");
443    
444                    return sb.toString();
445            }
446    
447    }