Search in sources :

Example 1 with CreateTableResult

use of org.b3log.latke.repository.jdbc.util.JdbcRepositories.CreateTableResult in project solo by b3log.

the class InitService method init.

/**
     * Initializes Solo.
     *
     * <p>
     * Initializes the followings in sequence:
     * <ol>
     * <li>Statistic</li>
     * <li>Preference</li>
     * <li>Administrator</li>
     * </ol>
     * </p>
     *
     * <p>
     * We will try to initialize Solo 3 times at most.
     * </p>
     *
     * <p>
     * Posts "Hello World!" article and its comment while Solo initialized.
     * </p>
     *
     * @param requestJSONObject the specified request json object, for example,      <pre>
     * {
     *     "userName": "",
     *     "userEmail": "",
     *     "userPassword": "", // Unhashed
     * }
     * </pre>
     *
     * @throws ServiceException service exception
     */
public void init(final JSONObject requestJSONObject) throws ServiceException {
    if (isInited()) {
        return;
    }
    final RuntimeEnv runtimeEnv = Latkes.getRuntimeEnv();
    if (RuntimeEnv.LOCAL == runtimeEnv) {
        LOGGER.log(Level.INFO, "Solo is running on [" + runtimeEnv + "] environment, database [{0}], creates " + "all tables", Latkes.getRuntimeDatabase());
        if (RuntimeDatabase.H2 == Latkes.getRuntimeDatabase()) {
            String dataDir = Latkes.getLocalProperty("jdbc.URL");
            dataDir = dataDir.replace("~", System.getProperty("user.home"));
            LOGGER.log(Level.INFO, "YOUR DATA will be stored in directory [" + dataDir + "], " + "please pay more attention to it~");
        }
        final List<CreateTableResult> createTableResults = JdbcRepositories.initAllTables();
        for (final CreateTableResult createTableResult : createTableResults) {
            LOGGER.log(Level.DEBUG, "Create table result[tableName={0}, isSuccess={1}]", new Object[] { createTableResult.getName(), createTableResult.isSuccess() });
        }
    }
    int retries = MAX_RETRIES_CNT;
    while (true) {
        final Transaction transaction = userRepository.beginTransaction();
        try {
            final JSONObject statistic = statisticRepository.get(Statistic.STATISTIC);
            if (null == statistic) {
                initStatistic();
                initPreference(requestJSONObject);
                initReplyNotificationTemplate();
                initAdmin(requestJSONObject);
                initLink();
            }
            transaction.commit();
            break;
        } catch (final Exception e) {
            if (0 == retries) {
                LOGGER.log(Level.ERROR, "Initialize Solo error", e);
                throw new ServiceException("Initailize Solo error: " + e.getMessage());
            }
            // Allow retry to occur
            --retries;
            LOGGER.log(Level.WARN, "Retrying to init Solo[retries={0}]", retries);
        } finally {
            if (transaction.isActive()) {
                transaction.rollback();
            }
        }
    }
    final Transaction transaction = userRepository.beginTransaction();
    try {
        helloWorld();
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Hello World error?!", e);
    }
    try {
        final URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
        final HTTPRequest req = new HTTPRequest();
        req.setURL(new URL(Latkes.getServePath() + "/blog/symphony/user"));
        urlFetchService.fetch(req);
    } catch (final Exception e) {
        LOGGER.log(Level.TRACE, "Sync account failed");
    }
    pluginManager.load();
}
Also used : HTTPRequest(org.b3log.latke.urlfetch.HTTPRequest) Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) URLFetchService(org.b3log.latke.urlfetch.URLFetchService) RuntimeEnv(org.b3log.latke.RuntimeEnv) CreateTableResult(org.b3log.latke.repository.jdbc.util.JdbcRepositories.CreateTableResult) JSONException(org.json.JSONException) ParseException(java.text.ParseException) ServiceException(org.b3log.latke.service.ServiceException) RepositoryException(org.b3log.latke.repository.RepositoryException) URL(java.net.URL)

Aggregations

URL (java.net.URL)1 ParseException (java.text.ParseException)1 RuntimeEnv (org.b3log.latke.RuntimeEnv)1 RepositoryException (org.b3log.latke.repository.RepositoryException)1 Transaction (org.b3log.latke.repository.Transaction)1 CreateTableResult (org.b3log.latke.repository.jdbc.util.JdbcRepositories.CreateTableResult)1 ServiceException (org.b3log.latke.service.ServiceException)1 HTTPRequest (org.b3log.latke.urlfetch.HTTPRequest)1 URLFetchService (org.b3log.latke.urlfetch.URLFetchService)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1