file-transformer-rda-json/core/src/main/java/org/opencdmp/filetransformer/rda/model/rda/mapper/ContactRDAMapper.java

49 lines
1.7 KiB
Java

package org.opencdmp.filetransformer.rda.model.rda.mapper;
import org.opencdmp.commonmodels.enums.ContactInfoType;
import org.opencdmp.commonmodels.models.UserContactInfoModel;
import org.opencdmp.commonmodels.models.UserModel;
import org.opencdmp.filetransformer.rda.model.rda.Contact;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Component
public class ContactRDAMapper{
private final ContactIdRDAMapper contactIdRDAMapper;
public ContactRDAMapper(ContactIdRDAMapper contactIdRDAMapper) {
this.contactIdRDAMapper = contactIdRDAMapper;
}
public Contact toRDA(UserModel model) {
if (model == null) return null;
if (model.getName() == null) throw new IllegalArgumentException("Contact Name is missing");
UserContactInfoModel emailContact = model.getContacts() != null ? model.getContacts().stream().filter(userContactInfo -> userContactInfo.getType().equals(ContactInfoType.Email)).findFirst().orElse(null) : null;
if (emailContact == null) throw new IllegalArgumentException("Contact Email is missing");
Contact rda = new Contact();
rda.setName(model.getName());
rda.setMbox(emailContact.getValue());
rda.setContactId(contactIdRDAMapper.toRDA(emailContact.getId()));
return rda;
}
public UserModel toEntity(Contact rda) {
if (rda == null) return null;
UserModel entity = new UserModel();
entity.setName(rda.getName());
UserContactInfoModel emailContactInfo = new UserContactInfoModel();
emailContactInfo.setId(contactIdRDAMapper.toModel(rda.getContactId()));
emailContactInfo.setType(ContactInfoType.Email);
emailContactInfo.setValue(rda.getMbox());
entity.setContacts(List.of(emailContactInfo));
return entity;
}
}