Adding Page Template in liferay Programmatically
We know that we can create the page template from the control panel, What if we want to create programmatically. So here we go,
1. We are reterving the page template name form the portlet.properties. If you want to know how to create the portlet.properties and retrieve follow the below link.
http://liferaytutorial.blogspot.in/2013/03/how-to-read-values-from-properties-file.html
So here i am retrieving the page template name as page.template from portlet.properties file.
String pageTemplateNames = PortletProps.get("page.template");
In Portlet. properties
page.template = Home,Test
Before adding the page template we are checking weather page template exists or not by dynamicQuery , because we don't have any finder method to check the page template by name. Here i am checking by descrition beacause name will be storing as JSON format in DB.
List<LayoutPrototype> layoutProtoTypeList = null;
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(LayoutPrototype.class);
dynamicQuery.add(RestrictionsFactoryUtil.eq("description", pageTemplate+"description"));
try {
layoutProtoTypeList = LayoutPrototypeLocalServiceUtil.dynamicQuery(dynamicQuery);
} catch (SystemException e) {
e.printStackTrace();
}
Also Page template name supports mulitilingual so here we are storing the name with JSON format as language id also,
public Map
Locale[] locales = LanguageUtil.getAvailableLocales();
Map
for (Locale locale : locales) {
if(locale.equals(siteLocale)) {
map.put(locale,value);
} else {
map.put(locale,StringPool.BLANK);
}
}
return map;
}
Here is the full code as ,
List<LayoutPrototype> layoutProtoTypeList = null;
String pageTemplateNames = PortletProps.get("page.template");
String pageTemplates [] = StringUtil.split(pageTemplateNames, ",");
for(String pageTemplate : pageTemplates) {
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(LayoutPrototype.class);
dynamicQuery.add(RestrictionsFactoryUtil.eq("description", pageTemplate+"description"));
try {
layoutProtoTypeList = LayoutPrototypeLocalServiceUtil.dynamicQuery(dynamicQuery);
} catch (SystemException e) {
e.printStackTrace();
}
if(Validator.isNotNull(layoutProtoTypeList) && layoutProtoTypeList.isEmpty()) {
boolean active = true;
String description = pageTemplate+"description";
Map
Locale siteLocale = themeDisplay.getLocale();
nameMap = getLocalizationMap(siteLocale,pageTemplate);
ServiceContext serviceContext = null;
try {
serviceContext = ServiceContextFactory.getInstance(request);
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}
try {
LayoutPrototypeLocalServiceUtil.addLayoutPrototype(themeDisplay.getUserId(), themeDisplay.getCompanyId(), nameMap, description, active,
serviceContext);
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}
}
}
Comments
Post a Comment