Friday, March 6, 2020

Checking Category of a Page in Liferay

import com.liferay.asset.kernel.model.AssetCategory;
import com.liferay.asset.kernel.model.AssetEntry;
import com.liferay.asset.kernel.service.AssetCategoryLocalServiceUtil;
import com.liferay.asset.kernel.service.AssetEntryLocalServiceUtil;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.model.Layout;
import com.liferay.portal.kernel.service.LayoutLocalServiceUtil;
import com.liferay.portal.kernel.util.LocaleUtil;

AssetEntry assetentry = null;
List<AssetCategory> categoryList = null;
Locale loc = LocaleUtil.ENGLISH;
//Using a dynamic query to fetch all the categories with the name "Not_Valid_Page"
DynamicQuery query = DynamicQueryFactoryUtil.forClass(AssetCategory.class).add(PropertyFactoryUtil.forName("name").eq("Not_Valid_Page"));
categoryList = AssetCategoryLocalServiceUtil.dynamicQuery(query, 0,1);

for(AssetCategory assetCategory:categoryList) {
    // It will Fetch the Category with name "Not_Valid_Page"
    System.out.println(" Category Name"+assetCategory.getName());
}

//Fetching all the Layouts
for(Layout layout:LayoutLocalServiceUtil.getLayouts(-1, -1)) {

assetentry = AssetEntryLocalServiceUtil.getEntry(Layout.class.getName(),layout.getPrimaryKey());
long assetCategoryId[] = assetentry.getCategoryIds();
List<AssetCategory> assetCategories = assetentry.getCategories();

if(assetCategories.contains(categoryList.get(0))){

//layout contains the detail of the page which contains the category of "Not_Valid_Page"

}
}

No comments:

Liferay DXP JNDI Data Source Cofiguration

 This Blog will help us to learn about the JNDI Data Source Configuration in Liferay DXP. We have tested this with Liferay 7.3 with Tomcat. ...