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

com.arextest.schedule.plan.PlanContextCreator Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
package com.arextest.schedule.plan;

import com.arextest.schedule.client.HttpWepServiceApiClient;
import com.arextest.schedule.model.AppServiceDescriptor;
import com.arextest.schedule.model.AppServiceOperationDescriptor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author jmo
 * @since 2021/9/22
 */
@Component
public final class PlanContextCreator {

  @Resource
  private HttpWepServiceApiClient apiClient;
  @Value("${arex.api.config.applicationService.url}")
  private String applicationServiceUrl;

  public PlanContext createByAppId(String appId) {
    PlanContext planContext = new PlanContext();
    GenericResponseType responseType = apiClient.get(applicationServiceUrl,
        Collections.singletonMap("appId",
            appId),
        GenericResponseType.class);
    if (responseType == null) {
      return planContext;
    }
    planContext.setAppId(appId);
    List appServiceDescriptorList = responseType.getBody();
    if (CollectionUtils.isEmpty(appServiceDescriptorList)) {
      return planContext;
    }
    Map> serviceKeyAppServices = appServiceDescriptorList.stream()
        .collect(Collectors.groupingBy(AppServiceDescriptor::getServiceKey));
    List distinctAppServiceDescriptor = new ArrayList<>(
        appServiceDescriptorList.size());
    for (Map.Entry> appServiceDescriptor : serviceKeyAppServices.entrySet()) {
      List appServiceDescriptorValue = appServiceDescriptor.getValue();
      if (CollectionUtils.isNotEmpty(appServiceDescriptorValue)) {
        AppServiceDescriptor distinctAppService = appServiceDescriptorValue.get(0);
        setupOperationParent(distinctAppService);
        distinctAppServiceDescriptor.add(distinctAppService);
      }
    }
    planContext.setAppServiceDescriptorList(distinctAppServiceDescriptor);
    return planContext;
  }

  private void setupOperationParent(AppServiceDescriptor appServiceDescriptor) {
    List operationList = appServiceDescriptor.getOperationList();
    if (CollectionUtils.isEmpty(operationList)) {
      return;
    }
    for (AppServiceOperationDescriptor operationDescriptor : operationList) {
      operationDescriptor.setParent(appServiceDescriptor);
    }
  }

  @Data
  private final static class GenericResponseType {

    private List body;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy