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