3 replies [Last post]
rustyspoone
Offline
Joined: 2010-06-23

I am using otd templates and Java objects to render pdfs. I need to display sections of data only when that data exists. For example, there is a comment area (with a border) that does not show if comment == null.

It looks like the only way to use a conditional section is the following:

cs_hasValue

Where hasValue is:

public boolean hasValue()
or
public boolean getHasValue()

I need to have the ability to do something like this:

<<cs_test{comments != null}>>
Comments: <<comments>>
<<es_>>

Or something like that. The first line does the condition check. I know this does not exactly match the syntax docmosis templates, but is there some way to do this without adding a method to the Java class.

HoSpiTaL_gHoSt
Offline
Joined: 2010-07-02
Also been wondering

Hello,

I have also been wondering how I could check if a value was null in order to show or hide a block of content. Up to now, I've always used this method:

*cs_{comments!=null}*
Comments: *comments*
*es_{comments!=null}*

This works for me, but I don't think it's comparing 'comments' to null, as far as I understand, something like getNull() is called, and because that does not exist in my code, the behaviour is the same as when comparing to the "real" null. Does this way of thinking make any sense (and if not, then how come it works for me)? I've never asked this question here on the forum, because my solution worked and I supposed it was valid (even though it might not be the best way of comparing to null).

rustyspoone
Offline
Joined: 2010-06-23
If it works... :)

I had not tried that. Even if it is going through some getNull() to get null to compare, that works.

Since I am not as familiar with the template syntax yet, I found another way to make this work. I think it is not as good as yours, but it would have the added benefit of endless flexibility, functions and data manipulation. In other words, I can now do whatever I want.

This is nothing special, I just created a wrapper class for the java object I am adding in. Before I only added one java object, like this:

DataProviderBuilder builder = new DataProviderBuilder();
builder.addJavaObject(myObj, "data");

Now my code looks like this:

DataProviderBuilder builder = new DataProviderBuilder();
builder.addJavaObject(myObj, "data");
builder.addJavaObject(new MyObjHelper(myObj), "helper");

The MyObjHelper class contains any boolean check methods I need. Also, if I want to do any data formatting, I can do it how I need it and am not limited to docmosis.

As a note I am adding two java objects. Certainly I could have only added the helper and then in the template reference myObj from the helper class.

helper.data.student.firstName

I did it like this because I had already created the templates without the helper class. Going back and changing the template references from "data.student.firstName" to "helper.data.student.firstName" was more than I could take. OpenOffice does not seem to allow me to edit the original mail merge reference. That means I would have to delete it and the create a new mail merge reference. So in trade I have to suffer with having two objects and remembering which data is in each one. win some lose some, I guess.

admin
Offline
Joined: 2010-09-06
Comments on Solutions

Hello rustyspoon.

HoSpiTaL_gHoSt is correct (see below - and thanks to HoSpiTaL_gHoSt) in that the "cs_test{comments!=null}" will do what you require. HoSpiTaL_gHoSt is also correct in that Docmosis will ask your Java system if it can satisfy "null" by calling a method (getNull()) - this behaviour will be upgraded in a future version of Docmosis since "=null" and "!=null" should be taken literally. For now, since getNull() doesn't exist in your Java objects, the effect is the same.

Your idea of wrapping your data in a report-specific Java object makes sense and can certainly simplify logic in the template. Such Java objects can house as much complexity as you require.

As for having trouble with editing fields in OpenOffice, you just need to right-click on the field, select "Fields..." and edit the reference there. This will change the underlying field reference. You can also use the left and right arrow buttons to move through the fields of your document. If you wish to change the display value for the field, left click on the field and change it there (which is what you would have been doing already).