Search in sources :

Example 1 with FeishuDeptsResponse

use of org.maxkey.synchronizer.feishu.entity.FeishuDeptsResponse in project MaxKey by dromara.

the class FeishuOrganizationService method requestDepartment.

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;
}
Also used : HttpRequestAdapter(org.maxkey.web.HttpRequestAdapter) HashMap(java.util.HashMap) FeishuDeptsResponse(org.maxkey.synchronizer.feishu.entity.FeishuDeptsResponse)

Example 2 with FeishuDeptsResponse

use of org.maxkey.synchronizer.feishu.entity.FeishuDeptsResponse in project MaxKey by dromara.

the class FeishuOrganizationService method requestDepartmentList.

public FeishuDeptsResponse requestDepartmentList(String access_token, String deptId) {
    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(DEPTS_URL, deptId), headers);
    FeishuDeptsResponse deptsResponse = JsonUtils.gson2Object(responseBody, FeishuDeptsResponse.class);
    _logger.trace("response : " + responseBody);
    return deptsResponse;
}
Also used : HttpRequestAdapter(org.maxkey.web.HttpRequestAdapter) HashMap(java.util.HashMap) FeishuDeptsResponse(org.maxkey.synchronizer.feishu.entity.FeishuDeptsResponse)

Example 3 with FeishuDeptsResponse

use of org.maxkey.synchronizer.feishu.entity.FeishuDeptsResponse in project MaxKey by dromara.

the class FeishuOrganizationService method sync.

public void sync() {
    _logger.info("Sync Feishu Organizations ...");
    LinkedBlockingQueue<String> deptsQueue = new LinkedBlockingQueue<String>();
    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.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());
                    // 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);
                }
            }
        }
    } catch (NoSuchElementException e) {
        _logger.debug("Sync Department successful .");
    }
}
Also used : FeishuDepts(org.maxkey.synchronizer.feishu.entity.FeishuDepts) Organizations(org.maxkey.entity.Organizations) SynchroRelated(org.maxkey.entity.SynchroRelated) FeishuDeptsResponse(org.maxkey.synchronizer.feishu.entity.FeishuDeptsResponse) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

FeishuDeptsResponse (org.maxkey.synchronizer.feishu.entity.FeishuDeptsResponse)3 HashMap (java.util.HashMap)2 HttpRequestAdapter (org.maxkey.web.HttpRequestAdapter)2 NoSuchElementException (java.util.NoSuchElementException)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 Organizations (org.maxkey.entity.Organizations)1 SynchroRelated (org.maxkey.entity.SynchroRelated)1 FeishuDepts (org.maxkey.synchronizer.feishu.entity.FeishuDepts)1