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.dynamicdatalists.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.StringUtil;
025    import com.liferay.portal.kernel.workflow.WorkflowConstants;
026    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
027    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
028    import com.liferay.portlet.dynamicdatalists.model.impl.DDLRecordImpl;
029    import com.liferay.util.dao.orm.CustomSQLUtil;
030    
031    import java.util.Iterator;
032    import java.util.List;
033    
034    /**
035     * @author Marcellus Tavares
036     */
037    public class DDLRecordFinderImpl extends BasePersistenceImpl<DDLRecord>
038            implements DDLRecordFinder {
039    
040            public static final String COUNT_BY_R_S =
041                    DDLRecordFinder.class.getName() + ".countByR_S";
042    
043            public static final String FIND_BY_R_S =
044                    DDLRecordFinder.class.getName() + ".findByR_S";
045    
046            @Override
047            public int countByR_S(long recordSetId, int status) throws SystemException {
048                    Session session = null;
049    
050                    try {
051                            session = openSession();
052    
053                            String sql = CustomSQLUtil.get(COUNT_BY_R_S);
054    
055                            if (status == WorkflowConstants.STATUS_ANY) {
056                                    sql = StringUtil.replace(
057                                            sql, "(DDLRecordVersion.status = ?) AND", "");
058                            }
059    
060                            SQLQuery q = session.createSQLQuery(sql);
061    
062                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
063    
064                            QueryPos qPos = QueryPos.getInstance(q);
065    
066                            if (status != WorkflowConstants.STATUS_ANY) {
067                                    qPos.add(status);
068                            }
069    
070                            qPos.add(recordSetId);
071    
072                            Iterator<Long> itr = q.iterate();
073    
074                            if (itr.hasNext()) {
075                                    Long count = itr.next();
076    
077                                    if (count != null) {
078                                            return count.intValue();
079                                    }
080                            }
081    
082                            return 0;
083                    }
084                    catch (Exception e) {
085                            throw new SystemException(e);
086                    }
087                    finally {
088                            closeSession(session);
089                    }
090            }
091    
092            @Override
093            public List<DDLRecord> findByR_S(
094                            long recordSetId, int status, int start, int end,
095                            OrderByComparator orderByComparator)
096                    throws SystemException {
097    
098                    Session session = null;
099    
100                    try {
101                            session = openSession();
102    
103                            String sql = CustomSQLUtil.get(FIND_BY_R_S);
104    
105                            if (status == WorkflowConstants.STATUS_ANY) {
106                                    sql = StringUtil.replace(
107                                            sql, "(DDLRecordVersion.status = ?) AND", "");
108                            }
109    
110                            sql = CustomSQLUtil.replaceOrderBy(sql, orderByComparator);
111    
112                            SQLQuery q = session.createSQLQuery(sql);
113    
114                            q.addEntity("DDLRecord", DDLRecordImpl.class);
115    
116                            QueryPos qPos = QueryPos.getInstance(q);
117    
118                            if (status != WorkflowConstants.STATUS_ANY) {
119                                    qPos.add(status);
120                            }
121    
122                            qPos.add(recordSetId);
123    
124                            return (List<DDLRecord>)QueryUtil.list(q, getDialect(), start, end);
125                    }
126                    catch (Exception e) {
127                            throw new SystemException(e);
128                    }
129                    finally {
130                            closeSession(session);
131                    }
132            }
133    
134    }