content: /*
   * Symphony - A modern community (forum/BBS/SNS/blog) platform written in Java.
   * Copyright (C) 2012-present, b3log.org
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Affero General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * This program 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 Affero General Public License for more details.
   *
   * You should have received a copy of the GNU Affero General Public License
   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
   */
  package org.b3log.symphony.event;
  
  import org.apache.commons.lang3.StringUtils;
  import org.apache.logging.log4j.Level;
  import org.apache.logging.log4j.LogManager;
  import org.apache.logging.log4j.Logger;
  import org.b3log.latke.event.AbstractEventListener;
  import org.b3log.latke.event.Event;
  import org.b3log.latke.ioc.Inject;
  import org.b3log.latke.ioc.Singleton;
  import org.b3log.symphony.model.Article;
  import org.b3log.symphony.model.Tag;
  import org.b3log.symphony.service.SearchMgmtService;
  import org.b3log.symphony.util.JSONs;
  import org.b3log.symphony.util.Symphonys;
  import org.json.JSONObject;
  
  /**
   * Sends an article to search engine.
   *
   * @author <a href="http://88250.b3log.org">Liang Ding</a>
   * @version 1.1.3.3, Aug 31, 2018
   * @since 1.4.0
   */
  @Singleton
  public class ArticleSearchAdder extends AbstractEventListener<JSONObject> {
  
      /**
       * Logger.
       */
      private static final Logger LOGGER = LogManager.getLogger(ArticleSearchAdder.class);
  
      /**
       * Search management service.
       */
      @Inject
      private SearchMgmtService searchMgmtService;
  
      @Override
      public void action(final Event<JSONObject> event) {
          final JSONObject data = event.getData();
          LOGGER.log(Level.TRACE, "Processing an event [type={}, data={}]", event.getType(), data);
  
          final JSONObject article = data.optJSONObject(Article.ARTICLE);
          if (Article.ARTICLE_TYPE_C_DISCUSSION == article.optInt(Article.ARTICLE_TYPE)
                  || Article.ARTICLE_TYPE_C_THOUGHT == article.optInt(Article.ARTICLE_TYPE)) {
              return;
          }
  
          final String tags = article.optString(Article.ARTICLE_TAGS);
          if (StringUtils.containsIgnoreCase(tags, Tag.TAG_TITLE_C_SANDBOX)) {
              return;
          }
  
          if (Symphonys.ALGOLIA_ENABLED) {
              searchMgmtService.updateAlgoliaDocument(JSONs.clone(article));
          }
  
          if (Symphonys.ES_ENABLED) {
              searchMgmtService.updateESDocument(JSONs.clone(article), Article.ARTICLE);
          }
      }
  
      /**
       * Gets the event type {@linkplain EventTypes#ADD_ARTICLE}.
       *
       * @return event type
       */
      @Override
      public String getEventType() {
          return EventTypes.ADD_ARTICLE;
      }
  }
  