001    /**
002     * Copyright (c) 2000-2013 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.concurrent.ThrowableAwareRunnable;
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.uuid.PortalUUIDUtil;
022    
023    import java.sql.Connection;
024    import java.sql.PreparedStatement;
025    import java.sql.ResultSet;
026    
027    import java.util.ArrayList;
028    import java.util.List;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class VerifyUUID extends VerifyProcess {
034    
035            public static void verifyModel(String modelName, String pkColumnName)
036                    throws Exception {
037    
038                    VerifyUUID verifyUUID = new VerifyUUID();
039    
040                    verifyUUID.doVerify(modelName, pkColumnName);
041            }
042    
043            @Override
044            protected void doVerify() throws Exception {
045                    List<VerifyUUIDRunnable> verifyUUIDRunnables =
046                            new ArrayList<VerifyUUIDRunnable>(_MODELS.length);
047    
048                    for (String[] model : _MODELS) {
049                            VerifyUUIDRunnable verifyUUIDRunnable = new VerifyUUIDRunnable(
050                                    model[0], model[1]);
051    
052                            verifyUUIDRunnables.add(verifyUUIDRunnable);
053                    }
054    
055                    doVerify(verifyUUIDRunnables);
056            }
057    
058            protected void doVerify(String modelName, String pkColumnName)
059                    throws Exception {
060    
061                    Connection con = null;
062                    PreparedStatement ps = null;
063                    ResultSet rs = null;
064    
065                    try {
066                            con = DataAccess.getUpgradeOptimizedConnection();
067    
068                            ps = con.prepareStatement(
069                                    "select " + pkColumnName + " from " + modelName +
070                                            " where uuid_ is null or uuid_ = ''");
071    
072                            rs = ps.executeQuery();
073    
074                            while (rs.next()) {
075                                    long pk = rs.getLong(pkColumnName);
076    
077                                    doVerify(modelName, pkColumnName, pk);
078                            }
079                    }
080                    finally {
081                            DataAccess.cleanUp(con, ps, rs);
082                    }
083            }
084    
085            protected void doVerify(String modelName, String pkColumnName, long pk)
086                    throws Exception {
087    
088                    String uuid = PortalUUIDUtil.generate();
089    
090                    DB db = DBFactoryUtil.getDB();
091    
092                    db.runSQL(
093                            "update " + modelName + " set uuid_ = '" + uuid + "' where " +
094                                    pkColumnName + " = " + pk);
095            }
096    
097            private static final String[][] _MODELS = new String[][] {
098                    new String[] {
099                            "Address", "addressId"
100                    },
101                    new String[] {
102                            "DLFileVersion", "fileVersionId"
103                    },
104                    new String[] {
105                            "EmailAddress", "emailAddressId"
106                    },
107                    new String[] {
108                            "Group_", "groupId"
109                    },
110                    new String[] {
111                            "JournalArticleResource", "resourcePrimKey"
112                    },
113                    new String[] {
114                            "JournalFeed", "id_"
115                    },
116                    new String[] {
117                            "Layout", "plid"
118                    },
119                    new String[] {
120                            "LayoutPrototype", "layoutPrototypeId"
121                    },
122                    new String[] {
123                            "LayoutSetPrototype", "layoutSetPrototypeId"
124                    },
125                    new String[] {
126                            "MBBan", "banId"
127                    },
128                    new String[] {
129                            "MBDiscussion", "discussionId"
130                    },
131                    new String[] {
132                            "MBThread", "threadId"
133                    },
134                    new String[] {
135                            "MBThreadFlag", "threadFlagId"
136                    },
137                    new String[] {
138                            "Organization_", "organizationId"
139                    },
140                    new String[] {
141                            "PasswordPolicy", "passwordPolicyId"
142                    },
143                    new String[] {
144                            "Phone", "phoneId"
145                    },
146                    new String[] {
147                            "PollsVote", "voteId"
148                    },
149                    new String[] {
150                            "Role_", "roleId"
151                    },
152                    new String[] {
153                            "UserGroup", "userGroupId"
154                    },
155                    new String[] {
156                            "Website", "websiteId"
157                    },
158                    new String[] {
159                            "WikiPageResource", "resourcePrimKey"
160                    }
161            };
162    
163            private class VerifyUUIDRunnable extends ThrowableAwareRunnable {
164    
165                    public VerifyUUIDRunnable(String modelName, String pkColumn) {
166                            _modelName = modelName;
167                            _pkColumn = pkColumn;
168                    }
169    
170                    @Override
171                    protected void doRun() throws Exception {
172                            doVerify(_modelName, _pkColumn);
173                    }
174    
175                    private final String _pkColumn;
176                    private final String _modelName;
177    
178            }
179    
180    }