protected String[] getStaticPortletIds(String position) throws PortalException {

    Layout layout = getLayout();

    String selector1 = StringPool.BLANK;

    Group group = layout.getGroup();

    if (group.isUser()) {
      selector1 = LayoutTypePortletConstants.STATIC_PORTLET_USER_SELECTOR;
    } else if (group.isOrganization()) {
      selector1 = LayoutTypePortletConstants.STATIC_PORTLET_ORGANIZATION_SELECTOR;
    } else if (group.isRegularSite()) {
      selector1 = LayoutTypePortletConstants.STATIC_PORTLET_REGULAR_SITE_SELECTOR;
    }

    String selector2 = layout.getFriendlyURL();

    String[] portletIds = PropsUtil.getArray(position, new Filter(selector1, selector2));

    for (int i = 0; i < portletIds.length; i++) {
      portletIds[i] = JS.getSafeName(portletIds[i]);
    }

    return portletIds;
  }
  protected void readPortletResource(String servletContextName, Element portletResourceElement) {

    String name = portletResourceElement.elementTextTrim("portlet-name");

    if (servletContextName != null) {
      name = name.concat(PortletConstants.WAR_SEPARATOR).concat(servletContextName);
    }

    name = JS.getSafeName(name);

    List<String> supportsActions =
        readSupportsActions(portletResourceElement, _portletResourceActions, name);

    supportsActions.addAll(getPortletMimeTypeActions(name));

    if (!name.equals(PortletKeys.PORTAL)) {
      checkPortletActions(name, supportsActions);
    }

    supportsActions = setActions(_portletResourceActions, name, supportsActions);

    readGroupDefaultActions(portletResourceElement, _portletResourceGroupDefaultActions, name);

    List<String> guestDefaultActions =
        readGuestDefaultActions(portletResourceElement, _portletResourceGuestDefaultActions, name);

    readGuestUnsupportedActions(
        portletResourceElement, _portletResourceGuestUnsupportedActions,
        name, guestDefaultActions);

    setActions(_portletResourceGuestDefaultActions, name, guestDefaultActions);

    readLayoutManagerActions(
        portletResourceElement, _portletResourceLayoutManagerActions, name, supportsActions);
  }
  protected String addPortletId(
      long userId,
      String portletId,
      String columnId,
      int columnPos,
      boolean checkPermission,
      boolean strictHasPortlet)
      throws PortalException {

    portletId = JS.getSafeName(portletId);

    Layout layout = getLayout();

    Portlet portlet = null;

    try {
      portlet = PortletLocalServiceUtil.getPortletById(layout.getCompanyId(), portletId);

      if (portlet == null) {
        if (_log.isWarnEnabled()) {
          _log.warn("Portlet " + portletId + " cannot be added because it is not registered");
        }

        return null;
      }

      PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker();

      if (checkPermission
          && !PortletPermissionUtil.contains(
              permissionChecker, layout, portlet, ActionKeys.ADD_TO_PAGE)) {

        return null;
      }
    } catch (Exception e) {
      _log.error(e, e);
    }

    if (portlet.isSystem()) {
      return null;
    }

    if (portlet.isInstanceable() && !PortletConstants.hasInstanceId(portletId)) {

      portletId =
          PortletConstants.assemblePortletId(portletId, PortletConstants.generateInstanceId());
    }

    if (hasPortletId(portletId, strictHasPortlet)) {
      return null;
    }

    if (columnId == null) {
      LayoutTemplate layoutTemplate = getLayoutTemplate();

      List<String> columns = layoutTemplate.getColumns();

      if (!columns.isEmpty()) {
        columnId = columns.get(0);
      }
    }

    if (columnId == null) {
      return null;
    }

    if (isCustomizable()) {
      if (isColumnDisabled(columnId)) {
        return null;
      }

      if ((PortletConstants.hasInstanceId(portletId) || portlet.isPreferencesUniquePerLayout())
          && hasUserPreferences()) {

        portletId = PortletConstants.assemblePortletId(portletId, userId);
      }
    }

    String columnValue = StringPool.BLANK;

    if (hasUserPreferences()) {
      columnValue = getUserPreference(columnId);
    } else {
      columnValue = getTypeSettingsProperty(columnId);
    }

    if ((columnValue == null) && columnId.startsWith(_NESTED_PORTLETS_NAMESPACE)) {

      addNestedColumn(columnId);
    }

    if (columnPos >= 0) {
      List<String> portletIds = ListUtil.fromArray(StringUtil.split(columnValue));

      if (columnPos <= portletIds.size()) {
        portletIds.add(columnPos, portletId);
      } else {
        portletIds.add(portletId);
      }

      columnValue = StringUtil.merge(portletIds);
    } else {
      columnValue = StringUtil.add(columnValue, portletId);
    }

    if (hasUserPreferences()) {
      setUserPreference(columnId, columnValue);
    } else {
      setTypeSettingsProperty(columnId, columnValue);
    }

    try {
      if (_enablePortletLayoutListener) {
        PortletLayoutListener portletLayoutListener = portlet.getPortletLayoutListenerInstance();

        if (portletLayoutListener != null) {
          portletLayoutListener.onAddToLayout(portletId, layout.getPlid());
        }
      }
    } catch (Exception e) {
      _log.error("Unable to fire portlet layout listener event", e);
    }

    return portletId;
  }
  public String addPortletId(
      long userId, String portletId, String columnId, int columnPos, boolean checkPermission)
      throws PortalException, SystemException {

    portletId = JS.getSafeName(portletId);

    Layout layout = getLayout();

    Portlet portlet = null;

    try {
      portlet = PortletLocalServiceUtil.getPortletById(layout.getCompanyId(), portletId);

      if (portlet == null) {
        if (_log.isWarnEnabled()) {
          _log.warn("Portlet " + portletId + " cannot be added because it is not registered");
        }

        return null;
      }

      PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker();

      if (checkPermission
          && !PortletPermissionUtil.contains(
              permissionChecker, layout, portlet, ActionKeys.ADD_TO_PAGE)) {

        return null;
      }
    } catch (Exception e) {
      _log.error(e, e);
    }

    if (portlet.isSystem()) {
      return null;
    }

    if ((portlet.isInstanceable())
        && (PortletConstants.getInstanceId(portlet.getPortletId()) == null)) {

      portletId = portletId + getFullInstanceSeparator();
    }

    if (hasPortletId(portletId)) {
      return null;
    }

    if (columnId == null) {
      LayoutTemplate layoutTemplate = getLayoutTemplate();

      List<String> columns = layoutTemplate.getColumns();

      if (columns.size() > 0) {
        columnId = columns.get(0);
      }
    }

    if (columnId != null) {
      if (isCustomizable() && isColumnDisabled(columnId)) {
        return null;
      }

      String columnValue = StringPool.BLANK;

      if (hasUserPreferences()) {
        columnValue = getUserPreference(columnId);
      } else {
        columnValue = getTypeSettingsProperties().getProperty(columnId);
      }

      if ((columnValue == null) && (columnId.startsWith(_nestedPortletsNamespace))) {

        addNestedColumn(columnId);
      }

      if (columnPos >= 0) {
        List<String> portletIds = ListUtil.fromArray(StringUtil.split(columnValue));

        if (columnPos <= portletIds.size()) {
          portletIds.add(columnPos, portletId);
        } else {
          portletIds.add(portletId);
        }

        columnValue = StringUtil.merge(portletIds);
      } else {
        columnValue = StringUtil.add(columnValue, portletId);
      }

      if (hasUserPreferences()) {
        setUserPreference(columnId, columnValue);
      } else {
        getTypeSettingsProperties().setProperty(columnId, columnValue);
      }
    }

    try {
      PortletLayoutListener portletLayoutListener = portlet.getPortletLayoutListenerInstance();

      if (_enablePortletLayoutListener && (portletLayoutListener != null)) {

        portletLayoutListener.onAddToLayout(portletId, layout.getPlid());
      }
    } catch (Exception e) {
      _log.error("Unable to fire portlet layout listener event", e);
    }

    return portletId;
  }
  protected void readModelResource(String servletContextName, Element modelResourceElement) {

    String name = modelResourceElement.elementTextTrim("model-name");

    Element portletRefElement = modelResourceElement.element("portlet-ref");

    for (Element portletNameElement : portletRefElement.elements("portlet-name")) {

      String portletName = portletNameElement.getTextTrim();

      if (servletContextName != null) {
        portletName = portletName.concat(PortletConstants.WAR_SEPARATOR).concat(servletContextName);
      }

      portletName = JS.getSafeName(portletName);

      // Reference for a portlet to child models

      Set<String> modelResources = _portletModelResources.get(portletName);

      if (modelResources == null) {
        modelResources = new HashSet<String>();

        _portletModelResources.put(portletName, modelResources);
      }

      modelResources.add(name);

      // Reference for a model to parent portlets

      Set<String> portletResources = _modelPortletResources.get(name);

      if (portletResources == null) {
        portletResources = new HashSet<String>();

        _modelPortletResources.put(name, portletResources);
      }

      portletResources.add(portletName);

      // Reference for a model to root portlets

      boolean root = GetterUtil.getBoolean(modelResourceElement.elementText("root"));

      if (root) {
        _portletRootModelResource.put(portletName, name);
      }
    }

    double weight = GetterUtil.getDouble(modelResourceElement.elementTextTrim("weight"), 100);

    _modelResourceWeights.put(name, weight);

    List<String> supportsActions =
        readSupportsActions(modelResourceElement, _modelResourceActions, name);

    checkModelActions(supportsActions);

    setActions(_modelResourceActions, name, supportsActions);

    readGroupDefaultActions(modelResourceElement, _modelResourceGroupDefaultActions, name);

    List<String> guestDefaultActions =
        readGuestDefaultActions(modelResourceElement, _modelResourceGuestDefaultActions, name);

    readGuestUnsupportedActions(
        modelResourceElement, _modelResourceGuestUnsupportedActions, name, guestDefaultActions);

    setActions(_modelResourceGuestDefaultActions, name, guestDefaultActions);

    readOwnerDefaultActions(modelResourceElement, _modelResourceOwnerDefaultActions, name);
  }