liferaydocumentliferay-velocity

how to show values associate with a custom document in liferay 6.2


I created a document type, and I'm using Asset Publisher to display this types of documents I would like view the values that I associated with my custom document, that I created with the document library. What is the way to do this with velocity?

I found this java code

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());

I try to make this in velocity

#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

but $storageUtil.getFields($dDMStorageId)) is empty


Solution

  • I suggest you try a different approach that avoids the usage of StorageEngineUtil.

    My templates are written in in Freemarker, but I am sure you can translate it quickly to Velocity syntax.

    First function takes instance of DLFileEntry as parameter and returns the fields map:

    <#function getDLFileEntryFieldsMap dlFileEntry>
        <#assign fileVersionId = dlFileEntry.getLatestFileVersion(true).getFileVersionId() />
        <#return dlFileEntry.getFieldsMap(fileVersionId) />
    </#function>
    

    The returned fields map is an instance of Map<String, com.liferay.portlet.dynamicdatamapping.storage.Fields>.

    Second function searches the fields map for a given field:

    <#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>
    

    The function iterates through the structures in the fields map and tries to find the first structure containing the given field.