Code example
The following example shows how data is retrieved from af collection of templates and used to create a collection of records (with values copied from the template).
String thisKeyToParentTemplate = "VALGTSKABELON"; // Entity
String templateSolutionName = "tasktemplate"; // Entity
ArrayList<String> fieldsTemplate = new ArrayList(String [] {"TASK","DEADLINE","RESPONSIBLE"}); // list of fields
String templateKeyToParentTemplate = "SKABELON"; // Entity
String instanceSolutionName = "taskinstance";
String instanceKeyToParent = "LIST";
Now to the code ...
int parentTemplateDataID = Parser.getInteger(c.fields.getElementByFieldName(thisKeyToParentTemplate).FieldValue);
try (Session session = SessionFactory.getSession(this)) {
//Get data
SolutionQuery opsaetning = session.getSolutionQuery(templateSolutionName);
for (int i = 0; i < fieldsTemplate.size(); i++)
opsaetning.addSelectField(fieldsTemplate.get(i));
opsaetning.addWhereCriterion(templateKeyToParentTemplate, parentTemplateDataID);
SolutionQueryResultSet recordsToCopy = opsaetning.executeQuery();
int recordCount = recordsToCopy.size();
for (int i = 0; i < recordCount; i++) {
SolutionRecordNew instance = session.getSolutionRecordNew(instanceSolutionName);
for (int fi = 0; fi < fieldsTemplate.size(); fi++) {
String fieldNameToCopy = fieldsTemplate.get(fi);
String value = recordsToCopy.getRecordValue(i, fieldNameToCopy);
instance.setValue(fieldNameToCopy, value);
}
instance.setValueInteger(instanceKeyToParent, c.DataID);
if (Parser.isNotEmpty(instanceKeyToTemplate))
instance.setValueInteger(instanceKeyToTemplate, recordsToCopy.getReference(i));
instance.persistChanges();
}
} catch(Exception e) {
e.printStackTrace();
} catch (TsCloseObjectRequired ex) {
}
The newer version (as of version 11230)
int parentTemplateDataID = Parser.getInteger(c.fields.getElementByFieldName(thisKeyToParentTemplate).FieldValue);
try (Session session = SessionFactory.getSession(this)) {
//Get data
SolutionQueryResultSet recordsToCopy = session
.query(templateSolutionName)
.select(fieldsTemplate)
.where(templateKeyToParentTemplate, parentTemplateDataID)
.execute();
SolutionRecord original;
while ((original = recordsToCopy.next()) != null) {
SolutionRecordNew instance = session.getSolutionRecordNew(instanceSolutionName);
for (String field : fieldsTemplate) {
String value = original.getValue(field);
instance.setValue(field, value);
}
instance.setValue(instanceKeyToParent, c.DataID);
if (Parser.isNotEmpty(instanceKeyToTemplate))
instance.setValue(instanceKeyToTemplate, original.getDataID());
instance.persistChanges();
}
} catch(Exception ex) {
ex.printStackTrace();
} catch (TsCloseObjectRequired ignore) {
}