use of org.incode.module.communications.dom.impl.comms.Communication in project estatio by estatio.
the class Document_sendByEmail method act.
@Action(semantics = SemanticsOf.NON_IDEMPOTENT, domainEvent = ActionDomainEvent.class)
@ActionLayout(cssClassFa = "at", contributed = Contributed.AS_ACTION)
public Communication act(@ParameterLayout(named = "to:") final EmailAddress toChannel, @Parameter(optionality = Optionality.OPTIONAL, maxLength = CommunicationChannel.EmailType.Meta.MAX_LEN, regexPattern = CommunicationChannel.EmailType.Meta.REGEX, regexPatternReplacement = CommunicationChannel.EmailType.Meta.REGEX_DESC) @ParameterLayout(named = "cc:") final String cc, @Parameter(optionality = Optionality.OPTIONAL, maxLength = CommunicationChannel.EmailType.Meta.MAX_LEN, regexPattern = CommunicationChannel.EmailType.Meta.REGEX, regexPatternReplacement = CommunicationChannel.EmailType.Meta.REGEX_DESC) @ParameterLayout(named = "cc (2):") final String cc2, @Parameter(optionality = Optionality.OPTIONAL, maxLength = CommunicationChannel.EmailType.Meta.MAX_LEN, regexPattern = CommunicationChannel.EmailType.Meta.REGEX, regexPatternReplacement = CommunicationChannel.EmailType.Meta.REGEX_DESC) @ParameterLayout(named = "cc (3):") final String cc3, @Parameter(optionality = Optionality.OPTIONAL, maxLength = CommunicationChannel.EmailType.Meta.MAX_LEN, regexPattern = CommunicationChannel.EmailType.Meta.REGEX, regexPatternReplacement = CommunicationChannel.EmailType.Meta.REGEX_DESC) @ParameterLayout(named = "bcc:") final String bcc, @Parameter(optionality = Optionality.OPTIONAL, maxLength = CommunicationChannel.EmailType.Meta.MAX_LEN, regexPattern = CommunicationChannel.EmailType.Meta.REGEX, regexPatternReplacement = CommunicationChannel.EmailType.Meta.REGEX_DESC) @ParameterLayout(named = "bcc (2):") final String bcc2) throws IOException {
if (this.document.getState() == DocumentState.NOT_RENDERED) {
// this shouldn't happen, but want to fail-fast in case a future programmer calls this directly
throw new IllegalArgumentException("Document is not yet rendered");
}
// create cover note
//
// nb: there is a presumption is that the cover note will not be automatically attached to any other objects,
// ie its AttachmentAdvisor should be AttachToNone.
final DocumentTemplate coverNoteTemplate = determineEmailCoverNoteTemplate();
final Document coverNoteDoc = documentCreatorService.createDocumentAndAttachPaperclips(this.document, coverNoteTemplate);
coverNoteDoc.render(coverNoteTemplate, this.document);
// create comm and correspondents
final String atPath = document.getAtPath();
final String subject = stripFileExtensionIfAny(coverNoteDoc.getName());
final Communication communication = communicationRepository.createEmail(subject, atPath, toChannel, cc, cc2, cc3, bcc, bcc2);
transactionService.flushTransaction();
// manually attach the cover note to the comm
paperclipRepository.attach(coverNoteDoc, DocumentConstants.PAPERCLIP_ROLE_COVER, communication);
// also attach this "primary" document to the comm
paperclipRepository.attach(this.document, DocumentConstants.PAPERCLIP_ROLE_PRIMARY, communication);
// also copy over as attachments to the comm anything else also attached to primary document
final List<Document> communicationAttachments = attachmentProvider.attachmentsFor(document);
for (Document communicationAttachment : communicationAttachments) {
paperclipRepository.attach(communicationAttachment, DocumentConstants.PAPERCLIP_ROLE_ATTACHMENT, communication);
}
transactionService.flushTransaction();
// finally, schedule the email to be sent
communication.scheduleSend();
return communication;
}
Aggregations