Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
jeff gerbracht
How do I call a util function in Velocity which takes an array?
August 6, 2010 8:56 AM
Answer

jeff gerbracht

Rank: Junior Member

Posts: 64

Join Date: June 5, 2008

Recent Posts

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.

 1#set( $tags=["gallery","63796"])
 2#set ($tagsEntryService= $serviceLocator.findService("com.liferay.portlet.tags.service.TagsEntryLocalService"))
 3#set ($entries=$tagsEntryService.getEntryIds($getterUtil.getLong($companyId),$tags))
 4#set ($entryid=$tagsEntryService.getEntry($getterUtil.getLong($companyId),$tags.get(0)).getEntryId())
 5#set ($entryid2=$tagsEntryService.getEntry($getterUtil.getLong($companyId),$tags.get(1)).getEntryId())
 6#set ( $tagIDs=[])
 7$tagIDs.add($entryid)
 8$tagIDs.add($entryid2)
 9tags=$tags
10entries = $entries 
11tagIDs=$tagIDs


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

1true 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
Jelmer Kuperus
RE: How do I call a util function in Velocity which takes an array?
August 6, 2010 2:49 PM
Answer

Jelmer Kuperus

Rank: Liferay Legend

Posts: 1192

Join Date: March 10, 2010

Recent Posts

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
RE: How do I call a util function in Velocity which takes an array?
August 24, 2010 10:17 AM
Answer

jeff gerbracht

Rank: Junior Member

Posts: 64

Join Date: June 5, 2008

Recent Posts

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
RE: How do I call a util function in Velocity which takes an array?
January 3, 2013 10:04 PM
Answer

Arun Kumar S

Rank: Regular Member

Posts: 182

Join Date: June 23, 2008

Recent Posts

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.
 1
 2
 3..............
 4#set($assetEntryQuery = $portal.getClass().forName( "com.liferay.portlet.asset.service.persistence.AssetEntryQuery" ).newInstance())
 5#set($className='com.liferay.portlet.journal.model.JournalArticle')
 6
 7#set($void = $assetEntryQuery.setClassName( $className ))
 8
 9#set($allTagIds = [])
10#foreach( $assetTag in $tagsAsset )
11    #set($void = $allTagIds.add($getterUtil.getLong($assetTag.getTagId())))
12#end
13
14#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
archana thakur
RE: How do I call a util function in Velocity which takes an array?
January 3, 2013 11:21 PM
Answer

archana thakur

Rank: New Member

Posts: 22

Join Date: March 12, 2012

Recent Posts

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


1 long[] tagIds = new long[]{1001,1002,1100};
2AssetEntryQuery query = new AssetEntryQuery();
3query.setAnyTagIds(tagIds );
4List<AssetEntry> entries = AssetEntryLocalServiceUtil.getEntries(query);



you can implement this code in Velocity template
Arun Kumar S
RE: How do I call a util function in Velocity which takes an array?
January 3, 2013 11:48 PM
Answer

Arun Kumar S

Rank: Regular Member

Posts: 182

Join Date: June 23, 2008

Recent Posts

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
RE: How do I call a util function in Velocity which takes an array?
January 4, 2013 12:06 AM
Answer

Arun Kumar S

Rank: Regular Member

Posts: 182

Join Date: June 23, 2008

Recent Posts

Hi Friends,

I found the solution.
The below is the work around

 1
 2
 3#set($assetEntryQuery = $portal.getClass().forName( "com.liferay.portlet.asset.service.persistence.AssetEntryQuery" ).newInstance())
 4
 5#set($allTagIds = [])
 6#foreach( $assetTag in $tagsAsset )
 7    #set($void = $allTagIds.add($getterUtil.getLong($assetTag.getTagId())))
 8#end
 9
10#set($tempArr=[])
11#set($tempArr=$allTagIds)
12#set($arr=$tempArr.toArray($allTagIds))
13#set($longArray=$arrayUtil.toArray($arrayUtil.toLongArray($arr)))
14#set($className='com.liferay.portlet.journal.model.JournalArticle')
15
16#set($void = $assetEntryQuery.setAllTagIds( $longArray ))
17#set($void = $assetEntryQuery.setClassName( $className ))
18
19<h1>$assetEntryQuery</h1>



Thanks,
Nam Phung
RE: How do I call a util function in Velocity which takes an array?
March 30, 2015 8:21 PM
Answer

Nam Phung

Rank: New Member

Posts: 3

Join Date: December 11, 2014

Recent Posts

Nice ! Thanks Arun.
Arunjyoti Banik
RE: How do I call a util function in Velocity which takes an array?
March 30, 2015 8:36 PM
Answer

Arunjyoti Banik

Rank: Junior Member

Posts: 62

Join Date: August 25, 2014

Recent Posts

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