Search in sources :

Example 6 with Transactional

use of org.b3log.latke.repository.annotation.Transactional in project symphony by b3log.

the class DomainMgmtService method addDomain.

/**
 * Adds a domain relation.
 *
 * @param domain the specified domain relation
 * @return domain id
 * @throws ServiceException service exception
 */
@Transactional
public String addDomain(final JSONObject domain) throws ServiceException {
    try {
        final JSONObject record = new JSONObject();
        record.put(Domain.DOMAIN_CSS, domain.optString(Domain.DOMAIN_CSS));
        record.put(Domain.DOMAIN_DESCRIPTION, domain.optString(Domain.DOMAIN_DESCRIPTION));
        record.put(Domain.DOMAIN_ICON_PATH, domain.optString(Domain.DOMAIN_ICON_PATH));
        record.put(Domain.DOMAIN_SEO_DESC, domain.optString(Domain.DOMAIN_SEO_DESC));
        record.put(Domain.DOMAIN_SEO_KEYWORDS, domain.optString(Domain.DOMAIN_SEO_KEYWORDS));
        record.put(Domain.DOMAIN_SEO_TITLE, domain.optString(Domain.DOMAIN_SEO_TITLE));
        record.put(Domain.DOMAIN_STATUS, domain.optInt(Domain.DOMAIN_STATUS));
        record.put(Domain.DOMAIN_TITLE, domain.optString(Domain.DOMAIN_TITLE));
        record.put(Domain.DOMAIN_URI, domain.optString(Domain.DOMAIN_URI));
        record.put(Domain.DOMAIN_TAG_COUNT, 0);
        record.put(Domain.DOMAIN_TYPE, "");
        record.put(Domain.DOMAIN_SORT, 10);
        record.put(Domain.DOMAIN_NAV, Domain.DOMAIN_NAV_C_ENABLED);
        final JSONObject domainCntOption = optionRepository.get(Option.ID_C_STATISTIC_DOMAIN_COUNT);
        final int domainCnt = domainCntOption.optInt(Option.OPTION_VALUE);
        domainCntOption.put(Option.OPTION_VALUE, domainCnt + 1);
        optionRepository.update(Option.ID_C_STATISTIC_DOMAIN_COUNT, domainCntOption);
        final String ret = domainRepository.add(record);
        // Refresh cache
        domainCache.loadDomains();
        return ret;
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Adds a domain failed", e);
        throw new ServiceException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) Transactional(org.b3log.latke.repository.annotation.Transactional)

Example 7 with Transactional

use of org.b3log.latke.repository.annotation.Transactional in project symphony by b3log.

the class DomainMgmtService method removeDomain.

/**
 * Removes the specified domain by the given domain id.
 *
 * @param domainId the given domain id
 * @throws ServiceException service exception
 */
@Transactional
public void removeDomain(final String domainId) throws ServiceException {
    try {
        domainTagRepository.removeByDomainId(domainId);
        domainRepository.remove(domainId);
        final JSONObject domainCntOption = optionRepository.get(Option.ID_C_STATISTIC_DOMAIN_COUNT);
        final int domainCnt = domainCntOption.optInt(Option.OPTION_VALUE);
        domainCntOption.put(Option.OPTION_VALUE, domainCnt - 1);
        optionRepository.update(Option.ID_C_STATISTIC_DOMAIN_COUNT, domainCntOption);
        // Refresh cache
        domainCache.loadDomains();
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Updates a domain [id=" + domainId + "] failed", e);
        throw new ServiceException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) Transactional(org.b3log.latke.repository.annotation.Transactional)

Example 8 with Transactional

use of org.b3log.latke.repository.annotation.Transactional in project symphony by b3log.

the class UserMgmtService method resetUnverifiedUsers.

/**
 * Resets unverified users.
 */
@Transactional
public void resetUnverifiedUsers() {
    final Date now = new Date();
    final long yesterdayTime = DateUtils.addDays(now, -1).getTime();
    final List<Filter> filters = new ArrayList<>();
    filters.add(new PropertyFilter(UserExt.USER_STATUS, FilterOperator.EQUAL, UserExt.USER_STATUS_C_NOT_VERIFIED));
    filters.add(new PropertyFilter(Keys.OBJECT_ID, FilterOperator.LESS_THAN_OR_EQUAL, yesterdayTime));
    filters.add(new PropertyFilter(User.USER_NAME, FilterOperator.NOT_EQUAL, UserExt.NULL_USER_NAME));
    final Query query = new Query().setFilter(new CompositeFilter(CompositeFilterOperator.AND, filters));
    try {
        final JSONObject result = userRepository.get(query);
        final JSONArray users = result.optJSONArray(Keys.RESULTS);
        for (int i = 0; i < users.length(); i++) {
            final JSONObject user = users.optJSONObject(i);
            final String id = user.optString(Keys.OBJECT_ID);
            user.put(User.USER_NAME, UserExt.NULL_USER_NAME);
            userRepository.update(id, user);
            LOGGER.log(Level.INFO, "Reset unverified user [email=" + user.optString(User.USER_EMAIL) + "]");
        }
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Reset unverified users failed", e);
    }
}
Also used : JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) Transactional(org.b3log.latke.repository.annotation.Transactional)

Example 9 with Transactional

use of org.b3log.latke.repository.annotation.Transactional in project symphony by b3log.

the class VerifycodeMgmtService method removeExpiredVerifycodes.

/**
 * Removes expired verifycodes.
 */
@Transactional
public void removeExpiredVerifycodes() {
    final Query query = new Query().setFilter(new PropertyFilter(Verifycode.EXPIRED, FilterOperator.LESS_THAN, new Date().getTime()));
    try {
        final JSONObject result = verifycodeRepository.get(query);
        final JSONArray verifycodes = result.optJSONArray(Keys.RESULTS);
        for (int i = 0; i < verifycodes.length(); i++) {
            final String id = verifycodes.optJSONObject(i).optString(Keys.OBJECT_ID);
            verifycodeRepository.remove(id);
        }
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Expires verifycodes failed", e);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Transactional(org.b3log.latke.repository.annotation.Transactional)

Example 10 with Transactional

use of org.b3log.latke.repository.annotation.Transactional in project symphony by b3log.

the class VerifycodeMgmtService method sendEmailVerifycode.

/**
 * Sends email verifycode.
 */
@Transactional
public void sendEmailVerifycode() {
    final List<Filter> filters = new ArrayList<>();
    filters.add(new PropertyFilter(Verifycode.TYPE, FilterOperator.EQUAL, Verifycode.TYPE_C_EMAIL));
    filters.add(new PropertyFilter(Verifycode.STATUS, FilterOperator.EQUAL, Verifycode.STATUS_C_UNSENT));
    final Query query = new Query().setFilter(new CompositeFilter(CompositeFilterOperator.AND, filters));
    try {
        final JSONObject result = verifycodeRepository.get(query);
        final JSONArray verifycodes = result.optJSONArray(Keys.RESULTS);
        for (int i = 0; i < verifycodes.length(); i++) {
            final JSONObject verifycode = verifycodes.optJSONObject(i);
            final String userId = verifycode.optString(Verifycode.USER_ID);
            final JSONObject user = userRepository.get(userId);
            if (null == user) {
                continue;
            }
            final Map<String, Object> dataModel = new HashMap<>();
            final String userName = user.optString(User.USER_NAME);
            dataModel.put(User.USER_NAME, userName);
            final String toMail = verifycode.optString(Verifycode.RECEIVER);
            final String code = verifycode.optString(Verifycode.CODE);
            String subject;
            final int bizType = verifycode.optInt(Verifycode.BIZ_TYPE);
            switch(bizType) {
                case Verifycode.BIZ_TYPE_C_REGISTER:
                    dataModel.put(Common.URL, Latkes.getServePath() + "/register?code=" + code);
                    subject = langPropsService.get("registerEmailSubjectLabel", Latkes.getLocale());
                    break;
                case Verifycode.BIZ_TYPE_C_RESET_PWD:
                    dataModel.put(Common.URL, Latkes.getServePath() + "/reset-pwd?code=" + code);
                    subject = langPropsService.get("forgetEmailSubjectLabel", Latkes.getLocale());
                    break;
                default:
                    LOGGER.warn("Send email verify code failed with wrong biz type [" + bizType + "]");
                    continue;
            }
            verifycode.put(Verifycode.STATUS, Verifycode.STATUS_C_SENT);
            verifycodeRepository.update(verifycode.optString(Keys.OBJECT_ID), verifycode);
            final String fromName = langPropsService.get("symphonyEnLabel") + " " + langPropsService.get("verifycodeEmailFromNameLabel", Latkes.getLocale());
            Mails.sendHTML(fromName, subject, toMail, Mails.TEMPLATE_NAME_VERIFYCODE, dataModel);
        }
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Sends verifycode failed", e);
    }
}
Also used : JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) Transactional(org.b3log.latke.repository.annotation.Transactional)

Aggregations

Transactional (org.b3log.latke.repository.annotation.Transactional)52 ServiceException (org.b3log.latke.service.ServiceException)42 JSONObject (org.json.JSONObject)20 JSONArray (org.json.JSONArray)11 RepositoryException (org.b3log.latke.repository.RepositoryException)6 Query (org.b3log.latke.repository.Query)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URL (java.net.URL)1 ExecutionException (java.util.concurrent.ExecutionException)1 PropertyFilter (org.b3log.latke.repository.PropertyFilter)1 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)1 TextHTMLRenderer (org.b3log.latke.servlet.renderer.TextHTMLRenderer)1