Forums de discussion

Liferay 6.1.0: DynamicQuery/Category issue

Mike Perico, modifié il y a 11 années.

Liferay 6.1.0: DynamicQuery/Category issue

Junior Member Publications: 25 Date d'inscription: 23/08/12 Publications récentes
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 ))
thumbnail
James Falkner, modifié il y a 11 années.

RE: Liferay 6.1.0: DynamicQuery/Category issue

Liferay Legend Publications: 1399 Date d'inscription: 17/09/10 Publications récentes
There isn't one unfortunately. If you want a list of all assets within a given category, use an AssetEntryQuery. i.e.


AssetEntryLocalServiceUtil.getEntries(query);


Where query is an instance of an AssetEntryQuery:


AssetEntryQuery query = new AssetEntryQuery();
query.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.


        long[][] tagArray = new long[tagIds.length][];
        for (int ar = 0; ar < tagIds.length; ar++) {
            tagArray[ar] = new long[] {tagIds[ar]};
        }
        query.setAllTagIdsArray(tagArray);
Mike Perico, modifié il y a 11 années.

RE: Liferay 6.1.0: DynamicQuery/Category issue

Junior Member Publications: 25 Date d'inscription: 23/08/12 Publications récentes
James,
Thanks for the reply, I will give this a try and and see if I can get it to work.

Mike
Mike Perico, modifié il y a 11 années.

RE: Liferay 6.1.0: DynamicQuery/Category issue

Junior Member Publications: 25 Date d'inscription: 23/08/12 Publications récentes
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.
thumbnail
Joseph Toman, modifié il y a 10 années.

RE: Liferay 6.1.0: DynamicQuery/Category issue

Junior Member Publications: 25 Date d'inscription: 28/01/11 Publications récentes
You're hitting the wrong service. You want to call getEntries on AssetEntryLocalService .