protected String addPortletId(
      long userId,
      String portletId,
      String columnId,
      int columnPos,
      boolean checkPermission,
      boolean strictHasPortlet) {

    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 && !portlet.isUndeployedPortlet()) {

        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;
  }
  @Override
  public InvokerPortlet create(
      Portlet portlet, ServletContext servletContext, boolean destroyPrevious)
      throws PortletException {

    if (destroyPrevious) {
      destroyRelated(portlet);
    }

    boolean instanceable = false;

    boolean deployed = !portlet.isUndeployedPortlet();

    if (portlet.isInstanceable()
        && deployed
        && PortletConstants.hasInstanceId(portlet.getPortletId())) {

      instanceable = true;
    }

    String rootPortletId = portlet.getRootPortletId();

    InvokerPortlet rootInvokerPortletInstance = null;

    Map<String, InvokerPortlet> portletInstances = null;

    if (deployed) {
      portletInstances = _pool.get(rootPortletId);

      if (portletInstances == null) {
        portletInstances = new ConcurrentHashMap<>();

        _pool.put(rootPortletId, portletInstances);
      } else {
        if (instanceable) {
          InvokerPortlet instanceInvokerPortletInstance =
              portletInstances.get(portlet.getPortletId());

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

        rootInvokerPortletInstance = portletInstances.get(rootPortletId);
      }
    }

    if (rootInvokerPortletInstance == null) {
      PortletBag portletBag = PortletBagPool.get(rootPortletId);

      // Portlet bag should never be null unless the portlet has been
      // undeployed

      if (portletBag == null) {
        PortletBagFactory portletBagFactory = new PortletBagFactory();

        portletBagFactory.setClassLoader(ClassLoaderUtil.getPortalClassLoader());
        portletBagFactory.setServletContext(servletContext);
        portletBagFactory.setWARFile(false);

        try {
          portletBag = portletBagFactory.create(portlet);
        } catch (Exception e) {
          throw new PortletException(e);
        }
      }

      PortletConfig portletConfig = PortletConfigFactoryUtil.create(portlet, servletContext);

      rootInvokerPortletInstance = init(portlet, portletConfig, portletBag.getPortletInstance());

      if (deployed) {
        portletInstances.put(rootPortletId, rootInvokerPortletInstance);
      }
    }

    if (!instanceable) {
      return rootInvokerPortletInstance;
    }

    javax.portlet.Portlet portletInstance = rootInvokerPortletInstance.getPortletInstance();

    PortletConfig portletConfig = PortletConfigFactoryUtil.create(portlet, servletContext);

    PortletContext portletContext = portletConfig.getPortletContext();
    boolean checkAuthToken = rootInvokerPortletInstance.isCheckAuthToken();
    boolean facesPortlet = rootInvokerPortletInstance.isFacesPortlet();
    boolean strutsPortlet = rootInvokerPortletInstance.isStrutsPortlet();
    boolean strutsBridgePortlet = rootInvokerPortletInstance.isStrutsBridgePortlet();

    InvokerPortlet instanceInvokerPortletInstance =
        _invokerPortletFactory.create(
            portlet,
            portletInstance,
            portletConfig,
            portletContext,
            (InvokerFilterContainer) rootInvokerPortletInstance,
            checkAuthToken,
            facesPortlet,
            strutsPortlet,
            strutsBridgePortlet);

    if (deployed) {
      portletInstances.put(portlet.getPortletId(), instanceInvokerPortletInstance);
    }

    return instanceInvokerPortletInstance;
  }