Foren

How do I call a util function in Velocity which takes an array?

jeff gerbracht, geändert vor 13 Jahren.

How do I call a util function in Velocity which takes an array?

Junior Member Beiträge: 65 Beitrittsdatum: 05.06.08 Neueste Beiträge
I've a velocity template (below) which is using the TagsEntryLocalService to retrieve the entryIDs. I need the entryIDS so I can then call getAssets in TagsAssetLocalService. The goal is to retrieve assets which have the following tags. (Yes I've tried the AssetPublisher but it doesn't work with URL params and the way our virtual host is configured, and it also returns each asset multiple times). The code below illustrates 2 methods to get the entryIDs, .getEntryIds() and getEntry().getEntryId(), I want to use.getEntryIds but it's not working in my 5.1.2 install.

#set( $tags=["gallery","63796"])
#set ($tagsEntryService= $serviceLocator.findService("com.liferay.portlet.tags.service.TagsEntryLocalService"))
#set ($entries=$tagsEntryService.getEntryIds($getterUtil.getLong($companyId),$tags))
#set ($entryid=$tagsEntryService.getEntry($getterUtil.getLong($companyId),$tags.get(0)).getEntryId())
#set ($entryid2=$tagsEntryService.getEntry($getterUtil.getLong($companyId),$tags.get(1)).getEntryId())
#set ( $tagIDs=[])
$tagIDs.add($entryid)
$tagIDs.add($entryid2)
tags=$tags
entries = $entries  
tagIDs=$tagIDs


and here's the output, entries shows the results of .getEntryIds() and tagIDs shows the results of the two .getEntry().getEntryId() calls.

true true tags=[gallery, 63796] entries = $entries tagIDs=[717820, 726893]

ideally the "entries = $entries" should actually read "entries = [717820, 726893]"

Thanks in advance for any insights,
Jeff
thumbnail
jelmer kuperus, geändert vor 13 Jahren.

RE: How do I call a util function in Velocity which takes an array?

Liferay Legend Beiträge: 1191 Beitrittsdatum: 10.03.10 Neueste Beiträge
The problem is that list construct in velocity creates an ArrayList so you cannot pass it to getEntryIds
without converting it to a string array first.

This is sadly is rather hard to do in velocity without an helper object, and i dont think any of the utils that are added to the velocitycontext by liferay can help you with this

This will probably work

$tagsEntryService.getEntryIds($getterUtil.getLong($companyId), $tags.toArray($tags.getClass().forName("java.lang.reflect.Array").getMethod("newInstance", $tags.getClass().forName("java.lang.Class"), $tags.getClass().forName("[I").getComponentType()).invoke(null, $tags.getClass().forName("java.lang.String"), $tags.size()))))

But i find it very hard to recommend this for obvious reasons ;)

Oh btw the first argument is the groupId not the company id
jeff gerbracht, geändert vor 13 Jahren.

RE: How do I call a util function in Velocity which takes an array?

Junior Member Beiträge: 65 Beitrittsdatum: 05.06.08 Neueste Beiträge
In the .java file for TagsEntryLocalService, it's referenced as companyID, v 5.1.2, has this changed???

public long[] getEntryIds(long companyId, java.lang.String[] names)
throws com.liferay.portal.SystemException;

Also according to a blog post from Ray Augé, http://www.liferay.com/web/james.min/blog/-/blogs/session-attributes-from-journal-content-and-velocity#_33_message_5077111

"Arrays are handled automatically in Velocity.

this is how you create an empty array: #set ($arr = []) a non-empty array: #set ($arr2 = [1, 2, 3])

But whenever you think "array", you should really think "ArrayList", which means that manipulation uses the java.util.List interface: $arr2.add(4), $arr2.size(), etc

But where you are passing this to a method call that really requires an array, velocity will autobox down from ArrayList to plain array, as long as the contents of the ArrayList fit the parameter array type of the method."

So I expected this to work. I'd also responded to Ray's Blog comments but haven't heard anything back.
Jeff
Arun Kumar S, geändert vor 11 Jahren.

RE: How do I call a util function in Velocity which takes an array?

Regular Member Beiträge: 182 Beitrittsdatum: 23.06.08 Neueste Beiträge
Hi Friends,

Version : Liferay Version 6.1.20
Server : Glassfish

The problem occurs when we pass the $allTagIds to $assetEntryQuery.setAllTagIds($allTagIds )
Below is my code.


..............
#set($assetEntryQuery = $portal.getClass().forName( "com.liferay.portlet.asset.service.persistence.AssetEntryQuery" ).newInstance()) 
#set($className='com.liferay.portlet.journal.model.JournalArticle')

#set($void = $assetEntryQuery.setClassName( $className ))

#set($allTagIds = [])
#foreach( $assetTag in $tagsAsset )
	#set($void = $allTagIds.add($getterUtil.getLong($assetTag.getTagId())))
#end

#set($void = $assetEntryQuery.setAllTagIds( $allTagIds ))





Output :-
When i print the assetEntryQuery object the allTagIds is empty.


{allCategoryIds=, allTagIds=, anyCategoryIds=, anyTagIds=, classNameIds=10108, classTypeIds=, end=-1, excludeZeroViewCount=false, expirationDate=Fri Jan 04 05:48:11 GMT 2013, groupIds=, linkedAssetEntryId=0, notAllCategoryIds=, notAllTagIds=, notAnyCategoryIds=, notAnyTagIds=, orderByCol1=null, orderByCol2=null, orderByType1=null, orderByType2=null, publishDate=Fri Jan 04 05:48:11 GMT 2013, start=-1, visible=true}



As per the Ray blog "Velocity will autobox down from ArrayList to plain array".
Please help me to solve this.

Thanks in Advance
Arun
thumbnail
archana thakur, geändert vor 11 Jahren.

RE: How do I call a util function in Velocity which takes an array?

Junior Member Beiträge: 27 Beitrittsdatum: 13.03.12 Neueste Beiträge
Hi Arun,
we have implemented that scenario in java to get all the entry for following tags.
set the tags using setAnyTags method of AssetEntryQuery class


 long[] tagIds = new long[]{1001,1002,1100};
AssetEntryQuery query = new AssetEntryQuery();
query.setAnyTagIds(tagIds );
List<assetentry> entries = AssetEntryLocalServiceUtil.getEntries(query);</assetentry>



you can implement this code in Velocity template
Arun Kumar S, geändert vor 11 Jahren.

RE: How do I call a util function in Velocity which takes an array?

Regular Member Beiträge: 182 Beitrittsdatum: 23.06.08 Neueste Beiträge
Dear Archana,

Thanks for your reply.

Its working fine with java code.
When i convert velocity code I am facing problem with ArrayList.

"Velocity will autobox down from ArrayList to plain array". its not appening.

is the any was explicitly convert ArrayList to long array in velocity ?
Thanks,
Arun.
Arun Kumar S, geändert vor 11 Jahren.

RE: How do I call a util function in Velocity which takes an array?

Regular Member Beiträge: 182 Beitrittsdatum: 23.06.08 Neueste Beiträge
Hi Friends,

I found the solution.
The below is the work around



#set($assetEntryQuery = $portal.getClass().forName( "com.liferay.portlet.asset.service.persistence.AssetEntryQuery" ).newInstance()) 

#set($allTagIds = [])
#foreach( $assetTag in $tagsAsset )
	#set($void = $allTagIds.add($getterUtil.getLong($assetTag.getTagId())))
#end

#set($tempArr=[])
#set($tempArr=$allTagIds)
#set($arr=$tempArr.toArray($allTagIds))
#set($longArray=$arrayUtil.toArray($arrayUtil.toLongArray($arr)))
#set($className='com.liferay.portlet.journal.model.JournalArticle')

#set($void = $assetEntryQuery.setAllTagIds( $longArray ))
#set($void = $assetEntryQuery.setClassName( $className ))

<h1>$assetEntryQuery</h1>



Thanks,
Nam Phung, geändert vor 9 Jahren.

RE: How do I call a util function in Velocity which takes an array?

New Member Beiträge: 3 Beitrittsdatum: 11.12.14 Neueste Beiträge
Nice ! Thanks Arun.
thumbnail
Arunjyoti Banik, geändert vor 9 Jahren.

RE: How do I call a util function in Velocity which takes an array?

Junior Member Beiträge: 74 Beitrittsdatum: 26.08.14 Neueste Beiträge
Hi All,

I found this issue has already been resolved but I want to share an opinion. Wont it be easy if we define our array or list object in java file and pass that to velocity using Map??? The vm file would be easy to read then, I believe.

Documentation:: https://www.liferay.com/community/wiki/-/wiki/Main/Custom+Velocity+Variables

Arun