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.bean.PortalBeanLocatorUtil;
018    import com.liferay.portal.kernel.concurrent.ThrowableAwareRunnable;
019    import com.liferay.portal.kernel.dao.jdbc.AutoBatchPreparedStatementUtil;
020    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
021    import com.liferay.portal.kernel.util.LoggingTimer;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
024    import com.liferay.portal.kernel.verify.model.VerifiableUUIDModel;
025    
026    import java.sql.Connection;
027    import java.sql.PreparedStatement;
028    import java.sql.ResultSet;
029    
030    import java.util.ArrayList;
031    import java.util.Collection;
032    import java.util.List;
033    import java.util.Map;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class VerifyUUID extends VerifyProcess {
039    
040            public static void verify(VerifiableUUIDModel... verifiableUUIDModels)
041                    throws Exception {
042    
043                    VerifyUUID verifyUUID = new VerifyUUID();
044    
045                    verifyUUID.doVerify(verifiableUUIDModels);
046            }
047    
048            @Override
049            protected void doVerify() throws Exception {
050                    Map<String, VerifiableUUIDModel> verifiableUUIDModelsMap =
051                            PortalBeanLocatorUtil.locate(VerifiableUUIDModel.class);
052    
053                    Collection<VerifiableUUIDModel> verifiableUUIDModels =
054                            verifiableUUIDModelsMap.values();
055    
056                    doVerify(
057                            verifiableUUIDModels.toArray(
058                                    new VerifiableUUIDModel[verifiableUUIDModels.size()]));
059            }
060    
061            protected void doVerify(VerifiableUUIDModel... verifiableUUIDModels)
062                    throws Exception {
063    
064                    List<VerifyUUIDRunnable> verifyUUIDRunnables = new ArrayList<>(
065                            verifiableUUIDModels.length);
066    
067                    for (VerifiableUUIDModel verifiableUUIDModel : verifiableUUIDModels) {
068                            VerifyUUIDRunnable verifyUUIDRunnable = new VerifyUUIDRunnable(
069                                    verifiableUUIDModel);
070    
071                            verifyUUIDRunnables.add(verifyUUIDRunnable);
072                    }
073    
074                    doVerify(verifyUUIDRunnables);
075            }
076    
077            protected void verifyUUID(VerifiableUUIDModel verifiableUUIDModel)
078                    throws Exception {
079    
080                    StringBundler sb = new StringBundler(5);
081    
082                    sb.append("update ");
083                    sb.append(verifiableUUIDModel.getTableName());
084                    sb.append(" set uuid_ = ? where ");
085                    sb.append(verifiableUUIDModel.getPrimaryKeyColumnName());
086                    sb.append(" = ?");
087    
088                    try (LoggingTimer loggingTimer = new LoggingTimer(
089                                    verifiableUUIDModel.getTableName());
090                            Connection con = DataAccess.getUpgradeOptimizedConnection();
091                            PreparedStatement ps1 = con.prepareStatement(
092                                    "select " + verifiableUUIDModel.getPrimaryKeyColumnName() +
093                                            " from " + verifiableUUIDModel.getTableName() +
094                                                    " where uuid_ is null or uuid_ = ''");
095                            ResultSet rs = ps1.executeQuery();
096                            PreparedStatement ps2 = AutoBatchPreparedStatementUtil.autoBatch(
097                                    con.prepareStatement(sb.toString()))) {
098    
099                            while (rs.next()) {
100                                    long pk = rs.getLong(
101                                            verifiableUUIDModel.getPrimaryKeyColumnName());
102    
103                                    ps2.setString(1, PortalUUIDUtil.generate());
104                                    ps2.setLong(2, pk);
105    
106                                    ps2.addBatch();
107                            }
108    
109                            ps2.executeBatch();
110                    }
111            }
112    
113            private class VerifyUUIDRunnable extends ThrowableAwareRunnable {
114    
115                    public VerifyUUIDRunnable(VerifiableUUIDModel verifiableUUIDModel) {
116                            _verifiableUUIDModel = verifiableUUIDModel;
117                    }
118    
119                    @Override
120                    protected void doRun() throws Exception {
121                            verifyUUID(_verifiableUUIDModel);
122                    }
123    
124                    private final VerifiableUUIDModel _verifiableUUIDModel;
125    
126            }
127    
128    }