Blog信息 |
blog名称: 日志总数:1304 评论数量:2242 留言数量:5 访问次数:7633379 建立时间:2006年5月29日 |

| |
[Java Open Source]Liferay Portal额外研究(4):修改用户登录后的默认布局和内容 软件技术
lhwork 发表于 2006/9/4 18:07:06 |
Liferay在用户第一次登陆后,都会为其产生一个默认的私有首页Layout。 在Liferay早期版本,可以在系统管理员管理界面中,配置默认的group layout。但是在4.1版本中已经没有这个功能,所以默认情况下,新注册的用户,在登陆后,首先看到的其私有place都是一样的内容,而这些内容都是liferay提供的。 对我们自定的Company来说,这肯定是不合适的,我们需要自己定义用户登录后,默认显示的内容和布局。 后来在Liferay forum中,找到了一篇帖子,给了解决此种问题的解决办法:在portlet-ext.properties中配置一个 default.user.layout.group 属性(自定义的),用于指明默认的layout-group。在ServicePreAction类
的扩展修改 addDefaultLayouts
操作,满足特定的需求应用。这种扩展的方式就是,将指定layout-group拷贝给用户第一默认的,这样就不会再使用liferay默认的了。——
当然这样的方式是比较简单的,可能在系统实施过程中,有比这更加复杂的需求。 新建默认的Community首先,通过新增一个新的Community,命名为User_Default,并新增一个private的page与内容. 配置默认的Community首先在ext项目的 ext-ejb目录下,修改portal-ext.properties文件,增加如下设置: default.user.layout.group=User_Default 然后通过build,将portal-ext.properties移动到 ext项目的 ext\servers\tomcat\webapps\ROOT\WEB-INF\classes 目录下。 构造LayoutCopy类500)this.width=500'>500)this.width=500'>public class LayoutCopy ...{500)this.width=500'> 500)this.width=500'> private static Log _log = LogFactory.getLog(LayoutCopy.class);500)this.width=500'> 500)this.width=500'> private User user;500)this.width=500'>500)this.width=500'> private String ownerId;500)this.width=500'>500)this.width=500'> private Group userGroup;500)this.width=500'>500)this.width=500'> public LayoutCopy(User user) ...{500)this.width=500'> super();500)this.width=500'>500)this.width=500'> this.user = user;500)this.width=500'>500)this.width=500'> this.ownerId = getOwnerId(user.getGroup().getGroupId(), false);500)this.width=500'> this.userGroup = user.getGroup();500)this.width=500'> }500)this.width=500'>500)this.width=500'> public void copyDefaultUserLayout(HttpServletRequest httpReq)500)this.width=500'>500)this.width=500'> throws SystemException, PortalException ...{500)this.width=500'> // set in the portal-ext.properties:500)this.width=500'> // default.user.layout.group=Default User500)this.width=500'> // and create then group/community "Default User" 500)this.width=500'> // CWPPropsUtil.DEFUALT_USER_LAYOUT_GROUP = "default.user.layout.group"500)this.width=500'> Group group = GroupLocalServiceUtil.getGroup(user.getCompanyId(),500)this.width=500'> PropsUtil.get("default.user.layout.group"));500)this.width=500'>500)this.width=500'>500)this.width=500'> try ...{500)this.width=500'> String groupOwnerId = getOwnerId(group.getGroupId(), true);500)this.width=500'> List privateLayouts = LayoutLocalServiceUtil500)this.width=500'> .getLayouts(groupOwnerId);500)this.width=500'>500)this.width=500'>500)this.width=500'> for (Iterator itr = privateLayouts.iterator(); itr.hasNext();) ...{500)this.width=500'> Layout layout = (Layout) itr.next();500)this.width=500'> Layout newLayout = copyLayout(layout);500)this.width=500'> copyPreferences(httpReq, newLayout, layout);500)this.width=500'> }500)this.width=500'>500)this.width=500'> } catch (PortalException e) ...{500)this.width=500'> _log.error("Cannot copy private layouts", e);500)this.width=500'>500)this.width=500'> } catch (Exception e) ...{500)this.width=500'> _log.error("Cannot copy public layouts", e);500)this.width=500'> }500)this.width=500'>500)this.width=500'>500)this.width=500'> try ...{500)this.width=500'> String groupOwnerId = getOwnerId(group.getGroupId(), false);500)this.width=500'> List publicLayouts = LayoutLocalServiceUtil500)this.width=500'> .getLayouts(groupOwnerId);500)this.width=500'>500)this.width=500'>500)this.width=500'> for (Iterator itr = publicLayouts.iterator(); itr.hasNext();) ...{500)this.width=500'> Layout layout = (Layout) itr.next();500)this.width=500'> Layout newLayout = copyLayout(layout);500)this.width=500'> copyPreferences(httpReq, newLayout, layout);500)this.width=500'> }500)this.width=500'>500)this.width=500'> } catch (PortalException e) ...{500)this.width=500'> _log.error("Cannot copy public layouts", e);500)this.width=500'>500)this.width=500'> } catch (Exception e) ...{500)this.width=500'> _log.error("Cannot copy public layouts", e);500)this.width=500'> }500)this.width=500'>500)this.width=500'> }500)this.width=500'>500)this.width=500'>500)this.width=500'> public void resetLayout(HttpServletRequest httpReq) throws SystemException, PortalException ...{500)this.width=500'> String ownerId = getOwnerId(user.getGroup().getGroupId(), false);500)this.width=500'> LayoutLocalServiceUtil.deleteLayouts(ownerId);500)this.width=500'> PortletPreferencesLocalServiceUtil.deletePortletPreferences(ownerId);500)this.width=500'>500)this.width=500'> ownerId = getOwnerId(user.getGroup().getGroupId(), true);500)this.width=500'> LayoutLocalServiceUtil.deleteLayouts(ownerId);500)this.width=500'> PortletPreferencesLocalServiceUtil.deletePortletPreferences(ownerId);500)this.width=500'>500)this.width=500'> copyDefaultUserLayout(httpReq);500)this.width=500'> }500)this.width=500'>500)this.width=500'> public Layout copyLayout(Layout groupDefaultLayout) throws SystemException,500)this.width=500'>500)this.width=500'> PortalException ...{500)this.width=500'> Layout layout = LayoutLocalServiceUtil.addLayout(500)this.width=500'> userGroup.getGroupId(), user.getUserId(), groupDefaultLayout500)this.width=500'> .isPrivateLayout(), groupDefaultLayout500)this.width=500'> .getParentLayoutId(), groupDefaultLayout.getName(user500)this.width=500'> .getLocale()), groupDefaultLayout.getType(),500)this.width=500'> groupDefaultLayout.isHidden(), null);500)this.width=500'>500)this.width=500'> LayoutLocalServiceUtil.updateLayout(layout.getLayoutId(), layout500)this.width=500'> .getOwnerId(), groupDefaultLayout.getTypeSettings());500)this.width=500'> layout = LayoutLocalServiceUtil.updateLookAndFeel(layout.getLayoutId(),500)this.width=500'> layout.getOwnerId(), groupDefaultLayout.getThemeId(),500)this.width=500'> groupDefaultLayout.getColorSchemeId());500)this.width=500'>500)this.width=500'> // layoutMapping.put(groupDefaultLayout.getPrimaryKey(),500)this.width=500'> // layout.getPrimaryKey());500)this.width=500'>500)this.width=500'> return layout;500)this.width=500'> }500)this.width=500'>500)this.width=500'> protected void copyPreferences(HttpServletRequest httpReq, Layout layout,500)this.width=500'>500)this.width=500'> Layout copyLayout) throws Exception ...{500)this.width=500'>500)this.width=500'> String companyId = layout.getCompanyId();500)this.width=500'>500)this.width=500'> LayoutTypePortlet copyLayoutTypePortlet = (LayoutTypePortlet) copyLayout500)this.width=500'> .getLayoutType();500)this.width=500'>500)this.width=500'> List copyPortletIds = copyLayoutTypePortlet.getPortletIds();500)this.width=500'>500)this.width=500'>500)this.width=500'> for (int i = 0; i < copyPortletIds.size(); i++) ...{500)this.width=500'> String copyPortletId = (String) copyPortletIds.get(i);500)this.width=500'>500)this.width=500'> // Copy preference500)this.width=500'>500)this.width=500'> PortletPreferencesPK prefsPK = PortletPreferencesFactory500)this.width=500'> .getPortletPreferencesPK(httpReq, copyPortletId, layout500)this.width=500'> .getPlid());500)this.width=500'>500)this.width=500'> PortletPreferencesLocalServiceUtil.getPreferences(companyId,500)this.width=500'> prefsPK);500)this.width=500'>500)this.width=500'> PortletPreferencesPK copyPrefsPK = PortletPreferencesFactory500)this.width=500'> .getPortletPreferencesPK(httpReq, copyPortletId, copyLayout500)this.width=500'> .getPlid());500)this.width=500'>500)this.width=500'> PortletPreferencesImpl copyPrefsImpl = (PortletPreferencesImpl) PortletPreferencesLocalServiceUtil500)this.width=500'> .getPreferences(companyId, copyPrefsPK);500)this.width=500'>500)this.width=500'> PortletPreferencesLocalServiceUtil.updatePreferences(prefsPK,500)this.width=500'> copyPrefsImpl);500)this.width=500'>500)this.width=500'> // Copy portlet setup500)this.width=500'>500)this.width=500'> prefsPK = new PortletPreferencesPK(copyPortletId, layout500)this.width=500'> .getLayoutId(), layout.getOwnerId());500)this.width=500'>500)this.width=500'> PortletPreferencesLocalServiceUtil.getPreferences(companyId,500)this.width=500'> prefsPK);500)this.width=500'>500)this.width=500'> copyPrefsPK = new PortletPreferencesPK(copyPortletId, copyLayout500)this.width=500'> .getLayoutId(), copyLayout.getOwnerId());500)this.width=500'>500)this.width=500'> copyPrefsImpl = (PortletPreferencesImpl) PortletPreferencesLocalServiceUtil500)this.width=500'> .getPreferences(companyId, copyPrefsPK);500)this.width=500'>500)this.width=500'> PortletPreferencesLocalServiceUtil.updatePreferences(prefsPK,500)this.width=500'> copyPrefsImpl);500)this.width=500'> }500)this.width=500'> }500)this.width=500'>500)this.width=500'>500)this.width=500'> private String getOwnerId(String groupId, boolean privateLayout) ...{500)this.width=500'>500)this.width=500'> if (privateLayout) ...{500)this.width=500'> return Layout.PRIVATE + groupId;500)this.width=500'>500)this.width=500'> } else ...{500)this.width=500'> return Layout.PUBLIC + groupId;500)this.width=500'> }500)this.width=500'> }500)this.width=500'>500)this.width=500'>} 修改com.liferay.portal.events.ServicePreAction类 是在 ext工程的 ext-ejb/src下修改,新建com.liferay.portal.events包,并把原始的ServicePreAction类拷贝至此,然后修改,修改后通过ext-ejb下的build.xml编译部署。 在ServicePreAction类中新增一个addDefaultLayouts方法: protected void addDefaultLayouts(HttpServletRequest httpReq, User user) throws PortalException, SystemException { if (user.hasPrivateLayouts()) { return; } (new LayoutCopy(user)).copyDefaultUserLayout(httpReq); } 然后,修改run方法中对原始addDefaultLayouts方法的调用方法: if (layoutsRequired) { String user_layout_group = PropsUtil.get("default.user.layout.group"); if(user_layout_group==null || user_layout_group.length()==0){ addDefaultLayouts(user); }else{ addDefaultLayouts(req, user); } } |
|
|