public class SourceDescriptionMapper {
private static final Logger logger = LoggerFactory.getLogger(CommonMapper.class);
public void toSourceDescription(Source dqSource, GedcomxConversionResult result) throws IOException {
Marker sourceContext = ConversionContext.getDetachedMarker(String.format("@%s@ SOUR", dqSource.getId()));
ConversionContext.addReference(sourceContext);
try {
SourceDescription gedxSourceDescription = new SourceDescription();
gedxSourceDescription.setId(dqSource.getId());
if (dqSource.getAbbreviation() != null) {
gedxSourceDescription.setTitles(Arrays.asList(new TextValue(dqSource.getAbbreviation())));
}
else if (dqSource.getTitle() != null) {
gedxSourceDescription.setTitles(Arrays.asList(new TextValue(dqSource.getTitle())));
}
org.gedcomx.source.SourceCitation citation = new org.gedcomx.source.SourceCitation();
citation.setCitationTemplate(new ResourceReference(URI.create("gedcom5:citation-template")));
citation.setFields(new ArrayList<CitationField>());
citation.setValue("");
if (dqSource.getAuthor() != null) {
CitationField field = new CitationField();
field.setName(URI.create("gedcom5:citation-template/author"));
field.setValue(dqSource.getAuthor());
citation.getFields().add(field);
citation.setValue(citation.getValue() + (citation.getValue().length() > 0 ? ", " + dqSource.getAuthor() : dqSource.getAuthor()));
}
if (dqSource.getTitle() != null) {
CitationField field = new CitationField();
field.setName(URI.create("gedcom5:citation-template/title"));
field.setValue(dqSource.getTitle());
citation.getFields().add(field);
citation.setValue(citation.getValue() + (citation.getValue().length() > 0 ? ", " + dqSource.getTitle() : dqSource.getTitle()));
}
if (dqSource.getPublicationFacts() != null) {
CitationField field = new CitationField();
field.setName(URI.create("gedcom5:citation-template/publication-facts"));
field.setValue(dqSource.getPublicationFacts());
citation.getFields().add(field);
citation.setValue(citation.getValue() + (citation.getValue().length() > 0 ? ", " + dqSource.getPublicationFacts() : dqSource.getPublicationFacts()));
}
if (dqSource.getText() != null) {
logger.warn(ConversionContext.getContext(), "GEDCOM X does not currently support text extracted from a source.");
}
if (dqSource.getRepositoryRef() != null) {
Marker repoContext = ConversionContext.getDetachedMarker("REPO");
ConversionContext.addReference(repoContext);
try {
RepositoryRef dqRepositoryRef = dqSource.getRepositoryRef();
if (dqRepositoryRef.getRef() != null) {
gedxSourceDescription.setMediator(new ResourceReference(URI.create(CommonMapper.getOrganizationReference(dqRepositoryRef.getRef()))));
// TODO: map NOTEs as another note associated with this SourceDescription
}
else {
String inlineRepoId = dqSource.getId() + ".REPO";
Agent gedxOrganization = new Agent();
gedxOrganization.setId(inlineRepoId);
for (Note dqNote : dqRepositoryRef.getNotes()) {
org.gedcomx.common.Note gedxNote = new org.gedcomx.common.Note();
gedxNote.setText(dqNote.getValue());
gedxOrganization.addExtensionElement(gedxNote);
}
for (NoteRef dqNoteRef : dqRepositoryRef.getNoteRefs()) {
logger.warn(ConversionContext.getContext(), "Unable to associate a note ({}) with the inline-defined organization ({})", dqNoteRef.getRef(), inlineRepoId);
}
result.addOrganization(gedxOrganization);
gedxSourceDescription.setMediator(new ResourceReference(URI.create(CommonMapper.getOrganizationReference(inlineRepoId))));
}
if (dqRepositoryRef.getCallNumber() != null) {
CitationField field = new CitationField();
field.setName(URI.create("gedcom5:citation-template/call-number"));
field.setValue(dqRepositoryRef.getCallNumber());
citation.getFields().add(field);
citation.setValue(citation.getValue() + (citation.getValue().length() > 0 ? ", " + dqRepositoryRef.getCallNumber() : dqRepositoryRef.getCallNumber()));
}
}
finally {
ConversionContext.removeReference(repoContext);
}
}
if (dqSource.getCallNumber() != null) {
CitationField field = new CitationField();
field.setName(URI.create("gedcom5:citation-template/call-number"));
field.setValue(dqSource.getCallNumber());
citation.getFields().add(field);
citation.setValue(citation.getValue() + (citation.getValue().length() > 0 ? ", " + dqSource.getCallNumber() : dqSource.getCallNumber()));
}
if (citation.getValue().length() > 0) {
citation.setValue(citation.getValue() + '.');
gedxSourceDescription.setCitations(Arrays.asList(citation));
}
// dqSource.getMediaType(); // nothing equivalent in the GEDCOM X model
int cntNotes = dqSource.getNotes().size() + dqSource.getNoteRefs().size();
if (cntNotes > 0) {
logger.warn(ConversionContext.getContext(), "Did not process {} notes or references to notes.", cntNotes);
}
int cntMedia = dqSource.getMedia().size() + dqSource.getMediaRefs().size();
if (cntMedia > 0) {
logger.warn(ConversionContext.getContext(), "Did not process {} media items or references to media items.", cntMedia);
}
if (dqSource.getType() != null) {
Marker nameTypeContext = ConversionContext.getDetachedMarker(dqSource.getTypeTag());
ConversionContext.addReference(nameTypeContext);
logger.warn(ConversionContext.getContext(), "Source type ({}) was ignored.", dqSource.getType());
ConversionContext.removeReference(nameTypeContext);
}
if (dqSource.getDate() != null) {
Marker dateContext = ConversionContext.getDetachedMarker("DATE");
ConversionContext.addReference(dateContext);
logger.warn(ConversionContext.getContext(), "Specificaton does not define the meaning of DATE in this context; value ({}) was ignored.", dqSource.getDate());
ConversionContext.removeReference(dateContext);
}
if (dqSource.getReferenceNumber() != null) {
Marker refnContext = ConversionContext.getDetachedMarker("REFN");
ConversionContext.addReference(refnContext);
logger.warn(ConversionContext.getContext(), "User reference number ({}) was ignored.", dqSource.getReferenceNumber());
ConversionContext.removeReference(refnContext);
}
if (dqSource.getRin() != null) {
logger.warn(ConversionContext.getContext(), "RIN ({}) was ignored.", dqSource.getRin());
}
if (dqSource.getUid() != null) {
Marker uidContext = ConversionContext.getDetachedMarker(dqSource.getUidTag());
ConversionContext.addReference(uidContext);
logger.warn(ConversionContext.getContext(), "UID ({}) was ignored.", dqSource.getUid());
ConversionContext.removeReference(uidContext);
}