Foros de discusión

How to access Custom Metadata values Programattically

Kevin Matthews, modificado hace 7 años.

How to access Custom Metadata values Programattically

Expert Mensajes: 253 Fecha de incorporación: 25/01/16 Mensajes recientes
Hi,
I am using the document and media portlet in my project. I have created two custom meta by creating the a new document type. Now I am writing a webservice to retrieve the documents and the document metadata from liferay database. I am having problems retrieving my custom metadata values. I am only able to retrieve per asset/file the custom metadata names but not the value using DDMSTructcure. Can someone tell me how I can retrieve the custom metadata values ?

I amusing liferay 7.0 CE. Widlfly 10. It seems there is a lot of packages that are remove to access the document API to read Metadata such as the FIeld class, StorageUtil class, DDMCOntent entity . Can someone tell me how can I access the the custom metadata values? Ifwe migrated to Liferay 7.0 to make it easier why is it trying reading metadata values so diifcult.



try{
dlFileEntryType = DLFileEntryTypeLocalServiceUtil.getFileEntryType(fileEntryTypeId);
List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();
for (DDMStructure ddmStructure : ddmStructures) {
//Fields fields = null;
DLFileEntryMetadata dlFileEntryMetadata = DLFileEntryMetadataLocalServiceUtil.fetchFileEntryMetadata(ddmStructure.getStructureId(), dlFileEntryType.getFileEntryTypeId());
Set<String> fieldNames = ddmStructure.getFieldNames();
log.info(fieldNames.size());
for (Iterator<String> iterator = fieldNames.iterator(); iterator.hasNext();) {
String fieldName = (String) iterator.next();
log.info("CUSTOM METADATA INFO =["+fieldName+"]"+"for Artifcat"+filename);


}

}
thumbnail
Sergio González, modificado hace 7 años.

RE: How to access Custom Metadata values Programattically

Expert Mensajes: 301 Fecha de incorporación: 7/01/10 Mensajes recientes
Hey Kevin,

I'm going to explain all the necessary steps from the beginning (although some of them you already got them in your source code but I want to make sure that the explanation is complete and correct):

1. Obtain the file entry type you are interested using the fileEntryTypeId:
DLFileEntryType dlFileEntryType = DLFileEntryTypeLocalServiceUtil.getFileEntryType(fileEntryTypeId);


2. Obtain the ddm structures used by that file entry type
List<ddmstructure> ddmStructures = dlFileEntryType.getDDMStructures();</ddmstructure>


3. Next we need to obtain the file entry metadata for a specific file version and ddm structure. Each file version can have multiple ddm structures, so we need to specify both (file version id and ddm structure id) in order to obtain a specific dl file entry metadata. In the source code that you wrote you used in the second parameter dlFileEntryType.getFileEntryTypeId() but that's wrong, it should be a fileVersionId and not a fileEntryTypeId. In case we want to obtain the information for the latest approved version of fileEntry the code should be like:
DLFileEntryMetadata dlFileEntryMetadata = DLFileEntryMetadataLocalServiceUtil.fetchFileEntryMetadata(ddmStructure.getStructureId(), fileEntry.getFileVersionId());


4. Finally, when you have the dl file entry metadata, you need to use the storage engine to obtain the ddm form values. StorageEngine is an interface that is implemented by OSGi compontes, so the best way of obtaining this component is using Declarative Services to obtain it, something like:


	@Reference(unbind = "-")
	public void setStorageEngine(StorageEngine storageEngine) {
		_storageEngine = storageEngine;
	}


Keep in mind that in order to do this the class where you are doing this needs to be an OSGi component too.

Once you have the StorageEngine, you can obtain the DDMFormValues that will contain all the information you need as follows:

DDMFormValues ddmFormValues = _storageEngine.getDDMFormValues(dlFileEntryMetadata.getDDMStorageId());


With DDMFormValues you should be able to obtain the information using DDMFormFieldValue using:

List<ddmformfieldvalue> ddmFormFieldValues =  ddmFormValues.getDDMFormFieldValues();</ddmformfieldvalue>


I hope this helps you to obtain the metadata information.

Sergio
Kevin Matthews, modificado hace 7 años.

RE: How to access Custom Metadata values Programattically

Expert Mensajes: 253 Fecha de incorporación: 25/01/16 Mensajes recientes
Hi Serigio, thanks for the response very well appreciated. I did get it to work and I forgot to post my solution. Let me show you what I did. I did everything like you did but with the exception of one step which was to inject the StorageEngine but I used torageEngineManagerUtil instead.
Also, I need to know how do i inject storage engine. I tried to put it in my class but it cannot find the package. I tried to declare private StorageEngine _storageEngine but can't find the package. Is there a OSGI jar I need to include?

Also on another note I have another post that has to do with service builder and blade. I try to run the service builder project that comes with liferay-glade project https://github.com/liferay/liferay-blade-samples/tree/master/liferay-gradle and it doesn't populate my tables. I tried to create a blade project using service builder template and it still doesnt generate my tables. I will post this in another thread. Just wanted to give you heads up.

Please see my solution below

try{
type = DLFileEntryTypeLocalServiceUtil.getFileEntryType(fileEntryId);

// if (type.getDDMStructures().size() > 0){

dLMSDddmStructures.addAll(type.getDDMStructures());
DDMFormValues ddmFormValues = null;

for (DDMStructure ddmStructure :dLMSDddmStructures) {
DLFileEntryMetadata fileEntryMetadata = DLFileEntryMetadataLocalServiceUtil.getFileEntryMetadata(ddmStructure.getStructureId(), fileVersion.getFileVersionId());
DDMFormValues vals = StorageEngineManagerUtil.getDDMFormValues(fileEntryMetadata.getDDMStorageId());
List<DDMFormFieldValue> fieldValueList = vals.getDDMFormFieldValues();
for(DDMFormFieldValue fieldVal : fieldValueList){
//log.info("DDFORM Field Name =["+fieldVal.getName()+"]");
DDMFormField fl = fieldVal.getDDMFormField();
//log.info("DDFORM Field Value =["+fieldVal.getValue().getString(locale)+"]");
sortOrder = fieldVal.getValue().getString(locale);


}

}

//log.info(sortOrder);
// }
} catch (Exception ex){

log.error(ex);
}
Jagadeep Kushwaha, modificado hace 7 años.

RE: How to access Custom Metadata values Programattically

New Member Mensaje: 1 Fecha de incorporación: 1/03/17 Mensajes recientes
HI ,

Thanks for the post ,
Using above step we could able to fetch all values of metadata . But we are facing below problem .

If Metadata type is Dropdown then JSON value is .
[{"instanceId":"lloq","name":"documentTypeDrp","value":{"en_US":"[\"Final report\"]"}},

while fetching the value
for (com.liferay.dynamic.data.mapping.storage.DDMFormFieldValue ddmFormFieldValue : ddmFormFieldValues){
ddmFormFieldValue.getValue().getString(locale) ---> we are getting value as ["Final report"] not as Final Report .

}

HOw to get exact value ...
thumbnail
Christophe Cariou, modificado hace 4 años.

RE: How to access Custom Metadata values Programattically

Junior Member Mensajes: 57 Fecha de incorporación: 1/10/07 Mensajes recientes
Hello Sergio,
very clear explanation.
But, how can we set values or add metadata on a fileEntry which has a fileEntryType ?
Regards
thumbnail
Karthik Nainupatruni, modificado hace 4 años.

RE: How to access Custom Metadata values Programattically

Junior Member Mensajes: 28 Fecha de incorporación: 5/05/15 Mensajes recientes
Hi Chrisophe,
You can set meta data fields for service context in  custom document type as shown below.
 ServiceContext serviceContext = new ServiceContext();
                    List<DLFileEntryType> fileEntryTypes;
                            DDMStructure ddmStructure = null;
                            fileEntryTypes = DLFileEntryTypeLocalServiceUtil
                                    .getFileEntryTypes(new long[] { themeDisplay.getScopeGroupId() });
                            _log.info("fileEntryTypes: " + fileEntryTypes);                            ddmStructure = fileEntryTypes.get(0).getDDMStructures().get(0);
                            _log.info("ddmStructure.getStructureId() : " + ddmStructure.getStructureId());
                            _log.info("fileEntrytypeId: "+fileEntryTypes.get(0).getFileEntryTypeId());
                            _log.info("ddmStructure : " + ddmStructure.getDDMFormFields(true).get(0).getName());
                            serviceContext.setAttribute("fileEntryTypeId", fileEntryTypes.get(0).getFileEntryTypeId());
                              serviceContext.setAttribute( 
                              ddmStructure.getStructureId()+ddmStructure.getDDMFormFields(true).get(0).getName(), hdnkeyword);
thumbnail
Christophe Cariou, modificado hace 4 años.

RE: How to access Custom Metadata values Programattically

Junior Member Mensajes: 57 Fecha de incorporación: 1/10/07 Mensajes recientes
Hi Karthik,you're a God !  you save my life :  my team is just working on this feature at the moment, desperate to achieve it.
But hopefully you answered my 7 month old question.
It works !
Thanks a lot​​​​​​​
Christophe