Custom Java action to return a list

0
  Hi there, I am trying to create a custom Java action. The program should crawl through a file directory and should return a list of  file path names (specific files only which I call ‘wfiles’). My code works fine stand-alone (not included in Mendix). In my code I have an array list of strings which I called “wfileList”. That’s the list of file path names. This list I want to make available in Mendix. Now I am struggling with the conversion to a Mendix list. This is the code: public class JA_FilesWalkWfile extends CustomJavaAction<java.util.List<IMendixObject>> { public JA_FilesWalkWfile(IContext context) { super(context); } @java.lang.Override public java.util.List<IMendixObject> executeAction() throws Exception { // BEGIN USER CODE try (Stream<Path> walk = Files.walk(Paths.get("C:\\Users\\myFileDirectoy"))) { // We want to find only regular files List<String> result = walk.filter(Files::isRegularFile) .map(x -> x.toString()).collect(Collectors.toList()); result.forEach(System.out::println); ArrayList<String> wfileList = new ArrayList<>(); for(int i = 0; i < result.size(); i++){ if(result.get(i).contains(".wsrfx")){ wfileList.add(result.get(i)); } } System.out.println("\n"); wfileList.forEach(System.out::println); ArrayList<IMendixObject> returnList = new ArrayList<IMendixObject>(); Integer index = 0; for (String wfile : wfileList) { IMendixObject listItem = Core.instantiate(getContext(), wfile); listItem.setValue(getContext(), wfile, index); returnList.add(listItem); index = index + 1; } return returnList; } catch (IOException e) { e.printStackTrace(); } return null; // END USER CODE } The problem occurs when I try to instantiate the IMendixObject on this line (line number 54 in my code): “IMendixObject listItem = Core.instantiate(getContext(), wfile);”   This is the error I get (abvreviated): Caused by: com.mendix.core.CoreRuntimeException: 'C:\Users\filename.wsrfx' is not a valid entity at com.mendix.basis.objectmanagement.SchemeManagerImplBase.getMetaObject(SchemeManagerImplBase.java:374) at com.mendix.basis.objectmanagement.SchemeManagerImplBase.getMetaObject(SchemeManagerImplBase.java:45) at com.mendix.basis.action.user.CreateAction.<init>(CreateAction.scala:20) at com.mendix.basis.component.ObjectManagementCore.instantiate(ObjectManagementCore.scala:23) at com.mendix.basis.component.ObjectManagementCore.instantiate$(ObjectManagementCore.scala:19) at com.mendix.basis.component.InternalCore.instantiate(InternalCore.scala:24) at com.mendix.core.Core.instantiate(Core.java:565) at sica.actions.JA_FilesWalkWfile.executeAction(JA_FilesWalkWfile.java:54) Can somebody tell me what I am getting wrong? Nice greetings, Henning  
asked
1 answers
1

You’re returning a list of strings which aren’t (database) entitities. Create a (non-persistable) entity in your domain model with a string attribute, create for each wfile an object and store the filename in the string attribute. Then return the new entity list.

answered