Adding Site Template Programmatically


We know that we can create the site template from the control panel, What if we want to create programmatically. So here we go,

1. We are reterving the site template name form the portlet.properties. If you want to know how to create the portlet.properties and reterive follow the below link.


http://liferaytutorial.blogspot.in/2013/03/how-to-read-values-from-properties-file.html

So here i am reterving the site name as site.template from portlet.properties file.
String siteTemplateNames = PortletProps.get("site.template");


In portlet.properties
site.template = SiteTemplate1 , SiteTemplate2



Before adding the site template we are checking weather site template exists for not by dynamicQuery , because we don't have any finder method to check the site template by name. Here i am checking by descrition beacause name will be storing as JSON format in DB.

DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(LayoutSetPrototype.class);
dynamicQuery.add(RestrictionsFactoryUtil.eq("description", templateName+"description"));
List<layoutsetprototype> layoutSetPrototypeList = null;
try {
layoutSetPrototypeList = LayoutSetPrototypeLocalServiceUtil.dynamicQuery(dynamicQuery);
} catch (SystemException e1) {
e1.printStackTrace();
}



We are getting the userId from the PrincipalThreadLocal class

String name = PrincipalThreadLocal.getName();
long userId = GetterUtil.getLong(name);


Also Site template name supports mulitilingual so here we are storing the name with JSON format as language id also,

public Map<locale string> getLocalizationMap(Locale siteLocale, String value) {

Locale[] locales = LanguageUtil.getAvailableLocales();

Map<locale string> map = new HashMap<locale string>();

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

String siteTemplateNames = PortletProps.get("site.template");
if(Validator.isNotNull(siteTemplateNames)) {

String [] siteTemplateName = StringUtil.split(siteTemplateNames, ",");

for(String templateName : siteTemplateName) {

DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(LayoutSetPrototype.class);
dynamicQuery.add(RestrictionsFactoryUtil.eq("description", templateName+"description"));
List<LayoutSetPrototype>layoutSetPrototypeList = null;
try {
layoutSetPrototypeList = LayoutSetPrototypeLocalServiceUtil.dynamicQuery(dynamicQuery);
} catch (SystemException e1) {
e1.printStackTrace();
}

if(Validator.isNotNull(layoutSetPrototypeList) && layoutSetPrototypeList.isEmpty()) {

ServiceContext serviceContext = null;
try {
serviceContext = ServiceContextFactory.getInstance(request);
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}

String name = PrincipalThreadLocal.getName();
long userId = GetterUtil.getLong(name);
User user = null;
try {
user = UserLocalServiceUtil.getUser(userId);
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}

Locale locale = themeDisplay.getLocale();
Map<Locale, String> nameMap = getLocalizationMap(locale, templateName);

String description = templateName+"description";
boolean active = true;
boolean layoutsUpdateable =true;

try {
LayoutSetPrototypeLocalServiceUtil.addLayoutSetPrototype(userId, user.getCompanyId(), nameMap, description, active, layoutsUpdateable, serviceContext);
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}
}

}

}

Comments

  1. Great post! I love how you explained adding site templates programmatically. It's so helpful for creating unique designs with Flowgiri Webflow templates. Thanks! #FlowgiriWebflowTemplates

    flowgiri

    ReplyDelete

Post a Comment

Popular posts from this blog

Theme display in javascript

How to know which liferay version we are using

Viewing the SQL Query in liferay : debugging the SQL Query in Hibernate