001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }