@Override
  public KaleoInstance addKaleoInstance(
      long kaleoDefinitionId,
      String kaleoDefinitionName,
      int kaleoDefinitionVersion,
      Map<String, Serializable> workflowContext,
      ServiceContext serviceContext)
      throws PortalException {

    User user = userLocalService.fetchUser(serviceContext.getUserId());

    if (user == null) {
      user = userLocalService.getDefaultUser(serviceContext.getCompanyId());
    }

    Date now = new Date();

    long kaleoInstanceId = counterLocalService.increment();

    KaleoInstance kaleoInstance = kaleoInstancePersistence.create(kaleoInstanceId);

    long groupId = StagingUtil.getLiveGroupId(serviceContext.getScopeGroupId());

    kaleoInstance.setGroupId(groupId);

    kaleoInstance.setCompanyId(user.getCompanyId());
    kaleoInstance.setUserId(user.getUserId());
    kaleoInstance.setUserName(user.getFullName());
    kaleoInstance.setCreateDate(now);
    kaleoInstance.setModifiedDate(now);
    kaleoInstance.setKaleoDefinitionId(kaleoDefinitionId);
    kaleoInstance.setKaleoDefinitionName(kaleoDefinitionName);
    kaleoInstance.setKaleoDefinitionVersion(kaleoDefinitionVersion);
    kaleoInstance.setClassName(
        (String) workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME));

    if (workflowContext.containsKey(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK)) {

      kaleoInstance.setClassPK(
          GetterUtil.getLong(
              (String) workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK)));
    }

    kaleoInstance.setCompleted(false);
    kaleoInstance.setWorkflowContext(WorkflowContextUtil.convert(workflowContext));

    kaleoInstancePersistence.update(kaleoInstance);

    return kaleoInstance;
  }
  public Group getGroup() throws PortalException {
    if (_group != null) {
      return _group;
    }

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

    long groupId = ParamUtil.getLong(_request, "groupId", themeDisplay.getSiteGroupId());

    _group = GroupLocalServiceUtil.getGroup(groupId);

    if (_group != null) {
      _group = StagingUtil.getLiveGroup(_group.getGroupId());
    }

    return _group;
  }
  @Override
  public void updateStagedPortlets(long groupId, Map<String, String> stagedPortletIds)
      throws PortalException {

    Group group = groupPersistence.findByPrimaryKey(groupId);

    GroupPermissionUtil.check(getPermissionChecker(), group, ActionKeys.UPDATE);

    UnicodeProperties typeSettingsProperties = group.getTypeSettingsProperties();

    for (String stagedPortletId : stagedPortletIds.keySet()) {
      typeSettingsProperties.setProperty(
          StagingUtil.getStagedPortletId(stagedPortletId), stagedPortletIds.get(stagedPortletId));
    }

    groupLocalService.updateGroup(group);
  }
  @Override
  protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);

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

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

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

    StagingUtil.setRecentLayoutBranchId(
        request, layoutSetBranchId, themeDisplay.getPlid(), layoutBranchId);

    ActionUtil.addLayoutBranchSessionMessages(actionRequest, actionResponse);
  }
  @Override
  protected void doReceive(Message message, MessageStatus messageStatus) throws PortalException {

    long exportImportConfigurationId = GetterUtil.getLong(message.getPayload());

    ExportImportConfiguration exportImportConfiguration =
        _exportImportConfigurationLocalService.fetchExportImportConfiguration(
            exportImportConfigurationId);

    if (exportImportConfiguration == null) {
      if (_log.isWarnEnabled()) {
        _log.warn(
            "Unable to find export import configuration with ID " + exportImportConfigurationId);
      }

      return;
    }

    messageStatus.setPayload(exportImportConfiguration);

    Map<String, Serializable> settingsMap = exportImportConfiguration.getSettingsMap();

    long userId = MapUtil.getLong(settingsMap, "userId");
    long sourceGroupId = MapUtil.getLong(settingsMap, "sourceGroupId");
    long targetGroupId = MapUtil.getLong(settingsMap, "targetGroupId");
    boolean privateLayout = MapUtil.getBoolean(settingsMap, "privateLayout");
    long[] layoutIds = GetterUtil.getLongValues(settingsMap.get("layoutIds"));
    Map<String, String[]> parameterMap = (Map<String, String[]>) settingsMap.get("parameterMap");

    initThreadLocals(userId, parameterMap);

    try {
      StagingUtil.publishLayouts(
          userId,
          sourceGroupId,
          targetGroupId,
          privateLayout,
          layoutIds,
          exportImportConfiguration.getName(),
          parameterMap);
    } finally {
      resetThreadLocals();
    }
  }
  /**
   * Returns an export layout settings map if the type is {@link
   * ExportImportConfigurationConstants#TYPE_EXPORT_LAYOUT}; otherwise, returns either a local or
   * remote publish layout settings map, depending on the staging type.
   *
   * @param portletRequest the portlet request
   * @param groupId the primary key of the group
   * @param type the export/import option type
   * @return an export layout settings map if the type is an export layout; otherwise, returns
   *     either a local or remote publish layout settings map, depending on the staging type
   */
  public static Map<String, Serializable> buildSettingsMap(
      PortletRequest portletRequest, long groupId, int type) throws PortalException {

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

    boolean privateLayout = ParamUtil.getBoolean(portletRequest, "privateLayout");

    Map<Long, Boolean> layoutIdMap = ExportImportHelperUtil.getLayoutIdMap(portletRequest);

    if (type == ExportImportConfigurationConstants.TYPE_EXPORT_LAYOUT) {
      long[] layoutIds = ExportImportHelperUtil.getLayoutIds(layoutIdMap);

      return buildExportLayoutSettingsMap(
          themeDisplay.getUserId(),
          groupId,
          privateLayout,
          layoutIds,
          portletRequest.getParameterMap(),
          themeDisplay.getLocale(),
          themeDisplay.getTimeZone());
    }

    Group stagingGroup = GroupLocalServiceUtil.getGroup(groupId);

    Group liveGroup = stagingGroup.getLiveGroup();

    Map<String, String[]> parameterMap =
        ExportImportConfigurationParameterMapFactory.buildParameterMap(portletRequest);

    if (liveGroup != null) {
      long[] layoutIds = ExportImportHelperUtil.getLayoutIds(layoutIdMap, liveGroup.getGroupId());

      return buildPublishLayoutLocalSettingsMap(
          themeDisplay.getUserId(),
          stagingGroup.getGroupId(),
          liveGroup.getGroupId(),
          privateLayout,
          layoutIds,
          parameterMap,
          themeDisplay.getLocale(),
          themeDisplay.getTimeZone());
    }

    UnicodeProperties groupTypeSettingsProperties = stagingGroup.getTypeSettingsProperties();

    String remoteAddress =
        ParamUtil.getString(
            portletRequest,
            "remoteAddress",
            groupTypeSettingsProperties.getProperty("remoteAddress"));

    remoteAddress = StagingUtil.stripProtocolFromRemoteAddress(remoteAddress);

    int remotePort =
        ParamUtil.getInteger(
            portletRequest,
            "remotePort",
            GetterUtil.getInteger(groupTypeSettingsProperties.getProperty("remotePort")));
    String remotePathContext =
        ParamUtil.getString(
            portletRequest,
            "remotePathContext",
            groupTypeSettingsProperties.getProperty("remotePathContext"));
    boolean secureConnection =
        ParamUtil.getBoolean(
            portletRequest,
            "secureConnection",
            GetterUtil.getBoolean(groupTypeSettingsProperties.getProperty("secureConnection")));
    long remoteGroupId =
        ParamUtil.getLong(
            portletRequest,
            "remoteGroupId",
            GetterUtil.getLong(groupTypeSettingsProperties.getProperty("remoteGroupId")));
    boolean remotePrivateLayout = ParamUtil.getBoolean(portletRequest, "remotePrivateLayout");

    StagingUtil.validateRemote(
        groupId, remoteAddress, remotePort, remotePathContext, secureConnection, remoteGroupId);

    return buildPublishLayoutRemoteSettingsMap(
        themeDisplay.getUserId(),
        groupId,
        privateLayout,
        layoutIdMap,
        parameterMap,
        remoteAddress,
        remotePort,
        remotePathContext,
        secureConnection,
        remoteGroupId,
        remotePrivateLayout,
        themeDisplay.getLocale(),
        themeDisplay.getTimeZone());
  }
 public long getSiteGroupIdOrLiveGroupId() {
   return StagingUtil.getLiveGroupId(_siteGroupId);
 }