gt:   protected String[] getActionCategory(String cateId) {
context: /*
   * Copyright (C) 2003-2008 eXo Platform SAS.
   *
   * 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 General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, see<http://www.gnu.org/licenses/>.
   */
  package org.exoplatform.answer.webui;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import javax.jcr.ItemExistsException;
  import javax.jcr.PathNotFoundException;
  
  import org.exoplatform.answer.webui.popup.UICategoryForm;
  import org.exoplatform.answer.webui.popup.UIExportForm;
  import org.exoplatform.answer.webui.popup.UIImportForm;
  import org.exoplatform.answer.webui.popup.UIMoveCategoryForm;
  import org.exoplatform.answer.webui.popup.UIQuestionForm;
  import org.exoplatform.answer.webui.popup.UIWatchManager;
  import org.exoplatform.container.PortalContainer;
  import org.exoplatform.faq.service.Category;
  import org.exoplatform.faq.service.FAQSetting;
  import org.exoplatform.faq.service.Utils;
  import org.exoplatform.faq.service.Watch;
  import org.exoplatform.forum.common.CommonUtils;
  import org.exoplatform.forum.common.webui.BaseEventListener;
  import org.exoplatform.forum.common.webui.UIPopupAction;
  import org.exoplatform.forum.common.webui.UIPopupContainer;
  import org.exoplatform.forum.common.webui.WebUIUtils;
  import org.exoplatform.services.log.ExoLogger;
  import org.exoplatform.services.log.Log;
  import org.exoplatform.web.application.ApplicationMessage;
  import org.exoplatform.webui.application.WebuiRequestContext;
  import org.exoplatform.webui.config.annotation.ComponentConfig;
  import org.exoplatform.webui.config.annotation.EventConfig;
  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
  import org.exoplatform.webui.event.Event;
  import org.exoplatform.webui.event.EventListener;
  
  
  @ComponentConfig(
      lifecycle = UIFormLifecycle.class,
      template = "app:/templates/answer/webui/UICategories.gtmpl", 
      events = { 
          @EventConfig(listeners = UICategories.AddCategoryActionListener.class),
          @EventConfig(listeners = UICategories.AddNewQuestionActionListener.class), 
          @EventConfig(listeners = UICategories.EditCategoryActionListener.class),
          @EventConfig(listeners = UICategories.DeleteCategoryActionListener.class, confirm = "UIQuestions.msg.confirm-delete-category"),
          @EventConfig(listeners = UICategories.MoveCategoryActionListener.class), 
          @EventConfig(listeners = UICategories.WatchActionListener.class),
          @EventConfig(listeners = UICategories.WatchManagerActionListener.class), 
          @EventConfig(listeners = UICategories.UnWatchActionListener.class),
          @EventConfig(listeners = UICategories.ExportActionListener.class), 
          @EventConfig(listeners = UICategories.ImportActionListener.class),
          @EventConfig(listeners = UICategories.ChangeIndexActionListener.class),
          @EventConfig(listeners = UICategories.OpenCategoryActionListener.class), 
          @EventConfig(listeners = UICategories.FilterQuestionsActionListener.class),
          @EventConfig(listeners = UICategories.MoveCategoryIntoActionListener.class) 
      }
  )
  public class UICategories extends BaseUIFAQForm {
    private static final Log log                      = ExoLogger.getLogger(UICategories.class);
  
    private String           FILTER_OPEN_QUESTIONS    = "openQuestions";
  
    private String           FILTER_PENDING_QUESTIONS = "pendingQuestions";
  
    public String            parentCateID_            = null;
  
    private String           categoryId_;
  
    private boolean          isSwap                   = false;
  
    private String           currentCategoryName      = "";
  
    private List<Category>   listCate                 = new ArrayList<Category>();
  
    private Map<String, Boolean> categoryMod          = new HashMap<String, Boolean>();
  
    private Map<String, String>  categoryIds          = new HashMap<String, String>();
  
    private boolean          isModerator              = false;
  
    private FAQSetting       faqSetting_              = new FAQSetting();
  
    private String[]         firstActionCate_         = new String[] { "Export", "Import", "AddCategory", "AddNewQuestion", "EditCategory", "DeleteCategory", "MoveCategory", "Watch" };
  
    private String[]         firstActionCateUnWatch_  = new String[] { "Export", "Import", "AddCategory", "AddNewQuestion", "EditCategory", "DeleteCategory", "MoveCategory", "UnWatch" };
  
    private String[]         secondActionCate_        = new String[] { "Export", "Import", "AddCategory", "AddNewQuestion", "EditCategory", "DeleteCategory", "MoveCategory", "Watch" };
  
    private String[]         secondActionCateUnWatch_ = new String[] { "Export", "Import", "AddCategory", "AddNewQuestion", "EditCategory", "DeleteCategory", "MoveCategory", "UnWatch" };
  
    private String[]         userActionsCate_         = new String[] { "AddNewQuestion", "Watch" };
  
    private String[]         userActionsCateUnWatch_  = new String[] { "AddNewQuestion", "UnWatch" };
  
    private String           portalName               = null;
  
    private String           font_weight[]            = new String[] { "bold", "none", "none" };
  
    public UICategories() throws Exception {
      portalName = getPortalName();
    }
  
    public void setFAQSetting(FAQSetting faqSetting) {
      this.faqSetting_ = faqSetting;
    }
  
    protected boolean isShowInfo() {
      return ((UIAnswersContainer) getParent()).getRenderChild();
    }
  
    protected long[] getCategoryInfo() {
      long[] result = new long[] { 0, 0, 0, 0 };
      try {
        boolean canEdit = faqSetting_.isCanEdit();
        faqSetting_.setCanEdit(isModerator);
        result = getFAQService().getCategoryInfo(categoryId_, faqSetting_);
        faqSetting_.setCanEdit(canEdit);
      } catch (Exception e) {
        log.debug("Failed to get category info,", e);
      }
      return result;
    }
  
    protected List<Category> getListCate() {
      return this.listCate;
    }
  
    public String getCategoryPath() {
      return categoryId_;
    }
  
    public void setPathCategory(String categoryPath) {
      this.categoryId_ = categoryPath;
      if (categoryPath.indexOf("/") >= 0)
        this.parentCateID_ = categoryPath.substring(0, categoryPath.lastIndexOf("/"));
      else
        this.parentCateID_ = Utils.CATEGORY_HOME;
      this.font_weight = new String[] { "bold", "none", "none" };
    }
  
    private void setIsModerators() throws Exception {
      categoryMod.clear();
      isModerator = false;
      if (faqSetting_.isAdmin())
        isModerator = true;
      if (!isModerator)
        isModerator = getFAQService().isCategoryModerator(categoryId_, null);
      if (!isModerator) {
        for (Category cat : listCate) {
          categoryMod.put(cat.getId(), getFAQService().isCategoryModerator(cat.getPath(), null));
        }
      }
    }
  
    protected boolean isCategoryModerator(String path) throws Exception {
      if (faqSetting_.isAdmin())
        return true;
      if (!FAQUtils.isFieldEmpty(categoryId_) && path.indexOf(categoryId_) >= 0 && isModerator)
        return true;
      String categoryId = path;
      if (categoryId.indexOf("/") > 0) {
        categoryId = categoryId.substring(categoryId.lastIndexOf("/") + 1);
      }
      if (categoryMod.containsKey(categoryId)) {
        return categoryMod.get(categoryId);
      } else {
        boolean isMod = getFAQService().isCategoryModerator(path, null);
        categoryMod.put(categoryId, isMod);
        return isMod;
      }
    }
  
    public String getCurrentName() {
      return currentCategoryName;
    }
  
    private boolean isWatched(String cateId) {
      return getFAQService().isUserWatched(faqSetting_.getCurrentUser(), cateId);
    }
  
    protected boolean hasWatch(String categoryPath) {
      return getFAQService().hasWatch(categoryPath);
    }
  
    private void checkAndSetListCategory(String categoryId) throws Exception {
      listCate = new ArrayList<Category>();
      FAQSetting faqSetting = new FAQSetting();
      boolean isAdmin = faqSetting_.isAdmin();
      if(!isAdmin) isAdmin = isModerator;
      faqSetting.setIsAdmin(String.valueOf(isAdmin));
      List<Category> list = getFAQService().getSubCategories(categoryId, faqSetting, null);
      for (Category category : list) {
        if (isAdmin || FAQUtils.hasPermission(category)) {
          listCate.add(category);
          categoryIds.put(category.getId(), category.getPath());
        }
      }
    }
  
    protected void setListCate() throws Exception {
      if (!isSwap) {
        setIsModerators();
        try {
          checkAndSetListCategory(categoryId_);
        } catch (PathNotFoundException e) {
          setPathCategory(parentCateID_);
          checkAndSetListCategory(categoryId_);
        } catch (Exception e) {
          log.debug("Failed to get list sub-categories in category", e);
        }
        if (categoryId_.equals(Utils.CATEGORY_HOME)) {
          currentCategoryName = getFAQService().getCategoryById(categoryId_).getName();
          currentCategoryName = "<img src=\"/eXoSkin/skin/images/system/HomeIcon.gif\" alt=\"" + currentCategoryName + "\"/>";
        } else {
          currentCategoryName = getFAQService().getCategoryById(categoryId_).getName();
        }
        if (currentCategoryName == null || currentCategoryName.trim().length() < 1){
          currentCategoryName = WebUIUtils.getLabel(null, "UIBreadcumbs.label.categories");
        }
      }
      isSwap = false;
    }
  
    public String getRSSLink(String cateId) {
      cateId = cateId.substring(cateId.lastIndexOf("/") + 1);
      return CommonUtils.getRSSLink("faq", portalName, cateId);
    }
  
    private String getPortalName() {
      PortalContainer pcontainer = PortalContainer.getInstance();
      return pcontainer.getPortalContainerInfo().getContainerName();
    }
  
    public void resetListCate() throws Exception {
      isSwap = true;
      setIsModerators();
      checkAndSetListCategory(parentCateID_);
    }
  
  