001    /**
002     * Copyright (c) 2000-2012 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            @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    }