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.portal.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.dao.orm.Type;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.model.ResourceBlock;
024    import com.liferay.portal.security.permission.ResourceBlockIdsBag;
025    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
026    import com.liferay.util.dao.orm.CustomSQLUtil;
027    
028    import java.util.Iterator;
029    
030    /**
031     * @author Connor McKay
032     */
033    public class ResourceBlockFinderImpl
034            extends BasePersistenceImpl<ResourceBlock>
035            implements ResourceBlockFinder {
036    
037            public static final String FIND_BY_C_G_N_R =
038                    ResourceBlockFinder.class.getName() + ".findByC_G_N_R";
039    
040            public ResourceBlockIdsBag findByC_G_N_R(
041                            long companyId, long groupId, String name, long[] roleIds)
042                    throws SystemException {
043    
044                    Session session = null;
045    
046                    try {
047                            session = openSession();
048    
049                            String sql = CustomSQLUtil.get(FIND_BY_C_G_N_R);
050    
051                            sql = StringUtil.replace(
052                                    sql, "[$ROLE_IDS$]", StringUtil.merge(roleIds));
053    
054                            SQLQuery q = session.createSQLQuery(sql);
055    
056                            q.addScalar("resourceBlockId", Type.LONG);
057                            q.addScalar("actionIds", Type.LONG);
058    
059                            QueryPos qPos = QueryPos.getInstance(q);
060    
061                            qPos.add(companyId);
062                            qPos.add(groupId);
063                            qPos.add(name);
064    
065                            ResourceBlockIdsBag resourceBlockIdsBag = new ResourceBlockIdsBag();
066    
067                            Iterator<Object[]> itr = q.iterate();
068    
069                            while (itr.hasNext()) {
070                                    Object[] array = itr.next();
071    
072                                    Long resourceBlockId = (Long)array[0];
073                                    Long actionIdsLong = (Long)array[1];
074    
075                                    resourceBlockIdsBag.addResourceBlockId(
076                                            resourceBlockId, actionIdsLong);
077                            }
078    
079                            return resourceBlockIdsBag;
080                    }
081                    catch (Exception e) {
082                            throw new SystemException(e);
083                    }
084                    finally {
085                            closeSession(session);
086                    }
087            }
088    
089    }