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.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            @Override
044            public List<DLSync> filterFindByC_M_R(
045                            long companyId, Date modifiedDate, long repositoryId)
046                    throws SystemException {
047    
048                    Session session = null;
049    
050                    try {
051                            session = openSession();
052    
053                            String sql = CustomSQLUtil.get(FIND_BY_C_M_R_T);
054    
055                            sql = InlineSQLHelperUtil.replacePermissionCheck(
056                                    sql, DLFolder.class.getName(), "DLSync.fileId", null,
057                                    "DLSync.repositoryId", new long[] {repositoryId}, null);
058    
059                            StringBundler sb = new StringBundler(3);
060    
061                            sb.append(sql);
062                            sb.append(" UNION ALL ");
063    
064                            sql = CustomSQLUtil.get(FIND_BY_C_M_R_T);
065    
066                            sql = InlineSQLHelperUtil.replacePermissionCheck(
067                                    sql, DLFileEntry.class.getName(), "DLSync.fileId", null,
068                                    "DLSync.repositoryId", new long[] {repositoryId}, null);
069    
070                            sb.append(sql);
071    
072                            sql = sb.toString();
073    
074                            SQLQuery q = session.createSQLQuery(sql);
075    
076                            q.addEntity("DLSync", DLSyncImpl.class);
077    
078                            QueryPos qPos = QueryPos.getInstance(q);
079    
080                            qPos.add(companyId);
081                            qPos.add(modifiedDate);
082                            qPos.add(repositoryId);
083                            qPos.add(DLSyncConstants.TYPE_FOLDER);
084                            qPos.add(companyId);
085                            qPos.add(modifiedDate);
086                            qPos.add(repositoryId);
087                            qPos.add(DLSyncConstants.TYPE_FILE);
088    
089                            return q.list();
090                    }
091                    catch (Exception e) {
092                            throw new SystemException(e);
093                    }
094                    finally {
095                            closeSession(session);
096                    }
097            }
098    
099    }