Saturday, August 29, 2015

Fetch/Retrieve Web Content (By Providing Structures and Categories) Programmaticlly in Liferay


Today we will discuss how to Retrieve/Fetch a web content by providing categories and structures in Liferay. Basically we filter Web contents as our Asset Publisher portlet do in Liferay.Before reading this blog you must know how to Create Categories, Structures and Templates. For this you may refer my previous blogs on Web Contents.

For this I already created 3 things:-

1)Categories:- Nokia, Sony and Camera . 
2)Web Contents:- I also created 4 web Contents and associated with Categories as:- 

  • Nokia 1600-->Nokia
  • Nokia Lumia 520--> Nokia and Camera
  • Sony Xperia C --> Sony and Camera
  • Sony Xperia L --> Sony and Camera

3)Structure:- MOBILE_STRUCTURE

So lets start step by step:-





Step 1:-Create Liferay Project and Portlet
Create a Liferay Project in eclipse and then create a portlet in the Project.For theses you may refer Step 1 and Step 2 of my previous blog Here .

Step 2:-Fetch Structure Ids by Structure Names
For filtering on the basis of Structures first we have to find Structure Ids by providing Structure Names as:-

public static long[] getStructureIdByName(List<String> structureNames) {
long[] allStructureIds = new long[structureNames.size()];
try {
List<DDMStructure> ddmStructures = DDMStructureLocalServiceUtil.getDDMStructures(0, DDMStructureLocalServiceUtil.getDDMStructuresCount());
int counter = 0;
for (DDMStructure structure : ddmStructures) {
for (String name : structureNames) {
if (structure.getName(Locale.ENGLISH).equalsIgnoreCase(name)) {
allStructureIds[counter] = structure.getStructureId();
counter++;
continue;
}
}
}
} catch (SystemException e) {
e.printStackTrace();
}
return allStructureIds;
}


Step 3:-Fetch Category Ids by Category Names
For filtering on the basis of Categories, we have to find category Ids by providing Category Names as:-

public static long[] getCategoryIdByName(List<String> categoryName) {
long[] allCategoryIds = null;
List<Long> allCategoriesList = new ArrayList<Long>();
try {
List<AssetCategory> assetCategory = AssetCategoryLocalServiceUtil.getAssetCategories(0, AssetCategoryLocalServiceUtil.getAssetCategoriesCount());
for (AssetCategory asset : assetCategory) {
for (String name : categoryName) {
if (asset.getName().equalsIgnoreCase(name)) {
allCategoriesList.add(asset.getCategoryId());
continue;
}
}
}
} catch (SystemException e) {
e.printStackTrace();
}
if ((allCategoriesList != null) && (allCategoriesList.size() > 0)) {
allCategoryIds = new long[allCategoriesList.size()];
for (int i = 0; i < allCategoriesList.size(); i++) {
allCategoryIds[i] = allCategoriesList.get(i);
}
}
return allCategoryIds;
}
view raw Category.java hosted with ❤ by GitHub


Step 4:-Fetch Asset Entries by Creating Query
Now we have Structure Ids(By Step 1) and Category Id(By Step 2) so create a AssetQuery and get List of Asset Entries by providing both:-

public static List<AssetEntry> getSelectedAssetEntry(List<String> categoryName){
List<AssetEntry>assetEntry = new ArrayList<AssetEntry>();
try {
AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
//assetEntryQuery.setAnyCategoryIds(getCategoryIdByName(categoryName));// Represent Any ie OR
assetEntryQuery.setAllCategoryIds(getCategoryIdByName(categoryName)); // Represent Both ie AND
assetEntryQuery.setClassName(JournalArticle.class.getName());
List<String>structureNames = new ArrayList<String>();
structureNames.add("MOBILE_STRUCTURE");
long [] structureIds= getStructureIdByName(structureNames);
assetEntryQuery.setClassTypeIds(structureIds);
assetEntry = AssetEntryServiceUtil.getEntries(assetEntryQuery);
} catch (Exception exc) {
exc.printStackTrace();
}
return assetEntry;
}
view raw AssetEntry.java hosted with ❤ by GitHub

Explanation:-
Here we create a AssetEntryQuery Object which contain many methods to filter out the result.Ex - setAllCategoryIds which represent OR operation between Categories.







Step 5:-Get Web Contents by AssetEnties
In Step 3 we have List of AssetEntries Now we fetch web contents by Providing AssetEntries as:-

public static List<JournalArticle> getSelectedWebContents(List<AssetEntry> assetEntries) {
List<JournalArticle> journalArticles = new ArrayList<JournalArticle>();
JournalArticleResource journalArticleResource = null;
JournalArticle journalArticle = null;
for (AssetEntry entry : assetEntries) {
try {
journalArticleResource = JournalArticleResourceLocalServiceUtil.getJournalArticleResource(entry.getClassPK());
journalArticle = JournalArticleLocalServiceUtil.getLatestArticle(journalArticleResource.getResourcePrimKey());
} catch (Exception e) {
e.printStackTrace();
}
journalArticles.add(journalArticle);
}
return journalArticles;
}
view raw WebContent.java hosted with ❤ by GitHub

Now by Writing all methods .My Controller become:-

package com.test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portlet.asset.model.AssetCategory;
import com.liferay.portlet.asset.model.AssetEntry;
import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
import com.liferay.portlet.asset.service.AssetEntryServiceUtil;
import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
import com.liferay.portlet.journal.model.JournalArticle;
import com.liferay.portlet.journal.model.JournalArticleResource;
import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class Demo extends MVCPortlet {
@Override
public void doView(RenderRequest renderRequest,RenderResponse renderResponse) throws IOException, PortletException {
List<String>categoryList = new ArrayList<String>();
categoryList.add("Nokia");
categoryList.add("Camera");
List<JournalArticle> journalArticles = getSelectedWebContents(getSelectedAssetEntry(categoryList));
for (JournalArticle journalArticle : journalArticles) {
System.out.println("Title=>"+journalArticle.getTitle(Locale.ENGLISH));
}
super.doView(renderRequest, renderResponse);
}
public static List<JournalArticle> getSelectedWebContents(List<AssetEntry> assetEntries) {
List<JournalArticle> journalArticles = new ArrayList<JournalArticle>();
JournalArticleResource journalArticleResource = null;
JournalArticle journalArticle = null;
for (AssetEntry entry : assetEntries) {
try {
journalArticleResource = JournalArticleResourceLocalServiceUtil.getJournalArticleResource(entry.getClassPK());
journalArticle = JournalArticleLocalServiceUtil.getLatestArticle(journalArticleResource.getResourcePrimKey());
} catch (Exception e) {
e.printStackTrace();
}
journalArticles.add(journalArticle);
}
return journalArticles;
}
public static List<AssetEntry> getSelectedAssetEntry(List<String> categoryName){
List<AssetEntry>assetEntry = new ArrayList<AssetEntry>();
try {
AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
//assetEntryQuery.setAnyCategoryIds(getCategoryIdByName(categoryName));// Represent Any ie OR
assetEntryQuery.setAllCategoryIds(getCategoryIdByName(categoryName)); // Represent Both ie AND
assetEntryQuery.setClassName(JournalArticle.class.getName());
List<String>structureNames = new ArrayList<String>();
structureNames.add("MOBILE_STRUCTURE");
long [] structureIds= getStructureIdByName(structureNames);
assetEntryQuery.setClassTypeIds(structureIds);
assetEntry = AssetEntryServiceUtil.getEntries(assetEntryQuery);
} catch (Exception exc) {
exc.printStackTrace();
}
return assetEntry;
}
public static long[] getCategoryIdByName(List<String> categoryName) {
long[] allCategoryIds = null;
List<Long> allCategoriesList = new ArrayList<Long>();
try {
List<AssetCategory> assetCategory = AssetCategoryLocalServiceUtil.getAssetCategories(0, AssetCategoryLocalServiceUtil.getAssetCategoriesCount());
for (AssetCategory asset : assetCategory) {
for (String name : categoryName) {
if (asset.getName().equalsIgnoreCase(name)) {
allCategoriesList.add(asset.getCategoryId());
continue;
}
}
}
} catch (SystemException e) {
e.printStackTrace();
}
if ((allCategoriesList != null) && (allCategoriesList.size() > 0)) {
allCategoryIds = new long[allCategoriesList.size()];
for (int i = 0; i < allCategoriesList.size(); i++) {
allCategoryIds[i] = allCategoriesList.get(i);
}
}
return allCategoryIds;
}
public static long[] getStructureIdByName(List<String> structureNames) {
long[] allStructureIds = new long[structureNames.size()];
try {
List<DDMStructure> ddmStructures = DDMStructureLocalServiceUtil.getDDMStructures(0, DDMStructureLocalServiceUtil.getDDMStructuresCount());
int counter = 0;
for (DDMStructure structure : ddmStructures) {
for (String name : structureNames) {
if (structure.getName(Locale.ENGLISH)
.equalsIgnoreCase(name)) {
allStructureIds[counter] = structure.getStructureId();
counter++;
continue;
}
}
}
} catch (SystemException e) {
e.printStackTrace();
}
return allStructureIds;
}
}
view raw Demo.java hosted with ❤ by GitHub


Step 6:-Output

1)If input is:-
List<String>categoryList = new ArrayList<String>();
categoryList.add("Nokia");
categoryList.add("Camera");
List<JournalArticle> journalArticles = getSelectedWebContents(getSelectedAssetEntry(categoryList));
for (JournalArticle journalArticle : journalArticles) {
System.out.println("Title=>"+journalArticle.getTitle(Locale.ENGLISH));
}
view raw Output1.java hosted with ❤ by GitHub

Then Output is:-
Title=>Nokia Lumia 520

2)If input is:-
List<String>categoryList = new ArrayList<String>();
categoryList.add("Nokia");
List<JournalArticle> journalArticles = getSelectedWebContents(getSelectedAssetEntry(categoryList));
for (JournalArticle journalArticle : journalArticles) {
System.out.println("Title=>"+journalArticle.getTitle(Locale.ENGLISH));
}
view raw Output2.java hosted with ❤ by GitHub

No comments:

Post a Comment

Total Pageviews

1041817

Number Of Unique Visitor

Free counters!