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