Commit 818d03f3 authored by Liang Ding's avatar Liang Ding

Merge pull request #192 from b3log/master

c
parents 407d4056 6e87ceed
/* /*
* Copyright (c) 2009, 2010, 2011, 2012, 2013, B3log Team * Copyright (c) 2009, 2010, 2011, 2012, 2013, B3log Team
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.b3log.solo.processor; package org.b3log.solo.processor;
import java.io.IOException; import java.io.IOException;
import java.sql.Connection; import java.sql.Connection;
import java.sql.Statement; import java.sql.Statement;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.b3log.latke.Keys; import org.b3log.latke.Keys;
import org.b3log.latke.Latkes; import org.b3log.latke.Latkes;
import org.b3log.latke.RuntimeEnv; import org.b3log.latke.RuntimeEnv;
import org.b3log.latke.mail.MailService; import org.b3log.latke.mail.MailService;
import org.b3log.latke.mail.MailServiceFactory; import org.b3log.latke.mail.MailServiceFactory;
import org.b3log.latke.model.Plugin; import org.b3log.latke.model.Plugin;
import org.b3log.latke.model.User; import org.b3log.latke.model.User;
import org.b3log.latke.repository.*; import org.b3log.latke.repository.*;
import org.b3log.latke.repository.jdbc.util.Connections; import org.b3log.latke.repository.jdbc.util.Connections;
import org.b3log.latke.service.LangPropsService; import org.b3log.latke.service.LangPropsService;
import org.b3log.latke.service.ServiceException; import org.b3log.latke.service.ServiceException;
import org.b3log.latke.servlet.HTTPRequestContext; import org.b3log.latke.servlet.HTTPRequestContext;
import org.b3log.latke.servlet.HTTPRequestMethod; import org.b3log.latke.servlet.HTTPRequestMethod;
import org.b3log.latke.servlet.annotation.RequestProcessing; import org.b3log.latke.servlet.annotation.RequestProcessing;
import org.b3log.latke.servlet.annotation.RequestProcessor; import org.b3log.latke.servlet.annotation.RequestProcessor;
import org.b3log.latke.servlet.renderer.TextHTMLRenderer; import org.b3log.latke.servlet.renderer.TextHTMLRenderer;
import org.b3log.latke.util.MD5; import org.b3log.latke.util.MD5;
import org.b3log.solo.SoloServletListener; import org.b3log.latke.util.Strings;
import org.b3log.solo.model.*; import org.b3log.solo.SoloServletListener;
import org.b3log.solo.repository.*; import org.b3log.solo.model.*;
import org.b3log.solo.repository.impl.*; import org.b3log.solo.repository.*;
import org.b3log.solo.service.PreferenceQueryService; import org.b3log.solo.repository.impl.*;
import org.json.JSONArray; import org.b3log.solo.service.PreferenceQueryService;
import org.json.JSONException; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONException;
import org.json.JSONObject;
/**
* Upgrader. /**
* * Upgrader.
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a> *
* @author <a href="mailto:dongxv.vang@gmail.com">Dongxu Wang</a> * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.1.1.6, Feb 4, 2013 * @author <a href="mailto:dongxv.vang@gmail.com">Dongxu Wang</a>
* @since 0.3.1 * @version 1.1.1.7, Feb 19, 2013
*/ * @since 0.3.1
@RequestProcessor */
public final class UpgradeProcessor { @RequestProcessor
public final class UpgradeProcessor {
/**
* Logger. /**
*/ * Logger.
private static final Logger LOGGER = Logger.getLogger(UpgradeProcessor.class.getName()); */
private static final Logger LOGGER = Logger.getLogger(UpgradeProcessor.class.getName());
/**
* Article repository. /**
*/ * Article repository.
private ArticleRepository articleRepository = ArticleRepositoryImpl.getInstance(); */
private ArticleRepository articleRepository = ArticleRepositoryImpl.getInstance();
/**
* Page repository. /**
*/ * Page repository.
private PageRepository pageRepository = PageRepositoryImpl.getInstance(); */
private PageRepository pageRepository = PageRepositoryImpl.getInstance();
/**
* User repository. /**
*/ * User repository.
private UserRepository userRepository = UserRepositoryImpl.getInstance(); */
private UserRepository userRepository = UserRepositoryImpl.getInstance();
/**
* Preference repository. /**
*/ * Preference repository.
private PreferenceRepository preferenceRepository = PreferenceRepositoryImpl.getInstance(); */
private PreferenceRepository preferenceRepository = PreferenceRepositoryImpl.getInstance();
/**
* Step for article updating. /**
*/ * Step for article updating.
private static final int STEP = 50; */
private static final int STEP = 50;
/**
* Preference Query Service. /**
*/ * Preference Query Service.
private PreferenceQueryService preferenceQueryService = PreferenceQueryService.getInstance(); */
private PreferenceQueryService preferenceQueryService = PreferenceQueryService.getInstance();
/**
* Mail Service. /**
*/ * Mail Service.
private static final MailService MAIL_SVC = MailServiceFactory.getMailService(); */
private static final MailService MAIL_SVC = MailServiceFactory.getMailService();
/**
* Whether the email has been sent. /**
*/ * Whether the email has been sent.
private boolean sent = false; */
private boolean sent = false;
/**
* Language service. /**
*/ * Language service.
private static LangPropsService langPropsService = LangPropsService.getInstance(); */
private static LangPropsService langPropsService = LangPropsService.getInstance();
/**
* Checks upgrade. /**
* * Checks upgrade.
* @param context the specified context *
*/ * @param context the specified context
@RequestProcessing(value = "/upgrade/checker.do", method = HTTPRequestMethod.GET) */
public void upgrade(final HTTPRequestContext context) { @RequestProcessing(value = "/upgrade/checker.do", method = HTTPRequestMethod.GET)
final TextHTMLRenderer renderer = new TextHTMLRenderer(); public void upgrade(final HTTPRequestContext context) {
final TextHTMLRenderer renderer = new TextHTMLRenderer();
context.setRenderer(renderer);
context.setRenderer(renderer);
try {
final JSONObject preference = preferenceRepository.get(Preference.PREFERENCE); try {
final JSONObject preference = preferenceRepository.get(Preference.PREFERENCE);
if (null == preference) {
LOGGER.log(Level.INFO, "Not init yet"); if (null == preference) {
renderer.setContent("Not init yet"); LOGGER.log(Level.INFO, "Not init yet");
renderer.setContent("Not init yet");
return;
} return;
}
renderer.setContent("Upgrade successfully ;-)");
renderer.setContent("Upgrade successfully ;-)");
final String version = preference.getString(Preference.VERSION);
final String version = preference.getString(Preference.VERSION);
if (SoloServletListener.VERSION.equals(version)) {
return; if (SoloServletListener.VERSION.equals(version)) {
} return;
}
if ("0.5.5".equals(version)) {
v055ToV056(); if ("0.5.5".equals(version)) {
} else { v055ToV056();
LOGGER.log(Level.WARNING, "Attempt to skip more than one version to upgrade. Expected: 0.5.0; Actually: {0}", version); } else {
if (!sent) { LOGGER.log(Level.WARNING, "Attempt to skip more than one version to upgrade. Expected: 0.5.0; Actually: {0}", version);
notifyUserByEmail(); if (!sent) {
sent = true; notifyUserByEmail();
} sent = true;
renderer.setContent(langPropsService.get("skipVersionAlert")); }
} renderer.setContent(langPropsService.get("skipVersionAlert"));
} catch (final Exception e) { }
LOGGER.log(Level.SEVERE, e.getMessage(), e); } catch (final Exception e) {
renderer.setContent( LOGGER.log(Level.SEVERE, e.getMessage(), e);
"Upgrade failed [" + e.getMessage() + "], please contact the B3log Solo developers or reports this " renderer.setContent(
+ "issue directly (<a href='https://github.com/b3log/b3log-solo/issues/new'>" "Upgrade failed [" + e.getMessage() + "], please contact the B3log Solo developers or reports this "
+ "https://github.com/b3log/b3log-solo/issues/new</a>) "); + "issue directly (<a href='https://github.com/b3log/b3log-solo/issues/new'>"
} + "https://github.com/b3log/b3log-solo/issues/new</a>) ");
} }
}
/**
* Upgrades from version 055 to version 056. /**
* * Upgrades from version 055 to version 056.
* @throws Exception upgrade fails *
*/ * @throws Exception upgrade fails
private void v055ToV056() throws Exception { */
LOGGER.info("Upgrading from version 055 to version 056...."); private void v055ToV056() throws Exception {
LOGGER.info("Upgrading from version 055 to version 056....");
articleRepository.setCacheEnabled(false);
articleRepository.setCacheEnabled(false);
Transaction transaction = null;
Transaction transaction = null;
try {
transaction = userRepository.beginTransaction(); try {
transaction = userRepository.beginTransaction();
// Upgrades preference model
final JSONObject preference = preferenceRepository.get(Preference.PREFERENCE); // Upgrades preference model
final JSONObject preference = preferenceRepository.get(Preference.PREFERENCE);
preference.put(Preference.VERSION, "0.5.6");
preferenceRepository.update(Preference.PREFERENCE, preference); preference.put(Preference.VERSION, "0.5.6");
preferenceRepository.update(Preference.PREFERENCE, preference);
upgradeUsers();
upgradeUsers();
final RuntimeEnv runtimeEnv = Latkes.getRuntimeEnv();
final RuntimeEnv runtimeEnv = Latkes.getRuntimeEnv();
if (RuntimeEnv.LOCAL == runtimeEnv || RuntimeEnv.BAE == runtimeEnv) {
final Connection connection = Connections.getConnection(); if (RuntimeEnv.LOCAL == runtimeEnv || RuntimeEnv.BAE == runtimeEnv) {
final Statement statement = connection.createStatement(); final Connection connection = Connections.getConnection();
final Statement statement = connection.createStatement();
final String tableName = Latkes.getLocalProperty("jdbc.tablePrefix") + '_' + Plugin.PLUGIN;
final String tablePrefix = Latkes.getLocalProperty("jdbc.tablePrefix");
statement.execute("ALTER TABLE " + tableName + " ADD setting text"); final String tableName = Strings.isEmptyOrNull(tablePrefix) ? Plugin.PLUGIN : tablePrefix + '_' + Plugin.PLUGIN;
connection.commit(); statement.execute("ALTER TABLE " + tableName + " ADD setting text");
}
connection.commit();
transaction.commit(); }
LOGGER.log(Level.FINEST, "Updated preference"); transaction.commit();
} catch (final Exception e) {
if (transaction.isActive()) { LOGGER.log(Level.FINEST, "Updated preference");
transaction.rollback(); } catch (final Exception e) {
} if (transaction.isActive()) {
transaction.rollback();
LOGGER.log(Level.SEVERE, "Upgrade failed.", e); }
throw new Exception("Upgrade failed from version 055 to version 056");
} finally { LOGGER.log(Level.SEVERE, "Upgrade failed.", e);
articleRepository.setCacheEnabled(true); throw new Exception("Upgrade failed from version 055 to version 056");
} } finally {
articleRepository.setCacheEnabled(true);
LOGGER.info("Upgraded from version 055 to version 056 successfully :-)"); }
}
LOGGER.info("Upgraded from version 055 to version 056 successfully :-)");
/** }
* Upgrades users.
* /**
* <p> * Upgrades users.
* Password hashing. *
* </p> * <p>
* * Password hashing.
* @throws Exception exception * </p>
*/ *
private void upgradeUsers() throws Exception { * @throws Exception exception
final JSONArray users = userRepository.get(new Query()).getJSONArray(Keys.RESULTS); */
private void upgradeUsers() throws Exception {
for (int i = 0; i < users.length(); i++) { final JSONArray users = userRepository.get(new Query()).getJSONArray(Keys.RESULTS);
final JSONObject user = users.getJSONObject(i);
final String oldPwd = user.optString(User.USER_PASSWORD); for (int i = 0; i < users.length(); i++) {
final JSONObject user = users.getJSONObject(i);
user.put(User.USER_PASSWORD, MD5.hash(oldPwd)); final String oldPwd = user.optString(User.USER_PASSWORD);
userRepository.update(user.optString(Keys.OBJECT_ID), user); user.put(User.USER_PASSWORD, MD5.hash(oldPwd));
LOGGER.log(Level.INFO, "Hashed user[name={0}] password.", user.optString(User.USER_NAME)); userRepository.update(user.optString(Keys.OBJECT_ID), user);
}
} LOGGER.log(Level.INFO, "Hashed user[name={0}] password.", user.optString(User.USER_NAME));
}
/** }
* Upgrades articles.
* /**
* @throws Exception exception * Upgrades articles.
*/ *
private void upgradeArticles() throws Exception { * @throws Exception exception
LOGGER.log(Level.INFO, "Adds a property [articleEditorType] to each of articles"); */
private void upgradeArticles() throws Exception {
final JSONArray articles = articleRepository.get(new Query()).getJSONArray(Keys.RESULTS); LOGGER.log(Level.INFO, "Adds a property [articleEditorType] to each of articles");
if (articles.length() <= 0) { final JSONArray articles = articleRepository.get(new Query()).getJSONArray(Keys.RESULTS);
LOGGER.log(Level.FINEST, "No articles");
return; if (articles.length() <= 0) {
} LOGGER.log(Level.FINEST, "No articles");
return;
Transaction transaction = null; }
try { Transaction transaction = null;
for (int i = 0; i < articles.length(); i++) {
if (0 == i % STEP || !transaction.isActive()) { try {
transaction = userRepository.beginTransaction(); for (int i = 0; i < articles.length(); i++) {
} if (0 == i % STEP || !transaction.isActive()) {
transaction = userRepository.beginTransaction();
final JSONObject article = articles.getJSONObject(i); }
final String articleId = article.optString(Keys.OBJECT_ID); final JSONObject article = articles.getJSONObject(i);
LOGGER.log(Level.INFO, "Found an article[id={0}]", articleId); final String articleId = article.optString(Keys.OBJECT_ID);
article.put(Article.ARTICLE_EDITOR_TYPE, "tinyMCE");
LOGGER.log(Level.INFO, "Found an article[id={0}]", articleId);
articleRepository.update(article.getString(Keys.OBJECT_ID), article); article.put(Article.ARTICLE_EDITOR_TYPE, "tinyMCE");
if (0 == i % STEP) { articleRepository.update(article.getString(Keys.OBJECT_ID), article);
transaction.commit();
LOGGER.log(Level.FINEST, "Updated some articles"); if (0 == i % STEP) {
} transaction.commit();
} LOGGER.log(Level.FINEST, "Updated some articles");
}
if (transaction.isActive()) { }
transaction.commit();
} if (transaction.isActive()) {
transaction.commit();
LOGGER.log(Level.FINEST, "Updated all articles"); }
} catch (final Exception e) {
if (transaction.isActive()) { LOGGER.log(Level.FINEST, "Updated all articles");
transaction.rollback(); } catch (final Exception e) {
} if (transaction.isActive()) {
transaction.rollback();
throw e; }
}
} throw e;
}
/** }
* Send an email to the user who upgrades B3log Solo with a discontinuous version.
* /**
* @throws ServiceException ServiceException * Send an email to the user who upgrades B3log Solo with a discontinuous version.
* @throws JSONException JSONException *
* @throws IOException IOException * @throws ServiceException ServiceException
*/ * @throws JSONException JSONException
private void notifyUserByEmail() throws ServiceException, JSONException, IOException { * @throws IOException IOException
final String adminEmail = preferenceQueryService.getPreference().getString(Preference.ADMIN_EMAIL); */
final MailService.Message message = new MailService.Message(); private void notifyUserByEmail() throws ServiceException, JSONException, IOException {
final String adminEmail = preferenceQueryService.getPreference().getString(Preference.ADMIN_EMAIL);
message.setFrom(adminEmail); final MailService.Message message = new MailService.Message();
message.addRecipient(adminEmail);
message.setSubject(langPropsService.get("skipVersionMailSubject")); message.setFrom(adminEmail);
message.setHtmlBody(langPropsService.get("skipVersionMailBody")); message.addRecipient(adminEmail);
MAIL_SVC.send(message); message.setSubject(langPropsService.get("skipVersionMailSubject"));
LOGGER.info("Send an email to the user who upgrades B3log Solo with a discontinuous version."); message.setHtmlBody(langPropsService.get("skipVersionMailBody"));
} MAIL_SVC.send(message);
} LOGGER.info("Send an email to the user who upgrades B3log Solo with a discontinuous version.");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment