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.convert.util;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.upgrade.util.Table;
021    
022    import java.sql.Connection;
023    import java.sql.PreparedStatement;
024    import java.sql.Types;
025    
026    import java.util.ArrayList;
027    import java.util.List;
028    
029    /**
030     * @author Alexander Chow
031     */
032    public class ResourcePermissionView extends Table {
033    
034            public static String getActionId(String[] values) {
035                    return values[4];
036            }
037    
038            public static long getCompanyId(String[] values) {
039                    return GetterUtil.getLong(values[0]);
040            }
041    
042            public static String getPrimaryKey(String[] values) {
043                    return values[2];
044            }
045    
046            public static long getRoleId(String[] values) {
047                    return GetterUtil.getLong(values[3]);
048            }
049    
050            public static int getScope(String[] values) {
051                    return GetterUtil.getInteger(values[1]);
052            }
053    
054            public ResourcePermissionView(String name) {
055                    super("ResourcePermissionView");
056    
057                    List<Object[]> columns = new ArrayList<Object[]>();
058    
059                    columns.add(new Object[] {"companyId", Types.BIGINT});
060                    columns.add(new Object[] {"scope", Types.INTEGER});
061                    columns.add(new Object[] {"primKey", Types.VARCHAR});
062                    columns.add(new Object[] {"roleId", Types.BIGINT});
063                    columns.add(new Object[] {"actionId", Types.VARCHAR});
064    
065                    setColumns(columns.toArray(new Object[0][]));
066    
067                    _name = name;
068            }
069    
070            @Override
071            public PreparedStatement getSelectPreparedStatement(Connection con)
072                    throws Exception {
073    
074                    String selectSQL = _SELECT_SQL + StringPool.QUESTION;
075    
076                    PreparedStatement ps = con.prepareStatement(selectSQL);
077    
078                    ps.setString(1, _name);
079    
080                    return ps;
081            }
082    
083            @Override
084            public String getSelectSQL() throws Exception {
085                    StringBundler sb = new StringBundler(4);
086    
087                    sb.append(_SELECT_SQL);
088                    sb.append(StringPool.APOSTROPHE);
089                    sb.append(_name);
090                    sb.append(StringPool.APOSTROPHE);
091    
092                    return sb.toString();
093            }
094    
095            private static final String _SELECT_SQL =
096                    "SELECT Permission_.companyId, ResourceCode.scope, " +
097                    "Resource_.primKey, Roles_Permissions.roleId, Permission_.actionId " +
098                    "FROM Roles_Permissions, Permission_, Resource_, ResourceCode WHERE " +
099                    "Permission_.permissionId = Roles_Permissions.permissionId AND " +
100                    "Permission_.resourceId = Resource_.resourceId AND " +
101                    "Resource_.codeId = ResourceCode.codeId AND ResourceCode.name = ";
102    
103            private String _name = StringPool.BLANK;
104    
105    }