Saving java object into pipeline xml file

Hello,

We sometimes handle big lists of documents, in which we make criteria based searches.
In order to optimize these searches, we have started using java HashMaps.

The problem is that java Objects cannot be saved in files using the service pu.flow:savePipelineToFile.

Is there a way to push some kind of serializer/deserializer for java Objects? Usually our HashMaps are pretty simple (key as String, value as IData) and it would help our debug processes a lot!

Thanks in advance.

Hrm… While Vector is supported, other things will get skipped.
Check out the stuff in the java api for:
com.wm.util.coder.*

Maybe you can wrap up hashmaps in something which implements Codable, StringCodable, IDataCodable or ValuesCodable…

Or another option might be to make your own java service to write the pipeline to a file…

I wouldn’t recommend using savePipelineToFile for produtive usage.

If you need to persist objects to a file, create a Java service writing the file. A working example can be found in PSUtilities, but this one does not resemble up to date implementation practises. So I would propose using this as an example and writing a new one. This way you can use all Java functionality for serializing you objects.

Hi, indeed the Codable interface seems to be the answer. I’ll try that and report the result here!

This is not intended for production, but for debugging purpose. Being able to use specific java objects in step by step mode will help us gain a lot of time!

How did you go? Would be interested in the resulting code if you’ve got it working…

Here’s an exemple of a wrapper for the HashMap class. I only tried the IDataCodable interface, but it seems to work perfectly:

  • The savePipelineToFile/restorePipelineFromFile work as they should
  • It will save everything that can be saved, meaning: any java Object that is either already recognized (like IData, String, List instances…) or another implementaion of IDataCodable
  • A bonus I hadn’t expected: in step by step mode, the HashMap is now represented not as its toString() representation, but as the IData you built in the getIData method. Meaning its MUUUUUUUCH more readable in debug mode.

Enjoy!

PS: in my implementation, I chose to represent the HashMap as an IData array. Of course, you can do anything you like instead.

public static class WMHashMap extends HashMap implements IDataCodable{
    public void setIData(IData idata){
        IDataCursor idc = null;
        this.clear();
        try{
            idc = idata.getCursor();
            IData[] tab = IDataUtil.getIDataArray(idc, "liste");
            for(IData id: tab){
                IDataCursor coupleCursor = null;
                try{
                    coupleCursor = id.getCursor();
                    Object cle = IDataUtil.get(coupleCursor, "cle");
                    Object valeur = IDataUtil.get(coupleCursor, "valeur");
                    this.put(cle, valeur);
                }    
                finally{
                    if(coupleCursor!=null) coupleCursor.destroy();
                }
            }
        }
        finally{
            if(idc!=null) idc.destroy();
        }
    }
 
    public IData getIData(){
        IData[] tableau = new IData[this.size()];
        int index = 0;
        for(Object cle: this.keySet()){
            IData doc = IDataFactory.create();
            IDataCursor idc = null;
            try{
                idc = doc.getCursor();
                IDataUtil.put(idc, "cle", cle );
                IDataUtil.put(idc, "valeur", this.get(cle));
                tableau[index] = doc;
            }
            finally{
                if(idc!=null) idc.destroy();
            }
            index++;
        }
        IData retour = IDataFactory.create();
        IDataCursor retourCursor = null;
        try{
            retourCursor = retour.getCursor();
            IDataUtil.put(retourCursor, "liste", tableau);
        }
        finally{
            if(retourCursor!=null) retourCursor.destroy();
        }
        return retour;
    }
}