MultiTemplateContent.java

  1. package fr.sii.ogham.core.message.content;

  2. import fr.sii.ogham.core.resource.path.ResourcePath;
  3. import fr.sii.ogham.core.resource.path.UnresolvedPath;
  4. import fr.sii.ogham.core.template.context.BeanContext;
  5. import fr.sii.ogham.core.template.context.Context;
  6. import fr.sii.ogham.core.template.context.NullContext;

  7. /**
  8.  * <p>
  9.  * A shortcut for using several templates with same context.
  10.  * </p>
  11.  * <p>
  12.  * You have to put all templates to include at once at the same place. Each file
  13.  * must be named identically. Only the extension must be different.
  14.  * </p>
  15.  * <p>
  16.  * Then you only need to indicate the path to the templates without any
  17.  * extension and provide the context.
  18.  * </p>
  19.  * <p>
  20.  * You can also specify all the extensions/variants to append to the template
  21.  * path. By default, the extensions/variants will be {@link EmailVariant#HTML}
  22.  * and {@link EmailVariant#TEXT}
  23.  * </p>
  24.  *
  25.  * @author AurĂ©lien Baudet
  26.  *
  27.  */
  28. public class MultiTemplateContent extends MultiContent {

  29.     /**
  30.      * Initialize with the template path (without extension/variant) and the
  31.      * context. You may also specify the extensions to use.
  32.      *
  33.      * @param templatePath
  34.      *            the path to the template (without extension/variant)
  35.      * @param context
  36.      *            the context to share
  37.      * @param variants
  38.      *            the variants to specify
  39.      */
  40.     public MultiTemplateContent(String templatePath, Context context, Variant... variants) {
  41.         this(new UnresolvedPath(templatePath), context, variants);
  42.     }

  43.     /**
  44.      * Initialize with the template path (without extension/variant) and the
  45.      * context. It uses the default variants ({@link EmailVariant#HTML} and
  46.      * {@link EmailVariant#TEXT}).
  47.      *
  48.      * @param templatePath
  49.      *            the path to the template (without extension/variant)
  50.      * @param context
  51.      *            the context to share
  52.      */
  53.     public MultiTemplateContent(String templatePath, Context context) {
  54.         this(new UnresolvedPath(templatePath), context);
  55.     }

  56.     /**
  57.      * Initialize with the template path (without extension/variant) and the
  58.      * context as simple bean. You may also specify the extensions/variants to
  59.      * use.
  60.      *
  61.      * @param templatePath
  62.      *            the path to the template (without extension/variant)
  63.      * @param bean
  64.      *            the context to share as a simple POJO object
  65.      * @param extensions
  66.      *            the extensions to specify
  67.      */
  68.     public MultiTemplateContent(String templatePath, Object bean, Variant... extensions) {
  69.         this(new UnresolvedPath(templatePath), bean, extensions);
  70.     }

  71.     /**
  72.      * Initialize with the template path (without extension/variant) and the
  73.      * context as simple bean. It uses the default variants (
  74.      * {@link EmailVariant#HTML} and {@link EmailVariant#TEXT}).
  75.      *
  76.      * @param templatePath
  77.      *            the path to the template (without extension/variant)
  78.      * @param bean
  79.      *            the context to share as a simple POJO object
  80.      */
  81.     public MultiTemplateContent(String templatePath, Object bean) {
  82.         this(new UnresolvedPath(templatePath), bean);
  83.     }
  84.    

  85.     /**
  86.      * Initialize with the template path (without extension/variant) and the
  87.      * context. You may also specify the extensions to use.
  88.      *
  89.      * @param templatePath
  90.      *            the path to the template (without extension/variant)
  91.      * @param context
  92.      *            the context to share
  93.      * @param variants
  94.      *            the variants to specify
  95.      */
  96.     public MultiTemplateContent(ResourcePath templatePath, Context context, Variant... variants) {
  97.         super(createTemplates(templatePath, context, variants));
  98.     }

  99.     /**
  100.      * Initialize with the template path (without extension/variant) and the
  101.      * context. It uses the default variants ({@link EmailVariant#HTML} and
  102.      * {@link EmailVariant#TEXT}).
  103.      *
  104.      * @param templatePath
  105.      *            the path to the template (without extension/variant)
  106.      * @param context
  107.      *            the context to share
  108.      */
  109.     public MultiTemplateContent(ResourcePath templatePath, Context context) {
  110.         this(templatePath, context, EmailVariant.TEXT, EmailVariant.HTML);
  111.     }

  112.     /**
  113.      * Initialize with the template path (without extension/variant) and the
  114.      * context as simple bean. You may also specify the extensions/variants to
  115.      * use.
  116.      *
  117.      * @param templatePath
  118.      *            the path to the template (without extension/variant)
  119.      * @param bean
  120.      *            the context to share as a simple POJO object
  121.      * @param extensions
  122.      *            the extensions to specify
  123.      */
  124.     public MultiTemplateContent(ResourcePath templatePath, Object bean, Variant... extensions) {
  125.         this(templatePath, bean != null ? new BeanContext(bean) : new NullContext(), extensions);
  126.     }

  127.     /**
  128.      * Initialize with the template path (without extension/variant) and the
  129.      * context as simple bean. It uses the default variants (
  130.      * {@link EmailVariant#HTML} and {@link EmailVariant#TEXT}).
  131.      *
  132.      * @param templatePath
  133.      *            the path to the template (without extension/variant)
  134.      * @param bean
  135.      *            the context to share as a simple POJO object
  136.      */
  137.     public MultiTemplateContent(ResourcePath templatePath, Object bean) {
  138.         this(templatePath, bean != null ? new BeanContext(bean) : new NullContext());
  139.     }

  140.     private static TemplateContent[] createTemplates(ResourcePath templatePath, Context context, Variant[] variants) {
  141.         TemplateContent[] contents = new TemplateContent[variants.length];
  142.         for (int i = 0; i < variants.length; i++) {
  143.             contents[i] = new TemplateVariantContent(templatePath, variants[i], context);
  144.         }
  145.         return contents;
  146.     }
  147. }