@ -6,43 +6,46 @@ import static java.lang.System.Logger.Level.TRACE;
import static java.text.MessageFormat.format ;
import static java.text.MessageFormat.format ;
import de.srsoftware.umbrella.core.model.UmbrellaUser ;
import de.srsoftware.umbrella.core.model.UmbrellaUser ;
import java.util.ArrayList ;
import java.util.HashSet ;
import java.util.* ;
import java.util.List ;
import java.util.function.BiFunction ;
import java.util.Set ;
import java.util.function.Function ;
import java.util.function.Function ;
public class CombinedMessage {
public class CombinedMessage {
private static final System . Logger LOG = System . getLogger ( CombinedMessage . class . getSimpleName ( ) ) ;
private static final System . Logger LOG = System . getLogger ( CombinedMessage . class . getSimpleName ( ) ) ;
private final Set < Attachment > attachments = new HashSet < > ( ) ;
private final Set < Attachment > attachments = new HashSet < > ( ) ;
private final StringBuilder body = new StringBuilder ( ) ;
private final StringBuilder combinedBody = new StringBuilder ( ) ;
private String combinedSubject = null ;
private final List < Message > mergedMessages = new ArrayList < > ( ) ;
private final List < Message > mergedMessages = new ArrayList < > ( ) ;
private final Function < String , String > translate ;
private final String subjectForCombinedMessage ;
private final BiFunction < String , Map < String , String > , String > translate ;
private UmbrellaUser sender = null ;
private UmbrellaUser sender = null ;
private String subject = null ;
public CombinedMessage ( Function < String , String > translate ) {
public CombinedMessage ( String subjectForCombinedMessage , Bi Function< String , Map < String , String > , String > translateFunction ) {
LOG . log ( DEBUG , "Creating combined message…" ) ;
LOG . log ( DEBUG , "Creating combined message…" ) ;
this . translate = translate ;
this . subjectForCombinedMessage = subjectForCombinedMessage ;
translate = translateFunction ;
}
}
public void merge ( Message message ) {
public void merge ( Message message ) {
LOG . log ( TRACE , "Merging {0} into combined message…" , message ) ;
LOG . log ( TRACE , "Merging {0} into combined message…" , message ) ;
var body = translate . apply ( message . body ( ) , message . fills ( ) ) ;
var subject = translate . apply ( message . subject ( ) , message . fills ( ) ) ;
switch ( mergedMessages . size ( ) ) {
switch ( mergedMessages . size ( ) ) {
case 0 :
case 0 :
body . append ( message . body ( ) ) ;
com binedB ody. append ( body ) ;
sender = message . sender ( ) ;
sender = message . sender ( ) ;
s ubject = message . subject ( ) ;
combinedS ubject = subject ;
break ;
break ;
case 1 :
case 1 :
body . insert ( 0 , format ( "# {0}:\n# {1}:\n\n" , sender , subject ) ) ; // insert sender and subject of first message right before the body of the first message
com binedB ody. insert ( 0 , format ( "# {0}:\n# {1}:\n\n" , sender , subject ) ) ; // insert sender and subject of first message right before the body of the first message
subject = translate . apply ( "Collected messages" ) ;
combinedSubject = subjectForCombinedMessage ;
// no break here, we need to append the subject and content
// no break here, we need to append the subject and content
default :
default :
body . append ( "\n\n# " ) . append ( message . sender ( ) ) . append ( ":\n" ) ;
com binedB ody. append ( "\n\n# " ) . append ( message . sender ( ) ) . append ( ":\n" ) ;
body . append ( "# " ) . append ( message . subject ( ) ) . append ( ":\n\n" ) ;
com binedB ody. append ( "# " ) . append ( subject ) . append ( ":\n\n" ) ;
body . append ( message . body ( ) ) ;
com binedB ody. append ( body ) ;
}
}
if ( message . attachments ( ) ! = null ) attachments . addAll ( message . attachments ( ) ) ;
if ( message . attachments ( ) ! = null ) attachments . addAll ( message . attachments ( ) ) ;
mergedMessages . add ( message ) ;
mergedMessages . add ( message ) ;
@ -53,7 +56,7 @@ public class CombinedMessage {
}
}
public String body ( ) {
public String body ( ) {
return body . toString ( ) ;
return com binedB ody. toString ( ) ;
}
}
public UmbrellaUser sender ( ) {
public UmbrellaUser sender ( ) {
@ -61,7 +64,7 @@ public class CombinedMessage {
}
}
public String subject ( ) {
public String subject ( ) {
return s ubject;
return combinedS ubject;
}
}
public List < Message > messages ( ) {
public List < Message > messages ( ) {