public Website addWebsite(
      long userId, String className, long classPK, String url, int typeId, boolean primary)
      throws PortalException, SystemException {

    User user = userPersistence.findByPrimaryKey(userId);
    long classNameId = PortalUtil.getClassNameId(className);
    Date now = new Date();

    validate(0, user.getCompanyId(), classNameId, classPK, url, typeId, primary);

    long websiteId = counterLocalService.increment();

    Website website = websitePersistence.create(websiteId);

    website.setCompanyId(user.getCompanyId());
    website.setUserId(user.getUserId());
    website.setUserName(user.getFullName());
    website.setCreateDate(now);
    website.setModifiedDate(now);
    website.setClassNameId(classNameId);
    website.setClassPK(classPK);
    website.setUrl(url);
    website.setTypeId(typeId);
    website.setPrimary(primary);

    websitePersistence.update(website);

    return website;
  }
  public Website updateWebsite(long websiteId, String url, int typeId, boolean primary)
      throws PortalException, SystemException {

    validate(websiteId, 0, 0, 0, url, typeId, primary);

    Website website = websitePersistence.findByPrimaryKey(websiteId);

    website.setModifiedDate(new Date());
    website.setUrl(url);
    website.setTypeId(typeId);
    website.setPrimary(primary);

    websitePersistence.update(website);

    return website;
  }
  protected void validate(
      long websiteId, long companyId, long classNameId, long classPK, boolean primary)
      throws SystemException {

    // Check to make sure there isn't another website with the same company
    // id, class name, and class pk that also has primary set to true

    if (primary) {
      List<Website> websites =
          websitePersistence.findByC_C_C_P(companyId, classNameId, classPK, primary);

      for (Website website : websites) {
        if ((websiteId <= 0) || (website.getWebsiteId() != websiteId)) {
          website.setPrimary(false);

          websitePersistence.update(website);
        }
      }
    }
  }
  protected void validate(
      long websiteId, long companyId, long classNameId, long classPK, boolean primary)
      throws SystemException {

    // Check to make sure there isn't another website with the same company
    // id, class name, and class pk that also has primary set to true

    if (primary) {
      Iterator<Website> itr =
          websitePersistence.findByC_C_C_P(companyId, classNameId, classPK, primary).iterator();

      while (itr.hasNext()) {
        Website website = itr.next();

        if ((websiteId <= 0) || (website.getWebsiteId() != websiteId)) {

          website.setPrimary(false);

          websitePersistence.update(website, false);
        }
      }
    }
  }