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.documentlibrary.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.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.documentlibrary.model.DLFileEntryType;
031    import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryTypeImpl;
032    import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryTypeFinder;
033    import com.liferay.util.dao.orm.CustomSQLUtil;
034    
035    import java.util.Iterator;
036    import java.util.List;
037    
038    /**
039     * @author Sergio Gonz??lez
040     * @author Connor McKay
041     * @author Alexander Chow
042     */
043    public class DLFileEntryTypeFinderImpl
044            extends BasePersistenceImpl<DLFileEntryType>
045            implements DLFileEntryTypeFinder {
046    
047            public static final String COUNT_BY_C_G_N_D_S =
048                    DLFileEntryTypeFinder.class.getName() + ".countByC_G_N_D_S";
049    
050            public static final String FIND_BY_C_G_N_D_S =
051                    DLFileEntryTypeFinder.class.getName() + ".findByC_G_N_D_S";
052    
053            public static final String JOIN_BY_FILE_ENTRY_TYPE =
054                    DLFileEntryTypeFinder.class.getName() + ".joinByFileEntryType";
055    
056            @Override
057            public int countByKeywords(
058                    long companyId, long[] groupIds, String keywords,
059                    boolean includeBasicFileEntryType) {
060    
061                    String[] names = null;
062                    String[] descriptions = null;
063                    boolean andOperator = false;
064    
065                    if (Validator.isNotNull(keywords)) {
066                            names = CustomSQLUtil.keywords(keywords);
067                            descriptions = CustomSQLUtil.keywords(keywords, false);
068                    }
069                    else {
070                            andOperator = true;
071                    }
072    
073                    return doCountByC_G_N_D_S(
074                            companyId, groupIds, names, descriptions, andOperator,
075                            includeBasicFileEntryType, false);
076            }
077    
078            @Override
079            public int filterCountByKeywords(
080                    long companyId, long[] groupIds, String keywords,
081                    boolean includeBasicFileEntryType) {
082    
083                    String[] names = null;
084                    String[] descriptions = null;
085                    boolean andOperator = false;
086    
087                    if (Validator.isNotNull(keywords)) {
088                            names = CustomSQLUtil.keywords(keywords);
089                            descriptions = CustomSQLUtil.keywords(keywords, false);
090                    }
091                    else {
092                            andOperator = true;
093                    }
094    
095                    return doCountByC_G_N_D_S(
096                            companyId, groupIds, names, descriptions, andOperator,
097                            includeBasicFileEntryType, true);
098            }
099    
100            @Override
101            public List<DLFileEntryType> filterFindByKeywords(
102                    long companyId, long[] groupIds, String keywords,
103                    boolean includeBasicFileEntryType, int start, int end,
104                    OrderByComparator<DLFileEntryType> orderByComparator) {
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 doFindByC_G_N_D_S(
119                            companyId, groupIds, names, descriptions, andOperator,
120                            includeBasicFileEntryType, start, end, orderByComparator, true);
121            }
122    
123            @Override
124            public List<DLFileEntryType> findByKeywords(
125                    long companyId, long[] groupIds, String keywords,
126                    boolean includeBasicFileEntryType, int start, int end,
127                    OrderByComparator<DLFileEntryType> orderByComparator) {
128    
129                    String[] names = null;
130                    String[] descriptions = null;
131                    boolean andOperator = false;
132    
133                    if (Validator.isNotNull(keywords)) {
134                            names = CustomSQLUtil.keywords(keywords);
135                            descriptions = CustomSQLUtil.keywords(keywords, false);
136                    }
137                    else {
138                            andOperator = true;
139                    }
140    
141                    return doFindByC_G_N_D_S(
142                            companyId, groupIds, names, descriptions, andOperator,
143                            includeBasicFileEntryType, start, end, orderByComparator, false);
144            }
145    
146            protected int doCountByC_G_N_D_S(
147                    long companyId, long[] groupIds, String[] names, String[] descriptions,
148                    boolean andOperator, boolean includeBasicFileEntryType,
149                    boolean inlineSQLHelper) {
150    
151                    names = CustomSQLUtil.keywords(names);
152                    descriptions = CustomSQLUtil.keywords(descriptions, false);
153    
154                    Session session = null;
155    
156                    try {
157                            session = openSession();
158    
159                            String sql = CustomSQLUtil.get(COUNT_BY_C_G_N_D_S);
160    
161                            if (inlineSQLHelper) {
162                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
163                                            sql, DLFileEntryType.class.getName(),
164                                            "DLFileEntryType.fileEntryTypeId", groupIds);
165                            }
166    
167                            sql = StringUtil.replace(
168                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds));
169                            sql = CustomSQLUtil.replaceKeywords(
170                                    sql, "lower(name)", StringPool.LIKE, false, names);
171                            sql = CustomSQLUtil.replaceKeywords(
172                                    sql, "description", StringPool.LIKE, true, descriptions);
173                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
174    
175                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
176    
177                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
178    
179                            QueryPos qPos = QueryPos.getInstance(q);
180    
181                            qPos.add(companyId);
182                            qPos.add(groupIds);
183                            qPos.add(names, 2);
184                            qPos.add(descriptions, 2);
185    
186                            int countValue = 0;
187    
188                            if (includeBasicFileEntryType) {
189                                    countValue = 1;
190                            }
191    
192                            Iterator<Long> itr = q.iterate();
193    
194                            if (itr.hasNext()) {
195                                    Long count = itr.next();
196    
197                                    if (count != null) {
198                                            return countValue + count.intValue();
199                                    }
200                            }
201    
202                            return countValue;
203                    }
204                    catch (Exception e) {
205                            throw new SystemException(e);
206                    }
207                    finally {
208                            closeSession(session);
209                    }
210            }
211    
212            protected List<DLFileEntryType> doFindByC_G_N_D_S(
213                    long companyId, long[] groupIds, String[] names, String[] descriptions,
214                    boolean andOperator, boolean includeBasicFileEntryType, int start,
215                    int end, OrderByComparator<DLFileEntryType> orderByComparator,
216                    boolean inlineSQLHelper) {
217    
218                    names = CustomSQLUtil.keywords(names);
219                    descriptions = CustomSQLUtil.keywords(descriptions, false);
220    
221                    Session session = null;
222    
223                    try {
224                            session = openSession();
225    
226                            String sql = CustomSQLUtil.get(FIND_BY_C_G_N_D_S);
227    
228                            if (inlineSQLHelper) {
229                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
230                                            sql, DLFileEntryType.class.getName(),
231                                            "DLFileEntryType.fileEntryTypeId", groupIds);
232                            }
233    
234                            sql = StringUtil.replace(
235                                    sql, "[$BASIC_DOCUMENT$]",
236                                    getBasicDocument(includeBasicFileEntryType));
237                            sql = StringUtil.replace(
238                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds));
239                            sql = CustomSQLUtil.replaceKeywords(
240                                    sql, "lower(name)", StringPool.LIKE, false, names);
241                            sql = CustomSQLUtil.replaceKeywords(
242                                    sql, "description", StringPool.LIKE, true, descriptions);
243                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
244    
245                            if (orderByComparator != null) {
246                                    String orderByFields = StringUtil.merge(
247                                            orderByComparator.getOrderByFields(), StringPool.COMMA);
248    
249                                    sql = StringUtil.replace(
250                                            sql, "name ASC", orderByFields.concat(" DESC"));
251                            }
252    
253                            if (includeBasicFileEntryType) {
254                                    sql = sql.concat(StringPool.CLOSE_PARENTHESIS);
255                            }
256    
257                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
258    
259                            q.addEntity("DLFileEntryType", DLFileEntryTypeImpl.class);
260    
261                            QueryPos qPos = QueryPos.getInstance(q);
262    
263                            if (includeBasicFileEntryType) {
264                                    qPos.add(names, 2);
265                                    qPos.add(descriptions, 2);
266                            }
267    
268                            qPos.add(companyId);
269                            qPos.add(groupIds);
270                            qPos.add(names, 2);
271                            qPos.add(descriptions, 2);
272    
273                            return (List<DLFileEntryType>)QueryUtil.list(
274                                    q, getDialect(), start, end);
275                    }
276                    catch (Exception e) {
277                            throw new SystemException(e);
278                    }
279                    finally {
280                            closeSession(session);
281                    }
282            }
283    
284            protected String getBasicDocument(boolean includeBasicFileEntryType) {
285                    if (!includeBasicFileEntryType) {
286                            return StringPool.BLANK;
287                    }
288    
289                    StringBundler sb = new StringBundler(6);
290    
291                    sb.append("(SELECT {DLFileEntryType.*} From DLFileEntryType WHERE ");
292                    sb.append("((companyId = 0) AND (groupId = 0) AND (");
293                    sb.append("(lower(name) LIKE ? [$AND_OR_NULL_CHECK$]) ");
294                    sb.append("[$AND_OR_CONNECTOR$] ");
295                    sb.append("(description LIKE ? [$AND_OR_NULL_CHECK$]) ");
296                    sb.append("))) UNION ALL (");
297    
298                    return sb.toString();
299            }
300    
301            protected String getGroupIds(long[] groupIds) {
302                    if (groupIds.length == 0) {
303                            return StringPool.BLANK;
304                    }
305    
306                    StringBundler sb = new StringBundler(groupIds.length * 2);
307    
308                    sb.append(StringPool.OPEN_PARENTHESIS);
309    
310                    for (int i = 0; i < groupIds.length; i++) {
311                            sb.append("groupId = ?");
312    
313                            if ((i + 1) < groupIds.length) {
314                                    sb.append(" OR ");
315                            }
316                    }
317    
318                    sb.append(") AND");
319    
320                    return sb.toString();
321            }
322    
323    }