public void merge(ServiceContext serviceContext) {
    setAddGroupPermissions(serviceContext.isAddGroupPermissions());
    setAddGuestPermissions(serviceContext.isAddGuestPermissions());

    if (serviceContext.getAssetCategoryIds() != null) {
      setAssetCategoryIds(serviceContext.getAssetCategoryIds());
    }

    if (serviceContext.getAssetLinkEntryIds() != null) {
      setAssetLinkEntryIds(serviceContext.getAssetLinkEntryIds());
    }

    if (serviceContext.getAssetTagNames() != null) {
      setAssetTagNames(serviceContext.getAssetTagNames());
    }

    if (serviceContext.getAttributes() != null) {
      setAttributes(serviceContext.getAttributes());
    }

    if (Validator.isNotNull(serviceContext.getCommand())) {
      setCommand(serviceContext.getCommand());
    }

    if (serviceContext.getCompanyId() > 0) {
      setCompanyId(serviceContext.getCompanyId());
    }

    if (serviceContext.getCreateDate() != null) {
      setCreateDate(serviceContext.getCreateDate());
    }

    if (Validator.isNotNull(serviceContext.getCurrentURL())) {
      setCurrentURL(serviceContext.getCurrentURL());
    }

    if (serviceContext.getExpandoBridgeAttributes() != null) {
      setExpandoBridgeAttributes(serviceContext.getExpandoBridgeAttributes());
    }

    if (serviceContext.getGroupPermissions() != null) {
      setGroupPermissions(serviceContext.getGroupPermissions());
    }

    if (serviceContext.getGuestPermissions() != null) {
      setGuestPermissions(serviceContext.getGuestPermissions());
    }

    if (serviceContext.getHeaders() != null) {
      setHeaders(serviceContext.getHeaders());
    }

    setFailOnPortalException(serviceContext.isFailOnPortalException());
    setLanguageId(serviceContext.getLanguageId());

    if (Validator.isNotNull(serviceContext.getLayoutFullURL())) {
      setLayoutFullURL(serviceContext.getLayoutFullURL());
    }

    if (Validator.isNotNull(serviceContext.getLayoutURL())) {
      setLayoutURL(serviceContext.getLayoutURL());
    }

    if (serviceContext.getModifiedDate() != null) {
      setModifiedDate(serviceContext.getModifiedDate());
    }

    if (Validator.isNotNull(serviceContext.getPathMain())) {
      setPathMain(serviceContext.getPathMain());
    }

    if (serviceContext.getPlid() > 0) {
      setPlid(serviceContext.getPlid());
    }

    if (Validator.isNotNull(serviceContext.getPortalURL())) {
      setPortalURL(serviceContext.getPortalURL());
    }

    if (serviceContext.getPortletPreferencesIds() != null) {
      setPortletPreferencesIds(serviceContext.getPortletPreferencesIds());
    }

    if (Validator.isNotNull(serviceContext.getRemoteAddr())) {
      setRemoteAddr(serviceContext.getRemoteAddr());
    }

    if (Validator.isNotNull(serviceContext.getRemoteHost())) {
      setRemoteHost(serviceContext.getRemoteHost());
    }

    if (serviceContext.getScopeGroupId() > 0) {
      setScopeGroupId(serviceContext.getScopeGroupId());
    }

    setSignedIn(serviceContext.isSignedIn());

    if (Validator.isNotNull(serviceContext.getUserDisplayURL())) {
      setUserDisplayURL(serviceContext.getUserDisplayURL());
    }

    if (serviceContext.getUserId() > 0) {
      setUserId(serviceContext.getUserId());
    }

    if (Validator.isNotNull(serviceContext.getUuid())) {
      setUuid(serviceContext.getUuid());
    }

    if (serviceContext.getWorkflowAction() > 0) {
      setWorkflowAction(serviceContext.getWorkflowAction());
    }
  }
  @Override
  public MBMessage addMessage(
      long parentMessageId,
      String subject,
      String body,
      String format,
      List<ObjectValuePair<String, InputStream>> inputStreamOVPs,
      boolean anonymous,
      double priority,
      boolean allowPingbacks,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    MBMessage parentMessage = mbMessagePersistence.findByPrimaryKey(parentMessageId);

    checkReplyToPermission(
        parentMessage.getGroupId(), parentMessage.getCategoryId(), parentMessageId);

    boolean preview = ParamUtil.getBoolean(serviceContext, "preview");

    int workFlowAction = serviceContext.getWorkflowAction();

    if ((workFlowAction == WorkflowConstants.STATUS_DRAFT)
        && !preview
        && !serviceContext.isSignedIn()) {

      MBMessagePermission.check(getPermissionChecker(), parentMessageId, ActionKeys.UPDATE);
    }

    if (lockLocalService.isLocked(MBThread.class.getName(), parentMessage.getThreadId())) {

      throw new LockedThreadException();
    }

    if (!MBCategoryPermission.contains(
        getPermissionChecker(),
        parentMessage.getGroupId(),
        parentMessage.getCategoryId(),
        ActionKeys.ADD_FILE)) {

      inputStreamOVPs = Collections.emptyList();
    }

    if (!MBCategoryPermission.contains(
        getPermissionChecker(),
        parentMessage.getGroupId(),
        parentMessage.getCategoryId(),
        ActionKeys.UPDATE_THREAD_PRIORITY)) {

      priority = MBThreadConstants.PRIORITY_NOT_GIVEN;
    }

    return mbMessageLocalService.addMessage(
        getGuestOrUserId(),
        null,
        parentMessage.getGroupId(),
        parentMessage.getCategoryId(),
        parentMessage.getThreadId(),
        parentMessageId,
        subject,
        body,
        format,
        inputStreamOVPs,
        anonymous,
        priority,
        allowPingbacks,
        serviceContext);
  }
  private LayoutRevision _getLayoutRevision(Layout layout, LayoutRevision layoutRevision)
      throws PortalException, SystemException {

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

    ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();

    if (!serviceContext.isSignedIn()) {
      LayoutRevision lastLayoutRevision = null;

      lastLayoutRevision =
          LayoutRevisionLocalServiceUtil.fetchLastLayoutRevision(layout.getPlid(), true);

      if (lastLayoutRevision == null) {
        lastLayoutRevision =
            LayoutRevisionLocalServiceUtil.fetchLastLayoutRevision(layout.getPlid(), false);
      }

      return lastLayoutRevision;
    }

    User user = UserLocalServiceUtil.getUser(serviceContext.getUserId());

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

    LayoutSet layoutSet = layout.getLayoutSet();

    LayoutSetBranch layoutSetBranch =
        LayoutSetBranchLocalServiceUtil.getUserLayoutSetBranch(
            serviceContext.getUserId(),
            layout.getGroupId(),
            layout.isPrivateLayout(),
            layoutSet.getLayoutSetId(),
            layoutSetBranchId);

    layoutSetBranchId = layoutSetBranch.getLayoutSetBranchId();

    long layoutRevisionId = ParamUtil.getLong(serviceContext, "layoutRevisionId");

    if (layoutRevisionId <= 0) {
      layoutRevisionId =
          StagingUtil.getRecentLayoutRevisionId(user, layoutSetBranchId, layout.getPlid());
    }

    if (layoutRevisionId > 0) {
      layoutRevision = LayoutRevisionLocalServiceUtil.fetchLayoutRevision(layoutRevisionId);

      if (layoutRevision.getStatus() != WorkflowConstants.STATUS_INACTIVE) {

        return layoutRevision;
      }

      layoutRevision = null;
    }

    List<LayoutRevision> layoutRevisions =
        LayoutRevisionLocalServiceUtil.getLayoutRevisions(
            layoutSetBranchId,
            layout.getPlid(),
            QueryUtil.ALL_POS,
            QueryUtil.ALL_POS,
            new LayoutRevisionCreateDateComparator(true));

    if (!layoutRevisions.isEmpty()) {
      layoutRevision = layoutRevisions.get(0);

      for (LayoutRevision curLayoutRevision : layoutRevisions) {
        if (curLayoutRevision.isHead()) {
          layoutRevision = curLayoutRevision;

          break;
        }
      }
    }

    if (layoutRevision != null) {
      StagingUtil.setRecentLayoutRevisionId(
          user, layoutSetBranchId, layout.getPlid(), layoutRevision.getLayoutRevisionId());

      return layoutRevision;
    }

    LayoutBranch layoutBranch =
        LayoutBranchLocalServiceUtil.getMasterLayoutBranch(
            layoutSetBranchId, layout.getPlid(), serviceContext);

    if (!MergeLayoutPrototypesThreadLocal.isInProgress()) {
      serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT);
    }

    return LayoutRevisionLocalServiceUtil.addLayoutRevision(
        serviceContext.getUserId(),
        layoutSetBranchId,
        layoutBranch.getLayoutBranchId(),
        LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID,
        false,
        layout.getPlid(),
        LayoutConstants.DEFAULT_PLID,
        layout.isPrivateLayout(),
        layout.getName(),
        layout.getTitle(),
        layout.getDescription(),
        layout.getKeywords(),
        layout.getRobots(),
        layout.getTypeSettings(),
        layout.getIconImage(),
        layout.getIconImageId(),
        layout.getThemeId(),
        layout.getColorSchemeId(),
        layout.getWapThemeId(),
        layout.getWapColorSchemeId(),
        layout.getCss(),
        serviceContext);
  }