1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.verify;
24  
25  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.LongWrapper;
29  import com.liferay.portal.kernel.util.MethodInvoker;
30  import com.liferay.portal.kernel.util.MethodWrapper;
31  import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
32  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
33  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
34  import com.liferay.portlet.journal.service.JournalFeedLocalServiceUtil;
35  import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
36  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
37  
38  import java.sql.Connection;
39  import java.sql.PreparedStatement;
40  import java.sql.ResultSet;
41  
42  /**
43   * <a href="VerifyUUID.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class VerifyUUID extends VerifyProcess {
49  
50      public void verify() throws VerifyException {
51          _log.info("Verifying");
52  
53          try {
54              verifyUUID();
55          }
56          catch (Exception e) {
57              throw new VerifyException(e);
58          }
59      }
60  
61      protected void verifyModel(
62              String serviceClassName, String modelName, String pkColumnName)
63          throws Exception {
64  
65          Connection con = null;
66          PreparedStatement ps = null;
67          ResultSet rs = null;
68  
69          try {
70              con = DataAccess.getConnection();
71  
72              ps = con.prepareStatement(
73                  "select " + pkColumnName + " from " + modelName +
74                      " where uuid_ is null or uuid_ = ''");
75  
76              rs = ps.executeQuery();
77  
78              while (rs.next()) {
79                  long pk = rs.getLong(pkColumnName);
80  
81                  verifyModel(serviceClassName, modelName, pk);
82              }
83          }
84          finally {
85              DataAccess.cleanUp(con, ps, rs);
86          }
87      }
88  
89      protected void verifyModel(
90              String serviceClassName, String modelName, long pk)
91          throws Exception {
92  
93          Object obj = MethodInvoker.invoke(
94              new MethodWrapper(
95                  serviceClassName, "get" + modelName, new LongWrapper(pk)));
96  
97          MethodInvoker.invoke(
98              new MethodWrapper(serviceClassName, "update" + modelName, obj));
99      }
100 
101     protected void verifyUUID() throws Exception {
102         for (String[] model : _MODELS) {
103             verifyModel(model[0], model[1], model[2]);
104         }
105     }
106 
107     private static final String[][] _MODELS = new String[][] {
108         new String[] {
109             IGFolderLocalServiceUtil.class.getName(),
110             "IGFolder",
111             "folderId"
112         },
113         new String[] {
114             IGImageLocalServiceUtil.class.getName(),
115             "IGImage",
116             "imageId"
117         },
118         new String[] {
119             JournalArticleLocalServiceUtil.class.getName(),
120             "JournalArticle",
121             "id_"
122         },
123         new String[] {
124             JournalFeedLocalServiceUtil.class.getName(),
125             "JournalFeed",
126             "id_"
127         },
128         new String[] {
129             JournalStructureLocalServiceUtil.class.getName(),
130             "JournalStructure",
131             "id_"
132         },
133         new String[] {
134             JournalTemplateLocalServiceUtil.class.getName(),
135             "JournalTemplate",
136             "id_"
137         }
138     };
139 
140     private static Log _log = LogFactoryUtil.getLog(VerifyUUID.class);
141 
142 }