protected void validate(
      long websiteId,
      long companyId,
      long classNameId,
      long classPK,
      String url,
      int typeId,
      boolean primary)
      throws PortalException, SystemException {

    if (!Validator.isUrl(url)) {
      throw new WebsiteURLException();
    }

    if (websiteId > 0) {
      Website website = websitePersistence.findByPrimaryKey(websiteId);

      companyId = website.getCompanyId();
      classNameId = website.getClassNameId();
      classPK = website.getClassPK();
    }

    listTypeService.validate(typeId, classNameId, ListTypeConstants.WEBSITE);

    validate(websiteId, companyId, classNameId, classPK, primary);
  }
  /**
   * Adds the website to the database. Also notifies the appropriate model listeners.
   *
   * @param website the website
   * @return the website that was added
   */
  @Indexable(type = IndexableType.REINDEX)
  @Override
  public Website addWebsite(Website website) {
    website.setNew(true);

    return websitePersistence.update(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);
        }
      }
    }
  }
  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;
  }
  @Override
  protected void doImportStagedModel(PortletDataContext portletDataContext, Website website)
      throws Exception {

    long userId = portletDataContext.getUserId(website.getUserUuid());

    ServiceContext serviceContext = portletDataContext.createServiceContext(website);

    Website existingWebsite =
        _websiteLocalService.fetchWebsiteByUuidAndCompanyId(
            website.getUuid(), portletDataContext.getCompanyGroupId());

    Website importedWebsite = null;

    if (existingWebsite == null) {
      serviceContext.setUuid(website.getUuid());

      importedWebsite =
          _websiteLocalService.addWebsite(
              userId,
              website.getClassName(),
              website.getClassPK(),
              website.getUrl(),
              website.getTypeId(),
              website.isPrimary(),
              serviceContext);
    } else {
      importedWebsite =
          _websiteLocalService.updateWebsite(
              existingWebsite.getWebsiteId(), website.getUrl(),
              website.getTypeId(), website.isPrimary());
    }

    portletDataContext.importClassedModel(website, importedWebsite);
  }