예제 #1
0
  // JAVADOC COMMENT ELIDED
  public void addToCategory(String category) throws PIMException {
    this.modified = false;
    if (category == null) {
      throw new NullPointerException("Null category");
    }

    int maxCategories = maxCategories();
    if (maxCategories == 0) {
      throw new PIMException("Categories are not supported", PIMException.FEATURE_NOT_SUPPORTED);
    }

    // Check if the category is in the database
    String[] listCategories = pimHandler.getCategories(pimListHandle);
    boolean foundCategory = false;
    for (int i = 0; i < listCategories.length; i++) {
      if (category.equals(listCategories[i])) {
        foundCategory = true;
        break;
      }
    }
    if (!foundCategory) {
      throw new PIMException("Category " + category + " is not in list");
    }

    if (categories == null) {
      this.categories = new String[] {category};
      this.modified = true;
    } else {
      for (int i = 0; i < categories.length; i++) {
        if (categories[i].equals(category)) {
          return;
        }
      }

      // -1 means unlimited number of categories
      if (maxCategories != -1 && maxCategories <= categories.length) {
        throw new PIMException("max categories that this item can " + "be assigned to is exceeded");
      }
      String[] a = new String[categories.length + 1];
      System.arraycopy(categories, 0, a, 0, categories.length);
      a[categories.length] = category;
      this.categories = a;
      this.modified = true;
    }
  }