protected long getParentCategoryId(ShoppingCategory category, long parentCategoryId)
      throws SystemException {

    if (parentCategoryId == ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {

      return parentCategoryId;
    }

    if (category.getCategoryId() == parentCategoryId) {
      return category.getParentCategoryId();
    } else {
      ShoppingCategory parentCategory =
          shoppingCategoryPersistence.fetchByPrimaryKey(parentCategoryId);

      if ((parentCategory == null) || (category.getGroupId() != parentCategory.getGroupId())) {

        return category.getParentCategoryId();
      }

      List<Long> subcategoryIds = new ArrayList<Long>();

      getSubcategoryIds(subcategoryIds, category.getGroupId(), category.getCategoryId());

      if (subcategoryIds.contains(parentCategoryId)) {
        return category.getParentCategoryId();
      }

      return parentCategoryId;
    }
  }
  @Override
  public List<ShoppingCategory> getParentCategories(ShoppingCategory category)
      throws PortalException, SystemException {

    List<ShoppingCategory> parentCategories = new ArrayList<ShoppingCategory>();

    ShoppingCategory tempCategory = category;

    while (true) {
      parentCategories.add(tempCategory);

      if (tempCategory.getParentCategoryId()
          == ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {

        break;
      }

      tempCategory =
          shoppingCategoryPersistence.findByPrimaryKey(tempCategory.getParentCategoryId());
    }

    Collections.reverse(parentCategories);

    return parentCategories;
  }
  public static boolean contains(
      PermissionChecker permissionChecker, ShoppingCategory category, String actionId)
      throws PortalException {

    if (actionId.equals(ActionKeys.ADD_CATEGORY)) {
      actionId = ActionKeys.ADD_SUBCATEGORY;
    }

    if (actionId.equals(ActionKeys.VIEW) && PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {

      long categoryId = category.getCategoryId();

      while (categoryId != ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {

        category = ShoppingCategoryLocalServiceUtil.getCategory(categoryId);

        if (!_hasPermission(permissionChecker, category, actionId)) {
          return false;
        }

        categoryId = category.getParentCategoryId();
      }

      return ShoppingPermission.contains(permissionChecker, category.getGroupId(), actionId);
    }

    return _hasPermission(permissionChecker, category, actionId);
  }
  @Override
  public ShoppingCategory getParentCategory(ShoppingCategory category)
      throws PortalException, SystemException {

    ShoppingCategory parentCategory =
        shoppingCategoryPersistence.findByPrimaryKey(category.getParentCategoryId());

    return parentCategory;
  }
  public int compareTo(ShoppingCategory shoppingCategory) {
    int value = 0;

    if (getParentCategoryId() < shoppingCategory.getParentCategoryId()) {
      value = -1;
    } else if (getParentCategoryId() > shoppingCategory.getParentCategoryId()) {
      value = 1;
    } else {
      value = 0;
    }

    if (value != 0) {
      return value;
    }

    value = getName().toLowerCase().compareTo(shoppingCategory.getName().toLowerCase());

    if (value != 0) {
      return value;
    }

    return 0;
  }
  public static JSONObject toJSONObject(ShoppingCategory model) {
    JSONObject jsonObj = new JSONObject();

    JSONUtil.put(jsonObj, "categoryId", model.getCategoryId());
    JSONUtil.put(jsonObj, "groupId", model.getGroupId());
    JSONUtil.put(jsonObj, "companyId", model.getCompanyId());
    JSONUtil.put(jsonObj, "userId", model.getUserId());
    JSONUtil.put(jsonObj, "userName", model.getUserName());
    JSONUtil.put(jsonObj, "createDate", model.getCreateDate());
    JSONUtil.put(jsonObj, "modifiedDate", model.getModifiedDate());
    JSONUtil.put(jsonObj, "parentCategoryId", model.getParentCategoryId());
    JSONUtil.put(jsonObj, "name", model.getName());
    JSONUtil.put(jsonObj, "description", model.getDescription());

    return jsonObj;
  }