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;
    }
  }
  protected void mergeCategories(ShoppingCategory fromCategory, long toCategoryId)
      throws PortalException, SystemException {

    List<ShoppingCategory> categories =
        shoppingCategoryPersistence.findByG_P(
            fromCategory.getGroupId(), fromCategory.getCategoryId());

    for (ShoppingCategory category : categories) {
      mergeCategories(category, toCategoryId);
    }

    List<ShoppingItem> items =
        shoppingItemPersistence.findByG_C(fromCategory.getGroupId(), fromCategory.getCategoryId());

    for (ShoppingItem item : items) {

      // Item

      item.setCategoryId(toCategoryId);

      shoppingItemPersistence.update(item);
    }

    deleteCategory(fromCategory);
  }
  @Override
  public void deleteCategory(ShoppingCategory category) throws PortalException, SystemException {

    // Categories

    List<ShoppingCategory> categories =
        shoppingCategoryPersistence.findByG_P(category.getGroupId(), category.getCategoryId());

    for (ShoppingCategory curCategory : categories) {
      deleteCategory(curCategory);
    }

    // Category

    shoppingCategoryPersistence.remove(category);

    // Resources

    resourceLocalService.deleteResource(
        category.getCompanyId(),
        ShoppingCategory.class.getName(),
        ResourceConstants.SCOPE_INDIVIDUAL,
        category.getCategoryId());

    // Items

    shoppingItemLocalService.deleteItems(category.getGroupId(), category.getCategoryId());
  }
  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 void getSubcategoryIds(List<Long> categoryIds, long groupId, long categoryId)
      throws SystemException {

    List<ShoppingCategory> categories = shoppingCategoryPersistence.findByG_P(groupId, categoryId);

    for (ShoppingCategory category : categories) {
      categoryIds.add(category.getCategoryId());

      getSubcategoryIds(categoryIds, category.getGroupId(), category.getCategoryId());
    }
  }
  @Override
  public void addCategoryResources(
      ShoppingCategory category, String[] groupPermissions, String[] guestPermissions)
      throws PortalException, SystemException {

    resourceLocalService.addModelResources(
        category.getCompanyId(),
        category.getGroupId(),
        category.getUserId(),
        ShoppingCategory.class.getName(),
        category.getCategoryId(),
        groupPermissions,
        guestPermissions);
  }
  protected long getCategory(ShoppingItem item, long categoryId) throws SystemException {

    if ((item.getCategoryId() != categoryId)
        && (categoryId != ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID)) {

      ShoppingCategory newCategory = shoppingCategoryPersistence.fetchByPrimaryKey(categoryId);

      if ((newCategory == null) || (item.getGroupId() != newCategory.getGroupId())) {

        categoryId = item.getCategoryId();
      }
    }

    return categoryId;
  }
  protected long getParentCategoryId(long groupId, long parentCategoryId) throws SystemException {

    if (parentCategoryId != ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {

      ShoppingCategory parentCategory =
          shoppingCategoryPersistence.fetchByPrimaryKey(parentCategoryId);

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

        parentCategoryId = ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
      }
    }

    return parentCategoryId;
  }
  @Override
  public void addCategoryResources(
      ShoppingCategory category, boolean addGroupPermissions, boolean addGuestPermissions)
      throws PortalException, SystemException {

    resourceLocalService.addResources(
        category.getCompanyId(),
        category.getGroupId(),
        category.getUserId(),
        ShoppingCategory.class.getName(),
        category.getCategoryId(),
        false,
        addGroupPermissions,
        addGuestPermissions);
  }
  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;
  }
  private static boolean _hasPermission(
      PermissionChecker permissionChecker, ShoppingCategory category, String actionId) {

    if (permissionChecker.hasOwnerPermission(
            category.getCompanyId(),
            ShoppingCategory.class.getName(),
            category.getCategoryId(),
            category.getUserId(),
            actionId)
        || permissionChecker.hasPermission(
            category.getGroupId(),
            ShoppingCategory.class.getName(),
            category.getCategoryId(),
            actionId)) {

      return true;
    }

    return false;
  }