Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Mike Perico
Liferay 6.1.0: DynamicQuery/Category issue
September 21, 2012 12:35 PM
Answer

Mike Perico

Rank: New Member

Posts: 19

Join Date: August 23, 2012

Recent Posts

I am trying to build a DynamicQuery in a velocity template and I am having a problem figuring out what classes to use for the AssetEntries_AssetCategories join table.
Can anyone help with this?



## Asset Category Query HERE
#set ( $AssetCategoryLocalService = $serviceLocator.findService( "com.liferay.portlet.asset.service.AssetCategoryLocalService" ))
#set ( $acClass = $portal.getClass().forName( "com.liferay.portlet.asset.model.AssetCategory" ))
#set ( $q1 = $dqfu.forClass( $acClass ))
#set ( $catNameCriteria = $rfu.eq( "name", "NU:Finance"))
#set ( $V = $q1.add( $catNameCriteria ))
#set ( $V = $q1.setProjection( $pfu.property("categoryId")))
#set ( $CatListDQ = $AssetCategoryLocalService.dynamicQuery( $q1 ))

## Asset Entry To Asset Category Query HERE
#set ( $AssetEntry_AssetCategoryService = $serviceLocator.findService( "com.liferay.portlet.asset.service.??" ))
## WHAT CLASS SHOULD THIS BE ^^^^^
#set ( $aeacClass = $portal.getClass().forName( "com.liferay.portlet.asset.model.??" ))
## WHAT CLASS IS THIS ^^^^^
#set ( $q2 = $dqfu.forClass( $aeacClass ))
#set ( $aeacCriteria = $rfu.in( "categoryId", $CatListDQ))
#set ( $V = $q2.add( $aeacCriteria ))
#set ( $V = $q2.setProjection( $pfu.property("entryId")))
#set ( $aeacDQ = $AssetCategoryLocalService.dynamicQuery( $q2 ))

## Asset Entry Query HERE
#set ( $AssetEntryLocalService = $serviceLocator.findService( "com.liferay.portlet.asset.service.AssetEntryLocalService" ))
#set ( $aeClass = $portal.getClass().forName( "com.liferay.portlet.asset.model.AssetEntry" ))
#set ( $q3 = $dqfu.forClass( $aeClass ))
#set ( $aeCriteria = $rfu.in( "entryId", $aeacDQ))
#set ( $V = $q3.add( $aeCriteria ))
#set ( $V = $q3.setProjection( $pfu.property("entryId")))
#set ( $AssetEntryListDQ = $AssetEntryLocalService.dynamicQuery( $q3 ))
James Falkner
RE: Liferay 6.1.0: DynamicQuery/Category issue
September 21, 2012 1:16 PM
Answer

James Falkner

Community Moderator

Rank: Liferay Legend

Posts: 1406

Join Date: September 17, 2010

Recent Posts

There isn't one unfortunately. If you want a list of all assets within a given category, use an AssetEntryQuery. i.e.

1
2AssetEntryLocalServiceUtil.getEntries(query);


Where query is an instance of an AssetEntryQuery:

1
2AssetEntryQuery query = new AssetEntryQuery();
3query.setAllCategoryIds(theIdsYouFoundViaYourFirstDQ);


Be sure to setGroupIds() too to avoid getting assets from other groups (sites, organizations).

Also watch out for a bug I believe was introduced in 6.1 GA2 as a result of LPS-26579. It was turning my "setAllTagIds()" into the equivalent of setAnyTagIds() (i.e. it was making an OR when it should have been an AND). The workaround for me was to construct an array of arrays of the tags when passing to setAllTagIds(). I don't know if this affects categories, but it might.

1
2        long[][] tagArray = new long[tagIds.length][];
3        for (int ar = 0; ar < tagIds.length; ar++) {
4            tagArray[ar] = new long[] {tagIds[ar]};
5        }
6        query.setAllTagIdsArray(tagArray);
Mike Perico
RE: Liferay 6.1.0: DynamicQuery/Category issue
September 24, 2012 6:41 AM
Answer

Mike Perico

Rank: New Member

Posts: 19

Join Date: August 23, 2012

Recent Posts

James,
Thanks for the reply, I will give this a try and and see if I can get it to work.

Mike
Mike Perico
RE: Liferay 6.1.0: DynamicQuery/Category issue
September 24, 2012 12:40 PM
Answer

Mike Perico

Rank: New Member

Posts: 19

Join Date: August 23, 2012

Recent Posts

James (or any one else that knows the answer)

I am still having a problem. I am trying to do this all in a velocity template and I think that is where most of the issues are stemming from, but this is what I have:

## Asset Category Query HERE
#set ( $AssetCategoryLocalService = $serviceLocator.findService( "com.liferay.portlet.asset.service.AssetCategoryLocalService" ))
#set ( $acClass = $portal.getClass().forName( "com.liferay.portlet.asset.model.AssetCategory" ))
#set ( $q1 = $dqfu.forClass( $acClass ))
#set ( $catNameCriteria = $rfu.eq( "name", "NU:Finance"))
#set ( $V = $q1.add( $catNameCriteria ))
#set ( $V = $q1.setProjection( $pfu.property("categoryId")))
#set ( $CatListDQ = $AssetCategoryLocalService.dynamicQuery( $q1 ))
CategoryCount: $CatListDQ.size() <br>

#set( $catArray = $getterUtil.getLong( $CatListDQ.get(0) ) )

#set ( $assetEntryQuery = $portal.getClass().forName( "com.liferay.portlet.asset.service.persistence.AssetEntryQuery" ).newInstance())
#set ( $V = $assetEntryQuery.setAllCategoryIds( $catArray ))
$assetEntryQuery <br>
$assetEntryQuery.getAllCategoryIds()<br>


#set ( $acsUtil = $portal.getClass().forName( "com.liferay.portlet.asset.service.AssetCategoryServiceUtil" ))
#set ( $AssetEntryList = $acsUtil.getEntries( $assetEntryQuery ))
AssetCount: $AssetEntryList.size() <br>

#foreach( $asset in $AssetEntryList )
$asset.getTitle() </br>
#end

And my output is:

CategoryCount: 1
Object={allCategoryIds=16495, allTagIds, anyCategoryIds, anyTagIds, classNameIds, classTypeIds, end-1, excludeZeroViewCountfalse, expirationDateMon Sep 24 17:48:07 GMT 2012, groupIds, linkedAssetEntryId0, notAllCategoryIds, notAllTagIds, notAnyCategoryIds, notAnyTagIds, orderByCol1null, orderByCol2null, orderByType1null, orderByType2null, publishDateMon Sep 24 17:48:07 GMT 2012, start-1, visibletrue}
Method=[J@3191ed44
AssetCount: $AssetEntryList.size()

So as you can see, I am setting the CategoryIds, but it is not giving me back any Asset Ids for that category, which I know exist by looking in the DB. I am also baffled as to why just after setting the category ids, when I go $assetEntryQuery.getAllCategoryIds()<br> It is not printing them back out to me. Velocity syntax error? But by printing the entire object you can see the value is set. I don't understand the problem.
Joseph Toman
RE: Liferay 6.1.0: DynamicQuery/Category issue
July 16, 2013 9:33 AM
Answer

Joseph Toman

Rank: New Member

Posts: 23

Join Date: January 28, 2011

Recent Posts

You're hitting the wrong service. You want to call getEntries on AssetEntryLocalService .