本文主要是介绍liferay中站点的建立及封装一个Util类用于站点模板的引用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
写这篇文章主要是记录一下在项目开发中遇到问题,分析问题,解决问题的过程.
由于项目需求,需要把创建站点,站点模板引用单独从控制面板中拿出来,于是去开始着手源码的阅读,这篇文章重点不在这,所以略去.
首先说一下组织和站点的关系
1.Organization和Group
每创建一个Organization 就会有一个对应的Group
表group_的classPK存的就是organizationId
2.Organization和Site
在liferay中建立了组织后可以为该组织创建站点
组织存在表organization_中,而站点是存在表layoutset中
下面是创建站点并引用站点模板的一个大体步骤:
1.利用OrganizationLocalServiceUtil的addOrganization方法创建一个常规组织,默认建立了一个站点,该方法返回一个Organization对象
Organization organization = OrganizationLocalServiceUtil.addOrganization(userId, parentOrganizationId, name, type, recursable, regionId, countryId, statusId, comments, site, serviceContext);
各项参数说明可以查看api(6.1.1CE),地址: http://docs.liferay.com/portal/6.1/javadocs/com/liferay/portal/service/OrganizationLocalServiceUtil.html
2.建立常规组织以后,默认建立以一个group,接着,为新建立的站点添加模板,这里如果调用LayoutSetUtil类的方法添加的话,新站点可能引用不到,于是需要自己封装一个Util类,出处:http://pastebin.com/hPNrG212,代码如下:
package com.example.util;import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.MethodKey;
import com.liferay.portal.kernel.util.PortalClassInvoker;
import com.liferay.portal.model.Group;
import com.liferay.portal.model.LayoutSet;
import com.liferay.portal.model.LayoutSetPrototype;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.service.LayoutSetLocalServiceUtil;
import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;public class LayoutsetUtil {public static void setupSitesFromSiteTemplate(long groupId,long publicSiteTemplateId, long privateSiteTemplateId)throws PortalException, SystemException {Group group = GroupLocalServiceUtil.getGroup(groupId);if (publicSiteTemplateId != 0)setSiteTemplate(group, publicSiteTemplateId, false);if (privateSiteTemplateId != 0)setSiteTemplate(group, privateSiteTemplateId, true);}public static void setSiteTemplate(Group group, long siteTemplateId,boolean isPrivateLayout) throws PortalException, SystemException {long groupId = group.getGroupId();LayoutSetPrototype prototype = LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototype(siteTemplateId);boolean layoutSetPrototypeLinkEnabled = true;LayoutSetLocalServiceUtil.updateLayoutSetPrototypeLinkEnabled(groupId,isPrivateLayout, layoutSetPrototypeLinkEnabled,prototype.getUuid());try {LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(groupId, isPrivateLayout);mergeLayoutSetProtypeLayouts(group, layoutSet);} catch (Exception e) {e.printStackTrace();}}public static void mergeLayoutSetProtypeLayouts(Group group,LayoutSet layoutSet) throws Exception {MethodKey key = SitesUtilMethodKey("mergeLayoutSetProtypeLayouts",Group.class, LayoutSet.class);invokePortalClassMethod(key, group, layoutSet);}/** copied from* http://www.liferay.com/community/forums/-/message_boards/view_message* /10488983#_19_message_10488983 post by Jelmer Kuperus* * key: key of method to be called, e.g.* com.liferay.portlet.sites.util.SitesUtil arguments: arguments to be* passed to the invoked method returns: result of the invoked method*/private static Object invokePortalClassMethod(MethodKey key,Object... arguments) throws PortalException {try {// noinspection uncheckedreturn PortalClassInvoker.invoke(false, key, arguments);} catch (PortalException e) {throw e;} catch (Exception e) {throw new RuntimeException(e);}}private static final String SITES_UTIL_CLASS_NAME = "com.liferay.portlet.sites.util.SitesUtil";private static MethodKey SitesUtilMethodKey(String methodName,Class<?>... parameterTypes) {return new MethodKey(SITES_UTIL_CLASS_NAME, methodName, parameterTypes);}
}
调用LayoutsetUtil的setupSitesFromSiteTemplate(long groupId,long publicSiteTemplateId, long privateSiteTemplateId)传入相应的参数就可以引用到站点模板了,下面对三个参数做一点说明
1.groupId:新建站点的groupId,可以通过organization.getGroupId()获取
2. publicSiteTemplateId(在公开页面引用):站点模板的id,可以在表layoutsetorototype表中查看layoutsetorototypeid获取,也可以通过api获取
3.privateSiteTemplateId(在私有页面引用):获取方式同 publicSiteTemplateId
当然了,我们还可能为站点设置了自定义字段,我们也可以用api来实现,代码如下:
// 给站点添加自定义字段
siteGroupId = organization.getGroupId();
Group group = GroupLocalServiceUtil.getGroup(organization.getGroupId());
group.getExpandoBridge().setAttribute("appCode", "cg");
于是,我们在控制面板中看到这样的效果:
这篇关于liferay中站点的建立及封装一个Util类用于站点模板的引用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!