kschork
I'm new here

FS_INDEX add Dataset with FS Access API

Jump to solution

I got as far as that I know now that the input component FS_INDEX  requires the type Index.

But I have some trouble creating an Index from scratch and adding a Dataset as a Record to the Index.

...

    Dataset pageTargetDataset = fsDatabaseHelper.getDataset("target", pageTargetId, "target_id");

    Index pageTargetIndex = (Index) new ArrayList<Index.Record>();

    pageTargetIndex.add((Record) pageTargetDataset);

   

    pageFormData.get(language, "pt_target_page").set(pageTargetIndex);

...

The code compiles without error,but on execution I get the following error.

ERROR 16.08.2019 13:29:49.370{dNR=} (de.espirit.firstspirit.server.scheduler.ScriptTaskExecutor): error during script execution : java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class de.espirit.firstspirit.client.access.editor.lists.Index (java.util.ArrayList is in module java.base of loader 'bootstrap'; de.espirit.firstspirit.client.access.editor.lists.Index is in unnamed module of loader 'app')

java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class de.espirit.firstspirit.client.access.editor.lists.Index (java.util.ArrayList is in module java.base of loader 'bootstrap'; de.espirit.firstspirit.client.access.editor.lists.Index is in unnamed module of loader 'app')

This error might suggest that I have to rearrange the module loaders...???

But it might also be a simple casting problem...

Help of any kind is welcome.

1 Solution

Accepted Solutions

Hi Kevin,

maybe this code is helpful:

Index globalPageRefTargetIndex = (Index) globalPageRefPageFormData.get(language, "pt_target_page").get();

globalPageRefTargetIndex.clear();

//your code

//Record globalPageRefTargetRecord = globalPageRefTargetIndex.create("{\"schema\":\"master_data\",\"gid\":\"" + globalPageRefTargetDataset.getEntity().getGid() + "\",\"table\":\"target\"}");

//better solution to create a record

GomIndex gomIndex = (GomIndex) globalPageRefPageFormData.getForm().findEditor("pt_target_page");

//context: a SpecialistsBroker instance, should be available from somewhere

DataAccessSession<Dataset> dataAccessSession = gomIndex.source().createSession(context, false);

String identifier = dataAccessSession.getIdentifier(globalPageRefTargetDataset);

Record globalPageRefTargetRecord = globalPageRefTargetIndex.create(identifier);

//your code continued

globalPageRefTargetIndex.add(globalPageRefTargetRecord);

globalPageRefPageFormData.get(language, "pt_target_page").set(globalPageRefTargetIndex);

globalPageRefPage.setFormData(globalPageRefPageFormData);

globalPageRefPage.save();

Michael

View solution in original post

5 Replies
StefanSchulz
I'm new here

Hi Kevin,

Yep, this is a "simple" casting problem. An ArrayList is no Index, and a Dataset is no Record.

The correct way would be to fetch the Index stored in "pt_target_page" (there always is one), to create a Record via the Index's API, add the Record and set the Index back to the form field. For creating a Record, you need access to a data stream instance of the DataAccessPlugin being used, which might be a bit more complicated depending on the context your code runs in.

Cheers,

Stefan

0 Kudos

Hi Stefan,

yes your are right. I found the same solution after a few more tries.

Here is the some example code that works for me. Maybe it is going to help someone in the future.

---

Index globalPageRefTargetIndex = (Index) globalPageRefPageFormData.get(language, "pt_target_page").get();

globalPageRefTargetIndex.clear();

   

Record globalPageRefTargetRecord = globalPageRefTargetIndex.create("{\"schema\":\"master_data\",\"gid\":\"" + globalPageRefTargetDataset.getEntity().getGid() + "\",\"table\":\"target\"}");

globalPageRefTargetIndex.add(globalPageRefTargetRecord);

globalPageRefPageFormData.get(language, "pt_target_page").set(globalPageRefTargetIndex);

globalPageRefPage.setFormData(globalPageRefPageFormData);

globalPageRefPage.save();

---

Greetings,

Kevin

0 Kudos

Hi Kevin,

Yes, that does work. But you are relying on the format of how the Data Access Plugin stores an index. That might change and your code will be broken.

Better to get hold of the Plugin, fetch a data access session (not a stream, as I wrongfully mentioned earlier), and use its API to get the identifier. For example, the DatasetDataAccessPlugin provided by e-Spirit does understand a Dataset instance.

I think, one way to get hold of a session is via the form definition provided by the form data container.

Cheers,

Stefan

0 Kudos

Hi Kevin,

maybe this code is helpful:

Index globalPageRefTargetIndex = (Index) globalPageRefPageFormData.get(language, "pt_target_page").get();

globalPageRefTargetIndex.clear();

//your code

//Record globalPageRefTargetRecord = globalPageRefTargetIndex.create("{\"schema\":\"master_data\",\"gid\":\"" + globalPageRefTargetDataset.getEntity().getGid() + "\",\"table\":\"target\"}");

//better solution to create a record

GomIndex gomIndex = (GomIndex) globalPageRefPageFormData.getForm().findEditor("pt_target_page");

//context: a SpecialistsBroker instance, should be available from somewhere

DataAccessSession<Dataset> dataAccessSession = gomIndex.source().createSession(context, false);

String identifier = dataAccessSession.getIdentifier(globalPageRefTargetDataset);

Record globalPageRefTargetRecord = globalPageRefTargetIndex.create(identifier);

//your code continued

globalPageRefTargetIndex.add(globalPageRefTargetRecord);

globalPageRefPageFormData.get(language, "pt_target_page").set(globalPageRefTargetIndex);

globalPageRefPage.setFormData(globalPageRefPageFormData);

globalPageRefPage.save();

Michael

Thank you very much Michael,

This code is very helpful indeed.

Works like a charm.

Greetings

Kevin

0 Kudos