001    /**
002     * Copyright (c) 2000-2013 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_1_5.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            @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 codeId = newPrimaryKeyValue;
036    
037                    long resourceId = getResourceId(codeId);
038    
039                    deleteDuplicateData("Resource_", codeId);
040    
041                    if (resourceId > 0) {
042                            DependencyManager resourceDependencyManager =
043                                    new ResourceDependencyManager();
044    
045                            resourceDependencyManager.setPrimaryKeyName("resourceId");
046    
047                            resourceDependencyManager.update(resourceId);
048                    }
049            }
050    
051            protected long getResourceId(long codeId) throws Exception {
052                    Connection con = null;
053                    PreparedStatement ps = null;
054                    ResultSet rs = null;
055    
056                    try {
057                            con = DataAccess.getUpgradeOptimizedConnection();
058    
059                            ps = con.prepareStatement(
060                                    "select resourceId from Resource_ where codeId = ?");
061    
062                            ps.setLong(1, codeId);
063    
064                            rs = ps.executeQuery();
065    
066                            while (rs.next()) {
067                                    long resourceId = rs.getLong("resourceId");
068    
069                                    return resourceId;
070                            }
071                    }
072                    finally {
073                            DataAccess.cleanUp(con, ps, rs);
074                    }
075    
076                    return 0;
077            }
078    
079    }