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.SQLQuery;
019    import com.liferay.portal.kernel.dao.orm.Session;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
023    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
024    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
025    import com.liferay.portlet.documentlibrary.model.DLFolder;
026    import com.liferay.portlet.documentlibrary.model.DLSync;
027    import com.liferay.portlet.documentlibrary.model.DLSyncConstants;
028    import com.liferay.portlet.documentlibrary.model.impl.DLSyncImpl;
029    import com.liferay.util.dao.orm.CustomSQLUtil;
030    
031    import java.util.Date;
032    import java.util.List;
033    
034    /**
035     * @author Michael Young
036     */
037    public class DLSyncFinderImpl
038            extends BasePersistenceImpl<DLSync> implements DLSyncFinder {
039    
040            public static final String FIND_BY_C_M_R_T =
041                    DLSyncFinder.class.getName() + ".findByC_M_R_T";
042    
043            public List<DLSync> filterFindByC_M_R(
044                            long companyId, Date modifiedDate, long repositoryId)
045                    throws SystemException {
046    
047                    Session session = null;
048    
049                    try {
050                            session = openSession();
051    
052                            String sql = CustomSQLUtil.get(FIND_BY_C_M_R_T);
053    
054                            sql = InlineSQLHelperUtil.replacePermissionCheck(
055                                    sql, DLFolder.class.getName(), "DLSync.fileId", null,
056                                    "DLSync.repositoryId", new long[] {repositoryId}, null);
057    
058                            StringBundler sb = new StringBundler(3);
059    
060                            sb.append(sql);
061                            sb.append(" UNION ALL ");
062    
063                            sql = CustomSQLUtil.get(FIND_BY_C_M_R_T);
064    
065                            sql = InlineSQLHelperUtil.replacePermissionCheck(
066                                    sql, DLFileEntry.class.getName(), "DLSync.fileId", null,
067                                    "DLSync.repositoryId", new long[] {repositoryId}, null);
068    
069                            sb.append(sql);
070    
071                            sql = sb.toString();
072    
073                            SQLQuery q = session.createSQLQuery(sql);
074    
075                            q.addEntity("DLSync", DLSyncImpl.class);
076    
077                            QueryPos qPos = QueryPos.getInstance(q);
078    
079                            qPos.add(companyId);
080                            qPos.add(modifiedDate);
081                            qPos.add(repositoryId);
082                            qPos.add(DLSyncConstants.TYPE_FOLDER);
083                            qPos.add(companyId);
084                            qPos.add(modifiedDate);
085                            qPos.add(repositoryId);
086                            qPos.add(DLSyncConstants.TYPE_FILE);
087    
088                            return q.list();
089                    }
090                    catch (Exception e) {
091                            throw new SystemException(e);
092                    }
093                    finally {
094                            closeSession(session);
095                    }
096            }
097    
098    }