All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.flyfish.oauth.common.sync.methods.PostSyncRequestAcceptor Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
package com.flyfish.oauth.common.sync.methods;

import com.flyfish.oauth.client.RestClient;
import com.flyfish.oauth.common.Function;
import com.flyfish.oauth.common.sync.AbstractSyncRequestAcceptor;
import com.flyfish.oauth.common.sync.support.CollectionComparator;
import com.flyfish.oauth.configuration.sync.SyncDomainService;
import com.flyfish.oauth.utils.ContainUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 接收post请求的同步操作
 * 新增本次提交的所有数据,标记唯一标识,如果存在则跳过
 *
 * @author wangyu
 */
public class PostSyncRequestAcceptor extends AbstractSyncRequestAcceptor {

    /**
     * 比较并返回比较结果,根据不同实现策略进行不同的比较
     *
     * @param originList    原数据
     * @param domainService 数据service
     * @return 结果
     */
    @Override
    protected  CollectionComparator compare(List originList, final SyncDomainService domainService) throws Exception {
        Function keyFunction = identifierFunction(domainService);
        List identifiers = ContainUtils.getIdentifiers(originList, keyFunction);
        // 查询出本批次存在的数据
        List saved = domainService.getList(identifiers);
        // 移除本次冲突的部分
        CollectionComparator comparator = CollectionComparator.newInstance(saved, originList, keyFunction);
        List added = comparator.added();
        // 写入
        domainService.saveAll(added);
        return comparator;
    }

    /**
     * 匹配
     *
     * @param request 请求
     * @return 结果
     */
    @Override
    public boolean accept(HttpServletRequest request) {
        return request.getMethod().equalsIgnoreCase(RestClient.HttpMethod.POST.name());
    }
}