protected ServiceContext getServiceContext() throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId());

    serviceContext.setAttribute("fileEntryTypeId", _contractDLFileEntryTypeId);

    Map<String, Serializable> expandoBridgeAttributes = serviceContext.getExpandoBridgeAttributes();

    expandoBridgeAttributes.put(_EXPANDO_ATTRIBUTE_NAME, StringPool.BLANK);

    serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);

    DLFileEntryType fileEntryType =
        DLFileEntryTypeLocalServiceUtil.getFileEntryType(_contractDLFileEntryTypeId);

    List<DDMStructure> ddmStructures = fileEntryType.getDDMStructures();

    for (DDMStructure ddmStructure : ddmStructures) {
      DDMFormValues ddmFormValues = createDDMFormValues(ddmStructure.getDDMForm());

      for (String fieldName : ddmStructure.getFieldNames()) {
        DDMFormFieldValue ddmFormFieldValue = createDDMFormFieldValue(fieldName);

        ddmFormValues.addDDMFormFieldValue(ddmFormFieldValue);
      }

      serviceContext.setAttribute(
          DDMFormValues.class.getName() + ddmStructure.getStructureId(), ddmFormValues);
    }

    return serviceContext;
  }
  /**
   * Returns a new service context object identical to this service context object.
   *
   * @return a new service context object
   */
  @Override
  public Object clone() {
    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(isAddGroupPermissions());
    serviceContext.setAddGuestPermissions(isAddGuestPermissions());
    serviceContext.setAssetCategoryIds(getAssetCategoryIds());
    serviceContext.setAssetEntryVisible(isAssetEntryVisible());
    serviceContext.setAssetLinkEntryIds(getAssetLinkEntryIds());
    serviceContext.setAssetTagNames(getAssetTagNames());
    serviceContext.setAttributes(getAttributes());
    serviceContext.setCommand(getCommand());
    serviceContext.setCompanyId(getCompanyId());
    serviceContext.setCreateDate(getCreateDate());
    serviceContext.setCurrentURL(getCurrentURL());
    serviceContext.setExpandoBridgeAttributes(getExpandoBridgeAttributes());
    serviceContext.setFailOnPortalException(isFailOnPortalException());
    serviceContext.setGroupPermissions(getGroupPermissions());
    serviceContext.setGuestPermissions(getGuestPermissions());
    serviceContext.setHeaders(getHeaders());
    serviceContext.setIndexingEnabled(isIndexingEnabled());
    serviceContext.setLanguageId(getLanguageId());
    serviceContext.setLayoutFullURL(getLayoutFullURL());
    serviceContext.setLayoutURL(getLayoutURL());
    serviceContext.setModifiedDate(getModifiedDate());
    serviceContext.setPathFriendlyURLPrivateGroup(getPathFriendlyURLPrivateGroup());
    serviceContext.setPathFriendlyURLPrivateUser(getPathFriendlyURLPrivateUser());
    serviceContext.setPathFriendlyURLPublic(getPathFriendlyURLPublic());
    serviceContext.setPathMain(getPathMain());
    serviceContext.setPlid(getPlid());
    serviceContext.setPortalURL(getPortalURL());
    serviceContext.setPortletPreferencesIds(getPortletPreferencesIds());
    serviceContext.setRemoteAddr(getRemoteAddr());
    serviceContext.setRemoteHost(getRemoteHost());
    serviceContext.setRequest(getRequest());
    serviceContext.setScopeGroupId(getScopeGroupId());
    serviceContext.setSignedIn(isSignedIn());
    serviceContext.setUserDisplayURL(getUserDisplayURL());
    serviceContext.setUserId(getUserId());
    serviceContext.setUuid(getUuid());
    serviceContext.setWorkflowAction(getWorkflowAction());

    return serviceContext;
  }
  protected ServiceContext getServiceContext() throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId());

    serviceContext.setAttribute("fileEntryTypeId", _contractDLFileEntryTypeId);

    Map<String, Serializable> expandoBridgeAttributes = serviceContext.getExpandoBridgeAttributes();

    expandoBridgeAttributes.put(_EXPANDO_ATTRIBUTE_NAME, StringPool.BLANK);

    serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);

    DLFileEntryType fileEntryType =
        DLFileEntryTypeLocalServiceUtil.getFileEntryType(_contractDLFileEntryTypeId);

    List<DDMStructure> ddmStructures = fileEntryType.getDDMStructures();

    for (DDMStructure ddmStructure : ddmStructures) {
      Fields fields = new Fields();

      Set<String> names = ddmStructure.getFieldNames();

      for (String name : names) {
        Field field = new Field(ddmStructure.getStructureId(), name, StringPool.BLANK);

        if (ddmStructure.isFieldPrivate(name)) {
          field.setValue(RandomTestUtil.randomString());
        }

        fields.put(field);
      }

      serviceContext.setAttribute(Fields.class.getName() + ddmStructure.getStructureId(), fields);
    }

    return serviceContext;
  }
  protected ServiceContext createServiceContext(
      Element element, String path, ClassedModel classedModel, String namespace) {

    Class<?> clazz = classedModel.getModelClass();
    long classPK = getClassPK(classedModel);

    ServiceContext serviceContext = new ServiceContext();

    // Theme display

    serviceContext.setCompanyId(getCompanyId());
    serviceContext.setScopeGroupId(getScopeGroupId());

    // Dates

    if (classedModel instanceof AuditedModel) {
      AuditedModel auditedModel = (AuditedModel) classedModel;

      serviceContext.setCreateDate(auditedModel.getCreateDate());
      serviceContext.setModifiedDate(auditedModel.getModifiedDate());
    }

    // Permissions

    if (!MapUtil.getBoolean(_parameterMap, PortletDataHandlerKeys.PERMISSIONS)) {

      serviceContext.setAddGroupPermissions(true);
      serviceContext.setAddGuestPermissions(true);
    }

    // Asset

    boolean portletMetadataAll =
        getBooleanParameter(namespace, PortletDataHandlerKeys.PORTLET_METADATA_ALL);

    if (isResourceMain(classedModel)) {
      if (portletMetadataAll || getBooleanParameter(namespace, "categories")) {

        long[] assetCategoryIds = getAssetCategoryIds(clazz, classPK);

        serviceContext.setAssetCategoryIds(assetCategoryIds);
      }

      if (portletMetadataAll || getBooleanParameter(namespace, "tags")) {
        String[] assetTagNames = getAssetTagNames(clazz, classPK);

        serviceContext.setAssetTagNames(assetTagNames);
      }
    }

    // Expando

    String expandoPath = null;

    if (element != null) {
      expandoPath = element.attributeValue("expando-path");
    } else {
      expandoPath = getExpandoPath(path);
    }

    if (Validator.isNotNull(expandoPath)) {
      try {
        Map<String, Serializable> expandoBridgeAttributes =
            (Map<String, Serializable>) getZipEntryAsObject(expandoPath);

        serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
      } catch (Exception e) {
        if (_log.isDebugEnabled()) {
          _log.debug(e, e);
        }
      }
    }

    return serviceContext;
  }
  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());
    }
  }