1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.verify;
16  
17  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
18  import com.liferay.portal.kernel.util.MethodCache;
19  import com.liferay.portal.kernel.util.MethodHandler;
20  import com.liferay.portal.kernel.util.MethodKey;
21  import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
22  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
23  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
24  import com.liferay.portlet.journal.service.JournalFeedLocalServiceUtil;
25  import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
26  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
27  
28  import java.lang.reflect.Method;
29  
30  import java.sql.Connection;
31  import java.sql.PreparedStatement;
32  import java.sql.ResultSet;
33  
34  /**
35   * <a href="VerifyUUID.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class VerifyUUID extends VerifyProcess {
40  
41      protected void doVerify() throws Exception {
42          for (String[] model : _MODELS) {
43              verifyModel(model[0], model[1], model[2]);
44          }
45      }
46  
47      protected void verifyModel(
48              String serviceClassName, String modelName, String pkColumnName)
49          throws Exception {
50  
51          Connection con = null;
52          PreparedStatement ps = null;
53          ResultSet rs = null;
54  
55          try {
56              con = DataAccess.getConnection();
57  
58              ps = con.prepareStatement(
59                  "select " + pkColumnName + " from " + modelName +
60                      " where uuid_ is null or uuid_ = ''");
61  
62              rs = ps.executeQuery();
63  
64              while (rs.next()) {
65                  long pk = rs.getLong(pkColumnName);
66  
67                  verifyModel(serviceClassName, modelName, pk);
68              }
69          }
70          finally {
71              DataAccess.cleanUp(con, ps, rs);
72          }
73      }
74  
75      protected void verifyModel(
76              String serviceClassName, String modelName, long pk)
77          throws Exception {
78  
79          MethodKey getPKMethodKey = new MethodKey(
80              serviceClassName, "get" + modelName, long.class);
81  
82          MethodHandler getPKMethodHandler = new MethodHandler(
83              getPKMethodKey, pk);
84  
85          Object pkValue = getPKMethodHandler.invoke(true);
86  
87          Method getPKMethod = MethodCache.get(getPKMethodKey);
88  
89          MethodKey updateUuidMethodKey = new MethodKey(
90              serviceClassName, "update" + modelName,
91              getPKMethod.getReturnType());
92  
93          MethodHandler updateUuidMethodHandler = new MethodHandler(
94              updateUuidMethodKey, pkValue);
95  
96          updateUuidMethodHandler.invoke(true);
97      }
98  
99      private static final String[][] _MODELS = new String[][] {
100         new String[] {
101             IGFolderLocalServiceUtil.class.getName(),
102             "IGFolder",
103             "folderId"
104         },
105         new String[] {
106             IGImageLocalServiceUtil.class.getName(),
107             "IGImage",
108             "imageId"
109         },
110         new String[] {
111             JournalArticleLocalServiceUtil.class.getName(),
112             "JournalArticle",
113             "id_"
114         },
115         new String[] {
116             JournalFeedLocalServiceUtil.class.getName(),
117             "JournalFeed",
118             "id_"
119         },
120         new String[] {
121             JournalStructureLocalServiceUtil.class.getName(),
122             "JournalStructure",
123             "id_"
124         },
125         new String[] {
126             JournalTemplateLocalServiceUtil.class.getName(),
127             "JournalTemplate",
128             "id_"
129         }
130     };
131 
132 }