001    /**
002     * Copyright (c) 2000-2011 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            @Override
029            public void update(
030                            long oldPrimaryKeyValue, Object[] oldColumnValues,
031                            Object[] oldExtraColumnValues, long newPrimaryKeyValue,
032                            Object[] newColumnValues, Object[] newExtraColumnValues)
033                    throws Exception {
034    
035                    long resourceId = newPrimaryKeyValue;
036    
037                    long permissionId = getPermissionId(resourceId);
038    
039                    deleteDuplicateData("Permission_", resourceId);
040    
041                    if (permissionId > 0) {
042                            DependencyManager permissionDependencyManager =
043                                    new PermissionDependencyManager();
044    
045                            permissionDependencyManager.setPrimaryKeyName("permissionId");
046    
047                            permissionDependencyManager.update(permissionId);
048                    }
049            }
050    
051            protected long getPermissionId(long resourceId) throws Exception {
052                    Connection con = null;
053                    PreparedStatement ps = null;
054                    ResultSet rs = null;
055    
056                    try {
057                            con = DataAccess.getConnection();
058    
059                            ps = con.prepareStatement(
060                                    "select permissionId from Permission_ where resourceId = ?");
061    
062                            ps.setLong(1, resourceId);
063    
064                            rs = ps.executeQuery();
065    
066                            while (rs.next()) {
067                                    long permissionId = rs.getLong("permissionId");
068    
069                                    return permissionId;
070                            }
071                    }
072                    finally {
073                            DataAccess.cleanUp(con, ps, rs);
074                    }
075    
076                    return 0;
077            }
078    
079    }