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.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            @Override
041            public ResourceBlockIdsBag findByC_G_N_R(
042                            long companyId, long groupId, String name, long[] roleIds)
043                    throws SystemException {
044    
045                    Session session = null;
046    
047                    try {
048                            session = openSession();
049    
050                            String sql = CustomSQLUtil.get(FIND_BY_C_G_N_R);
051    
052                            sql = StringUtil.replace(
053                                    sql, "[$ROLE_ID$]", StringUtil.merge(roleIds));
054    
055                            SQLQuery q = session.createSQLQuery(sql);
056    
057                            q.addScalar("resourceBlockId", Type.LONG);
058                            q.addScalar("actionIds", Type.LONG);
059    
060                            QueryPos qPos = QueryPos.getInstance(q);
061    
062                            qPos.add(companyId);
063                            qPos.add(groupId);
064                            qPos.add(name);
065    
066                            ResourceBlockIdsBag resourceBlockIdsBag = new ResourceBlockIdsBag();
067    
068                            Iterator<Object[]> itr = q.iterate();
069    
070                            while (itr.hasNext()) {
071                                    Object[] array = itr.next();
072    
073                                    Long resourceBlockId = (Long)array[0];
074                                    Long actionIdsLong = (Long)array[1];
075    
076                                    resourceBlockIdsBag.addResourceBlockId(
077                                            resourceBlockId, actionIdsLong);
078                            }
079    
080                            return resourceBlockIdsBag;
081                    }
082                    catch (Exception e) {
083                            throw new SystemException(e);
084                    }
085                    finally {
086                            closeSession(session);
087                    }
088            }
089    
090    }