Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
6.4 kB
2
Indexable
Never
@Service
@Slf4j
public class ChangeLogServiceImpl implements ChangeLogService {

    @Autowired
    private FeatureService featureService;

    @Autowired
    private ChangeLogRepository changeLogRepository;

    @Override
    public List<ChangeLog> getAll() {
        return changeLogRepository.findAll();
    }

    @Override
    public Page<ChangeLog> getAll(Pageable pageable) {
        Page<ChangeLog> allFeatureChangeLog = changeLogRepository.findAll(pageable);
        for (ChangeLog changeLog : allFeatureChangeLog.getContent()) {
            changeLog.setFeatureName(featureService.getFeature(changeLog.getFeatureId()).getKey());
        }
        return allFeatureChangeLog;
    }

    @Override
    public Page<ChangeLog> getAll(int featureId, Pageable pageable) {
        return changeLogRepository.findAllByFeatureId(featureId, pageable);
    }

    @Override
    public void deleteAll() {
        changeLogRepository.deleteAll();
    }

    @Override
    public void save(ChangeLog changeLog) {
        changeLogRepository.save(changeLog);
    }
}
///////////////////
@Slf4j
@Service
public class TssServiceImpl implements TssService {

    private static final String TSS_KEY = "Tss";
    private static final String CUSTOMER_FILE_FIELD_KEY = "TssCustomizationFile";
    private static final String CUSTOMER_FILE_NAME_FORMAT = "customer_%s.xml";

    @Autowired
    private FeatureService featureService;

    @Autowired
    private FeatureRuleConfigService featureRuleConfigService;

    @Autowired
    private FeatureExtraDataService featureExtraDataService;

    @Override
    @Transactional
    public void applyCustomerFile(ParameterDTO parameter, MultipartFile customerFile) {

        Feature tssFeature = featureService.getFeature(TSS_KEY);
        if (tssFeature == null || tssFeature.getId() == null) {
            throw new FeatureNotFoundException();
        }

        String customerFileUrl = uploadCustomerFile(tssFeature.getId(), parameter, customerFile);

        // If the rule is not created yet, create the rule and apply the file url.
        // As of now, each rule is different only by the operator code.

        if (TextUtils.isEmpty(parameter.getOperatorCode())) {
            throw new InvalidParameterException("'oc' parameter is required");
        }

        Rule rule = Rule.createBaseRule();
        rule.setOperatorCode(parameter.getOperatorCode().toUpperCase());
        FeatureRuleConfig featureRuleConfig = featureRuleConfigService
                .getFeatureRuleConfig(tssFeature.getId(), rule);
        if (featureRuleConfig == null) {
            featureRuleConfig = createFeatureRuleConfig(tssFeature.getId(), rule);

            FeatureRuleConfigField customerFileField = featureRuleConfig.getFields().stream()
                    .filter(field -> field.getKey().equals(CUSTOMER_FILE_FIELD_KEY))
                    .findFirst().orElseThrow(FieldKeyNotFoundException::new);
            customerFileField.setValue(customerFileUrl);
            featureRuleConfigService.updateFeatureRuleConfigById(
                    featureRuleConfig.getId(), featureRuleConfig);
        }
    }

    private String uploadCustomerFile(int tssFeatureId, ParameterDTO parameter,
                                      MultipartFile customerFile) {
        try {
            // As of now, each rule is different only by the operator code.
            String name = String.format(CUSTOMER_FILE_NAME_FORMAT,
                    parameter.getOperatorCode().toUpperCase());
            customerFile = new MockMultipartFile(
                    name, name, ContentType.APPLICATION_XML.getMimeType(), customerFile.getBytes());
        } catch (IOException exception) {
            String errorMsg = String.format("Failed to upload customer file: %s",
                    exception.getMessage());
            log.error(errorMsg);
            throw new MultipleExtraDataUploadException(errorMsg);
        }

        return featureExtraDataService.uploadFeatureExtraData(
                tssFeatureId, new MultipartFile[]{customerFile}, ExtraDataType.EXTRA,
                ContentType.APPLICATION_XML.getMimeType(), true);
    }

    private FeatureRuleConfig createFeatureRuleConfig(int tssFeatureId, Rule rule) {

        FeatureRuleConfig featureRuleConfig = new FeatureRuleConfig();
        featureRuleConfig.setFeatureId(tssFeatureId);
        featureRuleConfig.setSupport(true);
        featureRuleConfig.setExclude(false);
        featureRuleConfig.setRule(rule);
        featureRuleConfig.setFields(new ArrayList<>());

        // Copy all fields from base rule config.
        FeatureRuleConfig baseRuleConfig = featureRuleConfigService
                .getAllFeatureRuleConfigs(tssFeatureId).stream()
                .filter(ruleConfig -> ruleConfig.getRule().isBaseRule())
                .findFirst().orElseThrow(BaseRuleNotFoundException::new);
        baseRuleConfig.getFields().forEach(field -> {
            FeatureRuleConfigField newField = new FeatureRuleConfigField();
            newField.setKey(field.getKey());
            newField.setValue(field.getValue());
            newField.setType(field.getType());
            newField.setValueType(field.getValueType());
            featureRuleConfig.getFields().add(newField);
        });

        return featureRuleConfigService.createFeatureRuleConfig(tssFeatureId, featureRuleConfig);
    }
}
//////////////////////
@Slf4j
@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@Tag(name = "Tss API", description = "The APIs for TSS feature")
public class TssApiController {

    @Autowired
    private TssService tssService;

    @PostMapping(value = "/v1/tss/customer")
    @Operation(summary = "Apply customer.xml file to the rule")
    public ResultDTO applyCustomerFile(@NonNull HttpServletRequest request,
                                       @RequestPart("customerFile") MultipartFile customerFile) {
        log.info("applyCustomerFile: name: {}, size: {}", customerFile.getOriginalFilename(),
                customerFile.getSize());

        tssService.applyCustomerFile(ParameterDTO.extractConfigParameters(request), customerFile);
        return new ResultDTO(HttpStatus.OK);
    }
}
Leave a Comment