import static com.broadleafcommerce.common.privacy.anonymization.Constants.ANONYMOUS_ALPHA_STANDARD;
import static com.broadleafcommerce.common.privacy.anonymization.Constants.ANONYMOUS_EMAIL;
import static com.broadleafcommerce.customer.provider.RouteConstants.Persistence.CUSTOMER_ROUTE_PACKAGE;
@RequiredArgsConstructor
public class CustomerAnonymizationHandler extends AbstractAnonymizationHandler {
private final CustomerService<Customer> customerService; # (1)
@Override
public String getDataRoutePartition() {
return CUSTOMER_ROUTE_PACKAGE; # (2)
}
@Override
public int getOrder() {
return -100; # (3)
}
@Override
public List<AnonymizationRecord> anonymize(String boundedContextIdentifier,
Object context,
PrivacyUtility privacyUtility) { # (4)
ContextInfo contextInfo = (ContextInfo) context;
if (contextInfo == null) {
contextInfo = new ContextInfo(OperationType.READ);
}
Customer customer = customerService.readByContextId(boundedContextIdentifier, contextInfo);
return handleRecords(boundedContextIdentifier, customer, contextInfo, privacyUtility);
}
protected List<AnonymizationRecord> handleRecords(String boundedContextIdentifier,
Customer member,
ContextInfo contextInfo,
PrivacyUtility privacyUtility) {
List<AnonymizationRecord> records = new ArrayList<>();
records.add(process(boundedContextIdentifier, Customer.class.getSimpleName(), "email",
member::getEmail, member::setEmail, ANONYMOUS_EMAIL));
records.add(process(boundedContextIdentifier, Customer.class.getSimpleName(), "firstName",
member::getFirstName, member::setFirstName, ANONYMOUS_ALPHA_STANDARD));
records.add(process(boundedContextIdentifier, Customer.class.getSimpleName(), "lastName",
member::getLastName, member::setLastName, ANONYMOUS_ALPHA_STANDARD));
records.add(process(boundedContextIdentifier, Customer.class.getSimpleName(), "middleName",
member::getMiddleName, member::setMiddleName, ANONYMOUS_ALPHA_STANDARD));
records.add(process(boundedContextIdentifier, Customer.class.getSimpleName(), "fullName",
member::getFullName, member::setFullName, ANONYMOUS_ALPHA_STANDARD));
records.add(process(boundedContextIdentifier, Customer.class.getSimpleName(), "username",
member::getUsername, member::setUsername, ANONYMOUS_EMAIL));
records.addAll(process(boundedContextIdentifier, member.getPhone(),
Customer.class.getSimpleName(), "phone"));
records.addAll(processAdditionalPhones(boundedContextIdentifier,
member.getAdditionalPhones(), Customer.class.getSimpleName(), "additionalPhones"));
try {
customerService.replace(member.getId(), member,
contextInfo.withOperationType(contextInfo, OperationType.UPDATE));
} catch (RuntimeException e) {
records.clear();
records.add(fail(boundedContextIdentifier, Customer.class.getSimpleName(), e, false));
}
return records;
}
}