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.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            @Override
056            public int countByKeywords(
057                            long companyId, long[] groupIds, String keywords,
058                            boolean includeBasicFileEntryType)
059                    throws SystemException {
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                    throws SystemException {
083    
084                    String[] names = null;
085                    String[] descriptions = null;
086                    boolean andOperator = false;
087    
088                    if (Validator.isNotNull(keywords)) {
089                            names = CustomSQLUtil.keywords(keywords);
090                            descriptions = CustomSQLUtil.keywords(keywords, false);
091                    }
092                    else {
093                            andOperator = true;
094                    }
095    
096                    return doCountByC_G_N_D_S(
097                            companyId, groupIds, names, descriptions, andOperator,
098                            includeBasicFileEntryType, true);
099            }
100    
101            @Override
102            public List<DLFileEntryType> filterFindByKeywords(
103                            long companyId, long[] groupIds, String keywords,
104                            boolean includeBasicFileEntryType, int start, int end,
105                            OrderByComparator orderByComparator)
106                    throws SystemException {
107    
108                    String[] names = null;
109                    String[] descriptions = null;
110                    boolean andOperator = false;
111    
112                    if (Validator.isNotNull(keywords)) {
113                            names = CustomSQLUtil.keywords(keywords);
114                            descriptions = CustomSQLUtil.keywords(keywords, false);
115                    }
116                    else {
117                            andOperator = true;
118                    }
119    
120                    return doFindByC_G_N_D_S(
121                            companyId, groupIds, names, descriptions, andOperator,
122                            includeBasicFileEntryType, start, end, orderByComparator, true);
123            }
124    
125            @Override
126            public List<DLFileEntryType> findByKeywords(
127                            long companyId, long[] groupIds, String keywords,
128                            boolean includeBasicFileEntryType, int start, int end,
129                            OrderByComparator orderByComparator)
130                    throws SystemException {
131    
132                    String[] names = null;
133                    String[] descriptions = null;
134                    boolean andOperator = false;
135    
136                    if (Validator.isNotNull(keywords)) {
137                            names = CustomSQLUtil.keywords(keywords);
138                            descriptions = CustomSQLUtil.keywords(keywords, false);
139                    }
140                    else {
141                            andOperator = true;
142                    }
143    
144                    return doFindByC_G_N_D_S(
145                            companyId, groupIds, names, descriptions, andOperator,
146                            includeBasicFileEntryType, start, end, orderByComparator, false);
147            }
148    
149            protected int doCountByC_G_N_D_S(
150                            long companyId, long[] groupIds, String[] names,
151                            String[] descriptions, boolean andOperator,
152                            boolean includeBasicFileEntryType, boolean inlineSQLHelper)
153                    throws SystemException {
154    
155                    names = CustomSQLUtil.keywords(names);
156                    descriptions = CustomSQLUtil.keywords(descriptions, false);
157    
158                    Session session = null;
159    
160                    try {
161                            session = openSession();
162    
163                            String sql = CustomSQLUtil.get(COUNT_BY_C_G_N_D_S);
164    
165                            if (inlineSQLHelper) {
166                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
167                                            sql, DLFileEntryType.class.getName(),
168                                            "DLFileEntryType.fileEntryTypeId", groupIds);
169                            }
170    
171                            sql = StringUtil.replace(
172                                    sql, "[$WHERE$]", getWhere(includeBasicFileEntryType));
173                            sql = StringUtil.replace(
174                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds));
175                            sql = CustomSQLUtil.replaceKeywords(
176                                    sql, "lower(name)", StringPool.LIKE, false, names);
177                            sql = CustomSQLUtil.replaceKeywords(
178                                    sql, "description", StringPool.LIKE, true, descriptions);
179                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
180    
181                            SQLQuery q = session.createSQLQuery(sql);
182    
183                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
184    
185                            QueryPos qPos = QueryPos.getInstance(q);
186    
187                            if (includeBasicFileEntryType) {
188                                    qPos.add(names, 2);
189                                    qPos.add(descriptions, 2);
190                            }
191    
192                            qPos.add(companyId);
193                            qPos.add(groupIds);
194                            qPos.add(names, 2);
195                            qPos.add(descriptions, 2);
196    
197                            Iterator<Long> itr = q.iterate();
198    
199                            if (itr.hasNext()) {
200                                    Long count = itr.next();
201    
202                                    if (count != null) {
203                                            return count.intValue();
204                                    }
205                            }
206    
207                            return 0;
208                    }
209                    catch (Exception e) {
210                            throw new SystemException(e);
211                    }
212                    finally {
213                            closeSession(session);
214                    }
215            }
216    
217            protected List<DLFileEntryType> doFindByC_G_N_D_S(
218                            long companyId, long[] groupIds, String[] names,
219                            String[] descriptions, boolean andOperator,
220                            boolean includeBasicFileEntryType, int start, int end,
221                            OrderByComparator orderByComparator, boolean inlineSQLHelper)
222                    throws SystemException {
223    
224                    names = CustomSQLUtil.keywords(names);
225                    descriptions = CustomSQLUtil.keywords(descriptions, false);
226    
227                    Session session = null;
228    
229                    try {
230                            session = openSession();
231    
232                            String sql = CustomSQLUtil.get(FIND_BY_C_G_N_D_S);
233    
234                            if (inlineSQLHelper) {
235                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
236                                            sql, DLFileEntryType.class.getName(),
237                                            "DLFileEntryType.fileEntryTypeId", groupIds);
238                            }
239    
240                            sql = StringUtil.replace(
241                                    sql, "[$WHERE$]", getWhere(includeBasicFileEntryType));
242                            sql = StringUtil.replace(
243                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds));
244                            sql = CustomSQLUtil.replaceKeywords(
245                                    sql, "lower(name)", StringPool.LIKE, false, names);
246                            sql = CustomSQLUtil.replaceKeywords(
247                                    sql, "description", StringPool.LIKE, true, descriptions);
248                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
249    
250                            if (orderByComparator != null) {
251                                    String orderByFields = StringUtil.merge(
252                                            orderByComparator.getOrderByFields(), StringPool.COMMA);
253    
254                                    sql = StringUtil.replace(
255                                            sql, "name ASC", orderByFields.concat(" DESC"));
256                            }
257    
258                            SQLQuery q = session.createSQLQuery(sql);
259    
260                            q.addEntity("DLFileEntryType", DLFileEntryTypeImpl.class);
261    
262                            QueryPos qPos = QueryPos.getInstance(q);
263    
264                            if (includeBasicFileEntryType) {
265                                    qPos.add(names, 2);
266                                    qPos.add(descriptions, 2);
267                            }
268    
269                            qPos.add(companyId);
270                            qPos.add(groupIds);
271                            qPos.add(names, 2);
272                            qPos.add(descriptions, 2);
273    
274                            return (List<DLFileEntryType>)QueryUtil.list(
275                                    q, getDialect(), start, end);
276                    }
277                    catch (Exception e) {
278                            throw new SystemException(e);
279                    }
280                    finally {
281                            closeSession(session);
282                    }
283            }
284    
285            protected String getGroupIds(long[] groupIds) {
286                    if (groupIds.length == 0) {
287                            return StringPool.BLANK;
288                    }
289    
290                    StringBundler sb = new StringBundler(groupIds.length * 2);
291    
292                    sb.append("(");
293    
294                    for (int i = 0; i < groupIds.length; i++) {
295                            sb.append("groupId = ?");
296    
297                            if ((i + 1) < groupIds.length) {
298                                    sb.append(" OR ");
299                            }
300                    }
301    
302                    sb.append(") AND");
303    
304                    return sb.toString();
305            }
306    
307            protected String getWhere(boolean includeBasicFileEntryType) {
308                    if (!includeBasicFileEntryType) {
309                            return StringPool.BLANK;
310                    }
311    
312                    StringBundler sb = new StringBundler(5);
313    
314                    sb.append("((companyId = 0) AND (groupId = 0) AND (");
315                    sb.append("(lower(name) LIKE ? [$AND_OR_NULL_CHECK$]) ");
316                    sb.append("[$AND_OR_CONNECTOR$] ");
317                    sb.append("(description LIKE ? [$AND_OR_NULL_CHECK$]) ");
318                    sb.append(")) OR ");
319    
320                    return sb.toString();
321            }
322    
323    }