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.bookmarks.service.persistence;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryDefinition;
018    import com.liferay.portal.kernel.dao.orm.QueryPos;
019    import com.liferay.portal.kernel.dao.orm.QueryUtil;
020    import com.liferay.portal.kernel.dao.orm.SQLQuery;
021    import com.liferay.portal.kernel.dao.orm.Session;
022    import com.liferay.portal.kernel.dao.orm.Type;
023    import com.liferay.portal.kernel.exception.SystemException;
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.workflow.WorkflowConstants;
028    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
029    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
030    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
031    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
032    import com.liferay.portlet.bookmarks.model.impl.BookmarksFolderImpl;
033    import com.liferay.util.dao.orm.CustomSQLUtil;
034    
035    import java.util.ArrayList;
036    import java.util.Iterator;
037    import java.util.List;
038    
039    /**
040     * @author Eudaldo Alonso
041     * @author Alexander Chow
042     */
043    public class BookmarksFolderFinderImpl
044                    extends BasePersistenceImpl<BookmarksFolder>
045            implements BookmarksFolderFinder {
046    
047            public static final String COUNT_F_BY_G_P =
048                    BookmarksFolderFinder.class.getName() + ".countF_ByG_P";
049    
050            public static final String COUNT_F_BY_G_P_S =
051                    BookmarksFolderFinder.class.getName() + ".countF_ByG_P_S";
052    
053            public static final String COUNT_E_BY_G_F =
054                    BookmarksFolderFinder.class.getName() + ".countE_ByG_F";
055    
056            public static final String COUNT_E_BY_G_F_S =
057                    BookmarksFolderFinder.class.getName() + ".countE_ByG_F_S";
058    
059            public static final String FIND_BY_NO_ASSETS =
060                    BookmarksFolderFinder.class.getName() + ".findByNoAssets";
061    
062            public static final String FIND_F_BY_G_P =
063                    BookmarksFolderFinder.class.getName() + ".findF_ByG_P";
064    
065            public static final String FIND_F_BY_G_P_S =
066                    BookmarksFolderFinder.class.getName() + ".findF_ByG_P_S";
067    
068            public static final String FIND_E_BY_G_F =
069                    BookmarksFolderFinder.class.getName() + ".findE_ByG_F";
070    
071            public static final String FIND_E_BY_G_F_S =
072                    BookmarksFolderFinder.class.getName() + ".findE_ByG_F_S";
073    
074            @Override
075            public int countF_E_ByG_F(
076                            long groupId, long folderId, QueryDefinition queryDefinition)
077                    throws SystemException {
078    
079                    return doCountF_E_ByG_F(groupId, folderId, queryDefinition, false);
080            }
081    
082            @Override
083            public int filterCountF_E_ByG_F(
084                            long groupId, long folderId, QueryDefinition queryDefinition)
085                    throws SystemException {
086    
087                    return doCountF_E_ByG_F(groupId, folderId, queryDefinition, true);
088            }
089    
090            @Override
091            public List<Object> filterFindBF_E_ByG_F(
092                            long groupId, long folderId, QueryDefinition queryDefinition)
093                    throws SystemException {
094    
095                    return doFindF_E_ByG_F(groupId, folderId, queryDefinition, true);
096            }
097    
098            @Override
099            public List<BookmarksFolder> findByNoAssets() throws SystemException {
100                    Session session = null;
101    
102                    try {
103                            session = openSession();
104    
105                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
106    
107                            SQLQuery q = session.createSQLQuery(sql);
108    
109                            q.addEntity("BookmarksFolder", BookmarksFolderImpl.class);
110    
111                            return q.list(true);
112                    }
113                    catch (Exception e) {
114                            throw new SystemException(e);
115                    }
116                    finally {
117                            closeSession(session);
118                    }
119            }
120    
121            @Override
122            public List<Object> findF_E_ByG_F(
123                            long groupId, long folderId, QueryDefinition queryDefinition)
124                    throws SystemException {
125    
126                    return doFindF_E_ByG_F(groupId, folderId, queryDefinition, false);
127            }
128    
129            protected int doCountF_E_ByG_F(
130                            long groupId, long folderId, QueryDefinition queryDefinition,
131                            boolean inlineSQLHelper)
132                    throws SystemException {
133    
134                    Session session = null;
135    
136                    try {
137                            session = openSession();
138    
139                            StringBundler sb = new StringBundler(5);
140    
141                            sb.append(StringPool.OPEN_PARENTHESIS);
142    
143                            String sql = null;
144    
145                            if (queryDefinition.getStatus() == WorkflowConstants.STATUS_ANY) {
146                                    sql = CustomSQLUtil.get(COUNT_F_BY_G_P);
147                            }
148                            else {
149                                    sql = CustomSQLUtil.get(COUNT_F_BY_G_P_S);
150    
151                                    sql = replaceExcludeStatus(sql, queryDefinition);
152                            }
153    
154                            if (inlineSQLHelper) {
155                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
156                                            sql, BookmarksFolder.class.getName(),
157                                            "BookmarksFolder.folderId", groupId);
158                            }
159    
160                            sb.append(sql);
161                            sb.append(") UNION ALL (");
162    
163                            if (queryDefinition.getStatus() == WorkflowConstants.STATUS_ANY) {
164                                    sql = CustomSQLUtil.get(COUNT_E_BY_G_F);
165                            }
166                            else {
167                                    sql = CustomSQLUtil.get(COUNT_E_BY_G_F_S);
168    
169                                    sql = replaceExcludeStatus(sql, queryDefinition);
170                            }
171    
172                            if (inlineSQLHelper) {
173                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
174                                            sql, BookmarksEntry.class.getName(),
175                                            "BookmarksEntry.fileEntryId", groupId);
176                            }
177    
178                            sb.append(sql);
179                            sb.append(StringPool.CLOSE_PARENTHESIS);
180    
181                            sql = sb.toString();
182    
183                            SQLQuery q = session.createSQLQuery(sql);
184    
185                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
186    
187                            QueryPos qPos = QueryPos.getInstance(q);
188    
189                            qPos.add(groupId);
190                            qPos.add(folderId);
191    
192                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
193                                    qPos.add(queryDefinition.getStatus());
194                            }
195    
196                            qPos.add(groupId);
197                            qPos.add(folderId);
198    
199                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
200                                    qPos.add(queryDefinition.getStatus());
201                            }
202    
203                            int count = 0;
204    
205                            Iterator<Long> itr = q.iterate();
206    
207                            while (itr.hasNext()) {
208                                    Long l = itr.next();
209    
210                                    if (l != null) {
211                                            count += l.intValue();
212                                    }
213                            }
214    
215                            return count;
216                    }
217                    catch (Exception e) {
218                            throw new SystemException(e);
219                    }
220                    finally {
221                            closeSession(session);
222                    }
223            }
224    
225            protected List<Object> doFindF_E_ByG_F(
226                            long groupId, long folderId, QueryDefinition queryDefinition,
227                            boolean inlineSQLHelper)
228                    throws SystemException {
229    
230                    Session session = null;
231    
232                    try {
233                            session = openSession();
234    
235                            StringBundler sb = new StringBundler(5);
236    
237                            sb.append("SELECT * FROM (");
238    
239                            String sql = null;
240    
241                            if (queryDefinition.getStatus() == WorkflowConstants.STATUS_ANY) {
242                                    sql = CustomSQLUtil.get(FIND_F_BY_G_P);
243                            }
244                            else {
245                                    sql = CustomSQLUtil.get(FIND_F_BY_G_P_S);
246    
247                                    sql = replaceExcludeStatus(sql, queryDefinition);
248                            }
249    
250                            if (inlineSQLHelper) {
251                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
252                                            sql, BookmarksFolder.class.getName(),
253                                            "BookmarksFolder.folderId", groupId);
254                            }
255    
256                            sb.append(sql);
257                            sb.append(" UNION ALL ");
258    
259                            if (queryDefinition.getStatus() == WorkflowConstants.STATUS_ANY) {
260                                    sql = CustomSQLUtil.get(FIND_E_BY_G_F);
261                            }
262                            else {
263                                    sql = CustomSQLUtil.get(FIND_E_BY_G_F_S);
264    
265                                    sql = replaceExcludeStatus(sql, queryDefinition);
266                            }
267    
268                            if (inlineSQLHelper) {
269                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
270                                            sql, BookmarksEntry.class.getName(),
271                                            "BookmarksEntry.fileEntryId", groupId);
272                            }
273    
274                            sb.append(sql);
275                            sb.append(") TEMP_TABLE ORDER BY modelName ASC");
276    
277                            sql = sb.toString();
278    
279                            SQLQuery q = session.createSQLQuery(sql);
280    
281                            q.addScalar("modelId", Type.LONG);
282                            q.addScalar("modelName", Type.STRING);
283                            q.addScalar("modelFolder", Type.LONG);
284    
285                            QueryPos qPos = QueryPos.getInstance(q);
286    
287                            qPos.add(groupId);
288                            qPos.add(folderId);
289    
290                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
291                                    qPos.add(queryDefinition.getStatus());
292                            }
293    
294                            qPos.add(groupId);
295                            qPos.add(folderId);
296    
297                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
298                                    qPos.add(queryDefinition.getStatus());
299                            }
300    
301                            List<Object> models = new ArrayList<Object>();
302    
303                            Iterator<Object[]> itr = (Iterator<Object[]>)QueryUtil.iterate(
304                                    q, getDialect(), queryDefinition.getStart(),
305                                    queryDefinition.getEnd());
306    
307                            while (itr.hasNext()) {
308                                    Object[] array = itr.next();
309    
310                                    long modelId = (Long)array[0];
311                                    //String name = (String)array[1];
312                                    long modelFolder = (Long)array[2];
313    
314                                    Object obj = null;
315    
316                                    if (modelFolder == 0) {
317                                            obj = BookmarksFolderUtil.findByPrimaryKey(modelId);
318                                    }
319                                    else {
320                                            obj = BookmarksEntryUtil.findByPrimaryKey(modelId);
321                                    }
322    
323                                    models.add(obj);
324                            }
325    
326                            return models;
327                    }
328                    catch (Exception e) {
329                            throw new SystemException(e);
330                    }
331                    finally {
332                            closeSession(session);
333                    }
334            }
335    
336            protected String replaceExcludeStatus(
337                    String sql, QueryDefinition queryDefinition) {
338    
339                    if (queryDefinition.isExcludeStatus()) {
340                            sql = StringUtil.replace(sql, ".status = ?)", ".status != ?)");
341                    }
342    
343                    return sql;
344            }
345    
346    }