如何在 liferay 6.2 中显示与自定义文档关联的值
how to show values associate with a custom document in liferay 6.2
我创建了一个文档类型,我正在使用 Asset Publisher 来显示这种类型的文档
我想查看与我的自定义文档相关联的值,这些值是我使用文档库创建的。
有什么方法可以提高速度?
我找到了这个 java 代码
long classPK = GetterUtil.getLong((String) workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK))
long fileEntryTypeId = DLFileVersionLocalServiceUtil.getFileVersion(classPK).getFileEntry().getFileEntryTypeId()
DLFileEntryType dlFileEntryType = DLFileEntryTypeLocalServiceUtil.getFileEntryType(fileEntryTypeId);
List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();
DDMStructure ddmStructure = ddmStructures.get(0);
DLFileEntryMetadata dlFileEntryMetadata = DLFileEntryMetadataLocalServiceUtil.getFileEntryMetadata(ddmStructure.etStructureId(), classPK);
Fields fields = StorageEngineUtil.getFields(dlFileEntryMetadata.getDDMStorageId());
String value = GetterUtil.getString(fields.get("radio6255").getValue());
我试着用速度来做这个
#set($actualDoc = $curEntry.get(0))
#set($dlFileUtil = $serviceLocator.findService("com.liferay.portlet.documentlibrary.service.DLFileEntryLocalService"))
#set($metadata2 = $serviceLocator.findService("com.liferay.portlet.documentlibrary.service.DLFileEntryMetadataLocalService"))
#set($type = $serviceLocator.findService("com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalService"))
#set($gid = $getterUtil.getLong($request.get("theme-display").get("scope-group-id")))
#set($fileEntry = $dlFileUtil.getFileEntryByUuidAndGroupId($actualDoc.classUuid, $actualDoc.groupId))
#set($fileEntryTypeId = $fileEntry.getFileEntryTypeId())
#set ($dlFileEntryType = $type.getFileEntryType($fileEntryTypeId))
#set ($ddmStructures = $dlFileEntryType.getDDMStructures())
#set ($ddmStructure = $ddmStructures.get(0))
#set($dlFileEntryMetadata = $metadata2.getFileEntryMetadata($ddmStructure.getStructureId(), $fileEntry.getLatestFileVersion(true).getFileVersionId()))
##set ($storageUtil = $portal.getClass().forName("com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil").newInstance())
#set($storageUtil = $serviceLocator.findService("com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil"))
#set($dDMStorageId = $dlFileEntryMetadata.getDDMStorageId())
#set($fields = $storageUtil.getFields($dDMStorageId))
<br> fields> $fields
但是 $storageUtil.getFields($dDMStorageId))
是空的
我建议您尝试一种避免使用 StorageEngineUtil
的不同方法。
我的模板是用 Freemarker 编写的,但我相信您可以快速将其转换为 Velocity 语法。
第一个函数将 DLFileEntry
的实例作为参数,returns 字段映射:
<#function getDLFileEntryFieldsMap dlFileEntry>
<#assign fileVersionId = dlFileEntry.getLatestFileVersion(true).getFileVersionId() />
<#return dlFileEntry.getFieldsMap(fileVersionId) />
</#function>
返回的字段映射是 Map<String, com.liferay.portlet.dynamicdatamapping.storage.Fields>
的一个实例。
第二个函数在字段映射中搜索给定字段:
<#function getDLFileEntryFieldValue fieldsMap fieldName>
<#list fieldsMap?keys as structureKey>
<#list fieldsMap[structureKey].iterator() as field>
<#if field.getName() = fieldName>
<#return field.getValue()>
</#if>
</#list>
</#list>
<#return "">
</#function>
该函数遍历字段映射中的结构并尝试找到包含给定字段的第一个结构。
我已经实现了使用文档操作的portlet...也许我的部分源代码可以帮助您解决这个问题。
要收集文档的所有结构:
public Set<DDMStructure> findStructureOfEntry(DLFileEntry entry) {
LOGGER.log(Level.INFO, "Finding file entry structures.");
Set<DDMStructure> dLMSDddmStructures = new HashSet<DDMStructure>();
DLFileEntryType type = null;
try {
type = DLFileEntryTypeLocalServiceUtil.getFileEntryType(entry
.getFileEntryTypeId());
} catch (PortalException | SystemException e1) {
LOGGER.log(Level.SEVERE,
"Exception while getting DLFileEntry TYPE." + e1);
e1.printStackTrace();
}
try {
dLMSDddmStructures.addAll(type.getDDMStructures());
} catch (SystemException e) {
LOGGER.log(Level.SEVERE,
"Exception while getting DLFileEntry structures." + e);
e.printStackTrace();
}
LOGGER.log(Level.INFO, "Success.");
return dLMSDddmStructures;
}
现在从结构中获取字段:
Field field;
DLFileEntryMetadata dlFileEntryMetadata = null;
Fields fields = null;
try {
dlFileEntryMetadata = DLFileEntryMetadataLocalServiceUtil
.getFileEntryMetadata(structure.getStructureId(), entry
.getFileVersion().getFileVersionId());
} catch (PortalException | SystemException e) {
LOGGER.log(Level.SEVERE,
"Exception while getting DLFileEntry metadata sets."
+ e);
e.printStackTrace();
}
try {
fields = StorageEngineUtil.getFields(dlFileEntryMetadata
.getDDMStorageId());
} catch (StorageException e) {
LOGGER.log(Level.SEVERE,
"Exception while getting fields of DLFileEntry DDMStructures."
+ e);
e.printStackTrace();
}
然后您可以通过名称获取您想要的任何字段:
field = fields.get(name);
我认为您的 getFileEntryMetadata 方法中的参数有误...这就是为什么它是空的。
我能够使用此 Freemarker 模板获取自定义文档中自定义字段的值:
<#assign fileEntry = dlFileEntryService.getDLFileEntryByUuidAndCompanyId(entry.getClassUuid() , entry.getCompanyId() ) />
<#assign fileEntryTypeId = fileEntry.getFileEntryTypeId() />
<#if fileEntryTypeId gt 0 >
<#assign fileEntryType = dlFileEntryTypeService.getDLFileEntryType(fileEntryTypeId) />
<#assign dlFileVersion = dlFileVersionService.getLatestFileVersion(fileEntry.getUserId(), fileEntry.getFileEntryId()) />
<#assign fieldsMap = fileEntry.getFieldsMap(dlFileVersion.getFileVersionId()) />
<#list fieldsMap?keys as structureKey>
<#list fieldsMap[structureKey].iterator() as field>
<#if field.getName() == 'nameOfYourCustomField'>
<#assign nameOfYourCustomField = field.getValue() />
</#if>
</#list>
</#list>
<#-- Print the value of your custom field -->
${nameOfYourCustomField}
</#if>
我创建了一个文档类型,我正在使用 Asset Publisher 来显示这种类型的文档 我想查看与我的自定义文档相关联的值,这些值是我使用文档库创建的。 有什么方法可以提高速度?
我找到了这个 java 代码
long classPK = GetterUtil.getLong((String) workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK))
long fileEntryTypeId = DLFileVersionLocalServiceUtil.getFileVersion(classPK).getFileEntry().getFileEntryTypeId()
DLFileEntryType dlFileEntryType = DLFileEntryTypeLocalServiceUtil.getFileEntryType(fileEntryTypeId);
List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();
DDMStructure ddmStructure = ddmStructures.get(0);
DLFileEntryMetadata dlFileEntryMetadata = DLFileEntryMetadataLocalServiceUtil.getFileEntryMetadata(ddmStructure.etStructureId(), classPK);
Fields fields = StorageEngineUtil.getFields(dlFileEntryMetadata.getDDMStorageId());
String value = GetterUtil.getString(fields.get("radio6255").getValue());
我试着用速度来做这个
#set($actualDoc = $curEntry.get(0))
#set($dlFileUtil = $serviceLocator.findService("com.liferay.portlet.documentlibrary.service.DLFileEntryLocalService"))
#set($metadata2 = $serviceLocator.findService("com.liferay.portlet.documentlibrary.service.DLFileEntryMetadataLocalService"))
#set($type = $serviceLocator.findService("com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalService"))
#set($gid = $getterUtil.getLong($request.get("theme-display").get("scope-group-id")))
#set($fileEntry = $dlFileUtil.getFileEntryByUuidAndGroupId($actualDoc.classUuid, $actualDoc.groupId))
#set($fileEntryTypeId = $fileEntry.getFileEntryTypeId())
#set ($dlFileEntryType = $type.getFileEntryType($fileEntryTypeId))
#set ($ddmStructures = $dlFileEntryType.getDDMStructures())
#set ($ddmStructure = $ddmStructures.get(0))
#set($dlFileEntryMetadata = $metadata2.getFileEntryMetadata($ddmStructure.getStructureId(), $fileEntry.getLatestFileVersion(true).getFileVersionId()))
##set ($storageUtil = $portal.getClass().forName("com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil").newInstance())
#set($storageUtil = $serviceLocator.findService("com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil"))
#set($dDMStorageId = $dlFileEntryMetadata.getDDMStorageId())
#set($fields = $storageUtil.getFields($dDMStorageId))
<br> fields> $fields
但是 $storageUtil.getFields($dDMStorageId))
是空的
我建议您尝试一种避免使用 StorageEngineUtil
的不同方法。
我的模板是用 Freemarker 编写的,但我相信您可以快速将其转换为 Velocity 语法。
第一个函数将 DLFileEntry
的实例作为参数,returns 字段映射:
<#function getDLFileEntryFieldsMap dlFileEntry>
<#assign fileVersionId = dlFileEntry.getLatestFileVersion(true).getFileVersionId() />
<#return dlFileEntry.getFieldsMap(fileVersionId) />
</#function>
返回的字段映射是 Map<String, com.liferay.portlet.dynamicdatamapping.storage.Fields>
的一个实例。
第二个函数在字段映射中搜索给定字段:
<#function getDLFileEntryFieldValue fieldsMap fieldName>
<#list fieldsMap?keys as structureKey>
<#list fieldsMap[structureKey].iterator() as field>
<#if field.getName() = fieldName>
<#return field.getValue()>
</#if>
</#list>
</#list>
<#return "">
</#function>
该函数遍历字段映射中的结构并尝试找到包含给定字段的第一个结构。
我已经实现了使用文档操作的portlet...也许我的部分源代码可以帮助您解决这个问题。
要收集文档的所有结构:
public Set<DDMStructure> findStructureOfEntry(DLFileEntry entry) {
LOGGER.log(Level.INFO, "Finding file entry structures.");
Set<DDMStructure> dLMSDddmStructures = new HashSet<DDMStructure>();
DLFileEntryType type = null;
try {
type = DLFileEntryTypeLocalServiceUtil.getFileEntryType(entry
.getFileEntryTypeId());
} catch (PortalException | SystemException e1) {
LOGGER.log(Level.SEVERE,
"Exception while getting DLFileEntry TYPE." + e1);
e1.printStackTrace();
}
try {
dLMSDddmStructures.addAll(type.getDDMStructures());
} catch (SystemException e) {
LOGGER.log(Level.SEVERE,
"Exception while getting DLFileEntry structures." + e);
e.printStackTrace();
}
LOGGER.log(Level.INFO, "Success.");
return dLMSDddmStructures;
}
现在从结构中获取字段:
Field field;
DLFileEntryMetadata dlFileEntryMetadata = null;
Fields fields = null;
try {
dlFileEntryMetadata = DLFileEntryMetadataLocalServiceUtil
.getFileEntryMetadata(structure.getStructureId(), entry
.getFileVersion().getFileVersionId());
} catch (PortalException | SystemException e) {
LOGGER.log(Level.SEVERE,
"Exception while getting DLFileEntry metadata sets."
+ e);
e.printStackTrace();
}
try {
fields = StorageEngineUtil.getFields(dlFileEntryMetadata
.getDDMStorageId());
} catch (StorageException e) {
LOGGER.log(Level.SEVERE,
"Exception while getting fields of DLFileEntry DDMStructures."
+ e);
e.printStackTrace();
}
然后您可以通过名称获取您想要的任何字段:
field = fields.get(name);
我认为您的 getFileEntryMetadata 方法中的参数有误...这就是为什么它是空的。
我能够使用此 Freemarker 模板获取自定义文档中自定义字段的值:
<#assign fileEntry = dlFileEntryService.getDLFileEntryByUuidAndCompanyId(entry.getClassUuid() , entry.getCompanyId() ) />
<#assign fileEntryTypeId = fileEntry.getFileEntryTypeId() />
<#if fileEntryTypeId gt 0 >
<#assign fileEntryType = dlFileEntryTypeService.getDLFileEntryType(fileEntryTypeId) />
<#assign dlFileVersion = dlFileVersionService.getLatestFileVersion(fileEntry.getUserId(), fileEntry.getFileEntryId()) />
<#assign fieldsMap = fileEntry.getFieldsMap(dlFileVersion.getFileVersionId()) />
<#list fieldsMap?keys as structureKey>
<#list fieldsMap[structureKey].iterator() as field>
<#if field.getName() == 'nameOfYourCustomField'>
<#assign nameOfYourCustomField = field.getValue() />
</#if>
</#list>
</#list>
<#-- Print the value of your custom field -->
${nameOfYourCustomField}
</#if>