1
votes

I am working on an application where I am creating a java.util.TreeMap containing data fetched from various other documents of the application and then assigning that treemap to a sessionsScope variable. This is working fine. Now I want to provide a functionality wherein I need to store this map inside a NotesDocument.

But when I try doing this, I am getting an error.

var doc:NotesDocument = database.createDocument();
doc.replaceItemValue("Form","testForm");
print("json = "+sessionScope.get("Chart_Map"));
doc.replaceItemValue("Calender_Map",sessionScope.get("Chart_Map"));
doc.save();

Exception:

Error while executing JavaScript action expression Script interpreter error, line=4, col=13: [TypeError] Exception occurred calling method NotesDocument.replaceItemValue(string, java.util.TreeMap) null**

Is it possible to store a java.util.TreeMap in a notesdocument field?

If yes then how to implement that?

If no then why not? has that something to do with serializability?

2

2 Answers

5
votes

You can't store Java objects inside Document fields unless you use the MimeDomino Document data source http://www.openntf.org/main.nsf/blog.xsp?permaLink=NHEF-8XLA83

Or even better the new openntf Domino API that has this functionallity built in http://www.openntf.org/main.nsf/project.xsp?r=project/OpenNTF%20Domino%20API

using MimeStorage

0
votes

Fredrik is right, the MimeDomino makes most sense. If you are not ready and your field isn't too big for a normal Notes item, you could use CustomDataBytes as Sven suggested - or you use JSON by subclassing TreeMap. It could look like this:

import java.util.TreeMap;
import java.util.Vector;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import lotus.domino.Item;
import lotus.domino.NotesException;

public class TreeMapItem extends TreeMap<String, String> {
    private static final long   serialVersionUID    = 1L;
    public static TreeMapItem load(Item source) throws JsonSyntaxException, NotesException {
        Gson g = new Gson();
        TreeMapItem result = g.fromJson(source.getText(), TreeMapItem.class);
        return result;
    }
    public void save(Item target) throws NotesException {
        Gson g = new Gson();
        target.setValueString(g.toJson(this));
    }
}

I used Google's Gson, it is quite easy, but you might need to deploy it as plug-in for the Java security to work. There is build in JSON in XPages too - a little more work. An alternate approach would be to use 2 fields in Domino, one to load the keys from and one for the values - it would be in line with Domino practises from classic.

A third approach would be be to store the values separated using a pipe character:

@SuppressWarnings({ "unchecked", "rawtypes" })
public void saveCompact(Item target) throws NotesException {
    Vector v = new Vector();
    for (Map.Entry<String, String> me : this.entrySet()) {
        v.add(me.getKey()+"|"+me.getValue());
    }
    target.setValues(v);
}

    @SuppressWarnings("rawtypes")
public static TreeMapItem loadCompact(Item source) throws NotesException {
    TreeMapItem result = new TreeMapItem();
    Vector v = source.getValues();
    for (Object o : v) {
        String[] candidate = o.toString().split("|");
        if (candidate.length > 1) {
            result.put(candidate[0], candidate[1]);
        }
    }
    return result;
}

Let us know how it works for you