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.dao.db.DB;
019    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
020    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
023    import com.liferay.portal.verify.model.VerifiableUUIDModel;
024    
025    import java.sql.Connection;
026    import java.sql.PreparedStatement;
027    import java.sql.ResultSet;
028    
029    import java.util.Collection;
030    import java.util.Map;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class VerifyUUID extends VerifyProcess {
036    
037            public static void verify(VerifiableUUIDModel ... verifiableUUIDModels)
038                    throws Exception {
039    
040                    for (VerifiableUUIDModel verifiableUUIDModel : verifiableUUIDModels) {
041                            verifyUUID(verifiableUUIDModel);
042                    }
043            }
044    
045            protected static void updateUUID(
046                            VerifiableUUIDModel verifiableUUIDModel, long primKey)
047                    throws Exception {
048    
049                    DB db = DBFactoryUtil.getDB();
050    
051                    StringBundler sb = new StringBundler(8);
052    
053                    sb.append("update ");
054                    sb.append(verifiableUUIDModel.getTableName());
055                    sb.append(" set uuid_ = '");
056                    sb.append(PortalUUIDUtil.generate());
057                    sb.append("' where ");
058                    sb.append(verifiableUUIDModel.getPrimaryKeyColumnName());
059                    sb.append(" = ");
060                    sb.append(primKey);
061    
062                    db.runSQL(sb.toString());
063            }
064    
065            protected static void verifyUUID(VerifiableUUIDModel verifiableUUIDModel)
066                    throws Exception {
067    
068                    Connection con = null;
069                    PreparedStatement ps = null;
070                    ResultSet rs = null;
071    
072                    try {
073                            con = DataAccess.getUpgradeOptimizedConnection();
074    
075                            ps = con.prepareStatement(
076                                    "select " + verifiableUUIDModel.getPrimaryKeyColumnName() +
077                                            " from " + verifiableUUIDModel.getTableName() +
078                                                    " where uuid_ is null or uuid_ = ''");
079    
080                            rs = ps.executeQuery();
081    
082                            while (rs.next()) {
083                                    long pk = rs.getLong(
084                                            verifiableUUIDModel.getPrimaryKeyColumnName());
085    
086                                    updateUUID(verifiableUUIDModel, pk);
087                            }
088                    }
089                    finally {
090                            DataAccess.cleanUp(con, ps, rs);
091                    }
092            }
093    
094            @Override
095            protected void doVerify() throws Exception {
096                    Map<String, VerifiableUUIDModel> verifiableUUIDModelsMap =
097                            PortalBeanLocatorUtil.locate(VerifiableUUIDModel.class);
098    
099                    Collection<VerifiableUUIDModel> verifiableUUIDModels =
100                            verifiableUUIDModelsMap.values();
101    
102                    verify(
103                            verifiableUUIDModels.toArray(
104                                    new VerifiableUUIDModel[verifiableUUIDModels.size()]));
105            }
106    
107    }