de.tsl2.nano.replication.util.SimpleTransformer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tsl2.nano.replication Show documentation
Show all versions of tsl2.nano.replication Show documentation
Provides Database/XML Replication through JPA
package de.tsl2.nano.replication.util;
import java.lang.reflect.Field;
import java.util.function.Consumer;
/**
* tries to anonymize personal data.
*
* transforms all fields of an object, that match the regex found in system
* properties 'replication.transformer.regex' to the fixed string 'XXXXX'.
*
* @author ts
*/
public class SimpleTransformer implements Consumer {
static final String XXX = "XXXXX";
@Override
public void accept(T t) {
Field[] fields = t.getClass().getDeclaredFields();
String regex = System.getProperty("replication.transformer.regex", ".*[.]((pre|sur)?name|street|code|address)");
System.out.print("transforming entity: " + t + "... ");
int c = 0;
for (Field f : fields) {
if (String.class.isAssignableFrom(f.getType()) && f.toString().matches(regex)) {
try {
if (f.get(t) != null) {
f.set(t, XXX);
c++;
}
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
System.out.print(c + " fields transformed\n");
}
}