Showing posts with label site. Show all posts
Showing posts with label site. Show all posts

Wednesday, December 3, 2014

Create site programmatically in Liferay


Create site programmatically in Liferay



This code does creates a site and adds site template to it...

1. It will reaad site name from portal-ext.properties file
2. It will get the site template already created from browser and then add this template to our newly created site.
3. It will get logo from the class path -WEB-INF/mylogo.png
4. It will place logo to our site..


@Override
public void doView(RenderRequest renderRequest,
RenderResponse renderResponse) throws IOException, PortletException {
System.out.println("view calling");

String customSiteName = PropsUtil.get("custom.site.name");
System.out.println("site name == "+customSiteName);

int publicLayoutSetPrototypeId = 0;
int privateLayoutSetPrototypeId =0;

try {
ServiceContext serviceContext =  ServiceContextFactory.getInstance(Group.class.getName(), renderRequest);

Group grp = null;
try {
grp = GroupServiceUtil.getGroup(serviceContext.getThemeDisplay().getCompanyId(), customSiteName);
} catch (Exception e) {

}

if(grp == null){

System.out.println("group created");
grp = GroupServiceUtil.addGroup(GroupConstants.DEFAULT_PARENT_GROUP_ID, GroupConstants.DEFAULT_LIVE_GROUP_ID, customSiteName, customSiteName+"description", GroupConstants.TYPE_SITE_OPEN,
true, GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION,
"/"+customSiteName, true, true, serviceContext);
}

System.out.println("site created.."+grp.getName());



List<LayoutSetPrototype> layoutSetPrototypes=LayoutSetPrototypeServiceUtil.search(serviceContext.getThemeDisplay().getCompanyId(),Boolean.TRUE, null);

for(LayoutSetPrototype layoutSetPrototype : layoutSetPrototypes){

if(layoutSetPrototype.getName("").equals("mysitetemplate")){

publicLayoutSetPrototypeId = (int) layoutSetPrototype.getLayoutSetPrototypeId();
System.out.println("public pages template id :"+publicLayoutSetPrototypeId);
}
if(layoutSetPrototype.getName("").equals("mysitetemplate2")){

privateLayoutSetPrototypeId = (int) layoutSetPrototype.getLayoutSetPrototypeId();
System.out.println("private pages template ID : "+privateLayoutSetPrototypeId);
}
}



MethodKey methodKey  = new MethodKey(SitesUtil.class, "updateLayoutSetPrototypesLinks",Group.class,long.class, long.class,boolean.class,boolean.class);
PortalClassInvoker.invoke(true, methodKey, grp, publicLayoutSetPrototypeId, privateLayoutSetPrototypeId,true,true);

LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(grp.getGroupId(), true);

MethodKey _mergeLayoutSetPrototypeLayoutsMethodKey  = new MethodKey(SitesUtil.class,"mergeLayoutSetPrototypeLayouts",Group.class,LayoutSet.class);

PortalClassInvoker.invoke(true,_mergeLayoutSetPrototypeLayoutsMethodKey,grp, layoutSet);

System.out.println("suuccess");

java.io.File file = new java.io.File(getPortletContext().getRealPath("/WEB-INF/mylogo.png"));

LayoutSetServiceUtil.updateLogo(grp.getGroupId(), false, true, file);




} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}


super.doView(renderRequest, renderResponse);
}