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