Create calculated field(s) and populate them using Groovy

User 55ffa2f197

08-06-2015 16:32:32

Hi, I like to create calculated fields using Groovy instead of manual standard way from IJC client due to the comlexity of the calculation. I looked aound in the Groovy postings and found ones for creating Dynamic field. Can someone show how to create calculated fields in Groovy?


Thanks


Dong

ChemAxon d449bc1993

09-06-2015 12:51:59










dlee wrote:

Hi, I like to create calculated fields using Groovy instead of manual standard way from IJC client due to the comlexity of the calculation. I looked aound in the Groovy postings and found ones for creating Dynamic field. Can someone show how to create calculated fields in Groovy?


Thanks


Dong



Hi Dong,
here is an example. Just replace the calculation and list of parameters:


import com.im.df.api.dml.*
import com.im.df.api.capabilities.*
import com.im.df.api.support.*
import com.im.df.api.support.SelectionDescription
import com.im.ijc.core.api.util.IJCCoreUtils
import com.im.df.api.support.DFNewTypeWellKnownOptions.NewDBField


evaluate = { widget ->
def rs = widget.form.resultSet
def rootVS = rs.rootVertexState

def rootEntity = dataTree.rootVertex.entity
def schema = rootEntity.getSchema();

DFNewType<DFField> nt = DIFUtilities.findFirstAppropriateNewType(
rootEntity.getFields().getNewTypes(),
false,
[DFFieldCalcCapability.class] as Class<?>[],
null);


DFNewTypeOptions options = nt.getOptions();
NewDBField newFieldOptions = (NewDBField) options;

newFieldOptions.setNewDFItemName("Field Name");
schema.lockable.withLock('Adding Calculated Field') { envRW ->
DFField field = nt.create(envRW).iterator().next();

// add the calculation and a list of parameters
calc = CalcTemplate.create("some calculation", [] as DFField[])
cap = DIFUtilities.findCapability(field, DFFieldCalcCapability.class);
cap.setCalcTemplate(calc, envRW)
}
}


Thanks,


Jan

User 55ffa2f197

09-06-2015 13:35:28

Hi Jan,


I have gone down this path similar to what you described. But envRW would require the admin privilege of doing this. Whereas in IJC anyone can add a calc column regardless their role. Can this be relaxed so anyone can run this Groovy and add calc  column dynamically.


I suppose your "some calculation" would be another Groovy script to do the calculation, in which it uses one of the field in the entity as a variable just like what was defined in the UI interface for calc definition.


My intention is to do the calculation on the fly, the calculation would trigger some of our RESTFul service and bring back values and add these values to the view which users have trimmed down from the a grid view. Is there other way to do this on the fly in IJC


Of course we can store these calc data in another table, and promote it as a entity, and add it to the tree ....


Thanks


Dong

ChemAxon d449bc1993

10-06-2015 13:23:20

Dong,


I had modified the script so any user is able to create the calculated field.


Jan


evaluate = { widget ->
def rs = widget.form.resultSet
def rootVS = rs.rootVertexState

def rootEntity = dataTree.rootVertex.entity
def schema = rootEntity.getSchema();


DFNewType<DFField> nt = DIFUtilities.findAppropriateNewType(
rootEntity.getFields().getNewTypes(),
false,
DBImplConstants.FIELD_STANDARD_CALCULATED_TEXT)


DFNewTypeOptions options = nt.getOptions();
DFNewTypeWellKnownOptions.NewCalulatedField calcField = (DFNewTypeWellKnownOptions.NewCalulatedField) options

DFField paramField = rootEntity.getIdField();
Map<String, CalculableGroovyTemplate.FieldDetails> params = new HashMap<String, CalculableGroovyTemplate.FieldDetails>();
params.put("id", new CalculableGroovyTemplate.FieldDetails(rootEntity, paramField, null, CalculableGroovyTemplate.CalcOperation.SINGLE, null,Collections.<String>emptyList()));

calcField.setFieldsMapping(params);
calcField.setExpression(new Script("groovy", "1"));
values = [paramField: 1]
calcField.validateScript(values, 1)
calcField.setNewDFItemName("My Calculated Field");

schema.userLockable.withLock('Adding Calculated Field') { envRW ->
DFField field = nt.create(envRW).iterator().next();
}

}