001    /**
002     * Copyright (c) 2000-2010 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.StringUtil;
018    import com.liferay.portal.upgrade.util.Table;
019    
020    import java.sql.Types;
021    
022    import java.util.ArrayList;
023    import java.util.List;
024    
025    /**
026     * @author Alexander Chow
027     */
028    public class PermissionView extends Table {
029    
030            public static String getActionId(String[] values) {
031                    return values[values.length - 4];
032            }
033    
034            public static long getCompanyId(String[] values) {
035                    return Long.parseLong(values[values.length - 5]);
036            }
037    
038            public static String getNameId(String[] values) {
039                    return values[values.length - 2];
040            }
041    
042            public static long getPermissionId(String[] values) {
043                    return Long.parseLong(values[values.length - 6]);
044            }
045    
046            public static long getPrimaryKey(String[] values) {
047                    return Long.parseLong(values[0]);
048            }
049    
050            public static long getResourceId(String[] values) {
051                    return Long.parseLong(values[values.length - 3]);
052            }
053    
054            public static int getScopeId(String[] values) {
055                    return Integer.parseInt(values[values.length - 1]);
056            }
057    
058            public PermissionView(String tableName, String[] primKeys) {
059                    super(tableName);
060    
061                    List<Object[]> columns = new ArrayList<Object[]>();
062    
063                    for (String primKey : primKeys) {
064                            columns.add(new Object[] {primKey, Types.BIGINT});
065                    }
066    
067                    columns.add(new Object[] {"permissionId", Types.BIGINT});
068                    columns.add(new Object[] {"companyId", Types.BIGINT});
069                    columns.add(new Object[] {"actionId", Types.VARCHAR});
070                    columns.add(new Object[] {"resourceId", Types.BIGINT});
071                    columns.add(new Object[] {"name", Types.VARCHAR});
072                    columns.add(new Object[] {"scope", Types.INTEGER});
073    
074                    setColumns(columns.toArray(new Object[0][]));
075            }
076    
077            public String getSelectSQL() throws Exception {
078                    return StringUtil.replace(_SELECT_SQL, "_OLD_TABLE_", getTableName());
079            }
080    
081            private static final String _SELECT_SQL =
082                    "SELECT _OLD_TABLE_.*, Permission_.companyId, Permission_.actionId, " +
083                    "Resource_.resourceId, ResourceCode.name, ResourceCode.scope FROM " +
084                    "_OLD_TABLE_, Permission_, Resource_, ResourceCode WHERE " +
085                    "_OLD_TABLE_.permissionId = Permission_.permissionId AND " +
086                    "Permission_.resourceId = Resource_.resourceId AND " +
087                    "Resource_.codeId = ResourceCode.codeId ORDER BY " +
088                    "Resource_.resourceId";
089    
090    }