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;
  }
  protected HashMap<String, DDMFormValues> getDDMFormValuesMap(
      ServiceContext serviceContext, long fileEntryTypeId) throws PortalException {

    HashMap<String, DDMFormValues> ddmFormValuesMap = new HashMap<>();

    if (fileEntryTypeId <= 0) {
      return ddmFormValuesMap;
    }

    DLFileEntryType fileEntryType =
        DLFileEntryTypeLocalServiceUtil.getFileEntryType(fileEntryTypeId);

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

    for (DDMStructure ddmStructure : ddmStructures) {
      String namespace = String.valueOf(ddmStructure.getStructureId());

      DDMFormValues ddmFormValues =
          (DDMFormValues)
              serviceContext.getAttribute(
                  DDMFormValues.class.getName() + ddmStructure.getStructureId());

      if (ddmFormValues == null) {
        ddmFormValues =
            StorageEngineManagerUtil.getDDMFormValues(
                ddmStructure.getStructureId(), namespace, serviceContext);
      }

      ddmFormValuesMap.put(ddmStructure.getStructureKey(), ddmFormValues);
    }

    return ddmFormValuesMap;
  }
  @Override
  public List<DDMStructure> getDDMStructures() throws PortalException {
    DLFileEntryType dlFileEntryType =
        DLFileEntryTypeLocalServiceUtil.getFileEntryType(getFileEntryTypeId());

    return dlFileEntryType.getDDMStructures();
  }
  protected void initActionableDynamicQuery(ActionableDynamicQuery actionableDynamicQuery) {
    actionableDynamicQuery.setBaseLocalService(
        com.liferay.document.library.kernel.service.DLFileEntryTypeLocalServiceUtil.getService());
    actionableDynamicQuery.setClassLoader(getClassLoader());
    actionableDynamicQuery.setModelClass(DLFileEntryType.class);

    actionableDynamicQuery.setPrimaryKeyPropertyName("fileEntryTypeId");
  }
  @Override
  public ActionableDynamicQuery getActionableDynamicQuery() {
    ActionableDynamicQuery actionableDynamicQuery = new DefaultActionableDynamicQuery();

    actionableDynamicQuery.setBaseLocalService(
        com.liferay.document.library.kernel.service.DLFileEntryTypeLocalServiceUtil.getService());
    actionableDynamicQuery.setClassLoader(getClassLoader());
    actionableDynamicQuery.setModelClass(DLFileEntryType.class);

    actionableDynamicQuery.setPrimaryKeyPropertyName("fileEntryTypeId");

    return actionableDynamicQuery;
  }
  @Before
  public void setUp() throws Exception {
    setUpPermissionThreadLocal();
    setUpPrincipalThreadLocal();

    _group = GroupTestUtil.addGroup();

    setUpParentFolder();
    setUpResourcePermission();

    List<DLFileEntryType> dlFileEntryTypes =
        DLFileEntryTypeLocalServiceUtil.getFileEntryTypes(
            PortalUtil.getCurrentAndAncestorSiteGroupIds(_group.getGroupId()));

    for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
      String fileEntryTypeKey = dlFileEntryType.getFileEntryTypeKey();

      if (fileEntryTypeKey.equals(DLFileEntryTypeConstants.FILE_ENTRY_TYPE_KEY_CONTRACT)) {

        _contractDLFileEntryTypeId = dlFileEntryType.getFileEntryTypeId();
      }
    }

    ExpandoTable expandoTable =
        ExpandoTableLocalServiceUtil.addDefaultTable(
            PortalUtil.getDefaultCompanyId(), DLFileEntry.class.getName());

    ExpandoColumnLocalServiceUtil.addColumn(
        expandoTable.getTableId(),
        _EXPANDO_ATTRIBUTE_NAME,
        ExpandoColumnConstants.STRING,
        StringPool.BLANK);

    _serviceContext = getServiceContext();

    FileEntry fileEntry =
        DLAppServiceUtil.addFileEntry(
            _group.getGroupId(),
            _parentFolder.getFolderId(),
            _SOURCE_FILE_NAME,
            ContentTypes.APPLICATION_OCTET_STREAM,
            _TITLE,
            StringPool.BLANK,
            StringPool.BLANK,
            _DATA_VERSION_1,
            _serviceContext);

    _fileVersion =
        DLFileVersionLocalServiceUtil.getFileVersion(
            fileEntry.getFileEntryId(), DLFileEntryConstants.VERSION_DEFAULT);
  }
  protected void updateServiceContext(String expando, long fileEntryTypeId, String metadata)
      throws PortalException {

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

    expandoBridgeAttributes.put(_EXPANDO_ATTRIBUTE_NAME, expando);

    _serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);

    _serviceContext.setAttribute("fileEntryTypeId", fileEntryTypeId);

    if (fileEntryTypeId <= 0) {
      return;
    }

    DLFileEntryType fileEntryType =
        DLFileEntryTypeLocalServiceUtil.getFileEntryType(fileEntryTypeId);

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

    for (DDMStructure ddmStructure : ddmStructures) {
      DDMFormValues ddmFormValues =
          (DDMFormValues)
              _serviceContext.getAttribute(
                  DDMFormValues.class.getName() + ddmStructure.getStructureId());

      for (DDMFormFieldValue ddmFormFieldValue : ddmFormValues.getDDMFormFieldValues()) {

        String fieldType = ddmStructure.getFieldType(ddmFormFieldValue.getName());

        if (fieldType.equals("text")) {
          LocalizedValue localizedValue = new LocalizedValue(LocaleUtil.US);

          localizedValue.addString(LocaleUtil.US, metadata);

          ddmFormFieldValue.setValue(localizedValue);
        }
      }

      _serviceContext.setAttribute(
          DDMFormValues.class.getName() + ddmStructure.getStructureId(), ddmFormValues);
    }
  }
 @Override
 public DLFileEntryType getDLFileEntryType() throws PortalException {
   return DLFileEntryTypeLocalServiceUtil.getFileEntryType(getFileEntryTypeId());
 }