001    /**
002     * Copyright (c) 2000-present 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.upgrade.v7_0_0;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.model.ReleaseConstants;
019    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
020    import com.liferay.portal.kernel.util.StringBundler;
021    
022    import java.io.IOException;
023    
024    import java.sql.PreparedStatement;
025    import java.sql.ResultSet;
026    import java.sql.SQLException;
027    import java.sql.Timestamp;
028    
029    /**
030     * @author Miguel Pastor
031     */
032    public class UpgradeModules extends UpgradeProcess {
033    
034            protected void addRelease(String... bundleSymbolicNames)
035                    throws SQLException {
036    
037                    Timestamp timestamp = new Timestamp(System.currentTimeMillis());
038    
039                    PreparedStatement ps = null;
040                    ResultSet rs = null;
041    
042                    try {
043                            StringBundler sb = new StringBundler(5);
044    
045                            sb.append("insert into Release_ (mvccVersion, releaseId, ");
046                            sb.append("createDate, modifiedDate, servletContextName, ");
047                            sb.append("schemaVersion, buildNumber, buildDate, verified, ");
048                            sb.append("state_, testString) values (?, ?, ?, ?, ?, ?, ?, ?, ");
049                            sb.append("?, ?, ?)");
050    
051                            String sql = sb.toString();
052    
053                            ps = connection.prepareStatement(sql);
054    
055                            for (String bundleSymbolicName : bundleSymbolicNames) {
056                                    ps.setLong(1, 0);
057                                    ps.setLong(2, increment());
058                                    ps.setTimestamp(3, timestamp);
059                                    ps.setTimestamp(4, timestamp);
060                                    ps.setString(5, bundleSymbolicName);
061                                    ps.setString(6, "0.0.1");
062                                    ps.setInt(7, 001);
063                                    ps.setTimestamp(8, timestamp);
064                                    ps.setBoolean(9, false);
065                                    ps.setInt(10, 0);
066                                    ps.setString(11, ReleaseConstants.TEST_STRING);
067    
068                                    ps.addBatch();
069                            }
070    
071                            ps.executeBatch();
072                    }
073                    finally {
074                            DataAccess.cleanUp(ps, rs);
075                    }
076            }
077    
078            @Override
079            protected void doUpgrade() throws Exception {
080                    updateExtractedModules();
081    
082                    updateConvertedLegacyModules();
083            }
084    
085            protected boolean hasServiceComponent(String buildNamespace)
086                    throws SQLException {
087    
088                    PreparedStatement ps = null;
089                    ResultSet rs = null;
090    
091                    try {
092                            ps = connection.prepareStatement(
093                                    "select serviceComponentId from ServiceComponent " +
094                                            "where buildNamespace = ?");
095    
096                            ps.setString(1, buildNamespace);
097    
098                            rs = ps.executeQuery();
099    
100                            if (rs.next()) {
101                                    return true;
102                            }
103                    }
104                    finally {
105                            DataAccess.cleanUp(ps, rs);
106                    }
107    
108                    return false;
109            }
110    
111            protected void updateConvertedLegacyModules()
112                    throws IOException, SQLException {
113    
114                    for (String[] convertedLegacyModule : _convertedLegacyModules) {
115                            String oldServletContextName = convertedLegacyModule[0];
116                            String newServletContextName = convertedLegacyModule[1];
117                            String buildNamespace = convertedLegacyModule[2];
118    
119                            PreparedStatement ps = null;
120                            ResultSet rs = null;
121    
122                            try {
123                                    ps = connection.prepareStatement(
124                                            "select servletContextName, buildNumber from Release_ " +
125                                                    "where servletContextName = ?");
126    
127                                    ps.setString(1, oldServletContextName);
128    
129                                    rs = ps.executeQuery();
130    
131                                    if (!rs.next()) {
132                                            if (hasServiceComponent(buildNamespace)) {
133                                                    addRelease(newServletContextName);
134                                            }
135                                    }
136                                    else {
137                                            updateServletContextName(
138                                                    oldServletContextName, newServletContextName);
139                                    }
140                            }
141                            finally {
142                                    DataAccess.cleanUp(ps, rs);
143                            }
144                    }
145            }
146    
147            protected void updateExtractedModules() throws SQLException {
148                    addRelease(_bundleSymbolicNames);
149            }
150    
151            protected void updateServletContextName(
152                            String oldServletContextName, String newServletContextName)
153                    throws IOException, SQLException {
154    
155                    runSQL(
156                            "update Release_ set servletContextName = '" +
157                                    newServletContextName + "' where servletContextName = '" +
158                                            oldServletContextName + "'");
159            }
160    
161            private static final String[] _bundleSymbolicNames = new String[] {
162                    "com.liferay.amazon.rankings.web", "com.liferay.asset.browser.web",
163                    "com.liferay.asset.categories.navigation.web",
164                    "com.liferay.asset.publisher.web",
165                    "com.liferay.asset.tags.compiler.web",
166                    "com.liferay.asset.tags.navigation.web",
167                    "com.liferay.blogs.recent.bloggers.web", "com.liferay.blogs.web",
168                    "com.liferay.bookmarks.service", "com.liferay.bookmarks.web",
169                    "com.liferay.calendar.web", "com.liferay.comment.page.comments.web",
170                    "com.liferay.currency.converter.web", "com.liferay.dictionary.web",
171                    "com.liferay.document.library.service",
172                    "com.liferay.document.library.web",
173                    "com.liferay.dynamic.data.lists.service",
174                    "com.liferay.dynamic.data.lists.web",
175                    "com.liferay.dynamic.data.mapping.service",
176                    "com.liferay.exportimport.service", "com.liferay.exportimport.web",
177                    "com.liferay.flags.web", "com.liferay.hello.velocity.web",
178                    "com.liferay.iframe.web", "com.liferay.invitation.web",
179                    "com.liferay.item.selector.web",
180                    "com.liferay.journal.content.search.web",
181                    "com.liferay.journal.content.web", "com.liferay.journal.service",
182                    "com.liferay.journal.web", "com.liferay.layout.admin.web",
183                    "com.liferay.license.manager.web", "com.liferay.loan.calculator.web",
184                    "com.liferay.login.web", "com.liferay.message.boards.web",
185                    "com.liferay.mobile.device.rules.service",
186                    "com.liferay.mobile.device.rules.web", "com.liferay.my.account.web",
187                    "com.liferay.nested.portlets.web", "com.liferay.network.utilities.web",
188                    "com.liferay.password.generator.web", "com.liferay.plugins.admin.web",
189                    "com.liferay.polls.service",
190                    "com.liferay.portal.background.task.service",
191                    "com.liferay.portal.instances.web", "com.liferay.portal.lock.service",
192                    "com.liferay.portal.scheduler.quartz",
193                    "com.liferay.portal.settings.web",
194                    "com.liferay.portlet.configuration.css.web",
195                    "com.liferay.portlet.configuration.web", "com.liferay.quick.note.web",
196                    "com.liferay.ratings.page.ratings.web", "com.liferay.rss.web",
197                    "com.liferay.search.web", "com.liferay.server.admin.web",
198                    "com.liferay.shopping.service", "com.liferay.shopping.web",
199                    "com.liferay.site.browser.web", "com.liferay.site.my.sites.web",
200                    "com.liferay.site.navigation.breadcrumb.web",
201                    "com.liferay.site.navigation.directory.web",
202                    "com.liferay.site.navigation.language.web",
203                    "com.liferay.site.navigation.menu.web",
204                    "com.liferay.site.navigation.site.map.web",
205                    "com.liferay.social.activities.web", "com.liferay.social.activity.web",
206                    "com.liferay.social.group.statistics.web",
207                    "com.liferay.social.requests.web",
208                    "com.liferay.social.user.statistics.web", "com.liferay.staging.bar.web",
209                    "com.liferay.translator.web", "com.liferay.trash.web",
210                    "com.liferay.unit.converter.web", "com.liferay.web.proxy.web",
211                    "com.liferay.wiki.service", "com.liferay.wiki.web",
212                    "com.liferay.xsl.content.web"
213            };
214            private static final String[][] _convertedLegacyModules = {
215                    {
216                            "calendar-portlet", "com.liferay.calendar.service", "Calendar"
217                    },
218                    {
219                            "social-networking-portlet",
220                            "com.liferay.social.networking.service", "SN"
221                    },
222                    {
223                            "marketplace-portlet", "com.liferay.marketplace.service",
224                            "Marketplace"
225                    },
226                    {
227                            "kaleo-web", "com.liferay.portal.workflow.kaleo.service", "Kaleo"
228                    },
229                    {
230                            "microblogs-portlet", "com.liferay.microblogs.service", "Microblogs"
231                    }
232            };
233    
234    }