001    /**
002     * Copyright (c) 2000-2011 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.upgrade.v5_2_3.util;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    
019    import java.sql.Connection;
020    import java.sql.PreparedStatement;
021    import java.sql.ResultSet;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class ResourceDependencyManager extends DependencyManager {
027    
028            public void update(
029                            long oldPrimaryKeyValue, Object[] oldColumnValues,
030                            Object[] oldExtraColumnValues, long newPrimaryKeyValue,
031                            Object[] newColumnValues, Object[] newExtraColumnValues)
032                    throws Exception {
033    
034                    long resourceId = newPrimaryKeyValue;
035    
036                    long permissionId = getPermissionId(resourceId);
037    
038                    deleteDuplicateData("Permission_", resourceId);
039    
040                    if (permissionId > 0) {
041                            DependencyManager permissionDependencyManager =
042                                    new PermissionDependencyManager();
043    
044                            permissionDependencyManager.setPrimaryKeyName("permissionId");
045    
046                            permissionDependencyManager.update(permissionId);
047                    }
048            }
049    
050            protected long getPermissionId(long resourceId) throws Exception {
051                    Connection con = null;
052                    PreparedStatement ps = null;
053                    ResultSet rs = null;
054    
055                    try {
056                            con = DataAccess.getConnection();
057    
058                            ps = con.prepareStatement(
059                                    "select permissionId from Permission_ where resourceId = ?");
060    
061                            ps.setLong(1, resourceId);
062    
063                            rs = ps.executeQuery();
064    
065                            while (rs.next()) {
066                                    long permissionId = rs.getLong("permissionId");
067    
068                                    return permissionId;
069                            }
070                    }
071                    finally {
072                            DataAccess.cleanUp(con, ps, rs);
073                    }
074    
075                    return 0;
076            }
077    
078    }