/** * Copyright (c) 2000-present Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.liferay.localization.servlet; import com.liferay.localization.util.InstanceUtil; import com.liferay.portal.kernel.dao.db.DB; import com.liferay.portal.kernel.dao.db.DBManagerUtil; import com.liferay.portal.kernel.dao.jdbc.DataAccess; import com.liferay.portal.kernel.util.BasePortalLifecycle; import com.liferay.portal.kernel.util.FileUtil; import com.liferay.portal.kernel.util.PortalUtil; import com.liferay.portal.kernel.util.StringPool; import com.liferay.portal.kernel.util.StringUtil; import com.liferay.portal.kernel.xml.Document; import com.liferay.portal.kernel.xml.Element; import com.liferay.portal.kernel.xml.SAXReaderUtil; import java.io.InputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.List; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; /** * @author Brian Wing Shun Chan */ public class LocalizationServletContextListener extends BasePortalLifecycle implements ServletContextListener { public void contextDestroyed(ServletContextEvent servletContextEvent) { portalDestroy(); } public void contextInitialized(ServletContextEvent servletContextEvent) { registerPortalLifecycle(); } @Override protected void doPortalDestroy() throws Exception { } @Override protected void doPortalInit() throws Exception { importSQL(); long[] companyIds = PortalUtil.getCompanyIds(); for (long companyId : companyIds) { InstanceUtil.initInstance(companyId); } } protected int getCount(String sql) throws Exception { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { connection = DataAccess.getUpgradeOptimizedConnection(); preparedStatement = connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { return resultSet.getInt(1); } return 0; } finally { DataAccess.cleanUp(connection, preparedStatement, resultSet); } } protected void importSQL() throws Exception { Class<?> clazz = getClass(); ClassLoader classLoader = clazz.getClassLoader(); InputStream inputStream = classLoader.getResourceAsStream( "/resources/data/sql.xml"); String xml = new String(FileUtil.getBytes(inputStream)); Document document = SAXReaderUtil.read(xml); Element rootElement = document.getRootElement(); List<Element> batchElements = rootElement.elements("batch"); for (Element batchElement : batchElements) { String testSQL = batchElement.elementText("test-sql"); int count = getCount(testSQL); if (count > 0) { continue; } String[] importSQLs = StringUtil.split( batchElement.elementText("import-sql"), StringPool.SEMICOLON); runSQL(importSQLs); } } protected void runSQL(String[] sqls) throws Exception { DB db = DBManagerUtil.getDB(); db.runSQL(sqls); } }