BAEL-1509 jpa attribute converters (#3599)

* fix converter

* fix converter

* fix converter

* fix converter + more tests
This commit is contained in:
Marcos Lopez Gonzalez
2018-02-24 08:15:52 +01:00
committed by Grzegorz Piwowarek
parent 53627ae439
commit 2399b4d817
2 changed files with 156 additions and 12 deletions

View File

@@ -11,31 +11,49 @@ public class PersonNameConverter implements AttributeConverter<PersonName, Strin
private static final String SEPARATOR = ", ";
@Override
public String convertToDatabaseColumn(PersonName person) {
public String convertToDatabaseColumn(PersonName personName) {
if (personName == null) {
return null;
}
StringBuilder sb = new StringBuilder();
if (person.getSurname() != null) {
sb.append(person.getSurname());
if (personName.getSurname() != null && !personName.getSurname()
.isEmpty()) {
sb.append(personName.getSurname());
sb.append(SEPARATOR);
}
if (person.getName() != null) {
sb.append(person.getName());
if (personName.getName() != null && !personName.getName()
.isEmpty()) {
sb.append(personName.getName());
}
return sb.toString();
}
@Override
public PersonName convertToEntityAttribute(String dbPerson) {
String[] pieces = dbPerson.split(SEPARATOR);
if (pieces == null || pieces.length != 2) {
public PersonName convertToEntityAttribute(String dbPersonName) {
if (dbPersonName == null || dbPersonName.isEmpty()) {
return null;
}
PersonName personName = new PersonName();
personName.setSurname(pieces[0]);
personName.setName(pieces[1]);
String[] pieces = dbPersonName.split(SEPARATOR);
if (pieces == null || pieces.length == 0) {
return null;
}
PersonName personName = new PersonName();
String firstPiece = !pieces[0].isEmpty() ? pieces[0] : null;
if (dbPersonName.contains(SEPARATOR)) {
personName.setSurname(firstPiece);
if (pieces.length >= 2 && pieces[1] != null && !pieces[1].isEmpty()) {
personName.setName(pieces[1]);
}
} else {
personName.setName(firstPiece);
}
return personName;
}