001    /**
002     * Copyright (c) 2000-2010 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.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.util.MethodCache;
019    import com.liferay.portal.kernel.util.MethodHandler;
020    import com.liferay.portal.kernel.util.MethodKey;
021    import com.liferay.portal.service.LayoutLocalServiceUtil;
022    import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
023    import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
024    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
025    import com.liferay.portlet.journal.service.JournalFeedLocalServiceUtil;
026    import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
027    import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
028    import com.liferay.portlet.wiki.service.WikiPageResourceLocalServiceUtil;
029    
030    import java.lang.reflect.Method;
031    
032    import java.sql.Connection;
033    import java.sql.PreparedStatement;
034    import java.sql.ResultSet;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class VerifyUUID extends VerifyProcess {
040    
041            public static void verifyModel(
042                            String serviceClassName, String modelName, String pkColumnName)
043                    throws Exception {
044    
045                    Connection con = null;
046                    PreparedStatement ps = null;
047                    ResultSet rs = null;
048    
049                    try {
050                            con = DataAccess.getConnection();
051    
052                            ps = con.prepareStatement(
053                                    "select " + pkColumnName + " from " + modelName +
054                                            " where uuid_ is null or uuid_ = ''");
055    
056                            rs = ps.executeQuery();
057    
058                            while (rs.next()) {
059                                    long pk = rs.getLong(pkColumnName);
060    
061                                    verifyModel(serviceClassName, modelName, pk);
062                            }
063                    }
064                    finally {
065                            DataAccess.cleanUp(con, ps, rs);
066                    }
067            }
068    
069            public static void verifyModel(
070                            String serviceClassName, String modelName, long pk)
071                    throws Exception {
072    
073                    MethodKey getPKMethodKey = new MethodKey(
074                            serviceClassName, "get" + modelName, long.class);
075    
076                    MethodHandler getPKMethodHandler = new MethodHandler(
077                            getPKMethodKey, pk);
078    
079                    Object pkValue = getPKMethodHandler.invoke(true);
080    
081                    Method getPKMethod = MethodCache.get(getPKMethodKey);
082    
083                    MethodKey updateUuidMethodKey = new MethodKey(
084                            serviceClassName, "update" + modelName,
085                            getPKMethod.getReturnType());
086    
087                    MethodHandler updateUuidMethodHandler = new MethodHandler(
088                            updateUuidMethodKey, pkValue);
089    
090                    updateUuidMethodHandler.invoke(true);
091            }
092    
093            protected void doVerify() throws Exception {
094                    for (String[] model : _MODELS) {
095                            verifyModel(model[0], model[1], model[2]);
096                    }
097            }
098    
099            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                    new String[] {
131                            LayoutLocalServiceUtil.class.getName(),
132                            "Layout",
133                            "plid"
134                    },
135                    new String[] {
136                            WikiPageResourceLocalServiceUtil.class.getName(),
137                            "WikiPageResource",
138                            "resourcePrimKey"
139                    }
140            };
141    
142    }