@Override
  public String getTypeName(Locale locale, long subtypeId) {
    try {
      DLFileEntryType dlFileEntryType = DLFileEntryTypeLocalServiceUtil.getFileEntryType(subtypeId);

      return dlFileEntryType.getName(locale);
    } catch (Exception e) {
      return super.getTypeName(locale, subtypeId);
    }
  }
  @Before
  public void setUp() throws Exception {
    SimpleAction simpleAction = new AddDefaultDocumentLibraryStructuresAction();

    String companyIdString = String.valueOf(TestPropsValues.getCompanyId());

    simpleAction.run(new String[] {companyIdString});

    _group = GroupTestUtil.addGroup();

    _folder =
        DLAppLocalServiceUtil.addFolder(
            TestPropsValues.getUserId(),
            _group.getGroupId(),
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            "Folder A",
            "",
            ServiceContextTestUtil.getServiceContext(_group.getGroupId()));

    _subfolder =
        DLAppLocalServiceUtil.addFolder(
            TestPropsValues.getUserId(),
            _group.getGroupId(),
            _folder.getFolderId(),
            "SubFolder AA",
            "",
            ServiceContextTestUtil.getServiceContext(_group.getGroupId()));

    _basicDocumentDLFileEntryType =
        DLFileEntryTypeLocalServiceUtil.getFileEntryType(
            DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT);

    _dlFileEntryTypes =
        DLFileEntryTypeLocalServiceUtil.getFileEntryTypes(
            PortalUtil.getCurrentAndAncestorSiteGroupIds(_group.getGroupId()));

    for (DLFileEntryType dlFileEntryType : _dlFileEntryTypes) {
      String name = dlFileEntryType.getName(LocaleUtil.getSiteDefault());

      if (name.equals(DLFileEntryTypeConstants.NAME_CONTRACT)) {
        _contractDLFileEntryType = dlFileEntryType;
      } else if (name.equals(DLFileEntryTypeConstants.NAME_MARKETING_BANNER)) {

        _marketingBannerDLFileEntryType = dlFileEntryType;
      }
    }
  }
  @Test
  public void testLocalizedSiteUpdateFileEntryType() throws Exception {
    Group group = GroupTestUtil.addGroup();

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId());

    Locale locale = LocaleThreadLocal.getSiteDefaultLocale();

    try {
      LocaleThreadLocal.setSiteDefaultLocale(LocaleUtil.SPAIN);

      String name = RandomTestUtil.randomString();
      String description = RandomTestUtil.randomString();
      DDMStructure ddmStructure =
          DDMStructureTestUtil.addStructure(
              DLFileEntry.class.getName(), new Locale[] {LocaleUtil.SPAIN}, LocaleUtil.SPAIN);

      DLFileEntryType dlFileEntryType =
          DLFileEntryTypeLocalServiceUtil.addFileEntryType(
              TestPropsValues.getUserId(),
              group.getGroupId(),
              name,
              description,
              new long[] {ddmStructure.getStructureId()},
              serviceContext);

      name = RandomTestUtil.randomString();
      description = RandomTestUtil.randomString();

      DLFileEntryTypeLocalServiceUtil.updateFileEntryType(
          TestPropsValues.getUserId(),
          dlFileEntryType.getFileEntryTypeId(),
          name,
          description,
          new long[] {ddmStructure.getStructureId()},
          serviceContext);

      dlFileEntryType =
          DLFileEntryTypeLocalServiceUtil.getFileEntryType(dlFileEntryType.getFileEntryTypeId());

      Assert.assertEquals(name, dlFileEntryType.getName(LocaleUtil.US, true));
      Assert.assertEquals(description, dlFileEntryType.getDescription(LocaleUtil.US, true));
    } finally {
      LocaleThreadLocal.setSiteDefaultLocale(locale);
    }
  }
  protected HashMap<String, Fields> getFieldsMap(
      ServiceContext serviceContext, long fileEntryTypeId) throws PortalException, SystemException {

    HashMap<String, Fields> fieldsMap = new HashMap<String, Fields>();

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

    DLFileEntryType fileEntryType =
        DLFileEntryTypeLocalServiceUtil.getFileEntryType(fileEntryTypeId);

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

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

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

      Fields fields =
          (Fields)
              serviceContext.getAttribute(Fields.class.getName() + ddmStructure.getStructureId());

      if (fields == null) {
        fields = new Fields();

        for (String name : fieldNames) {
          Field field = new Field();

          field.setName(name);

          String value = ParamUtil.getString(serviceContext, namespace + name);

          field.setValue(value);

          fields.put(field);
        }
      }

      fieldsMap.put(ddmStructure.getStructureKey(), fields);
    }

    return fieldsMap;
  }
  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) {
      Fields fields =
          (Fields)
              _serviceContext.getAttribute(Fields.class.getName() + ddmStructure.getStructureId());

      for (Field field : fields) {
        String type = field.getType();

        if (!field.isPrivate() && type.equals("text")) {
          field.setValue(metadata);
        }
      }

      _serviceContext.setAttribute(Fields.class.getName() + ddmStructure.getStructureId(), fields);
    }
  }
  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;
  }