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(DDMStructure.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    
320                            if (groupIds != null) {
321                                    qPos.add(groupIds);
322                            }
323    
324                            qPos.add(classNameIds, 2);
325                            qPos.add(names, 2);
326                            qPos.add(descriptions, 2);
327                            qPos.add(storageTypes, 2);
328                            qPos.add(type);
329    
330                            Iterator<Long> itr = q.iterate();
331    
332                            if (itr.hasNext()) {
333                                    Long count = itr.next();
334    
335                                    if (count != null) {
336                                            return count.intValue();
337                                    }
338                            }
339    
340                            return 0;
341                    }
342                    catch (Exception e) {
343                            throw new SystemException(e);
344                    }
345                    finally {
346                            closeSession(session);
347                    }
348            }
349    
350            protected List<DDMStructure> doFindByC_G_C_N_D_S_T(
351                            long companyId, long[] groupIds, long[] classNameIds,
352                            String[] names, String[] descriptions, String[] storageTypes,
353                            int type, boolean andOperator, int start, int end,
354                            OrderByComparator orderByComparator, boolean inlineSQLHelper)
355                    throws SystemException {
356    
357                    String[] classNameIdsString = null;
358    
359                    if (classNameIds == null) {
360                            classNameIdsString = new String[] {null};
361                    }
362                    else {
363                            classNameIdsString = ArrayUtil.toStringArray(classNameIds);
364                    }
365    
366                    names = CustomSQLUtil.keywords(names);
367                    descriptions = CustomSQLUtil.keywords(descriptions, false);
368                    storageTypes = CustomSQLUtil.keywords(storageTypes, false);
369    
370                    Session session = null;
371    
372                    try {
373                            session = openSession();
374    
375                            String sql = CustomSQLUtil.get(FIND_BY_C_G_C_N_D_S_T);
376    
377                            if (inlineSQLHelper) {
378                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
379                                            sql, DDMStructure.class.getName(),
380                                            "DDMStructure.structureId", groupIds);
381                            }
382    
383                            sql = StringUtil.replace(
384                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds));
385                            sql = CustomSQLUtil.replaceKeywords(
386                                    sql, "classNameId", StringPool.EQUAL, false,
387                                    classNameIdsString);
388                            sql = CustomSQLUtil.replaceKeywords(
389                                    sql, "lower(DDMStructure.name)", StringPool.LIKE, false, names);
390                            sql = CustomSQLUtil.replaceKeywords(
391                                    sql, "description", StringPool.LIKE, false, descriptions);
392                            sql = CustomSQLUtil.replaceKeywords(
393                                    sql, "storageType", StringPool.LIKE, true, storageTypes);
394                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
395    
396                            if (orderByComparator != null) {
397                                    sql = CustomSQLUtil.replaceOrderBy(sql, orderByComparator);
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    
408                            if (groupIds != null) {
409                                    qPos.add(groupIds);
410                            }
411    
412                            qPos.add(classNameIds, 2);
413                            qPos.add(names, 2);
414                            qPos.add(descriptions, 2);
415                            qPos.add(storageTypes, 2);
416                            qPos.add(type);
417    
418                            return (List<DDMStructure>)QueryUtil.list(
419                                    q, getDialect(), start, end);
420                    }
421                    catch (Exception e) {
422                            throw new SystemException(e);
423                    }
424                    finally {
425                            closeSession(session);
426                    }
427            }
428    
429            protected String getGroupIds(long[] groupIds) {
430                    if (ArrayUtil.isEmpty(groupIds)) {
431                            return StringPool.BLANK;
432                    }
433    
434                    StringBundler sb = new StringBundler(groupIds.length * 2);
435    
436                    sb.append(StringPool.OPEN_PARENTHESIS);
437    
438                    for (int i = 0; i < groupIds.length; i++) {
439                            sb.append("groupId = ?");
440    
441                            if ((i + 1) < groupIds.length) {
442                                    sb.append(" OR ");
443                            }
444                    }
445    
446                    sb.append(") AND");
447    
448                    return sb.toString();
449            }
450    
451    }