public List<Group> getMySites(String[] classNames, boolean includeControlPanel, int max)
      throws PortalException, SystemException {

    ThreadLocalCache<List<Group>> threadLocalCache =
        ThreadLocalCacheManager.getThreadLocalCache(Lifecycle.REQUEST, UserImpl.class.getName());

    String key = StringUtil.toHexString(max);

    if ((classNames != null) && (classNames.length > 0)) {
      key = StringUtil.merge(classNames).concat(StringPool.POUND).concat(key);
    }

    key = key.concat(StringPool.POUND).concat(String.valueOf(includeControlPanel));

    List<Group> myPlaces = threadLocalCache.get(key);

    if (myPlaces != null) {
      return myPlaces;
    }

    myPlaces = GroupServiceUtil.getUserPlaces(getUserId(), classNames, includeControlPanel, max);

    threadLocalCache.put(key, myPlaces);

    return myPlaces;
  }
  protected void checkSystemRole(
      long companyId, String name, Map<Locale, String> descriptionMap, int type)
      throws PortalException, SystemException {

    String companyIdHexString = StringUtil.toHexString(companyId);

    String key = companyIdHexString.concat(name);

    Role role = _systemRolesMap.get(key);

    try {
      if (role == null) {
        role = rolePersistence.findByC_N(companyId, name);
      }

      if (!descriptionMap.equals(role.getDescriptionMap())) {
        role.setDescriptionMap(descriptionMap);

        roleLocalService.updateRole(role, false);
      }
    } catch (NoSuchRoleException nsre) {
      role = roleLocalService.addRole(0, companyId, name, null, descriptionMap, type);

      if (name.equals(RoleConstants.USER)) {
        initPersonalControlPanelPortletsPermissions(role);
      }
    }

    _systemRolesMap.put(key, role);
  }
  /**
   * Escapes the input text as a hexadecimal value, based on the mode (type). The encoding types
   * include: {@link #ESCAPE_MODE_ATTRIBUTE}, {@link #ESCAPE_MODE_CSS}, {@link #ESCAPE_MODE_JS},
   * {@link #ESCAPE_MODE_TEXT}, and {@link #ESCAPE_MODE_URL}.
   *
   * <p>Note that <code>escape(text, ESCAPE_MODE_TEXT)</code> returns the same as <code>escape(text)
   * </code>.
   *
   * @param text the text to escape
   * @param mode the encoding type
   * @return the escaped hexadecimal value of the input text, based on the mode, or <code>null
   *     </code> if the text is <code>null</code>
   */
  @Override
  public String escape(String text, int mode) {
    if (text == null) {
      return null;
    }

    if (text.length() == 0) {
      return StringPool.BLANK;
    }

    String prefix = StringPool.BLANK;
    String postfix = StringPool.BLANK;

    if (mode == ESCAPE_MODE_ATTRIBUTE) {
      prefix = "&#x";
      postfix = StringPool.SEMICOLON;
    } else if (mode == ESCAPE_MODE_CSS) {
      prefix = StringPool.BACK_SLASH;
    } else if (mode == ESCAPE_MODE_JS) {
      prefix = "\\x";
    } else if (mode == ESCAPE_MODE_URL) {
      return HttpUtil.encodeURL(text, true);
    } else {
      return escape(text);
    }

    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < text.length(); i++) {
      char c = text.charAt(i);

      if ((c > 255)
          || Character.isLetterOrDigit(c)
          || (c == CharPool.DASH)
          || (c == CharPool.UNDERLINE)) {

        sb.append(c);
      } else {
        sb.append(prefix);

        String hexString = StringUtil.toHexString(c);

        if (hexString.length() == 1) {
          sb.append(StringPool.ASCII_TABLE[48]);
        }

        sb.append(hexString);
        sb.append(postfix);
      }
    }

    if (sb.length() == text.length()) {
      return text;
    } else {
      return sb.toString();
    }
  }
  /**
   * Returns the role with the name in the company.
   *
   * <p>The method searches the system roles map first for default roles. If a role with the name is
   * not found, then the method will query the database.
   *
   * @param companyId the primary key of the company
   * @param name the role's name
   * @return Returns the role with the name or <code>null</code> if a role with the name could not
   *     be found in the company
   * @throws SystemException if a system exception occurred
   */
  @Skip
  public Role fetchRole(long companyId, String name) throws SystemException {
    String companyIdHexString = StringUtil.toHexString(companyId);

    Role role = _systemRolesMap.get(companyIdHexString.concat(name));

    if (role != null) {
      return role;
    }

    return roleLocalService.loadFetchRole(companyId, name);
  }
  @Override
  public String getAUICompatibleId(String text) {
    if (Validator.isNull(text)) {
      return text;
    }

    StringBundler sb = null;

    int lastReplacementIndex = 0;

    for (int i = 0; i < text.length(); i++) {
      char c = text.charAt(i);

      if (((c <= 127) && (Validator.isChar(c) || Validator.isDigit(c)))
          || ((c > 127)
              && (c != CharPool.FIGURE_SPACE)
              && (c != CharPool.NARROW_NO_BREAK_SPACE)
              && (c != CharPool.NO_BREAK_SPACE))) {

        continue;
      }

      if (sb == null) {
        sb = new StringBundler();
      }

      if (i > lastReplacementIndex) {
        sb.append(text.substring(lastReplacementIndex, i));
      }

      sb.append(StringPool.UNDERLINE);

      if (c != CharPool.UNDERLINE) {
        sb.append(StringUtil.toHexString(c));
      }

      sb.append(StringPool.UNDERLINE);

      lastReplacementIndex = i + 1;
    }

    if (sb == null) {
      return text;
    }

    if (lastReplacementIndex < text.length()) {
      sb.append(text.substring(lastReplacementIndex));
    }

    return sb.toString();
  }
  /**
   * Checks to ensure that the system roles map has appropriate default roles in the company.
   *
   * @param companyId the primary key of the company
   * @throws PortalException if the current user did not have permission to set applicable
   *     permissions on a role
   * @throws SystemException if a system exception occurred
   */
  @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
  public void checkSystemRoles(long companyId) throws PortalException, SystemException {

    String companyIdHexString = StringUtil.toHexString(companyId);

    for (Role role : roleFinder.findBySystem(companyId)) {
      _systemRolesMap.put(companyIdHexString.concat(role.getName()), role);
    }

    // Regular roles

    String[] systemRoles = PortalUtil.getSystemRoles();

    for (String name : systemRoles) {
      String key =
          "system.role."
              + StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD)
              + ".description";

      Map<Locale, String> descriptionMap = new HashMap<Locale, String>();

      descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));

      int type = RoleConstants.TYPE_REGULAR;

      checkSystemRole(companyId, name, descriptionMap, type);
    }

    // Organization roles

    String[] systemOrganizationRoles = PortalUtil.getSystemOrganizationRoles();

    for (String name : systemOrganizationRoles) {
      String key =
          "system.organization.role."
              + StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD)
              + ".description";

      Map<Locale, String> descriptionMap = new HashMap<Locale, String>();

      descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));

      int type = RoleConstants.TYPE_ORGANIZATION;

      checkSystemRole(companyId, name, descriptionMap, type);
    }

    // Site roles

    String[] systemSiteRoles = PortalUtil.getSystemSiteRoles();

    for (String name : systemSiteRoles) {
      String key =
          "system.site.role."
              + StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD)
              + ".description";

      Map<Locale, String> descriptionMap = new HashMap<Locale, String>();

      descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));

      int type = RoleConstants.TYPE_SITE;

      checkSystemRole(companyId, name, descriptionMap, type);
    }
  }