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

rafaelc
  • 57,686
  • 15
  • 58
  • 82
user60108
  • 3,270
  • 2
  • 27
  • 43

3 Answers3

2

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.

Tomas Pinos
  • 2,812
  • 14
  • 22
2

I have implemented portlet operating with documents... Maybe parts of my source code will help you to solve this problem.

To collect all structures of document:

    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;

}

Now getting Fields from Structures:

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

and then you can get whatever field you want to by it's name:

field = fields.get(name);

I think you have wrong parameters in your getFileEntryMetadata method... that's why it's empty then.

Matej Šípka
  • 190
  • 4
  • 24
1

I was able to get the value the value of a custom field in a custom document using this Freemarker template:

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