Spaces:
Build error
Build error
File size: 6,228 Bytes
9373c61 5cfe5c4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
package com.dalab.policyengine.mapper;
import java.util.Collections;
import java.util.UUID;
import java.util.stream.Collectors;
import org.springframework.stereotype.Component;
import com.dalab.policyengine.dto.PolicyEvaluationOutputDTO;
import com.dalab.policyengine.dto.PolicyEvaluationSummaryDTO;
// Add missing DTO imports
import com.dalab.policyengine.dto.PolicyInputDTO;
import com.dalab.policyengine.dto.PolicyOutputDTO;
import com.dalab.policyengine.dto.PolicyRuleDTO;
import com.dalab.policyengine.dto.PolicySummaryDTO;
// Add missing entity imports
import com.dalab.policyengine.model.Policy;
import com.dalab.policyengine.model.PolicyEvaluation;
import com.dalab.policyengine.model.PolicyEvaluationStatus;
import com.dalab.policyengine.model.PolicyRule;
@Component
public class PolicyMapper {
public PolicySummaryDTO toPolicySummaryDTO(Policy policy) {
if (policy == null) return null;
PolicySummaryDTO dto = new PolicySummaryDTO();
dto.setId(policy.getId());
dto.setName(policy.getName());
dto.setDescription(policy.getDescription());
dto.setStatus(policy.getStatus());
dto.setRuleCount(policy.getRules() != null ? policy.getRules().size() : 0);
dto.setCreatedAt(policy.getCreatedAt());
dto.setUpdatedAt(policy.getUpdatedAt());
return dto;
}
public PolicyOutputDTO toPolicyOutputDTO(Policy policy) {
if (policy == null) return null;
PolicyOutputDTO dto = new PolicyOutputDTO();
dto.setId(policy.getId());
dto.setName(policy.getName());
dto.setDescription(policy.getDescription());
dto.setStatus(policy.getStatus());
dto.setConditionLogic(policy.getConditionLogic());
dto.setActions(policy.getActions());
dto.setRules(policy.getRules().stream().map(this::toPolicyRuleDTO).collect(Collectors.toList()));
dto.setCreatedAt(policy.getCreatedAt());
dto.setUpdatedAt(policy.getUpdatedAt());
dto.setCreatedByUserId(policy.getCreatedByUserId());
dto.setUpdatedByUserId(policy.getUpdatedByUserId());
return dto;
}
public PolicyRuleDTO toPolicyRuleDTO(PolicyRule policyRule) {
if (policyRule == null) return null;
PolicyRuleDTO dto = new PolicyRuleDTO();
dto.setId(policyRule.getId() != null ? policyRule.getId().toString() : null);
dto.setName(policyRule.getName());
dto.setDescription(policyRule.getDescription());
dto.setCondition(policyRule.getCondition());
dto.setPriority(policyRule.getPriority());
dto.setActions(policyRule.getActions());
return dto;
}
public Policy toPolicyEntity(PolicyInputDTO dto) {
if (dto == null) return null;
Policy policy = new Policy();
updatePolicyEntityFromInputDTO(policy, dto); // Reuse logic for update
return policy;
}
public void updatePolicyEntityFromInputDTO(Policy policy, PolicyInputDTO dto) {
if (policy == null || dto == null) return;
policy.setName(dto.getName());
policy.setDescription(dto.getDescription());
policy.setStatus(dto.getStatus());
policy.setConditionLogic(dto.getConditionLogic());
policy.setActions(dto.getActions());
// Handle rules update carefully
if (dto.getRules() != null) {
// Simple strategy: remove all existing and add new ones based on DTO
// More sophisticated merging could be done (e.g. based on rule ID or name)
policy.getRules().clear();
dto.getRules().forEach(ruleDTO -> policy.addRule(toPolicyRuleEntity(ruleDTO)));
} else {
policy.setRules(Collections.emptyList());
}
}
public PolicyRule toPolicyRuleEntity(PolicyRuleDTO dto) {
if (dto == null) return null;
PolicyRule rule = new PolicyRule();
// ID is not set here for new rules, or could be used to fetch existing if updating
// For simplicity, if ID is present in DTO, we assume it might be used by service layer
// to find and update existing rule, but mapper focuses on new entity creation or field mapping.
if (dto.getId() != null) {
try {
rule.setId(UUID.fromString(dto.getId()));
} catch (IllegalArgumentException e) {
// Handle invalid UUID string if necessary, or let it be null for new entity
}
}
rule.setName(dto.getName());
rule.setDescription(dto.getDescription());
rule.setCondition(dto.getCondition());
rule.setPriority(dto.getPriority());
rule.setActions(dto.getActions());
return rule;
}
public PolicyEvaluationOutputDTO toPolicyEvaluationOutputDTO(PolicyEvaluation evaluation, String policyName) {
if (evaluation == null) return null;
PolicyEvaluationOutputDTO dto = new PolicyEvaluationOutputDTO();
dto.setId(evaluation.getId());
dto.setPolicyId(evaluation.getPolicyId());
dto.setPolicyName(policyName); // Policy name fetched separately for convenience
dto.setTargetAssetId(evaluation.getTargetAssetId());
dto.setStatus(evaluation.getStatus());
dto.setEvaluationDetails(evaluation.getEvaluationDetails());
dto.setTriggeredActions(evaluation.getTriggeredActions());
dto.setEvaluatedAt(evaluation.getEvaluatedAt());
dto.setEvaluationTriggeredByUserId(evaluation.getEvaluationTriggeredByUserId());
return dto;
}
public PolicyEvaluationSummaryDTO toPolicyEvaluationSummaryDTO(PolicyEvaluation evaluation, String policyName) {
if (evaluation == null) return null;
PolicyEvaluationSummaryDTO dto = new PolicyEvaluationSummaryDTO();
dto.setId(evaluation.getId());
dto.setPolicyId(evaluation.getPolicyId());
dto.setPolicyName(policyName);
dto.setTargetAssetId(evaluation.getTargetAssetId());
dto.setStatus(evaluation.getStatus());
dto.setEvaluatedAt(evaluation.getEvaluatedAt());
return dto;
}
} |