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.model.ClassName;
018    import com.liferay.portal.kernel.service.ClassNameLocalServiceUtil;
019    import com.liferay.portal.kernel.util.LoggingTimer;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
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 deleteOrphaned() throws Exception {
032                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
033                            for (String[] orphanedAttachedModel : getOrphanedAttachedModels()) {
034                                    String tableName = orphanedAttachedModel[0];
035    
036                                    if (!hasTable(tableName) ||
037                                            !hasColumn(tableName, "classNameId")) {
038    
039                                            continue;
040                                    }
041    
042                                    try (PreparedStatement ps = connection.prepareStatement(
043                                                    "select distinct classNameId from " + tableName);
044                                            ResultSet rs = ps.executeQuery()) {
045    
046                                            while (rs.next()) {
047                                                    long classNameId = rs.getLong("classNameId");
048    
049                                                    ClassName className =
050                                                            ClassNameLocalServiceUtil.fetchClassName(
051                                                                    classNameId);
052    
053                                                    if (className == null) {
054                                                            continue;
055                                                    }
056    
057                                                    String classNameValue = className.getValue();
058    
059                                                    String orphanedClassName = orphanedAttachedModel[1];
060    
061                                                    if (!classNameValue.equals(orphanedClassName)) {
062                                                            continue;
063                                                    }
064    
065                                                    String orphanedTableName = orphanedAttachedModel[2];
066                                                    String orphanedColumnName = orphanedAttachedModel[3];
067    
068                                                    if (!hasTable(orphanedTableName)) {
069                                                            continue;
070                                                    }
071    
072                                                    deleteOrphaned(
073                                                            tableName, orphanedTableName, orphanedColumnName);
074                                            }
075                                    }
076                            }
077                    }
078            }
079    
080            protected void deleteOrphaned(
081                            String tableName, String orphanedTableName,
082                            String orphanedColumnName)
083                    throws Exception {
084    
085                    StringBundler sb = new StringBundler(7);
086    
087                    sb.append("delete from ");
088                    sb.append(tableName);
089                    sb.append(" where classPK not in (select ");
090                    sb.append(orphanedColumnName);
091                    sb.append(" from ");
092                    sb.append(orphanedTableName);
093                    sb.append(StringPool.CLOSE_PARENTHESIS);
094    
095                    runSQL(sb.toString());
096            }
097    
098            @Override
099            protected void doVerify() throws Exception {
100                    deleteOrphaned();
101            }
102    
103            protected String[][] getOrphanedAttachedModels() {
104                    return _ORPHANED_ATTACHED_MODELS;
105            }
106    
107            private static final String[][] _ORPHANED_ATTACHED_MODELS = new String[][] {
108                    new String[] {
109                            "KaleoInstance",
110                            "com.liferay.portal.workflow.kaleo.forms.model.KaleoProcess",
111                            "DDLRecord", "recordId"
112                    },
113                    new String[] {
114                            "KaleoInstanceToken",
115                            "com.liferay.portal.workflow.kaleo.forms.model.KaleoProcess",
116                            "DDLRecord", "recordId"
117                    },
118                    new String[] {
119                            "WorkflowDefinitionLink",
120                            "com.liferay.portal.workflow.kaleo.forms.model.KaleoProcess",
121                            "DDLRecord", "recordId"
122                    },
123                    new String[] {
124                            "WorkflowDefinitionLink",
125                            "com.liferay.portal.workflow.kaleo.forms.model.KaleoProcess",
126                            "KaleoProcess", "kaleoProcessId"
127                    }
128            };
129    
130    }