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