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