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.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 ResourceCodeDependencyManager 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 codeId = newPrimaryKeyValue;
035    
036                    long resourceId = getResourceId(codeId);
037    
038                    deleteDuplicateData("Resource_", codeId);
039    
040                    if (resourceId > 0) {
041                            DependencyManager resourceDependencyManager =
042                                    new ResourceDependencyManager();
043    
044                            resourceDependencyManager.setPrimaryKeyName("resourceId");
045    
046                            resourceDependencyManager.update(resourceId);
047                    }
048            }
049    
050            protected long getResourceId(long codeId) 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 resourceId from Resource_ where codeId = ?");
060    
061                            ps.setLong(1, codeId);
062    
063                            rs = ps.executeQuery();
064    
065                            while (rs.next()) {
066                                    long resourceId = rs.getLong("resourceId");
067    
068                                    return resourceId;
069                            }
070                    }
071                    finally {
072                            DataAccess.cleanUp(con, ps, rs);
073                    }
074    
075                    return 0;
076            }
077    
078    }