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