在实际的BPM集成开发过程中,Liferay和Activiti这两个异构的系统之间,用户、组的同步需求非常重要,用来实现签收组的概念,比如指定签收组、会签、抢签都需要用到。
Activiti可以通过自定义实现SessionFactory,实现customUserManager和customRoleManager的方式集成第三方的用户关系。
这里我介绍一个方法,可以借助Liferay的实体监听器来做用户、组、用户-组成员关系的三种同步。
其实实现原理非常简单
用户组的同步,就是实现 BaseModelListener 泛型接口
package com.lifiti.hooks; import org.osgi.service.component.annotations.Component; import com.liferay.portal.kernel.exception.ModelListenerException; import com.liferay.portal.kernel.model.BaseModelListener; import com.liferay.portal.kernel.model.ModelListener; import com.liferay.portal.kernel.model.UserGroup; import com.lifiti.util.IdentifyHelper; @Component(immediate = true, service = ModelListener.class) public class UserGroupListener extends BaseModelListener<UserGroup>{ @Override public void onAfterCreate(UserGroup ug) throws ModelListenerException{ System.out.println(ug.getUuid()+ug.getName() + " Added."); IdentifyHelper.saveGroup(ug.getUuid(), ug.getName(), 1); } @Override public void onAfterRemove(UserGroup ug) throws ModelListenerException { System.out.println(ug.getName() + " Deleted."); IdentifyHelper.delGroup(ug.getUuid()); } @Override public void onAfterUpdate(UserGroup ug) throws ModelListenerException { System.out.println(ug.getName() + " Modified."); IdentifyHelper.delGroup(ug.getUuid()); IdentifyHelper.saveGroup(ug.getUuid(), ug.getName(), 1); } }
用户,成员关系的同步
其中onAfterAddAssociation 就是监听成员关系,可以在liferay的users_usergroups表和activiti的act_id_membership查看
实际上就是一个关联中间表
package com.lifiti.hooks; import org.osgi.service.component.annotations.Component; import com.liferay.portal.kernel.exception.ModelListenerException; import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.model.BaseModelListener; import com.liferay.portal.kernel.model.ModelListener; import com.liferay.portal.kernel.model.User; import com.liferay.portal.kernel.service.UserGroupLocalServiceUtil; import com.liferay.portal.kernel.service.UserLocalServiceUtil; import com.lifiti.util.IdentifyHelper; @Component(immediate = true, service = ModelListener.class) public class UserListener extends BaseModelListener<User> { @Override public void onAfterCreate(User u) throws ModelListenerException { System.out.println(u.getScreenName() + " Added."); IdentifyHelper.saveUser(u.getScreenName(), u.getLastName() + u.getFirstName(), u.getEmailAddress()); } @Override public void onAfterRemove(User u) throws ModelListenerException { System.out.println(u.getUserId() + " Deleted."); IdentifyHelper.delUser(u.getScreenName()); } @Override public void onAfterUpdate(User u) throws ModelListenerException { System.out.println(u.getUserId() + " Modified."); IdentifyHelper.delUser(u.getScreenName()); IdentifyHelper.saveUser(u.getScreenName(), u.getLastName() + u.getFirstName(), u.getEmailAddress()); } @Override public void onAfterAddAssociation(Object classPK, String associationClassName, Object associationClassPK) throws ModelListenerException { try { //对关系类型进行过滤,比如com.liferay.portal.kernel.model.UserRole先不处理 if ("com.liferay.portal.kernel.model.UserGroup".equals(associationClassName)) { IdentifyHelper.createMembership( UserLocalServiceUtil.getUser(Long.parseLong(classPK.toString())).getScreenName(), UserGroupLocalServiceUtil.getUserGroup(Long.parseLong(associationClassPK.toString())) .getUuid()); System.out.println(classPK.toString() + "#" + associationClassName + "#" + associationClassPK.toString() + " associationClassName Modified."); } } catch (NumberFormatException e) { e.printStackTrace(); } catch (PortalException e) { e.printStackTrace(); } } @Override public void onAfterRemoveAssociation(Object classPK, String associationClassName, Object associationClassPK) throws ModelListenerException { try { if ("com.liferay.portal.kernel.model.UserGroup".equals(associationClassName)) { IdentifyHelper.deleteMembership( UserLocalServiceUtil.getUser(Long.parseLong(classPK.toString())).getScreenName(), UserGroupLocalServiceUtil.getUserGroup(Long.parseLong(associationClassPK.toString())) .getUuid()); System.out.println(classPK.toString() + "#" + associationClassName + "#" + associationClassPK.toString() + " associationClassName Removed."); } } catch (NumberFormatException e) { e.printStackTrace(); } catch (PortalException e) { e.printStackTrace(); } } }
运行情况:
1、用户同步,在liferay的用户管理后台添加用户:
添加后,activiti也有同名的用户了(自动同步),即使修改或删除也能同步:
2、用户组同步
注意,liferay叫用户群组,acitiviti叫组
liferay添加组以后,activiti也自动同步:
3、成员关系同步
同步后,发现activiti也有同样的 “用户-组” 关系了
Activiti的集成开发系列文章集合在这里:
http://www.cnblogs.com/starcrm/p/6047486.html
全部工程源代码下载: