Automatically import XML file

0
I have an application that needs to import an XML file every night. I already have everything working successfully manually. Anybody an idea how I can manage that the application is picking up the file (in a specific folder) automatically every night and start my XML import microflow? ---EDIT---- Ok. I got it working now the way I needed it with the help and examples from Ronald (Thanks!) Input: -a path -an extension -an renameExtension -a FileDocument The procedure will find the first file in the given path with the given extension and puts it in the FileDocument. After that, the orginal file is renamed, so it will not be imported again. I loop it untill there are no new files found. Below the code as it is now. Maybe somebody can benefit from it or maybe somebody has some tips to improve. // This file was generated by Mendix Business Modeler. // // 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 general.actions; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FilenameFilter; import com.mendix.core.Core; import com.mendix.core.CoreException; import com.mendix.systemwideinterfaces.core.IContext; import com.mendix.systemwideinterfaces.core.IMendixObject; import com.mendix.systemwideinterfaces.core.UserAction; import com.mendix.webui.CustomJavaAction; /** * */ public class GetAndRenameFile extends CustomJavaAction<Boolean> { private String mapPad; private String fileExtension; private IMendixObject __doelObject; private general.proxies.ScheduledExchange doelObject; private String renameExtension; public GetAndRenameFile(IContext context, String mapPad, String fileExtension, IMendixObject doelObject, String renameExtension) { super(context); this.mapPad = mapPad; this.fileExtension = fileExtension; this.__doelObject = doelObject; this.renameExtension = renameExtension; } @Override public Boolean executeAction() throws Exception { this.doelObject = __doelObject == null ? null : general.proxies.ScheduledExchange.initialize(getContext(), __doelObject); // BEGIN USER CODE IContext context = this.getContext(); File myDir = new File(this.mapPad); if( myDir.exists() && myDir.isDirectory()) { File[] files = myDir.listFiles(new MyFileNameFilter(fileExtension)); if(files.length == 0) { Core.getLogger("GetAndRenameFile").info("No files found!"); return false; } File file = files[0]; Core.getLogger("GetAndRenameFile").info("Get and rename File: " + file.getAbsolutePath()); if( !file.exists() ) throw new CoreException( "No file exists at location: " + mapPad ); if( !file.canRead() ) throw new CoreException( "The file could not be read, the location is: " + file ); try { FileInputStream storeStream = new FileInputStream(file); Core.storeFileDocumentContent(context, this.doelObject.getMendixObject(), file.getName(), storeStream); storeStream.close(); File newfilename = new File(file.getAbsolutePath() + "." + renameExtension); if(!file.renameTo(newfilename)) try { Core.getLogger("GetAndRenameFile").info("Rename of File failed!!"); return false; } catch( Exception e ) { throw new CoreException( "General error in rename function" + file, e ); } } catch( FileNotFoundException e ) { throw new CoreException( "The file could not be stored because something went wrong while reading the file, the location was: " + file, e ); } } else { Core.getLogger("GetAndRenameFile").info("No directory found with path: " + this.mapPad); return false; } return true; // END USER CODE } /** * Returns a string representation of this action */ @Override public String toString() { return "GetAndRenameFile"; } // BEGIN EXTRA CODE //FileNameFilter implementation public static class MyFileNameFilter implements FilenameFilter{ private String ext; public MyFileNameFilter(String ext){ this.ext = ext.toLowerCase(); } @Override public boolean accept(File dir, String name) { return name.toLowerCase().endsWith(ext); } } // END EXTRA CODE
asked
4 answers
1

René,

I have some sample code on how to retrieve a local file. It takes a path as a string and an object that should be a file document. Depending on the system used (Unix, Windows) you should take care of the / or \ in the path.

Regards,

Ronald

// This file was generated by Mendix Business Modeler.
//
// 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 gegevensimport.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.mendix.core.Core;
import com.mendix.core.CoreException;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.systemwideinterfaces.core.UserAction;
import com.mendix.webui.CustomJavaAction;

/**
 * 
 */
public class ExcelMapUitlezen extends CustomJavaAction<Boolean>
{
    private String mapPad;
    private IMendixObject __doelObject;
    private system.proxies.FileDocument doelObject;

    public ExcelMapUitlezen(IContext context, String mapPad, IMendixObject doelObject)
    {
        super(context);
        this.mapPad = mapPad;
        this.__doelObject = doelObject;
    }

    @Override
    public Boolean executeAction() throws Exception
    {
        this.doelObject = __doelObject == null ? null : system.proxies.FileDocument.initialize(getContext(), __doelObject);

        // BEGIN USER CODE

        IContext context = this.getContext();

        File myDir = new File(this.mapPad);
        if( myDir.exists() && myDir.isDirectory())
        {
            Core.getLogger("Files retrieve").info("Directory found: " + this.mapPad);

            File[] files = myDir.listFiles();
            for(int i=0; i < files.length; i++)
            {
                File file = files[i];

                Core.getLogger("Files retrieve").info("File: " + file.getName());

                if( !file.exists() )
                    throw new CoreException( "No file exists at location: " + mapPad );
                if( !file.canRead() )
                    throw new CoreException( "The file could not be read, the location is: " + file );

                try {  
                    FileInputStream storeStream = new FileInputStream(file);

                    Core.storeFileDocumentContent(context, this.doelObject.getMendixObject(), file.getName(), storeStream);   
                }
                catch( FileNotFoundException e ) {
                    throw new CoreException( "The file could not be stored because something went wrong while reading the file, the location was: " + file, e );
                }
             }
            if(files.length == 0)
            {
                Core.getLogger("Files retrieve").info("No files found");
                return false;
            }

          }
        else
        {
            Core.getLogger("Files retrieve").info("No directory found with path: " + this.mapPad);
            return false;
        }

        return true;

        // END USER CODE
    }

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

    // BEGIN EXTRA CODE
    // END EXTRA CODE
}
answered
1

Rene,

If you're working on premise then make sure the file is stored somewhere accesible by the Mendix server. Create a java action that picksup the file and stores this as a filedocument and then perform the rest of the actions to import the file in Mendix. Run the MF in a scheduled event.

If you're working in the cloud you could try to have the file availabel on a ftp server and get the file from there with a small java action, and the rest is the same as before.

Hope this helps in finding a solution.

answered
0

Hi Ronald,

Thanks for the example! That got me started perfectly! I create a custom java action in the modeler with your code. I altered it a bit to also include a fileExtension (so it only picks up *.XML files) and a renameExtension as input for the action. I almost got it working now. The code is generating a SheduledExchange (which has FildeDocument as generalization) object with the file perfectly. The only action that I can't get to work is that the file should be renamed to the "renameExtension", so that it won't be imported again. If the rename fails, it should return FALSE. I tried to steal some code from other java actions but it doesn't work. Can you give me some help to get me started on that part??

EDIT: --Removed Code--

answered
0

Ok. I got it working now the way i needed it. The problem with renaming was that I had to close the stream before rename.

Also I changed that it only does one file per run. In the modeler I will loop it, until there are no new files found.

Below the code as it is now. Maybe somebody can benefit from it or maybe somebody has some tips to improve.

// This file was generated by Mendix Business Modeler.
//
// 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 general.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import com.mendix.core.Core;
import com.mendix.core.CoreException;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.systemwideinterfaces.core.UserAction;
import com.mendix.webui.CustomJavaAction;

/**
 * 
 */
public class GetAndRenameFile extends CustomJavaAction<Boolean>
{
    private String mapPad;
    private String fileExtension;
    private IMendixObject __doelObject;
    private general.proxies.ScheduledExchange doelObject;
    private String renameExtension;

    public GetAndRenameFile(IContext context, String mapPad, String fileExtension, IMendixObject doelObject, String renameExtension)
    {
        super(context);
        this.mapPad = mapPad;
        this.fileExtension = fileExtension;
        this.__doelObject = doelObject;
        this.renameExtension = renameExtension;
    }

    @Override
    public Boolean executeAction() throws Exception
    {
        this.doelObject = __doelObject == null ? null : general.proxies.ScheduledExchange.initialize(getContext(), __doelObject);

        // BEGIN USER CODE
        IContext context = this.getContext();

        File myDir = new File(this.mapPad);
        if( myDir.exists() && myDir.isDirectory())
        {
            File[] files = myDir.listFiles(new MyFileNameFilter(fileExtension));
            if(files.length == 0)
            {
                Core.getLogger("GetAndRenameFile").info("No files found!");
                return false;
            }

            File file = files[0];
            Core.getLogger("GetAndRenameFile").info("Get and rename File: " + file.getAbsolutePath());

            if( !file.exists() )
                throw new CoreException( "No file exists at location: " + mapPad );
            if( !file.canRead() )
                throw new CoreException( "The file could not be read, the location is: " + file );

            try {  
                FileInputStream storeStream = new FileInputStream(file);
                Core.storeFileDocumentContent(context, this.doelObject.getMendixObject(), file.getName(), storeStream); 
                storeStream.close();

                File newfilename = new File(file.getAbsolutePath() + "." + renameExtension);
                if(!file.renameTo(newfilename))  
                    try {
                        Core.getLogger("GetAndRenameFile").info("Rename of File failed!!");
                        return false;
                        } 
                        catch( Exception e ) {
                        throw new CoreException( "General error in rename function" + file, e );
                        }
                }

                catch( FileNotFoundException e ) {
                    throw new CoreException( "The file could not be stored because something went wrong while reading the file, the location was: " + file, e );
                }
          }

        else
        {
            Core.getLogger("GetAndRenameFile").info("No directory found with path: " + this.mapPad);
            return false;
        }

        return true; 


        // END USER CODE
    }

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

    // BEGIN EXTRA CODE
    //FileNameFilter implementation
    public static class MyFileNameFilter implements FilenameFilter{

        private String ext;

        public MyFileNameFilter(String ext){
            this.ext = ext.toLowerCase();
        }
        @Override
        public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith(ext);
        }
    }


    // END EXTRA CODE
answered