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