Polling a directory for pdf files

0
Hello, I need to poll a directory where pdf files will be stored. What Mendix should do is look after a set time and retrieve the pdf files in that directory and store them in a system.filedocument entity and delete them from the directory after the import. I know this should be done in java. Has anybody got a code example I could work with? Regards, Ronald
asked
2 answers
1

I'd do the polling part in a scheduled event, no need to have a thread running to poll all the time.

Here's a code sample I have laying around. Be sure to read the class description (you can read dutch right?) as it contains some pointers on how to perform this action safely so that even if you delete the files and your process throws an exception and rolls back later, you don't lose the new FileDocuments.

Oh and the boolean check whether to delete files in executeAction() is redundant now that all files are always added to the delete list when this boolean is true in parseDirectory().

// This file was generated by Mendix Business Modeler 2.5.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à, etc. are supported in comments.

package sila.actions;

import com.mendix.systemwideinterfaces.core.UserAction;
import com.mendix.systemwideinterfaces.core.IContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import sila.proxies.InkomendBestand;
import sila.proxies.SilaMutatieVerwerkingInkomend;
import com.mendix.core.Core;
import com.mendix.core.CoreException;
import com.mendix.systemwideinterfaces.MendixRuntimeException;
import com.mendix.systemwideinterfaces.core.IMendixObject;

/**
 * Als de parameter silaMutatieVerwerking empty is, dan wordt er een aangemaakt in deze actie en deze wordt ook teruggegeven. Dit omwille van de robuustheid van het proces, het inlezen van de bestanden (en het verwijderen daarvan) gebeurt dan in een aparte transactie, zodat de bestanden in ieder geval niet kwijt zijn mocht er later in het proces iets misgaan.
 */
public class HaalBestandenUitMapActie extends UserAction<IMendixObject>
{
    private String bestandslokatie;
    private IMendixObject __silaMutatieVerwerking;
    private sila.proxies.SilaMutatieVerwerkingInkomend silaMutatieVerwerking;
    private Boolean verwijderBestandenNaInlezen;

    public HaalBestandenUitMapActie(String bestandslokatie, IMendixObject silaMutatieVerwerking, Boolean verwijderBestandenNaInlezen)
    {
        super();
        this.bestandslokatie = bestandslokatie;
        this.__silaMutatieVerwerking = silaMutatieVerwerking;
        this.verwijderBestandenNaInlezen = verwijderBestandenNaInlezen;
    }

    @Override
    public IMendixObject executeAction() throws Exception
    {
        this.silaMutatieVerwerking = __silaMutatieVerwerking == null ? null : sila.proxies.SilaMutatieVerwerkingInkomend.initialize(this.getContext(), __silaMutatieVerwerking);

        // BEGIN USER CODE
        IContext importContext = this.getContext().getSession().getContext();
        if (silaMutatieVerwerking == null)
            silaMutatieVerwerking = SilaMutatieVerwerkingInkomend.initialize(importContext, (IMendixObject) Core.execute(importContext, "Sila.MaakNieuweSilaMutatieVerwerkingInkomend"));
        Core.getLogger("log").debug("Starten met inlezen bestanden van lokatie: " + bestandslokatie);
        File importDir = new File(bestandslokatie);
        if (!importDir.isDirectory()) 
            throw new IllegalArgumentException("Import directory "+ bestandslokatie + " bestaat niet.");
        parseDirectory(importDir, importContext);
        if (verwijderBestandenNaInlezen)
            deleteFiles();
        addRefreshClass(InkomendBestand.getType());
        importContext.endTransaction();
        return silaMutatieVerwerking.getMendixObject();
        // END USER CODE
    }

    /**
     * Returns a string representation of this action
     */
    @Override
    public String toString()
    {
        return "HaalBestandenUitMapActie";
    }

    // BEGIN EXTRA CODE
    private void parseDirectory(File importDir, IContext importContext) throws CoreException 
    {
        File[] files = importDir.listFiles();
        InkomendBestand bestand;
        for (File f : files) 
        {
            if (f.isDirectory()) 
            {
                //parseDirectory(f, importContext);     don't parse subdirs for now
            }
            else 
            {
                bestand = InkomendBestand.create(importContext);
                bestand.setDatumIngelezen(new Date());
                bestand.setName(f.getName());
                bestand.setInkomendBestand_SilaMutatieVerwerkingInkomend(silaMutatieVerwerking);
                try 
                {
                    IMendixObject bestandMendixObject = bestand.getMendixObject();
                    Core.storeFileDocumentContent(importContext, bestandMendixObject, f.getName(), new FileInputStream(f));
                    if (verwijderBestandenNaInlezen)
                        deleteList.add(f);  //Add to list for deletion; 
                } 
                catch (FileNotFoundException e) 
                {
                    throw new MendixRuntimeException("Fout bij importeren " + f.getName(), e);
                }
            }
        }
    }

    private void deleteFiles()
    {
        for (File f : deleteList) 
        {
            try 
            {
                f.delete();
            }
            catch (java.lang.SecurityException se) 
            {
                throw new MendixRuntimeException("Kan sila importbestand niet verwijderen: " + f.getName(), se);
            } 
        }
    }

    private List<File> deleteList = new ArrayList<File>();
    // END EXTRA CODE
}
answered
0

Thank you very much. I am going to try it out tomorrow.

answered