Copying FileDocument contents with custom java

2
I have some custom Java that takes a list of objects. The object’s name is called TestFile and the generalization is System.Filedocument. I loop through each TestFile in java and store the binary contents into a string just for practice. It works fine. BUT I’m also trying to have it add a NEW TestFile and copy the contents of the file. The reason I’m doing this in java is because I intend to manipulate the content of the document first. But I’m having trouble:   int length = this.Parameter.size(); for(int a = 0;a<length;a++) { InputStream inputStream; TestFile myobject = this.Parameter.get(a); inputStream = Core.getFileDocumentContent(getContext(), __Parameter.get(a)); String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8); myobject.setStringBody(result); myobject.commit(); TestFile newobject = new TestFile(getContext()); Core.storeFileDocumentContent(getContext(), newobject, inputStream); newobject.setStringBody("new"); int lengthstream = inputStream.available(); newobject.setContents(getContext(), inputStream,lengthstream); newobject.commit(); }   The error is on this line:       Core.storeFileDocumentContent(getContext(), newobject, inputStream);   The method storeFileDocumentContent(IContext, IMendixObject, InputStream) in the type Core is not applicable for the arguments (IContext, TestFile, InputStream)   Is there something I need to do this “newobject” before I use it as an argument?
asked
2 answers
1

Try using the getMendixObject() method on the newobject. It should be in the generated proxy class for TestFile.

Core.storeFileDocumentContent(getContext(), newobject.getMendixObject(), inputStream);

Good luck!

answered
1

I tried something a little different. The following does NOT give me an error, but it copies nothing. Mendix shows HasContents=true but the size is 0KB, which I verified after I downloaded the file locally. Does anyone know why the file (which is only 40K) isn’t copying from the inputstream?

 

		for(int a = 0;a<length;a++) {
			InputStream inputStream;
			TestFile myobject = this.Parameter.get(a);
			inputStream = Core.getFileDocumentContent(getContext(), __Parameter.get(a));
			String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
			myobject.setStringBody(result);
			myobject.commit();
	
			
			IMendixObject newobject = Core.instantiate(getContext(), "MusicBack.TestFile");
			Core.storeFileDocumentContent(getContext(), newobject, "TEST.WAV", inputStream);
			Core.commit(getContext(), newobject);
			
			InputStream inputStream2;
			inputStream2 = Core.getFileDocumentContent(getContext(), newobject);
			String result2 = IOUtils.toString(inputStream2, StandardCharsets.UTF_8);
			newobject.setValue(getContext(), "StringBody", result2);
			Core.commit(getContext(), newobject);
			
			}

 

answered