synchronizer optimize
This commit is contained in:
+82
-43
@@ -30,6 +30,7 @@ import org.maxkey.constants.ConstsStatus;
|
||||
import org.maxkey.constants.ldap.OrganizationalUnit;
|
||||
import org.maxkey.entity.HistorySynchronizer;
|
||||
import org.maxkey.entity.Organizations;
|
||||
import org.maxkey.entity.SynchroRelated;
|
||||
import org.maxkey.persistence.ldap.ActiveDirectoryUtils;
|
||||
import org.maxkey.persistence.ldap.LdapUtils;
|
||||
import org.maxkey.synchronizer.AbstractSynchronizerService;
|
||||
@@ -45,51 +46,13 @@ public class ActiveDirectoryOrganizationService extends AbstractSynchronizerSer
|
||||
ActiveDirectoryUtils ldapUtils;
|
||||
|
||||
public void sync() {
|
||||
loadOrgsById("1");
|
||||
loadOrgsByInstId(this.synchronizer.getInstId(),Organizations.ROOT_ORG_ID);
|
||||
_logger.info("Sync ActiveDirectory Organizations ...");
|
||||
try {
|
||||
SearchControls constraints = new SearchControls();
|
||||
constraints.setSearchScope(ldapUtils.getSearchScope());
|
||||
String filter = "(&(objectClass=OrganizationalUnit))";
|
||||
if(StringUtils.isNotBlank(this.getSynchronizer().getFilters())) {
|
||||
//filter = this.getSynchronizer().getFilters();
|
||||
}
|
||||
|
||||
NamingEnumeration<SearchResult> results =
|
||||
ldapUtils.getConnection().search(ldapUtils.getBaseDN(), filter, constraints);
|
||||
|
||||
ArrayList<Organizations> orgsList = new ArrayList<Organizations>();
|
||||
ArrayList<Organizations> orgsList = queryActiveDirectory();
|
||||
int maxLevel = 0;
|
||||
long recordCount = 0;
|
||||
while (null != results && results.hasMoreElements()) {
|
||||
Object obj = results.nextElement();
|
||||
if (obj instanceof SearchResult) {
|
||||
SearchResult sr = (SearchResult) obj;
|
||||
if(sr.getNameInNamespace().contains("OU=Domain Controllers")||StringUtils.isEmpty(sr.getName())) {
|
||||
_logger.info("Skip '' or 'OU=Domain Controllers' .");
|
||||
continue;
|
||||
}
|
||||
_logger.debug("Sync OrganizationalUnit {} , name [{}] , NameInNamespace [{}]" ,
|
||||
(++recordCount),sr.getName(),sr.getNameInNamespace());
|
||||
|
||||
HashMap<String,Attribute> attributeMap = new HashMap<String,Attribute>();
|
||||
NamingEnumeration<? extends Attribute> attrs = sr.getAttributes().getAll();
|
||||
while (null != attrs && attrs.hasMoreElements()) {
|
||||
Attribute objAttrs = attrs.nextElement();
|
||||
_logger.trace("attribute {} : {}" ,
|
||||
objAttrs.getID(),
|
||||
ActiveDirectoryUtils.getAttrStringValue(objAttrs)
|
||||
);
|
||||
attributeMap.put(objAttrs.getID().toLowerCase(), objAttrs);
|
||||
}
|
||||
|
||||
Organizations organization = buildOrganization(attributeMap,sr.getName(),sr.getNameInNamespace());
|
||||
if(organization != null) {
|
||||
orgsList.add(organization);
|
||||
maxLevel = (maxLevel < organization.getLevel()) ? organization.getLevel() : maxLevel ;
|
||||
}
|
||||
|
||||
}
|
||||
for(Organizations organization : orgsList) {
|
||||
maxLevel = (maxLevel < organization.getLevel()) ? organization.getLevel() : maxLevel ;
|
||||
}
|
||||
|
||||
for (int level = 2 ; level <= maxLevel ; level++) {
|
||||
@@ -111,7 +74,24 @@ public class ActiveDirectoryOrganizationService extends AbstractSynchronizerSer
|
||||
organization.setCodePath(parentOrg.getCodePath()+"/"+organization.getId());
|
||||
_logger.info("parentNamePath " + parentNamePath+" , namePah " + organization.getNamePath());
|
||||
|
||||
organizationsService.saveOrUpdate(organization);
|
||||
//synchro Related
|
||||
SynchroRelated synchroRelated =
|
||||
synchroRelatedService.findByOriginId(
|
||||
this.synchronizer,organization.getLdapDn(),Organizations.CLASS_TYPE );
|
||||
if(synchroRelated == null) {
|
||||
organization.setId(organization.generateId());
|
||||
organizationsService.insert(organization);
|
||||
_logger.debug("Organizations : " + organization);
|
||||
|
||||
synchroRelated = buildSynchroRelated(organization,organization.getLdapDn(),organization.getName());
|
||||
}else {
|
||||
organization.setId(synchroRelated.getObjectId());
|
||||
organizationsService.update(organization);
|
||||
}
|
||||
|
||||
synchroRelatedService.updateSynchroRelated(
|
||||
this.synchronizer,synchroRelated,Organizations.CLASS_TYPE);
|
||||
|
||||
orgsNamePathMap.put(organization.getNamePath(), organization);
|
||||
|
||||
HistorySynchronizer historySynchronizer
|
||||
@@ -137,6 +117,65 @@ public class ActiveDirectoryOrganizationService extends AbstractSynchronizerSer
|
||||
|
||||
}
|
||||
|
||||
private ArrayList<Organizations> queryActiveDirectory() throws NamingException {
|
||||
SearchControls constraints = new SearchControls();
|
||||
constraints.setSearchScope(ldapUtils.getSearchScope());
|
||||
String filter = "(&(objectClass=OrganizationalUnit))";
|
||||
if(StringUtils.isNotBlank(this.getSynchronizer().getFilters())) {
|
||||
//filter = this.getSynchronizer().getFilters();
|
||||
}
|
||||
|
||||
NamingEnumeration<SearchResult> results =
|
||||
ldapUtils.getConnection().search(ldapUtils.getBaseDN(), filter, constraints);
|
||||
|
||||
ArrayList<Organizations> orgsList = new ArrayList<Organizations>();
|
||||
long recordCount = 0;
|
||||
while (null != results && results.hasMoreElements()) {
|
||||
Object obj = results.nextElement();
|
||||
if (obj instanceof SearchResult) {
|
||||
SearchResult sr = (SearchResult) obj;
|
||||
if(sr.getNameInNamespace().contains("OU=Domain Controllers")||StringUtils.isEmpty(sr.getName())) {
|
||||
_logger.info("Skip '' or 'OU=Domain Controllers' .");
|
||||
continue;
|
||||
}
|
||||
_logger.debug("Sync OrganizationalUnit {} , name [{}] , NameInNamespace [{}]" ,
|
||||
(++recordCount),sr.getName(),sr.getNameInNamespace());
|
||||
|
||||
HashMap<String,Attribute> attributeMap = new HashMap<String,Attribute>();
|
||||
NamingEnumeration<? extends Attribute> attrs = sr.getAttributes().getAll();
|
||||
while (null != attrs && attrs.hasMoreElements()) {
|
||||
Attribute objAttrs = attrs.nextElement();
|
||||
_logger.trace("attribute {} : {}" ,
|
||||
objAttrs.getID(),
|
||||
ActiveDirectoryUtils.getAttrStringValue(objAttrs)
|
||||
);
|
||||
attributeMap.put(objAttrs.getID().toLowerCase(), objAttrs);
|
||||
}
|
||||
|
||||
Organizations organization = buildOrganization(attributeMap,sr.getName(),sr.getNameInNamespace());
|
||||
if(organization != null) {
|
||||
orgsList.add(organization);
|
||||
}
|
||||
}
|
||||
}
|
||||
return orgsList;
|
||||
}
|
||||
|
||||
public SynchroRelated buildSynchroRelated(Organizations organization,String ldapDN,String name) {
|
||||
return new SynchroRelated(
|
||||
organization.getId(),
|
||||
organization.getName(),
|
||||
organization.getName(),
|
||||
Organizations.CLASS_TYPE,
|
||||
synchronizer.getId(),
|
||||
synchronizer.getName(),
|
||||
ldapDN,
|
||||
name,
|
||||
"",
|
||||
organization.getParentId(),
|
||||
synchronizer.getInstId());
|
||||
}
|
||||
|
||||
public Organizations buildOrganization(HashMap<String,Attribute> attributeMap,String name,String nameInNamespace) {
|
||||
try {
|
||||
Organizations org = new Organizations();
|
||||
|
||||
+21
-1
@@ -27,8 +27,10 @@ import javax.naming.directory.SearchResult;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.maxkey.constants.ConstsStatus;
|
||||
import org.maxkey.constants.ldap.ActiveDirectoryUser;
|
||||
import org.maxkey.crypto.DigestUtils;
|
||||
import org.maxkey.entity.HistorySynchronizer;
|
||||
import org.maxkey.entity.Organizations;
|
||||
import org.maxkey.entity.SynchroRelated;
|
||||
import org.maxkey.entity.UserInfo;
|
||||
import org.maxkey.persistence.ldap.ActiveDirectoryUtils;
|
||||
import org.maxkey.persistence.ldap.LdapUtils;
|
||||
@@ -46,7 +48,7 @@ public class ActiveDirectoryUsersService extends AbstractSynchronizerService
|
||||
|
||||
public void sync() {
|
||||
_logger.info("Sync ActiveDirectory Users...");
|
||||
loadOrgsById("1");
|
||||
loadOrgsByInstId(this.synchronizer.getInstId(),Organizations.ROOT_ORG_ID);
|
||||
try {
|
||||
SearchControls constraints = new SearchControls();
|
||||
constraints.setSearchScope(ldapUtils.getSearchScope());
|
||||
@@ -79,11 +81,29 @@ public class ActiveDirectoryUsersService extends AbstractSynchronizerService
|
||||
attributeMap.put(objAttrs.getID().toLowerCase(), objAttrs);
|
||||
}
|
||||
|
||||
String originId = DigestUtils.md5B64(sr.getNameInNamespace());
|
||||
|
||||
UserInfo userInfo =buildUserInfo(attributeMap,sr.getName(),sr.getNameInNamespace());
|
||||
if(userInfo != null) {
|
||||
userInfo.setPassword(userInfo.getUsername() + UserInfo.DEFAULT_PASSWORD_SUFFIX);
|
||||
userInfoService.saveOrUpdate(userInfo);
|
||||
_logger.info("userInfo " + userInfo);
|
||||
|
||||
SynchroRelated synchroRelated = new SynchroRelated(
|
||||
userInfo.getId(),
|
||||
userInfo.getUsername(),
|
||||
userInfo.getDisplayName(),
|
||||
UserInfo.CLASS_TYPE,
|
||||
synchronizer.getId(),
|
||||
synchronizer.getName(),
|
||||
originId,
|
||||
userInfo.getDisplayName(),
|
||||
"",
|
||||
"",
|
||||
synchronizer.getInstId());
|
||||
|
||||
synchroRelatedService.updateSynchroRelated(
|
||||
this.synchronizer,synchroRelated,UserInfo.CLASS_TYPE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+87
-13
@@ -17,12 +17,12 @@
|
||||
|
||||
package org.maxkey.synchronizer.dingtalk;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import org.maxkey.constants.ConstsStatus;
|
||||
import org.maxkey.entity.Organizations;
|
||||
import org.maxkey.entity.SynchroRelated;
|
||||
import org.maxkey.synchronizer.AbstractSynchronizerService;
|
||||
import org.maxkey.synchronizer.ISynchronizerService;
|
||||
import org.slf4j.Logger;
|
||||
@@ -30,7 +30,9 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.dingtalk.api.DefaultDingTalkClient;
|
||||
import com.dingtalk.api.DingTalkClient;
|
||||
import com.dingtalk.api.request.OapiV2DepartmentGetRequest;
|
||||
import com.dingtalk.api.request.OapiV2DepartmentListsubRequest;
|
||||
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
|
||||
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
|
||||
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse.DeptBaseResponse;
|
||||
import com.taobao.api.ApiException;
|
||||
@@ -39,24 +41,67 @@ import com.taobao.api.ApiException;
|
||||
public class DingtalkOrganizationService extends AbstractSynchronizerService implements ISynchronizerService{
|
||||
final static Logger _logger = LoggerFactory.getLogger(DingtalkOrganizationService.class);
|
||||
|
||||
static Long ROOT_DEPT_ID = 1L;
|
||||
|
||||
String access_token;
|
||||
|
||||
public void sync() {
|
||||
_logger.info("Sync Dingtalk Organizations ...");
|
||||
LinkedBlockingQueue<Long> deptsQueue = new LinkedBlockingQueue<Long>();
|
||||
deptsQueue.add(1L);
|
||||
HashMap<Long,DeptBaseResponse> deptMap = new HashMap<Long,DeptBaseResponse>();
|
||||
deptsQueue.add(ROOT_DEPT_ID);
|
||||
try {
|
||||
//root
|
||||
Organizations rootOrganization = organizationsService.get(Organizations.ROOT_ORG_ID);
|
||||
OapiV2DepartmentGetResponse rootDeptRsp = requestDepartment(access_token,ROOT_DEPT_ID);
|
||||
_logger.debug("root dept deptId {} , name {} , parentId {}"
|
||||
,rootDeptRsp.getResult().getDeptId(),
|
||||
rootDeptRsp.getResult().getName(),
|
||||
rootDeptRsp.getResult().getParentId());
|
||||
//root
|
||||
SynchroRelated rootSynchroRelated = buildSynchroRelated(rootOrganization,
|
||||
rootDeptRsp.getResult().getDeptId()+"",
|
||||
rootDeptRsp.getResult().getName(),
|
||||
rootDeptRsp.getResult().getParentId()+"");
|
||||
|
||||
synchroRelatedService.updateSynchroRelated(
|
||||
this.synchronizer,rootSynchroRelated,Organizations.CLASS_TYPE);
|
||||
|
||||
while(deptsQueue.element() != null) {
|
||||
OapiV2DepartmentListsubResponse rsp = requestDepartmentList(access_token,deptsQueue.poll());
|
||||
|
||||
for(DeptBaseResponse dept : rsp.getResult()) {
|
||||
_logger.info("dept : " + dept.getDeptId()+" "+ dept.getName()+" "+ dept.getParentId());
|
||||
_logger.debug("dept deptId {} , name {} , parentId {} " ,
|
||||
dept.getDeptId(),
|
||||
dept.getName(),
|
||||
dept.getParentId());
|
||||
|
||||
deptsQueue.add(dept.getDeptId());
|
||||
deptMap.put(dept.getDeptId(), dept);
|
||||
Organizations organization = buildOrganization(dept,deptMap.get(dept.getParentId()));
|
||||
organizationsService.saveOrUpdate(organization);
|
||||
_logger.info("Organizations : " + organization);
|
||||
|
||||
//synchro Related
|
||||
SynchroRelated synchroRelated =
|
||||
synchroRelatedService.findByOriginId(
|
||||
this.synchronizer,dept.getDeptId() + "",Organizations.CLASS_TYPE );
|
||||
|
||||
Organizations organization = buildOrganization(dept);
|
||||
if(synchroRelated == null) {
|
||||
organization.setId(organization.generateId());
|
||||
organizationsService.insert(organization);
|
||||
_logger.debug("Organizations : " + organization);
|
||||
|
||||
synchroRelated = buildSynchroRelated(organization,
|
||||
dept.getDeptId() + "",
|
||||
dept.getName(),
|
||||
dept.getParentId() + "");
|
||||
|
||||
}else {
|
||||
organization.setId(synchroRelated.getObjectId());
|
||||
organizationsService.update(organization);
|
||||
}
|
||||
|
||||
synchroRelatedService.updateSynchroRelated(
|
||||
this.synchronizer,synchroRelated,Organizations.CLASS_TYPE);
|
||||
|
||||
_logger.debug("Organizations : " + organization);
|
||||
}
|
||||
}
|
||||
} catch (ApiException e) {
|
||||
@@ -73,19 +118,48 @@ public class DingtalkOrganizationService extends AbstractSynchronizerService im
|
||||
req.setDeptId(deptId);
|
||||
req.setLanguage("zh_CN");
|
||||
OapiV2DepartmentListsubResponse rspDepts = client.execute(req, access_token);
|
||||
_logger.info("response : " + rspDepts.getBody());
|
||||
_logger.trace("response : " + rspDepts.getBody());
|
||||
return rspDepts;
|
||||
}
|
||||
|
||||
public Organizations buildOrganization(DeptBaseResponse dept,DeptBaseResponse parentDept) {
|
||||
public OapiV2DepartmentGetResponse requestDepartment(String access_token,Long deptId) throws ApiException {
|
||||
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");
|
||||
OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
|
||||
req.setDeptId(deptId);
|
||||
req.setLanguage("zh_CN");
|
||||
OapiV2DepartmentGetResponse rspDepts = client.execute(req, access_token);
|
||||
_logger.trace("response : " + rspDepts.getBody());
|
||||
return rspDepts;
|
||||
}
|
||||
|
||||
public SynchroRelated buildSynchroRelated(Organizations organization,String deptId,String name,String parentId) {
|
||||
return new SynchroRelated(
|
||||
organization.getId(),
|
||||
organization.getName(),
|
||||
organization.getName(),
|
||||
Organizations.CLASS_TYPE,
|
||||
synchronizer.getId(),
|
||||
synchronizer.getName(),
|
||||
deptId+"",
|
||||
name,
|
||||
"",
|
||||
parentId,
|
||||
synchronizer.getInstId());
|
||||
}
|
||||
|
||||
public Organizations buildOrganization(DeptBaseResponse dept) {
|
||||
//Parent
|
||||
SynchroRelated synchroRelatedParent =
|
||||
synchroRelatedService.findByOriginId(
|
||||
this.synchronizer,dept.getParentId() + "",Organizations.CLASS_TYPE);
|
||||
Organizations org = new Organizations();
|
||||
org.setId(dept.getDeptId()+"");
|
||||
org.setCode(dept.getDeptId()+"");
|
||||
org.setName(dept.getName());
|
||||
org.setParentId(dept.getParentId()+"");
|
||||
org.setParentCode(dept.getParentId()+"");
|
||||
if(parentDept != null) {
|
||||
org.setParentName(parentDept.getName());
|
||||
if(synchroRelatedParent != null) {
|
||||
org.setParentId(synchroRelatedParent.getObjectId());
|
||||
org.setParentName(synchroRelatedParent.getObjectName());
|
||||
}
|
||||
org.setInstId(this.synchronizer.getInstId());
|
||||
org.setStatus(ConstsStatus.ACTIVE);
|
||||
|
||||
+37
-22
@@ -17,14 +17,13 @@
|
||||
|
||||
package org.maxkey.synchronizer.dingtalk;
|
||||
|
||||
import java.sql.Types;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.format.DateTimeFormat;
|
||||
import org.maxkey.constants.ConstsStatus;
|
||||
import org.maxkey.entity.Organizations;
|
||||
import org.maxkey.entity.SynchroRelated;
|
||||
import org.maxkey.entity.UserInfo;
|
||||
import org.maxkey.synchronizer.AbstractSynchronizerService;
|
||||
import org.maxkey.synchronizer.ISynchronizerService;
|
||||
@@ -46,17 +45,14 @@ public class DingtalkUsersService extends AbstractSynchronizerService implement
|
||||
public void sync() {
|
||||
_logger.info("Sync Dingtalk Users...");
|
||||
try {
|
||||
List<SynchroRelated> synchroRelateds =
|
||||
synchroRelatedService.findOrgs(this.synchronizer);
|
||||
|
||||
List<Organizations> organizations =
|
||||
organizationsService.find("instid = ?",
|
||||
new Object[] { this.synchronizer.getInstId() },
|
||||
new int[] { Types.VARCHAR});
|
||||
|
||||
for(Organizations dept : organizations) {
|
||||
for(SynchroRelated relatedOrg : synchroRelateds) {
|
||||
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list");
|
||||
OapiV2UserListRequest req = new OapiV2UserListRequest();
|
||||
_logger.info("DingTalk deptId : {}" , dept.getCode());
|
||||
req.setDeptId(Long.parseLong(dept.getCode()));
|
||||
_logger.debug("DingTalk deptId : {}" , relatedOrg.getOriginId());
|
||||
req.setDeptId(Long.parseLong(relatedOrg.getOriginId()));
|
||||
req.setCursor(0L);
|
||||
req.setSize(100L);
|
||||
req.setOrderField("modify_desc");
|
||||
@@ -68,10 +64,28 @@ public class DingtalkUsersService extends AbstractSynchronizerService implement
|
||||
if(rsp.getErrcode()==0) {
|
||||
for(ListUserResponse user :rsp.getResult().getList()) {
|
||||
_logger.debug("name : {} , {} , {}", user.getName(),user.getLoginId(),user.getUserid());
|
||||
UserInfo userInfo = buildUserInfo(user);
|
||||
|
||||
UserInfo userInfo = buildUserInfo(user,relatedOrg);
|
||||
_logger.trace("userInfo {}" , userInfo);
|
||||
userInfo.setPassword(userInfo.getUsername() + UserInfo.DEFAULT_PASSWORD_SUFFIX);
|
||||
userInfoService.saveOrUpdate(userInfo);
|
||||
|
||||
SynchroRelated synchroRelated = new SynchroRelated(
|
||||
userInfo.getId(),
|
||||
userInfo.getUsername(),
|
||||
userInfo.getDisplayName(),
|
||||
UserInfo.CLASS_TYPE,
|
||||
synchronizer.getId(),
|
||||
synchronizer.getName(),
|
||||
user.getUnionid(),
|
||||
user.getName(),
|
||||
user.getUserid(),
|
||||
"",
|
||||
synchronizer.getInstId());
|
||||
synchroRelatedService.updateSynchroRelated(
|
||||
this.synchronizer,synchroRelated,UserInfo.CLASS_TYPE);
|
||||
|
||||
socialsAssociate(synchroRelated,"dingtalk");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -83,24 +97,25 @@ public class DingtalkUsersService extends AbstractSynchronizerService implement
|
||||
|
||||
}
|
||||
|
||||
public UserInfo buildUserInfo(ListUserResponse user) {
|
||||
public UserInfo buildUserInfo(ListUserResponse user,SynchroRelated relatedOrg) {
|
||||
UserInfo userInfo = new UserInfo();
|
||||
|
||||
userInfo.setUsername(user.getUserid());//閻ц缍嶉崥锟�
|
||||
userInfo.setNickName(user.getName());//閻€劍鍩涢崥锟�
|
||||
userInfo.setDisplayName(user.getName());//閻€劍鍩涢崥锟�
|
||||
userInfo.setFormattedName(user.getName());//閻€劍鍩涢崥锟�
|
||||
userInfo.setUsername(user.getUserid());
|
||||
userInfo.setNickName(user.getName());
|
||||
userInfo.setDisplayName(user.getName());
|
||||
userInfo.setFormattedName(user.getName());
|
||||
|
||||
userInfo.setEmail(StringUtils.isBlank(user.getEmail())? user.getUserid() +"@maxkey.top":user.getEmail());
|
||||
userInfo.setEntryDate(new DateTime(user.getHiredDate()).toString(DateTimeFormat.forPattern("yyyy-MM-dd")));
|
||||
userInfo.setMobile(user.getMobile());//閹靛婧�
|
||||
userInfo.setMobile(user.getMobile());
|
||||
|
||||
userInfo.setDepartmentId(user.getDeptIdList().get(0)+"");
|
||||
userInfo.setDepartmentId(relatedOrg.getObjectId()+"");
|
||||
userInfo.setDepartment(relatedOrg.getObjectName());
|
||||
userInfo.setEmployeeNumber(user.getJobNumber());
|
||||
userInfo.setJobTitle(user.getTitle());//閼卞苯濮�
|
||||
userInfo.setWorkEmail(user.getOrgEmail());//瀹搞儰缍旈柇顔绘
|
||||
userInfo.setWorkPhoneNumber(user.getTelephone());//閸忣剙寰冮悽浣冪樈
|
||||
userInfo.setWorkOfficeName(user.getWorkPlace());//閸旂偛鍙曠�癸拷
|
||||
userInfo.setJobTitle(user.getTitle());
|
||||
userInfo.setWorkEmail(user.getOrgEmail());
|
||||
userInfo.setWorkPhoneNumber(user.getTelephone());
|
||||
userInfo.setWorkOfficeName(user.getWorkPlace());
|
||||
if(user.getActive()) {
|
||||
userInfo.setStatus(ConstsStatus.ACTIVE);
|
||||
}else {
|
||||
|
||||
+72
-17
@@ -23,6 +23,7 @@ import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import org.maxkey.constants.ConstsStatus;
|
||||
import org.maxkey.entity.Organizations;
|
||||
import org.maxkey.entity.SynchroRelated;
|
||||
import org.maxkey.synchronizer.AbstractSynchronizerService;
|
||||
import org.maxkey.synchronizer.ISynchronizerService;
|
||||
import org.maxkey.synchronizer.feishu.entity.FeishuDepts;
|
||||
@@ -40,30 +41,55 @@ public class FeishuOrganizationService extends AbstractSynchronizerService imple
|
||||
|
||||
String access_token;
|
||||
|
||||
static String DEPTS_URL="https://open.feishu.cn/open-apis/contact/v3/departments/%s/children?page_size=50";
|
||||
|
||||
static String DEPTS_URL = "https://open.feishu.cn/open-apis/contact/v3/departments/%s/children?page_size=50";
|
||||
static String ROOT_DEPT_URL = "https://open.feishu.cn/open-apis/contact/v3/departments/%s";
|
||||
static String ROOT_DEPT_ID = "0";
|
||||
public void sync() {
|
||||
_logger.info("Sync Feishu Organizations ...");
|
||||
|
||||
LinkedBlockingQueue<String> deptsQueue = new LinkedBlockingQueue<String>();
|
||||
deptsQueue.add("0");
|
||||
HashMap<String,FeishuDepts> deptMap = new HashMap<String,FeishuDepts>();
|
||||
|
||||
deptsQueue.add(ROOT_DEPT_ID);
|
||||
//root
|
||||
FeishuDeptsResponse rspRoot = requestDepartment(ROOT_DEPT_URL,ROOT_DEPT_ID,access_token);
|
||||
Organizations rootOrganization = organizationsService.get(Organizations.ROOT_ORG_ID);
|
||||
SynchroRelated rootSynchroRelated = buildSynchroRelated(rootOrganization,rspRoot.getData().getDepartment());
|
||||
|
||||
synchroRelatedService.updateSynchroRelated(
|
||||
this.synchronizer,rootSynchroRelated,Organizations.CLASS_TYPE);
|
||||
|
||||
//child
|
||||
try {
|
||||
while(deptsQueue.element() != null) {
|
||||
FeishuDeptsResponse rsp = requestDepartmentList(access_token,deptsQueue.poll());
|
||||
if(rsp.getCode() == 0 && rsp.getData().getItems() != null) {
|
||||
for(FeishuDepts dept : rsp.getData().getItems()) {
|
||||
_logger.info("dept : id {} , Parent {} , Name {} , od {}" ,
|
||||
_logger.debug("dept : id {} , Parent {} , Name {} , od {}" ,
|
||||
dept.getDepartment_id(),
|
||||
dept.getParent_department_id(),
|
||||
dept.getName(),
|
||||
dept.getOpen_department_id()
|
||||
);
|
||||
deptsQueue.add(dept.getOpen_department_id());
|
||||
deptMap.put(dept.getOpen_department_id(), dept);
|
||||
Organizations organization = buildOrganization(dept,deptMap.get(dept.getParent_department_id()));
|
||||
organizationsService.saveOrUpdate(organization);
|
||||
_logger.info("Organizations : " + organization);
|
||||
//synchro Related
|
||||
SynchroRelated synchroRelated =
|
||||
synchroRelatedService.findByOriginId(
|
||||
this.synchronizer,dept.getOpen_department_id(),Organizations.CLASS_TYPE );
|
||||
Organizations organization = buildOrganization(dept);
|
||||
if(synchroRelated == null) {
|
||||
organization.setId(organization.generateId());
|
||||
organizationsService.insert(organization);
|
||||
_logger.debug("Organizations : " + organization);
|
||||
synchroRelated = buildSynchroRelated(organization,dept);
|
||||
|
||||
}else {
|
||||
organization.setId(synchroRelated.getObjectId());
|
||||
organizationsService.update(organization);
|
||||
}
|
||||
|
||||
|
||||
synchroRelatedService.updateSynchroRelated(
|
||||
this.synchronizer,synchroRelated,Organizations.CLASS_TYPE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -80,21 +106,50 @@ public class FeishuOrganizationService extends AbstractSynchronizerService imple
|
||||
String responseBody = request.get(String.format(DEPTS_URL, deptId),headers);
|
||||
FeishuDeptsResponse deptsResponse =JsonUtils.gson2Object(responseBody, FeishuDeptsResponse.class);
|
||||
|
||||
_logger.info("response : " + responseBody);
|
||||
_logger.trace("response : " + responseBody);
|
||||
|
||||
return deptsResponse;
|
||||
}
|
||||
|
||||
public Organizations buildOrganization(FeishuDepts dept,FeishuDepts parentDept) {
|
||||
public FeishuDeptsResponse requestDepartment(String url ,String deptId ,String access_token) {
|
||||
HttpRequestAdapter request =new HttpRequestAdapter();
|
||||
HashMap<String,String> headers =new HashMap<String,String>();
|
||||
headers.put("Authorization", AuthorizationHeaderUtils.createBearer(access_token));
|
||||
String responseBody = request.get(String.format(url, deptId),headers);
|
||||
FeishuDeptsResponse deptsResponse =JsonUtils.gson2Object(responseBody, FeishuDeptsResponse.class);
|
||||
|
||||
_logger.trace("response : " + responseBody);
|
||||
|
||||
return deptsResponse;
|
||||
}
|
||||
|
||||
public SynchroRelated buildSynchroRelated(Organizations org,FeishuDepts dept) {
|
||||
return new SynchroRelated(
|
||||
org.getId(),
|
||||
org.getName(),
|
||||
org.getName(),
|
||||
Organizations.CLASS_TYPE,
|
||||
synchronizer.getId(),
|
||||
synchronizer.getName(),
|
||||
dept.getOpen_department_id(),
|
||||
dept.getName(),
|
||||
dept.getDepartment_id(),
|
||||
dept.getParent_department_id(),
|
||||
synchronizer.getInstId());
|
||||
}
|
||||
|
||||
public Organizations buildOrganization(FeishuDepts dept) {
|
||||
//Parent
|
||||
SynchroRelated synchroRelatedParent =
|
||||
synchroRelatedService.findByOriginId(
|
||||
this.synchronizer,dept.getParent_department_id(),Organizations.CLASS_TYPE);
|
||||
|
||||
Organizations org = new Organizations();
|
||||
org.setId(dept.getOpen_department_id()+"");
|
||||
org.setCode(dept.getDepartment_id()+"");
|
||||
org.setName(dept.getName());
|
||||
if(parentDept == null) {
|
||||
org.setParentId("1");
|
||||
}else {
|
||||
org.setParentId(parentDept.getOpen_department_id()+"");
|
||||
}
|
||||
org.setFullName(dept.getName());
|
||||
org.setParentId(synchroRelatedParent.getObjectId());
|
||||
org.setParentName(synchroRelatedParent.getObjectName());
|
||||
org.setSortIndex(Integer.parseInt(dept.getOrder()));
|
||||
org.setInstId(this.synchronizer.getInstId());
|
||||
org.setStatus(ConstsStatus.ACTIVE);
|
||||
|
||||
+35
-14
@@ -17,12 +17,11 @@
|
||||
|
||||
package org.maxkey.synchronizer.feishu;
|
||||
|
||||
import java.sql.Types;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.maxkey.constants.ConstsStatus;
|
||||
import org.maxkey.entity.Organizations;
|
||||
import org.maxkey.entity.SynchroRelated;
|
||||
import org.maxkey.entity.UserInfo;
|
||||
import org.maxkey.synchronizer.AbstractSynchronizerService;
|
||||
import org.maxkey.synchronizer.ISynchronizerService;
|
||||
@@ -46,23 +45,41 @@ public class FeishuUsersService extends AbstractSynchronizerService implements I
|
||||
public void sync() {
|
||||
_logger.info("Sync Feishu Users...");
|
||||
try {
|
||||
List<Organizations> organizations =
|
||||
organizationsService.find("instid = ?",
|
||||
new Object[] { this.synchronizer.getInstId() },
|
||||
new int[] { Types.VARCHAR});
|
||||
for(Organizations dept : organizations) {
|
||||
List<SynchroRelated> synchroRelateds =
|
||||
synchroRelatedService.findOrgs(this.synchronizer);
|
||||
|
||||
for(SynchroRelated relatedOrg : synchroRelateds) {
|
||||
HttpRequestAdapter request =new HttpRequestAdapter();
|
||||
HashMap<String,String> headers =new HashMap<String,String>();
|
||||
headers.put("Authorization", AuthorizationHeaderUtils.createBearer(access_token));
|
||||
String responseBody = request.get(String.format(USERS_URL,dept.getId()),headers);
|
||||
String responseBody = request.get(String.format(USERS_URL,relatedOrg.getOriginId()),headers);
|
||||
FeishuUsersResponse usersResponse =JsonUtils.gson2Object(responseBody, FeishuUsersResponse.class);
|
||||
_logger.info("response : " + responseBody);
|
||||
_logger.trace("response : " + responseBody);
|
||||
if(usersResponse.getCode() == 0 && usersResponse.getData().getItems() != null) {
|
||||
for(FeishuUsers user : usersResponse.getData().getItems()) {
|
||||
UserInfo userInfo = buildUserInfo(user);
|
||||
_logger.info("userInfo : " + userInfo);
|
||||
for(FeishuUsers feiShuUser : usersResponse.getData().getItems()) {
|
||||
UserInfo userInfo = buildUserInfo(feiShuUser,relatedOrg);
|
||||
_logger.debug("userInfo : " + userInfo);
|
||||
userInfo.setPassword(userInfo.getUsername() + UserInfo.DEFAULT_PASSWORD_SUFFIX);
|
||||
userInfoService.saveOrUpdate(userInfo);
|
||||
|
||||
SynchroRelated synchroRelated = new SynchroRelated(
|
||||
userInfo.getId(),
|
||||
userInfo.getUsername(),
|
||||
userInfo.getDisplayName(),
|
||||
UserInfo.CLASS_TYPE,
|
||||
synchronizer.getId(),
|
||||
synchronizer.getName(),
|
||||
feiShuUser.getOpen_id(),
|
||||
feiShuUser.getName(),
|
||||
feiShuUser.getUser_id(),
|
||||
feiShuUser.getUnion_id(),
|
||||
synchronizer.getInstId());
|
||||
synchroRelatedService.updateSynchroRelated(
|
||||
this.synchronizer,synchroRelated,UserInfo.CLASS_TYPE);
|
||||
|
||||
synchroRelated.setOriginId(feiShuUser.getUnion_id());
|
||||
socialsAssociate(synchroRelated,"feishu");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -77,8 +94,9 @@ public class FeishuUsersService extends AbstractSynchronizerService implements I
|
||||
|
||||
}
|
||||
|
||||
public UserInfo buildUserInfo(FeishuUsers user) {
|
||||
public UserInfo buildUserInfo(FeishuUsers user,SynchroRelated relatedOrg) {
|
||||
UserInfo userInfo = new UserInfo();
|
||||
userInfo.setId(userInfo.generateId());
|
||||
userInfo.setUsername(user.getUser_id());//账号
|
||||
userInfo.setNickName(user.getNickname());//名字
|
||||
userInfo.setDisplayName(user.getName());//名字
|
||||
@@ -89,7 +107,10 @@ public class FeishuUsersService extends AbstractSynchronizerService implements I
|
||||
|
||||
userInfo.setEmployeeNumber(user.getEmployee_no());
|
||||
userInfo.setWorkPhoneNumber(user.getMobile());//工作电话
|
||||
userInfo.setDepartmentId(user.getDepartment_ids()[0]+"");
|
||||
|
||||
userInfo.setDepartmentId(relatedOrg.getObjectId());
|
||||
userInfo.setDepartment(relatedOrg.getObjectName());
|
||||
|
||||
userInfo.setJobTitle(user.getJob_title());//职务
|
||||
userInfo.setWorkAddressFormatted(user.getWork_station());//工作地点
|
||||
|
||||
|
||||
+10
@@ -26,6 +26,8 @@ public class FeishuDeptsData extends ResponseData {
|
||||
String page_token;
|
||||
ArrayList<FeishuDepts> items;
|
||||
|
||||
FeishuDepts department;
|
||||
|
||||
public boolean isHas_more() {
|
||||
return has_more;
|
||||
}
|
||||
@@ -50,6 +52,14 @@ public class FeishuDeptsData extends ResponseData {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public FeishuDepts getDepartment() {
|
||||
return department;
|
||||
}
|
||||
|
||||
public void setDepartment(FeishuDepts department) {
|
||||
this.department = department;
|
||||
}
|
||||
|
||||
public FeishuDeptsData() {
|
||||
super();
|
||||
}
|
||||
|
||||
+78
-47
@@ -31,6 +31,7 @@ import org.maxkey.constants.ConstsStatus;
|
||||
import org.maxkey.constants.ldap.OrganizationalUnit;
|
||||
import org.maxkey.entity.HistorySynchronizer;
|
||||
import org.maxkey.entity.Organizations;
|
||||
import org.maxkey.entity.SynchroRelated;
|
||||
import org.maxkey.persistence.ldap.LdapUtils;
|
||||
import org.maxkey.synchronizer.AbstractSynchronizerService;
|
||||
import org.maxkey.synchronizer.ISynchronizerService;
|
||||
@@ -46,44 +47,12 @@ public class LdapOrganizationService extends AbstractSynchronizerService implem
|
||||
|
||||
public void sync() {
|
||||
_logger.info("Sync Ldap Organizations ...");
|
||||
loadOrgsById("1");
|
||||
loadOrgsByInstId(this.synchronizer.getInstId(),Organizations.ROOT_ORG_ID);
|
||||
try {
|
||||
SearchControls constraints = new SearchControls();
|
||||
constraints.setSearchScope(ldapUtils.getSearchScope());
|
||||
String filter = "(&(objectClass=OrganizationalUnit))";
|
||||
if(StringUtils.isNotBlank(this.getSynchronizer().getFilters())) {
|
||||
//filter = this.getSynchronizer().getFilters();
|
||||
}
|
||||
NamingEnumeration<SearchResult> results =
|
||||
ldapUtils.getConnection().search(ldapUtils.getBaseDN(), filter , constraints);
|
||||
|
||||
ArrayList<Organizations> orgsList = new ArrayList<Organizations>();
|
||||
ArrayList<Organizations> orgsList = queryLdap();
|
||||
int maxLevel = 0;
|
||||
long recordCount = 0;
|
||||
while (null != results && results.hasMoreElements()) {
|
||||
Object obj = results.nextElement();
|
||||
if (obj instanceof SearchResult) {
|
||||
SearchResult sr = (SearchResult) obj;
|
||||
_logger.debug("Sync OrganizationalUnit {} , name [{}] , NameInNamespace [{}]" ,
|
||||
(++recordCount),sr.getName(),sr.getNameInNamespace());
|
||||
|
||||
HashMap<String,Attribute> attributeMap = new HashMap<String,Attribute>();
|
||||
NamingEnumeration<? extends Attribute> attrs = sr.getAttributes().getAll();
|
||||
while (null != attrs && attrs.hasMoreElements()) {
|
||||
Attribute objAttrs = attrs.nextElement();
|
||||
_logger.trace("attribute {} : {}" ,
|
||||
objAttrs.getID(),
|
||||
LdapUtils.getAttrStringValue(objAttrs)
|
||||
);
|
||||
attributeMap.put(objAttrs.getID().toLowerCase(), objAttrs);
|
||||
}
|
||||
|
||||
Organizations organization = buildOrganization(attributeMap,sr.getName(),sr.getNameInNamespace());
|
||||
if(organization != null) {
|
||||
orgsList.add(organization);
|
||||
maxLevel = (maxLevel < organization.getLevel()) ? organization.getLevel() : maxLevel ;
|
||||
}
|
||||
}
|
||||
for(Organizations organization : orgsList) {
|
||||
maxLevel = (maxLevel < organization.getLevel()) ? organization.getLevel() : maxLevel ;
|
||||
}
|
||||
|
||||
for (int level = 2 ; level <= maxLevel ; level++) {
|
||||
@@ -105,7 +74,24 @@ public class LdapOrganizationService extends AbstractSynchronizerService implem
|
||||
organization.setCodePath(parentOrg.getCodePath()+"/"+organization.getId());
|
||||
_logger.info("parentNamePath " + parentNamePath+" , namePah " + organization.getNamePath());
|
||||
|
||||
organizationsService.saveOrUpdate(organization);
|
||||
//synchro Related
|
||||
SynchroRelated synchroRelated =
|
||||
synchroRelatedService.findByOriginId(
|
||||
this.synchronizer,organization.getLdapDn(),Organizations.CLASS_TYPE );
|
||||
if(synchroRelated == null) {
|
||||
organization.setId(organization.generateId());
|
||||
organizationsService.insert(organization);
|
||||
_logger.debug("Organizations : " + organization);
|
||||
|
||||
synchroRelated = buildSynchroRelated(organization,organization.getLdapDn(),organization.getName());
|
||||
}else {
|
||||
organization.setId(synchroRelated.getObjectId());
|
||||
organizationsService.update(organization);
|
||||
}
|
||||
|
||||
synchroRelatedService.updateSynchroRelated(
|
||||
this.synchronizer,synchroRelated,Organizations.CLASS_TYPE);
|
||||
|
||||
orgsNamePathMap.put(organization.getNamePath(), organization);
|
||||
|
||||
_logger.info("Organizations " + organization);
|
||||
@@ -129,6 +115,61 @@ public class LdapOrganizationService extends AbstractSynchronizerService implem
|
||||
|
||||
}
|
||||
|
||||
private ArrayList<Organizations> queryLdap() throws NamingException {
|
||||
SearchControls constraints = new SearchControls();
|
||||
constraints.setSearchScope(ldapUtils.getSearchScope());
|
||||
String filter = "(&(objectClass=OrganizationalUnit))";
|
||||
if(StringUtils.isNotBlank(this.getSynchronizer().getFilters())) {
|
||||
//filter = this.getSynchronizer().getFilters();
|
||||
}
|
||||
NamingEnumeration<SearchResult> results =
|
||||
ldapUtils.getConnection().search(ldapUtils.getBaseDN(), filter , constraints);
|
||||
|
||||
ArrayList<Organizations> orgsList = new ArrayList<Organizations>();
|
||||
|
||||
long recordCount = 0;
|
||||
while (null != results && results.hasMoreElements()) {
|
||||
Object obj = results.nextElement();
|
||||
if (obj instanceof SearchResult) {
|
||||
SearchResult sr = (SearchResult) obj;
|
||||
_logger.debug("Sync OrganizationalUnit {} , name [{}] , NameInNamespace [{}]" ,
|
||||
(++recordCount),sr.getName(),sr.getNameInNamespace());
|
||||
|
||||
HashMap<String,Attribute> attributeMap = new HashMap<String,Attribute>();
|
||||
NamingEnumeration<? extends Attribute> attrs = sr.getAttributes().getAll();
|
||||
while (null != attrs && attrs.hasMoreElements()) {
|
||||
Attribute objAttrs = attrs.nextElement();
|
||||
_logger.trace("attribute {} : {}" ,
|
||||
objAttrs.getID(),
|
||||
LdapUtils.getAttrStringValue(objAttrs)
|
||||
);
|
||||
attributeMap.put(objAttrs.getID().toLowerCase(), objAttrs);
|
||||
}
|
||||
|
||||
Organizations organization = buildOrganization(attributeMap,sr.getName(),sr.getNameInNamespace());
|
||||
if(organization != null) {
|
||||
orgsList.add(organization);
|
||||
}
|
||||
}
|
||||
}
|
||||
return orgsList;
|
||||
}
|
||||
|
||||
public SynchroRelated buildSynchroRelated(Organizations organization,String ldapDN,String name) {
|
||||
return new SynchroRelated(
|
||||
organization.getId(),
|
||||
organization.getName(),
|
||||
organization.getName(),
|
||||
Organizations.CLASS_TYPE,
|
||||
synchronizer.getId(),
|
||||
synchronizer.getName(),
|
||||
ldapDN,
|
||||
name,
|
||||
"",
|
||||
organization.getParentId(),
|
||||
synchronizer.getInstId());
|
||||
}
|
||||
|
||||
public Organizations buildOrganization(HashMap<String,Attribute> attributeMap,String name,String nameInNamespace) {
|
||||
try {
|
||||
Organizations org = new Organizations();
|
||||
@@ -158,16 +199,6 @@ public class LdapOrganizationService extends AbstractSynchronizerService implem
|
||||
org.setInstId(this.synchronizer.getInstId());
|
||||
org.setStatus(ConstsStatus.ACTIVE);
|
||||
_logger.info("org " + org);
|
||||
HistorySynchronizer historySynchronizer =new HistorySynchronizer();
|
||||
historySynchronizer.setId(historySynchronizer.generateId());
|
||||
historySynchronizer.setSyncId(this.synchronizer.getId());
|
||||
historySynchronizer.setSyncName(this.synchronizer.getName());
|
||||
historySynchronizer.setObjectId(org.getId());
|
||||
historySynchronizer.setObjectName(org.getName());
|
||||
historySynchronizer.setObjectType(Organizations.class.getSimpleName());
|
||||
historySynchronizer.setInstId(synchronizer.getInstId());
|
||||
historySynchronizer.setResult("success");
|
||||
this.historySynchronizerService.insert(historySynchronizer);
|
||||
return org;
|
||||
} catch (NamingException e) {
|
||||
_logger.error("NamingException " , e);
|
||||
|
||||
+19
-2
@@ -26,8 +26,10 @@ import javax.naming.directory.SearchResult;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.maxkey.constants.ldap.InetOrgPerson;
|
||||
import org.maxkey.crypto.DigestUtils;
|
||||
import org.maxkey.entity.HistorySynchronizer;
|
||||
import org.maxkey.entity.Organizations;
|
||||
import org.maxkey.entity.SynchroRelated;
|
||||
import org.maxkey.entity.UserInfo;
|
||||
import org.maxkey.persistence.ldap.LdapUtils;
|
||||
import org.maxkey.synchronizer.AbstractSynchronizerService;
|
||||
@@ -44,7 +46,7 @@ public class LdapUsersService extends AbstractSynchronizerService implements IS
|
||||
|
||||
public void sync() {
|
||||
_logger.info("Sync Ldap Users ...");
|
||||
loadOrgsById("1");
|
||||
loadOrgsByInstId(this.synchronizer.getInstId(),Organizations.ROOT_ORG_ID);
|
||||
try {
|
||||
SearchControls constraints = new SearchControls();
|
||||
constraints.setSearchScope(ldapUtils.getSearchScope());
|
||||
@@ -71,10 +73,25 @@ public class LdapUsersService extends AbstractSynchronizerService implements IS
|
||||
);
|
||||
attributeMap.put(objAttrs.getID(), objAttrs);
|
||||
}
|
||||
|
||||
String originId = DigestUtils.md5B64(sr.getNameInNamespace());
|
||||
UserInfo userInfo = buildUserInfo(attributeMap,sr.getName(),sr.getNameInNamespace());
|
||||
userInfo.setPassword(userInfo.getUsername() + UserInfo.DEFAULT_PASSWORD_SUFFIX);
|
||||
userInfoService.saveOrUpdate(userInfo);
|
||||
SynchroRelated synchroRelated = new SynchroRelated(
|
||||
userInfo.getId(),
|
||||
userInfo.getUsername(),
|
||||
userInfo.getDisplayName(),
|
||||
UserInfo.CLASS_TYPE,
|
||||
synchronizer.getId(),
|
||||
synchronizer.getName(),
|
||||
originId,
|
||||
userInfo.getDisplayName(),
|
||||
"",
|
||||
"",
|
||||
synchronizer.getInstId());
|
||||
|
||||
synchroRelatedService.updateSynchroRelated(
|
||||
this.synchronizer,synchroRelated,UserInfo.CLASS_TYPE);
|
||||
_logger.info("userInfo " + userInfo);
|
||||
}
|
||||
}
|
||||
|
||||
+54
-7
@@ -19,6 +19,7 @@ package org.maxkey.synchronizer.workweixin;
|
||||
|
||||
import org.maxkey.constants.ConstsStatus;
|
||||
import org.maxkey.entity.Organizations;
|
||||
import org.maxkey.entity.SynchroRelated;
|
||||
import org.maxkey.synchronizer.AbstractSynchronizerService;
|
||||
import org.maxkey.synchronizer.ISynchronizerService;
|
||||
import org.maxkey.synchronizer.workweixin.entity.WorkWeixinDepts;
|
||||
@@ -36,6 +37,7 @@ public class WorkweixinOrganizationService extends AbstractSynchronizerService i
|
||||
String access_token;
|
||||
|
||||
static String DEPTS_URL="https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=%s";
|
||||
static long ROOT_DEPT_ID = 1;
|
||||
|
||||
public void sync() {
|
||||
_logger.info("Sync Workweixin Organizations ...");
|
||||
@@ -44,9 +46,34 @@ public class WorkweixinOrganizationService extends AbstractSynchronizerService i
|
||||
WorkWeixinDeptsResponse rsp = requestDepartmentList(access_token);
|
||||
|
||||
for(WorkWeixinDepts dept : rsp.getDepartment()) {
|
||||
_logger.info("dept : " + dept.getId()+" "+ dept.getName()+" "+ dept.getParentid());
|
||||
Organizations organization = buildOrganization(dept);
|
||||
organizationsService.saveOrUpdate(organization);
|
||||
_logger.debug("dept : " + dept.getId()+" "+ dept.getName()+" "+ dept.getParentid());
|
||||
//root
|
||||
if(dept.getId() == ROOT_DEPT_ID) {
|
||||
Organizations rootOrganization = organizationsService.get(Organizations.ROOT_ORG_ID);
|
||||
SynchroRelated rootSynchroRelated = buildSynchroRelated(rootOrganization,dept);
|
||||
synchroRelatedService.updateSynchroRelated(
|
||||
this.synchronizer,rootSynchroRelated,Organizations.CLASS_TYPE);
|
||||
}else {
|
||||
//synchro Related
|
||||
SynchroRelated synchroRelated =
|
||||
synchroRelatedService.findByOriginId(
|
||||
this.synchronizer,dept.getId() + "",Organizations.CLASS_TYPE );
|
||||
|
||||
Organizations organization = buildOrganization(dept);
|
||||
if(synchroRelated == null) {
|
||||
organization.setId(organization.generateId());
|
||||
organizationsService.insert(organization);
|
||||
_logger.debug("Organizations : " + organization);
|
||||
|
||||
synchroRelated = buildSynchroRelated(organization,dept);
|
||||
}else {
|
||||
organization.setId(synchroRelated.getObjectId());
|
||||
organizationsService.update(organization);
|
||||
}
|
||||
|
||||
synchroRelatedService.updateSynchroRelated(
|
||||
this.synchronizer,synchroRelated,Organizations.CLASS_TYPE);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
@@ -55,23 +82,43 @@ public class WorkweixinOrganizationService extends AbstractSynchronizerService i
|
||||
|
||||
}
|
||||
|
||||
public SynchroRelated buildSynchroRelated(Organizations organization,WorkWeixinDepts dept) {
|
||||
return new SynchroRelated(
|
||||
organization.getId(),
|
||||
organization.getName(),
|
||||
organization.getName(),
|
||||
Organizations.CLASS_TYPE,
|
||||
synchronizer.getId(),
|
||||
synchronizer.getName(),
|
||||
dept.getId()+"",
|
||||
dept.getName(),
|
||||
"",
|
||||
dept.getParentid()+"",
|
||||
synchronizer.getInstId());
|
||||
}
|
||||
|
||||
public WorkWeixinDeptsResponse requestDepartmentList(String access_token) {
|
||||
HttpRequestAdapter request =new HttpRequestAdapter();
|
||||
String responseBody = request.get(String.format(DEPTS_URL, access_token));
|
||||
WorkWeixinDeptsResponse deptsResponse =JsonUtils.gson2Object(responseBody, WorkWeixinDeptsResponse.class);
|
||||
|
||||
_logger.info("response : " + responseBody);
|
||||
_logger.trace("response : " + responseBody);
|
||||
for(WorkWeixinDepts dept : deptsResponse.getDepartment()) {
|
||||
_logger.info("WorkWeixinDepts : " + dept);
|
||||
_logger.debug("WorkWeixinDepts : " + dept);
|
||||
}
|
||||
return deptsResponse;
|
||||
}
|
||||
|
||||
public Organizations buildOrganization(WorkWeixinDepts dept) {
|
||||
//Parent
|
||||
SynchroRelated synchroRelatedParent =
|
||||
synchroRelatedService.findByOriginId(
|
||||
this.synchronizer,dept.getParentid() + "",Organizations.CLASS_TYPE);
|
||||
Organizations org = new Organizations();
|
||||
org.setId(dept.getId()+"");
|
||||
org.setName(dept.getName());
|
||||
org.setParentId(dept.getParentid()+"");
|
||||
org.setCode(dept.getId()+"");
|
||||
org.setParentId(synchroRelatedParent.getObjectId());
|
||||
org.setParentName(synchroRelatedParent.getObjectName());
|
||||
org.setSortIndex(dept.getOrder());
|
||||
org.setInstId(this.synchronizer.getInstId());
|
||||
org.setStatus(ConstsStatus.ACTIVE);
|
||||
|
||||
+26
-10
@@ -17,11 +17,10 @@
|
||||
|
||||
package org.maxkey.synchronizer.workweixin;
|
||||
|
||||
import java.sql.Types;
|
||||
import java.util.List;
|
||||
|
||||
import org.maxkey.constants.ConstsStatus;
|
||||
import org.maxkey.entity.Organizations;
|
||||
import org.maxkey.entity.SynchroRelated;
|
||||
import org.maxkey.entity.UserInfo;
|
||||
import org.maxkey.synchronizer.AbstractSynchronizerService;
|
||||
import org.maxkey.synchronizer.ISynchronizerService;
|
||||
@@ -44,21 +43,38 @@ public class WorkweixinUsersService extends AbstractSynchronizerService implemen
|
||||
public void sync() {
|
||||
_logger.info("Sync Workweixin Users...");
|
||||
try {
|
||||
List<Organizations> organizations =
|
||||
organizationsService.find("instid = ?",
|
||||
new Object[] { this.synchronizer.getInstId() },
|
||||
new int[] { Types.VARCHAR});
|
||||
for(Organizations dept : organizations) {
|
||||
List<SynchroRelated> synchroRelateds =
|
||||
synchroRelatedService.findOrgs(this.synchronizer);
|
||||
|
||||
for(SynchroRelated relatedOrg : synchroRelateds) {
|
||||
HttpRequestAdapter request =new HttpRequestAdapter();
|
||||
String responseBody = request.get(String.format(USERS_URL, access_token,dept.getId()));
|
||||
String responseBody = request.get(String.format(USERS_URL, access_token,relatedOrg.getOriginId()));
|
||||
WorkWeixinUsersResponse usersResponse =JsonUtils.gson2Object(responseBody, WorkWeixinUsersResponse.class);
|
||||
_logger.info("response : " + responseBody);
|
||||
_logger.trace("response : " + responseBody);
|
||||
|
||||
for(WorkWeixinUsers user : usersResponse.getUserlist()) {
|
||||
UserInfo userInfo = buildUserInfo(user);
|
||||
_logger.info("userInfo : " + userInfo);
|
||||
_logger.debug("userInfo : " + userInfo);
|
||||
userInfo.setPassword(userInfo.getUsername() + UserInfo.DEFAULT_PASSWORD_SUFFIX);
|
||||
userInfoService.saveOrUpdate(userInfo);
|
||||
|
||||
SynchroRelated synchroRelated = new SynchroRelated(
|
||||
userInfo.getId(),
|
||||
userInfo.getUsername(),
|
||||
userInfo.getDisplayName(),
|
||||
UserInfo.CLASS_TYPE,
|
||||
synchronizer.getId(),
|
||||
synchronizer.getName(),
|
||||
user.getUserid(),
|
||||
user.getName(),
|
||||
user.getUserid(),
|
||||
"",
|
||||
synchronizer.getInstId());
|
||||
|
||||
synchroRelatedService.updateSynchroRelated(
|
||||
this.synchronizer,synchroRelated,UserInfo.CLASS_TYPE);
|
||||
|
||||
socialsAssociate(synchroRelated,"workweixin");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+43
-8
@@ -17,14 +17,19 @@
|
||||
|
||||
package org.maxkey.synchronizer;
|
||||
|
||||
import java.sql.Types;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.maxkey.entity.Organizations;
|
||||
import org.maxkey.entity.SocialsAssociate;
|
||||
import org.maxkey.entity.SynchroRelated;
|
||||
import org.maxkey.entity.Synchronizers;
|
||||
import org.maxkey.persistence.service.HistorySynchronizerService;
|
||||
import org.maxkey.persistence.service.OrganizationsService;
|
||||
import org.maxkey.persistence.service.SynchroRelatedService;
|
||||
import org.maxkey.persistence.service.UserInfoService;
|
||||
import org.maxkey.persistence.service.SocialsAssociatesService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -35,10 +40,12 @@ public abstract class AbstractSynchronizerService {
|
||||
|
||||
@Autowired
|
||||
protected OrganizationsService organizationsService;
|
||||
|
||||
@Autowired
|
||||
protected UserInfoService userInfoService;
|
||||
|
||||
@Autowired
|
||||
protected SynchroRelatedService synchroRelatedService;
|
||||
@Autowired
|
||||
protected SocialsAssociatesService socialsAssociatesService;
|
||||
@Autowired
|
||||
protected HistorySynchronizerService historySynchronizerService;
|
||||
|
||||
@@ -49,20 +56,20 @@ public abstract class AbstractSynchronizerService {
|
||||
protected Organizations rootOrganization = null;
|
||||
|
||||
|
||||
public HashMap<String,Organizations> loadOrgsById(String orgId) {
|
||||
List<Organizations> orgsList = organizationsService.query(null);
|
||||
if(orgId== null || orgId.equals("")) {
|
||||
orgId="1";
|
||||
public HashMap<String,Organizations> loadOrgsByInstId(String instId,String rootOrgId) {
|
||||
List<Organizations> orgsList = organizationsService.find("instid = '" + instId + "'");
|
||||
if(rootOrgId== null || rootOrgId.equals("")) {
|
||||
rootOrgId="1";
|
||||
}
|
||||
|
||||
for(Organizations org : orgsList) {
|
||||
if(org.getId().equals(orgId) && orgId.equals("1")) {
|
||||
if(org.getId().equals(rootOrgId) && rootOrgId.equals("1")) {
|
||||
rootOrganization = org;
|
||||
rootOrganization.setNamePath("/"+rootOrganization.getName());
|
||||
rootOrganization.setCodePath("/1");
|
||||
rootOrganization.setParentId("-1");
|
||||
rootOrganization.setParentName("");
|
||||
}else if(org.getId().equals(orgId)){
|
||||
}else if(org.getId().equals(rootOrgId)){
|
||||
rootOrganization = org;
|
||||
}
|
||||
}
|
||||
@@ -75,6 +82,26 @@ public abstract class AbstractSynchronizerService {
|
||||
return orgsNamePathMap;
|
||||
}
|
||||
|
||||
public void socialsAssociate(SynchroRelated synchroRelated,String provider) {
|
||||
SocialsAssociate socialsAssociate =
|
||||
socialsAssociatesService.findOne("instid = ? and userid = ? and socialuserid = ? and provider = ? ",
|
||||
new Object[] {
|
||||
synchroRelated.getInstId(),
|
||||
synchroRelated.getObjectId(),
|
||||
synchroRelated.getOriginId(),
|
||||
provider
|
||||
},
|
||||
new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,Types.VARCHAR});
|
||||
if(socialsAssociate == null) {
|
||||
socialsAssociate = new SocialsAssociate();
|
||||
socialsAssociate.setUserId(synchroRelated.getObjectId());
|
||||
socialsAssociate.setUsername(synchroRelated.getObjectName());
|
||||
socialsAssociate.setInstId(synchroRelated.getInstId());
|
||||
socialsAssociate.setProvider(provider);
|
||||
socialsAssociate.setSocialUserId(synchroRelated.getOriginId());
|
||||
socialsAssociatesService.insert(socialsAssociate);
|
||||
}
|
||||
}
|
||||
public void push(HashMap<String,Organizations> orgsNamePathMap,
|
||||
List<Organizations> orgsList,
|
||||
Organizations parentOrg) {
|
||||
@@ -141,6 +168,14 @@ public abstract class AbstractSynchronizerService {
|
||||
public void setHistorySynchronizerService(HistorySynchronizerService historySynchronizerService) {
|
||||
this.historySynchronizerService = historySynchronizerService;
|
||||
}
|
||||
|
||||
public SynchroRelatedService getSynchroRelatedService() {
|
||||
return synchroRelatedService;
|
||||
}
|
||||
|
||||
public void setSynchroRelatedService(SynchroRelatedService synchroRelatedService) {
|
||||
this.synchroRelatedService = synchroRelatedService;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user