Search the FirstSpirit Knowledge Base
FirstSpirit API version: 4.2.454
Old style:
Dataset dataSet = csVideo.getDataset(entityVideo);
Data data = dataSet.getData();
FileEditorValue editorFile = (FileEditorValue) data.get("cs_file").getEditor();
editorFile.get(language).setMedium(mediaVideo);
Since getData is deprecated, there probably is a way to do that using:
Dataset dataSet = csVideo.getDataset(entityVideo);
FormData videoFormData = videoDataSet.getFormData();
fileFormField.set(mediaVideo); !! does not work
The question is how do I do that?
Well, a Media is no BinaryMedium ... it works analogue to using the editorvalue in your first post:
FormField<?> fileFormField = videoFormData.get(language,"cs_file");
BinaryMedium container = (BinaryMedium) fileFormField.get();
container.setMedium(mediaVideo);
fileFormField.set(container);
...
Stefan.
Sorry I forgot the declaration of fileFormField:
FormField<?> fileFormField = videoFormData.get(language, "cs_file");
Could you please elaborate on what's not working? Is there an exception?
assuming mediaVideo is correct typed (mediaVideo.getClass().isInstance(formField.getType)), you have to set the new and modfied FormData.
videoDataSet.setFormData(videoFormData)
Using the FormData instead of Data I simply do not know how to set a Medium. If you look at the old style version, there you get a FileEditorValue which after that is straightforward, but using the FormData I do not know how to get to the FileEditorValue, if I simply use
FormField<?> fileFormField = videoFormData.get(language, "cs_file");
fileFormField.set(mediaVideo);
I get the following exception:
Exception in thread "main" java.lang.IllegalArgumentException: Cannot assimilate value from '<MEDIUM editor="2001" filename="FileNametxt" id="11291" revision="3395" type="file" uniquedescription="FileName.txt_19">
<FILE crc="c55b6e4" description="" extension="FileName.txt" fileRevision="3394" size="7"/>
</MEDIUM>
'!
at de.espirit.firstspirit.client.access.editor.AbstractEditorValue.assimilate(AbstractEditorValue.java:775)
at de.espirit.firstspirit.client.access.editor.AbstractMediumEditorValue.assimilate(AbstractMediumEditorValue.java:392)
at de.espirit.firstspirit.store.access.DataWrappingFormData$EditorValueWrappingFormField.set(DataWrappingFormData.java:165)
at consumer.fsm.pim.PimImportCMSTest.main(PimImportCMSTest.java:178)
mediaVideo being a Media entity which already exists in the media store.
I am setting the the formData afterwards, but the problem seems to be that Media is not the correct type to use.
The types of the form fields does not match. Please post the form for both components.
It is the videoimporter example I am using for some tests: cs_file is the one I am trying to set using the API. I am not quite sure of what you mean by "both" components.
The module:
<CMS_MODULE>
<CMS_INPUT_NUMBER name="cs_id" type="long" length="10" singleLine="no" useLanguages="no">
<LANGINFOS>
<LANGINFO lang="*" label="Video id" description="" format="#"/>
</LANGINFOS>
</CMS_INPUT_NUMBER>
<CMS_INPUT_COMBOBOX name="cs_category" useLanguages="no">
<CMS_INCLUDE_CONTENT type="entries" table="videoimport.category">
<LABEL lang="*">#row.name</LABEL>
</CMS_INCLUDE_CONTENT>
<LANGINFOS>
<LANGINFO lang="*" label="Category" description="Category"/>
</LANGINFOS>
</CMS_INPUT_COMBOBOX>
<CMS_INPUT_COMBOBOX name="cs_director" useLanguages="no">
<CMS_INCLUDE_CONTENT type="entries" table="videoimport.director">
<LABEL lang="*">#row.name</LABEL>
</CMS_INCLUDE_CONTENT>
<LANGINFOS>
<LANGINFO lang="*" label="Director" description="Director"/>
</LANGINFOS>
</CMS_INPUT_COMBOBOX>
<CMS_INPUT_COMBOBOX name="cs_event" useLanguages="no">
<CMS_INCLUDE_CONTENT type="entries" table="videoimport.event">
<LABEL lang="*">#row.name</LABEL>
</CMS_INCLUDE_CONTENT>
<LANGINFOS>
<LANGINFO lang="*" label="Event" description="Event"/>
</LANGINFOS>
</CMS_INPUT_COMBOBOX>
<CMS_INPUT_FILE name="cs_file" useLanguages="no">
<LANGINFOS>
<LANGINFO lang="*" label="File"/>
</LANGINFOS>
</CMS_INPUT_FILE>
<CMS_INPUT_DATE name="cs_date" useLanguages="no">
<LANGINFOS>
<LANGINFO lang="*" label="Date"/>
</LANGINFOS>
</CMS_INPUT_DATE>
<CMS_INPUT_TEXT name="cs_title" hFill="yes" maxInputLength="200" singleLine="no">
<LANGINFOS>
<LANGINFO lang="*" label="Title"/>
</LANGINFOS>
</CMS_INPUT_TEXT>
<CMS_INPUT_DOM name="cs_description" hFill="yes">
<LANGINFOS>
<LANGINFO lang="*" label="Description"/>
</LANGINFOS>
</CMS_INPUT_DOM>
<CMS_INPUT_PICTURE name="cs_thumb_m" hFill="yes" lean="mandatory" useLanguages="no">
<LANGINFOS>
<LANGINFO lang="*" label="Thumb M"/>
</LANGINFOS>
</CMS_INPUT_PICTURE>
<CMS_INPUT_PICTURE name="cs_thumb_s" hFill="yes" lean="mandatory" useLanguages="no">
<LANGINFOS>
<LANGINFO lang="*" label="Thumb S"/>
</LANGINFOS>
</CMS_INPUT_PICTURE>
<CMS_INPUT_PICTURE name="cs_thumb_l" hFill="yes" lean="mandatory" useLanguages="no">
<LANGINFOS>
<LANGINFO lang="*" label="Thumb L"/>
</LANGINFOS>
</CMS_INPUT_PICTURE>
<CMS_INPUT_LIST name="cs_athletes" useLanguages="no">
<CMS_INCLUDE_CONTENT type="entries" table="videoimport.athlete">
<LABEL lang="*">#row.name</LABEL>
</CMS_INCLUDE_CONTENT>
<LANGINFOS>
<LANGINFO lang="*" label="Athletes" description="Athletes"/>
</LANGINFOS>
</CMS_INPUT_LIST>
</CMS_MODULE>
The java code to set the Media:
Media mediaVideo = null;
final String fileName = "FileName.txt";
try {
mediaVideo = videoFolder.createMedia(fileName, fileName, Media.FILE, false, true);
mediaVideo.setLock(true, false);
byte[] byteInput = "My Test".getBytes();
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInput);
mediaVideo.getFile(language).setFile(byteInput.length, inputStream, fileName);
System.out.println("Writing video " + fileName);
mediaVideo.save();
} catch (LockException le) {
le.printStackTrace();
} catch (ElementDeletedException ede) {
ede.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
mediaVideo.setLock(false);
} catch (Exception e) {
System.out.println("FATAL!");
e.printStackTrace();
}
}
<snip>....
Dataset videoDataSet = csVideo.getDataset(entityVideo);
FormData videoFormData = videoDataSet.getFormData();
FormField<?> fileFormField = videoFormData.get(language, "cs_file");
fileFormField.set(mediaVideo);
videoDataSet.setFormData(videoFormData);
videoDataSet.save();
<snip>....
Hi,
While the FormField does not contain explicit information about the value's type, it still corresponds to the container type BinaryMedium, as described in the documentation on the file component. This also is the type to use for setting the field. Easiest would be to get the current value from the field, change its value, and set it back to the field (i.e., the way you did with EditorValue).
Cheers,
Stefan
I still do not seem to get it I tried this, which does not work:
FormField<BinaryMedium> fileFormField = (FormField<BinaryMedium>) videoFormData.get(language,"cs_file");
fileFormField.set(mediaVideo);