dbarra / xdocreport

Automatically exported from code.google.com/p/xdocreport
1 stars 0 forks source link

dynamic image within foreach. #288

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1.add the image field into metadata with addFieldAsImage
2. add the image field into the context
3. create the report

What is the expected output? What do you see instead?
different image for each loop.
Throws an exception:

org.apache.velocity.exception.MethodInvocationException: Invocation of method 
'registerImage' in  class 
fr.opensagres.xdocreport.document.odt.images.ODTImageRegistry threw exception 
fr.opensagres.xdocreport.core.XDocReportException: Image provider for field 
[stkPic] cannot be null! at 
fr.opensagres.xdocreport.document.odt.ODTReport@35371c0c!content.xml[line 1, 
column 28415]

What version of the product are you using? On what operating system?
xdocreport 1.0.3, windows 7, velocity

Please provide any additional information below.
Attached sample code of inserting image in metadata and context and an ODT 
file. As far as I know the imaga I'm passing in not null.

Original issue reported on code.google.com by PanicMom...@gmail.com on 19 Aug 2013 at 11:08

Attachments:

GoogleCodeExporter commented 8 years ago
I suggest you to see sample 
https://code.google.com/p/xdocreport/source/browse/samples/fr.opensagres.xdocrep
ort.samples.odtandvelocity/src/fr/opensagres/xdocreport/samples/odtandvelocity/O
DTProjectWithVelocityAndImageList.java?repo=samples 

You must set the image as list with addFieldAsList.

Regards Angelo

Original comment by angelo.z...@gmail.com on 19 Aug 2013 at 12:07

GoogleCodeExporter commented 8 years ago
Do I need to addFieldAsList even if I'm only using it within foreach loop? not 
inside a table?

tried adding as list with addFieldAsList but still getting the same exception.

Original comment by PanicMom...@gmail.com on 22 Aug 2013 at 6:37

GoogleCodeExporter commented 8 years ago
Ooops sorry for my answer. You use #foreach, so you must not to use 
addFieldAsList.

In your odt, you use : 

---------------------------------------
#foreach($msh in $cprsMajorStockHolderTabRecords)

   stkPic (Image name)
   $msh.ideNamFirst

#end
---------------------------------------

and you fill the context like this : 

---------------------------------------
context.put("stkPic", picture);
---------------------------------------

Are you sure that it's the well odt or the well Java context?
To work this report, you must put in the context a Java collection which 
contains IImageProvider ;

---------------------------------------
Collection<IIMageProvider> cprsMajorStockHolderTabRecords = ....
context.put("cprsMajorStockHolderTabRecords", cprsMajorStockHolderTabRecords)
---------------------------------------

Your simple field "$msh.ideNamFirst" should not work too. More you must to use 
input field to set $msh.ideNamFirst

Regards Angelo

Original comment by angelo.z...@gmail.com on 22 Aug 2013 at 7:13

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Hello,

"$msh.ideNamFirst" is working on my part, it displays all the names for each 
record inside the "values" object which contain an arraylist of 
"cprsMajorStockHolderTabRecords".

I need an example of how to manage dynamic images within #foreach. Do I still 
need to create a Java collection to attain this?

Thanks!

Original comment by PanicMom...@gmail.com on 22 Aug 2013 at 7:44

GoogleCodeExporter commented 8 years ago
It's difficult for me to understand that you wish to do, because you have not 
attached your Pojo and not filled the context with 
cprsMajorStockHolderTabRecords.

If I understand, you want loop for a "value" of cprsMajorStockHolderTabRecords 
($msh) and display a simple text ($msh.ideNamFirst) and an image (stkPic (Image 
name) which is transformed with $msh.stkPic (because you have written 
metadata.addFieldAsImage("stkPic", "msh.stkPic");)

so your Pojo value should be like this : 

----------------------------------
public class MshPojo {

   public String getIdeNamFirst() {
     return ...
   }

   public IImageProvider getStkPic () {
     return ...
   }
}
----------------------------------

If my answer doesn't help you, please attach a simply Java main+Pojo that I can 
launch to see your problem.

Regards Angelo

Original comment by angelo.z...@gmail.com on 22 Aug 2013 at 8:00

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Hello,

I think I cannot give a java main and pojo to replicate my problem because it a 
very big and complicated system.

What I need to see is an example of how to include the different images to the 
metadata fields, on the context and what name to use in mapping it to the ODT 
file, with only #foreach, if possible can you show me a similar example like 
ODTProjectWithVelocityAndImageList but inside the #foreach loop not inside the 
table.

Sorry, I'm not that really good in explaining things in text.

Thank you!

Original comment by PanicMom...@gmail.com on 22 Aug 2013 at 8:23

GoogleCodeExporter commented 8 years ago
Inside the #foreach loop and not inside the table is the same thing except you 
must not set the addFieldAsList.

I don't understand your provided sample because you do 

-----------------------------------
context.put("stkPic", picture);
-----------------------------------

So it's not a lits of images and you wish "different image for each loop."
If you want to display just this picture for each loop, you must do

-----------------------------------
metadata.addFieldAsImage("stkPic");
-----------------------------------

without mapping field.

After that, I cannot help you more if you cannot provide me a simple sample 
with your case.

Regards Angelo

Original comment by angelo.z...@gmail.com on 22 Aug 2013 at 8:37

GoogleCodeExporter commented 8 years ago
What I want is to display different images for each loop.

Sorry for the example I have given let me post the method I use to set the 
context and metadata.

Attached the code for setting context and metadata. *disregard the previous 
example I have given(stkPic)

Original comment by PanicMom...@gmail.com on 22 Aug 2013 at 8:58

Attachments:

GoogleCodeExporter commented 8 years ago
Sorry I cannot help you more with your attached sample. I don't know what is 
the value of "picName". Please provide a simple sample, in order to avoid I try 
and guess your need.

If you want to display different images for each loop of the list of 
cprsMajorStockHolderTabRecords your list must contaisn a Pojo which returns the 
IIMageProvider (in your sample IIMageProvider is not a list) like 
ODTProjectWithVelocityAndImageList.

I suggest you to play with ODTProjectWithVelocityAndImageList by setting the 
image inside the #foreach. 

Original comment by angelo.z...@gmail.com on 22 Aug 2013 at 9:05

GoogleCodeExporter commented 8 years ago
Hello,
Thank you for helping me until now.

Is this the right way of creating a list of IIMageProvider without the use of 
Developers model?
(see attached file)

Original comment by PanicMom...@gmail.com on 22 Aug 2013 at 10:41

Attachments:

GoogleCodeExporter commented 8 years ago
Sorry I don't understand what you mean. Please provide a Java main+Pojo+odt.

It will be more easy for me to understand what you wish to do.

Original comment by angelo.z...@gmail.com on 22 Aug 2013 at 11:21

GoogleCodeExporter commented 8 years ago
Hello!

I tried playing with ODTProjectWithVelocityAndImageList sample and the photo 
within the table works fine, but when I transferred it to the custom #foreach I 
got the same Exception as above.

I just commented out the line

----------------------------------
metadata.addFieldAsList( "developers.Photo" );
----------------------------------

I think there is something wrong with my ODT?
*attached the ODT I'm using to test ODTProjectWithVelocityAndImageList

Original comment by PanicMom...@gmail.com on 23 Aug 2013 at 1:57

Attachments:

GoogleCodeExporter commented 8 years ago
Hi,

When you wish to use Velocity template engine you must follow Velocity syntax. 
SO if you want to loop of developpers and display name you write : 

----------------------------------------
#foreach($developer in $developers)
$developer.Name
#end
----------------------------------------

You don't write $developers.Name but you write $developer.Name.

Now if you want to use lazy loop with table, you must write : 

----------------------------------------
$developers.Name
----------------------------------------

(and not $developer.Name) and set a mapping with 
addFieldAsList("$developers.Name). When you use addFieldAsList, XDocReport 
preprocess the docx and generates automaticly the #foreach and #end on the well 
location (before/after the row).

Your attached ODT is wrong, because your image name is not correct. You must :

1) name the image with $developer.Name (and not with $developers.Name)
2) call metadata.addFieldAsList( "developer.Photo" );

Original comment by angelo.z...@gmail.com on 23 Aug 2013 at 7:39

GoogleCodeExporter commented 8 years ago
Hello,

I was able to fix the issue, it turns out that I only need to convert the 
base64 format  to ByteArrayImageProvider, and cprsMajorStockHolderTabRecords is 
already a collection so no need to create another collection to be mapped.

Thank you Angelo!

btw, we will be using xdocreport in production.

Original comment by PanicMom...@gmail.com on 28 Aug 2013 at 5:10

GoogleCodeExporter commented 8 years ago
Ok that's cool. I close this issue.

Original comment by angelo.z...@gmail.com on 28 Aug 2013 at 7:13