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.verify;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.model.ClassName;
021    import com.liferay.portal.service.ClassNameLocalServiceUtil;
022    
023    import java.sql.Connection;
024    import java.sql.PreparedStatement;
025    import java.sql.ResultSet;
026    
027    /**
028     * @author Shinn Lok
029     */
030    public class VerifyWorkflow extends VerifyProcess {
031    
032            protected void deleteOrphanedWorkflowDefinitionLinks() throws Exception {
033                    Connection con = null;
034                    PreparedStatement ps = null;
035                    ResultSet rs = null;
036    
037                    try {
038                            con = DataAccess.getUpgradeOptimizedConnection();
039    
040                            ps = con.prepareStatement(
041                                    "select distinct classNameId from WorkflowDefinitionLink");
042    
043                            rs = ps.executeQuery();
044    
045                            while (rs.next()) {
046                                    long classNameId = rs.getLong("classNameId");
047    
048                                    ClassName className = ClassNameLocalServiceUtil.fetchClassName(
049                                            classNameId);
050    
051                                    if (className == null) {
052                                            continue;
053                                    }
054    
055                                    String classNameValue = className.getValue();
056    
057                                    for (String[] orphanedAttachedModel :
058                                                    getOrphanedAttachedModels()) {
059    
060                                            String orphanedClassName = orphanedAttachedModel[0];
061    
062                                            if (classNameValue.equals(orphanedClassName)) {
063                                                    String orphanedTableName = orphanedAttachedModel[1];
064                                                    String orphanedColumnName = orphanedAttachedModel[2];
065    
066                                                    deleteOrphanedWorkflowDefinitionLinks(
067                                                            orphanedTableName, orphanedColumnName);
068                                            }
069                                    }
070                            }
071                    }
072                    finally {
073                            DataAccess.cleanUp(con, ps, rs);
074                    }
075            }
076    
077            protected void deleteOrphanedWorkflowDefinitionLinks(
078                            String tableName, String columnName)
079                    throws Exception {
080    
081                    StringBundler sb = new StringBundler(6);
082    
083                    sb.append("delete from WorkflowDefinitionLink where classPK not ");
084                    sb.append("in (select ");
085                    sb.append(columnName);
086                    sb.append(" from ");
087                    sb.append(tableName);
088                    sb.append(StringPool.CLOSE_PARENTHESIS);
089    
090                    runSQL(sb.toString());
091            }
092    
093            @Override
094            protected void doVerify() throws Exception {
095                    deleteOrphanedWorkflowDefinitionLinks();
096            }
097    
098            protected String[][] getOrphanedAttachedModels() {
099                    return _ORPHANED_ATTACHED_MODELS;
100            }
101    
102            private static final String[][] _ORPHANED_ATTACHED_MODELS = new String[][] {
103                    new String[] {
104                            "com.liferay.portal.workflow.kaleo.forms.model.KaleoProcess",
105                            "KaleoProcess", "kaleoProcessId"
106                    }
107            };
108    
109    }