001    /**
002     * Copyright (c) 2000-2011 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.service.persistence.impl.BasePersistenceImpl;
029    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
030    import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryTypeImpl;
031    import com.liferay.util.dao.orm.CustomSQLUtil;
032    
033    import java.util.Iterator;
034    import java.util.List;
035    
036    /**
037     * @author Sergio González
038     * @author Connor McKay
039     * @author Alexander Chow
040     */
041    public class DLFileEntryTypeFinderImpl
042            extends BasePersistenceImpl<DLFileEntryType>
043            implements DLFileEntryTypeFinder {
044    
045            public static String COUNT_BY_C_G_N_D_S =
046                    DLFileEntryTypeFinder.class.getName() + ".countByC_G_N_D_S";
047    
048            public static String FIND_BY_C_G_N_D_S =
049                    DLFileEntryTypeFinder.class.getName() + ".findByC_G_N_D_S";
050    
051            public static String JOIN_BY_FILE_ENTRY_TYPE =
052                    DLFileEntryTypeFinder.class.getName() + ".joinByFileEntryType";
053    
054            public int countByKeywords(
055                            long companyId, long[] groupIds, String keywords,
056                            boolean includeBasicFileEntryType)
057                    throws SystemException {
058    
059                    String[] names = null;
060                    String[] descriptions = null;
061                    boolean andOperator = false;
062    
063                    if (Validator.isNotNull(keywords)) {
064                            names = CustomSQLUtil.keywords(keywords);
065                            descriptions = CustomSQLUtil.keywords(keywords, false);
066                    }
067                    else {
068                            andOperator = true;
069                    }
070    
071                    return countByC_G_N_D_S(
072                            companyId, groupIds, names, descriptions, andOperator,
073                            includeBasicFileEntryType);
074            }
075    
076            public List<DLFileEntryType> findByKeywords(
077                            long companyId, long[] groupIds, String keywords,
078                            boolean includeBasicFileEntryType, int start, int end,
079                            OrderByComparator orderByComparator)
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 findByC_G_N_D_S(
095                            companyId, groupIds, names, descriptions, andOperator,
096                            includeBasicFileEntryType, start, end, orderByComparator);
097            }
098    
099            protected int countByC_G_N_D_S(
100                            long companyId, long[] groupIds, String[] names,
101                            String[] descriptions, boolean andOperator,
102                            boolean includeBasicFileEntryType)
103                    throws SystemException {
104    
105                    names = CustomSQLUtil.keywords(names);
106                    descriptions = CustomSQLUtil.keywords(descriptions, false);
107    
108                    Session session = null;
109    
110                    try {
111                            session = openSession();
112    
113                            String sql = CustomSQLUtil.get(COUNT_BY_C_G_N_D_S);
114    
115                            sql = StringUtil.replace(
116                                    sql, "[$WHERE$]", getWhere(includeBasicFileEntryType));
117                            sql = StringUtil.replace(
118                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds));
119                            sql = CustomSQLUtil.replaceKeywords(
120                                    sql, "lower(name)", StringPool.LIKE, false, names);
121                            sql = CustomSQLUtil.replaceKeywords(
122                                    sql, "description", StringPool.LIKE, false, descriptions);
123                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
124    
125                            SQLQuery q = session.createSQLQuery(sql);
126    
127                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
128    
129                            QueryPos qPos = QueryPos.getInstance(q);
130    
131                            if (includeBasicFileEntryType) {
132                                    qPos.add(names, 2);
133                                    qPos.add(descriptions, 2);
134                            }
135    
136                            qPos.add(companyId);
137                            qPos.add(groupIds);
138                            qPos.add(names, 2);
139                            qPos.add(descriptions, 2);
140    
141                            Iterator<Long> itr = q.iterate();
142    
143                            if (itr.hasNext()) {
144                                    Long count = itr.next();
145    
146                                    if (count != null) {
147                                            return count.intValue();
148                                    }
149                            }
150    
151                            return 0;
152                    }
153                    catch (Exception e) {
154                            throw new SystemException(e);
155                    }
156                    finally {
157                            closeSession(session);
158                    }
159            }
160    
161            protected List<DLFileEntryType> findByC_G_N_D_S(
162                            long companyId, long[] groupIds, String[] names,
163                            String[] descriptions, boolean andOperator,
164                            boolean includeBasicFileEntryType, int start, int end,
165                            OrderByComparator orderByComparator)
166                    throws SystemException {
167    
168                    names = CustomSQLUtil.keywords(names);
169                    descriptions = CustomSQLUtil.keywords(descriptions, false);
170    
171                    Session session = null;
172    
173                    try {
174                            session = openSession();
175    
176                            String sql = CustomSQLUtil.get(FIND_BY_C_G_N_D_S);
177    
178                            sql = StringUtil.replace(
179                                    sql, "[$WHERE$]", getWhere(includeBasicFileEntryType));
180                            sql = StringUtil.replace(
181                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds));
182                            sql = CustomSQLUtil.replaceKeywords(
183                                    sql, "lower(name)", StringPool.LIKE, false, names);
184                            sql = CustomSQLUtil.replaceKeywords(
185                                    sql, "description", StringPool.LIKE, false, descriptions);
186                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
187    
188                            if (orderByComparator != null) {
189                                    String orderByFields = StringUtil.merge(
190                                            orderByComparator.getOrderByFields(), StringPool.COMMA);
191    
192                                    sql = StringUtil.replace(
193                                            sql, "name ASC", orderByFields.concat(" DESC"));
194                            }
195    
196                            SQLQuery q = session.createSQLQuery(sql);
197    
198                            q.addEntity("DLFileEntryType", DLFileEntryTypeImpl.class);
199    
200                            QueryPos qPos = QueryPos.getInstance(q);
201    
202                            if (includeBasicFileEntryType) {
203                                    qPos.add(names, 2);
204                                    qPos.add(descriptions, 2);
205                            }
206    
207                            qPos.add(companyId);
208                            qPos.add(groupIds);
209                            qPos.add(names, 2);
210                            qPos.add(descriptions, 2);
211    
212                            return (List<DLFileEntryType>)QueryUtil.list(
213                                    q, getDialect(), start, end);
214                    }
215                    catch (Exception e) {
216                            throw new SystemException(e);
217                    }
218                    finally {
219                            closeSession(session);
220                    }
221            }
222    
223            protected String getGroupIds(long[] groupIds) {
224                    if (groupIds.length == 0) {
225                            return StringPool.BLANK;
226                    }
227    
228                    StringBundler sb = new StringBundler(groupIds.length * 2);
229    
230                    sb.append("(");
231    
232                    for (int i = 0; i < groupIds.length; i++) {
233                            sb.append("groupId = ?");
234    
235                            if ((i + 1) < groupIds.length) {
236                                    sb.append(" OR ");
237                            }
238                    }
239    
240                    sb.append(") AND");
241    
242                    return sb.toString();
243            }
244    
245            protected String getWhere(boolean includeBasicFileEntryType) {
246                    if (!includeBasicFileEntryType) {
247                            return StringPool.BLANK;
248                    }
249    
250                    StringBundler sb = new StringBundler(5);
251    
252                    sb.append("((companyId = 0) AND (groupId = 0) AND (");
253                    sb.append("(lower(name) LIKE ? [$AND_OR_NULL_CHECK$]) ");
254                    sb.append("[$AND_OR_CONNECTOR$] ");
255                    sb.append("(description LIKE ? [$AND_OR_NULL_CHECK$]) ");
256                    sb.append(")) OR ");
257    
258                    return sb.toString();
259            }
260    
261    }