javahashmapxdocreport

XDocReport: How to manage a List/Map of images within a Collection?


I'm using XDocReport with Velocity to generate a DOCX file. I'm working currently on Images and, for the time being, I've been able to cope with them.

But now, I'm trying to figure out how to implement the following:

I've a set of Ports. Each port may have from 1 to N Catalogs. Each catalog have a HashMap (it may be changed to List) of IImageProvider.

I don't know whether is it possible to implement it or no. Here they are the example classes:

//The list of ports seems like:
List<Port> ports = ....

public class Port{

      private String descriptionPort;
      private String title;
      private String detail;
      private IImageProvider image1;
      private IImageProvider image2;
      private ArrayList<Catalogue> listCatalogue;
      .......
}

public class Catalogue{

      private String description;
      private String detail;
      private String duration;
      /* Formerly I had this one (it worked) instead of logos:
       * private IImageProvider logo;
       */
      //new: multiple images per catalogue
      private HashMap<String, IImageProvider> logos;
      private Number logosCount;
      ..........
}

So, when it comes to the creation of the DOCX Report:

InputStream in = fileTemp.getBinaryStream();
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in,   TemplateEngineKind.Velocity);
FieldsMetadata fieldsMetadata = report.createFieldsMetadata();
fieldsMetadata.load("ports", Port.class);
fieldsMetadata.addFieldAsImage("photoPort1", "p.Image1");
fieldsMetadata.addFieldAsImage("photoPort2", "p.Image2");
/*Formerly it worked this one when having a unique Image per Catalogue:
 *fieldsMetadata.addFieldAsImage("photoCat", "cat.Logo.");
 */
report.setFieldsMetadata(fieldsMetadata);
IContext context = report.createContext();
context.put("ports", ports);
report.process(context, outputStream); //ignore outputStream

Therefore, I wonder how can I add Images from the Hash of a Set of Catalogues. Its my proposal supported by XDocReport?

Thanks in advance. Every help would mean a lot for me, I'd be very grateful :).


Solution

  • Use POJO as described in the samples (DeveloperWithImage.java)

    /* Load the photos as list in the metadata */
    FieldsMetadata metadata = report.createFieldsMetadata();
    metadata.load( "photos", Photo.class, true );
    report.setFieldsMetadata(metadata);
    
    List<Photo> photos = ...
    context.put( "photos", photos );
    

    The Photo.java:

    public class Photo {
    private IImageProvider photo;
    
    @FieldMetadata( images = { @ImageMetadata( name = "photo" ) }, description="Photo"  )
    public IImageProvider getPhoto() {
        return photo;
    }
    
    public void setPhoto(IImageProvider photo) {
        this.photo = photo;
    }}
    

    The docx bookmark is done as below: enter image description here

    An article is shared here