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