001    /**
002     * Copyright (c) 2000-2010 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.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.JournalArticleResourceLocalServiceUtil;
026    import com.liferay.portlet.journal.service.JournalFeedLocalServiceUtil;
027    import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
028    import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
029    import com.liferay.portlet.wiki.service.WikiPageResourceLocalServiceUtil;
030    
031    import java.lang.reflect.Method;
032    
033    import java.sql.Connection;
034    import java.sql.PreparedStatement;
035    import java.sql.ResultSet;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class VerifyUUID extends VerifyProcess {
041    
042            public static void verifyModel(
043                            String serviceClassName, String modelName, String pkColumnName)
044                    throws Exception {
045    
046                    Connection con = null;
047                    PreparedStatement ps = null;
048                    ResultSet rs = null;
049    
050                    try {
051                            con = DataAccess.getConnection();
052    
053                            ps = con.prepareStatement(
054                                    "select " + pkColumnName + " from " + modelName +
055                                            " where uuid_ is null or uuid_ = ''");
056    
057                            rs = ps.executeQuery();
058    
059                            while (rs.next()) {
060                                    long pk = rs.getLong(pkColumnName);
061    
062                                    verifyModel(serviceClassName, modelName, pk);
063                            }
064                    }
065                    finally {
066                            DataAccess.cleanUp(con, ps, rs);
067                    }
068            }
069    
070            public static void verifyModel(
071                            String serviceClassName, String modelName, long pk)
072                    throws Exception {
073    
074                    MethodKey getPKMethodKey = new MethodKey(
075                            serviceClassName, "get" + modelName, long.class);
076    
077                    MethodHandler getPKMethodHandler = new MethodHandler(
078                            getPKMethodKey, pk);
079    
080                    Object pkValue = getPKMethodHandler.invoke(true);
081    
082                    Method getPKMethod = MethodCache.get(getPKMethodKey);
083    
084                    MethodKey updateUuidMethodKey = new MethodKey(
085                            serviceClassName, "update" + modelName,
086                            getPKMethod.getReturnType());
087    
088                    MethodHandler updateUuidMethodHandler = new MethodHandler(
089                            updateUuidMethodKey, pkValue);
090    
091                    updateUuidMethodHandler.invoke(true);
092            }
093    
094            protected void doVerify() throws Exception {
095                    for (String[] model : _MODELS) {
096                            verifyModel(model[0], model[1], model[2]);
097                    }
098            }
099    
100            private static final String[][] _MODELS = new String[][] {
101                    new String[] {
102                            IGFolderLocalServiceUtil.class.getName(),
103                            "IGFolder",
104                            "folderId"
105                    },
106                    new String[] {
107                            IGImageLocalServiceUtil.class.getName(),
108                            "IGImage",
109                            "imageId"
110                    },
111                    new String[] {
112                            JournalArticleLocalServiceUtil.class.getName(),
113                            "JournalArticle",
114                            "id_"
115                    },
116                    new String[] {
117                            JournalArticleResourceLocalServiceUtil.class.getName(),
118                            "JournalArticleResource",
119                            "resourcePrimKey"
120                    },
121                    new String[] {
122                            JournalFeedLocalServiceUtil.class.getName(),
123                            "JournalFeed",
124                            "id_"
125                    },
126                    new String[] {
127                            JournalStructureLocalServiceUtil.class.getName(),
128                            "JournalStructure",
129                            "id_"
130                    },
131                    new String[] {
132                            JournalTemplateLocalServiceUtil.class.getName(),
133                            "JournalTemplate",
134                            "id_"
135                    },
136                    new String[] {
137                            LayoutLocalServiceUtil.class.getName(),
138                            "Layout",
139                            "plid"
140                    },
141                    new String[] {
142                            WikiPageResourceLocalServiceUtil.class.getName(),
143                            "WikiPageResource",
144                            "resourcePrimKey"
145                    }
146            };
147    
148    }