protected void updatePasswordPolicyOrganizations(ActionRequest actionRequest) throws Exception {

    long passwordPolicyId = ParamUtil.getLong(actionRequest, "passwordPolicyId");

    long[] addOrganizationIds =
        StringUtil.split(ParamUtil.getString(actionRequest, "addOrganizationIds"), 0L);
    long[] removeOrganizationIds =
        StringUtil.split(ParamUtil.getString(actionRequest, "removeOrganizationIds"), 0L);

    OrganizationServiceUtil.addPasswordPolicyOrganizations(passwordPolicyId, addOrganizationIds);
    OrganizationServiceUtil.unsetPasswordPolicyOrganizations(
        passwordPolicyId, removeOrganizationIds);
  }
  protected Organization updateOrganization(ActionRequest req) throws Exception {

    long organizationId = ParamUtil.getLong(req, "organizationId");

    long parentOrganizationId =
        ParamUtil.getLong(
            req, "parentOrganizationId", OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID);
    String name = ParamUtil.getString(req, "name");
    boolean recursable = ParamUtil.getBoolean(req, "recursable");
    int statusId = ParamUtil.getInteger(req, "statusId");
    int type = ParamUtil.getInteger(req, "type");
    long regionId = ParamUtil.getLong(req, "regionId");
    long countryId = ParamUtil.getLong(req, "countryId");
    String comments = ParamUtil.getString(req, "comments");

    Organization organization = null;

    if (organizationId <= 0) {

      // Add organization

      organization =
          OrganizationServiceUtil.addOrganization(
              parentOrganizationId,
              name,
              type,
              recursable,
              regionId,
              countryId,
              statusId,
              comments);
    } else {

      // Update organization

      organization =
          OrganizationServiceUtil.updateOrganization(
              organizationId,
              parentOrganizationId,
              name,
              type,
              recursable,
              regionId,
              countryId,
              statusId,
              comments);
    }

    return organization;
  }
  protected void deleteOrganizations(ActionRequest req) throws Exception {
    long[] deleteOrganizationIds =
        StringUtil.split(ParamUtil.getString(req, "deleteOrganizationIds"), 0L);

    for (int i = 0; i < deleteOrganizationIds.length; i++) {
      OrganizationServiceUtil.deleteOrganization(deleteOrganizationIds[i]);
    }
  }
  protected void deleteOrganizations(ActionRequest actionRequest) throws Exception {

    long[] deleteOrganizationIds =
        StringUtil.split(ParamUtil.getString(actionRequest, "deleteOrganizationIds"), 0L);

    for (long deleteOrganizationId : deleteOrganizationIds) {
      OrganizationServiceUtil.deleteOrganization(deleteOrganizationId);
    }
  }
  protected Organization updateOrganization(ActionRequest actionRequest) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    long organizationId = ParamUtil.getLong(actionRequest, "organizationId");

    long parentOrganizationId =
        ParamUtil.getLong(
            actionRequest,
            "parentOrganizationSearchContainerPrimaryKeys",
            OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID);
    String name = ParamUtil.getString(actionRequest, "name");
    int statusId = ParamUtil.getInteger(actionRequest, "statusId");
    String type = ParamUtil.getString(actionRequest, "type");
    long regionId = ParamUtil.getLong(actionRequest, "regionId");
    long countryId = ParamUtil.getLong(actionRequest, "countryId");
    String comments = ParamUtil.getString(actionRequest, "comments");
    boolean deleteLogo = ParamUtil.getBoolean(actionRequest, "deleteLogo");

    byte[] logoBytes = null;

    long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");

    if (fileEntryId > 0) {
      FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(fileEntryId);

      logoBytes = FileUtil.getBytes(fileEntry.getContentStream());
    }

    boolean site = ParamUtil.getBoolean(actionRequest, "site");
    List<Address> addresses = UsersAdminUtil.getAddresses(actionRequest);
    List<EmailAddress> emailAddresses = UsersAdminUtil.getEmailAddresses(actionRequest);
    List<OrgLabor> orgLabors = UsersAdminUtil.getOrgLabors(actionRequest);
    List<Phone> phones = UsersAdminUtil.getPhones(actionRequest);
    List<Website> websites = UsersAdminUtil.getWebsites(actionRequest);

    ServiceContext serviceContext =
        ServiceContextFactory.getInstance(Organization.class.getName(), actionRequest);

    Organization organization = null;

    if (organizationId <= 0) {

      // Add organization

      organization =
          OrganizationServiceUtil.addOrganization(
              parentOrganizationId,
              name,
              type,
              regionId,
              countryId,
              statusId,
              comments,
              site,
              addresses,
              emailAddresses,
              orgLabors,
              phones,
              websites,
              serviceContext);
    } else {

      // Update organization

      organization =
          OrganizationServiceUtil.updateOrganization(
              organizationId,
              parentOrganizationId,
              name,
              type,
              regionId,
              countryId,
              statusId,
              comments,
              !deleteLogo,
              logoBytes,
              site,
              addresses,
              emailAddresses,
              orgLabors,
              phones,
              websites,
              serviceContext);
    }

    // Layout set prototypes

    long publicLayoutSetPrototypeId =
        ParamUtil.getLong(actionRequest, "publicLayoutSetPrototypeId");
    long privateLayoutSetPrototypeId =
        ParamUtil.getLong(actionRequest, "privateLayoutSetPrototypeId");
    boolean publicLayoutSetPrototypeLinkEnabled =
        ParamUtil.getBoolean(
            actionRequest, "publicLayoutSetPrototypeLinkEnabled", (publicLayoutSetPrototypeId > 0));
    boolean privateLayoutSetPrototypeLinkEnabled =
        ParamUtil.getBoolean(
            actionRequest,
            "privateLayoutSetPrototypeLinkEnabled",
            (privateLayoutSetPrototypeId > 0));

    Group organizationGroup = organization.getGroup();

    if (GroupPermissionUtil.contains(
        themeDisplay.getPermissionChecker(), organizationGroup, ActionKeys.UPDATE)) {

      SitesUtil.updateLayoutSetPrototypesLinks(
          organizationGroup,
          publicLayoutSetPrototypeId,
          privateLayoutSetPrototypeId,
          publicLayoutSetPrototypeLinkEnabled,
          privateLayoutSetPrototypeLinkEnabled);
    }

    // Reminder queries

    String reminderQueries = actionRequest.getParameter("reminderQueries");

    PortletPreferences portletPreferences = organization.getPreferences();

    LocalizationUtil.setLocalizedPreferencesValues(
        actionRequest, portletPreferences, "reminderQueries");

    portletPreferences.setValue("reminderQueries", reminderQueries);

    portletPreferences.store();

    return organization;
  }