protected Entity addItem(Entity parentEntity, Integer qtyValue, Double priceValue) { EntityDefinition rootEntityDefn = parentEntity.getDefinition(); EntityDefinition itemDefn = (EntityDefinition) rootEntityDefn.getChildDefinition("item"); Entity item = (Entity) itemDefn.createNode(); if (qtyValue != null) { NodeDefinition qtyDefn = itemDefn.getChildDefinition("qty"); IntegerAttribute qty = (IntegerAttribute) qtyDefn.createNode(); qty.setValue(new IntegerValue(qtyValue, null)); item.add(qty); } if (priceValue != null) { NumericAttributeDefinition priceDefn = (NumericAttributeDefinition) itemDefn.getChildDefinition("price"); RealAttribute price = (RealAttribute) priceDefn.createNode(); price.setValue(new RealValue(priceValue, null)); item.add(price); } NumberAttributeDefinition totalDefn = (NumberAttributeDefinition) itemDefn.getChildDefinition("total"); RealAttribute total = (RealAttribute) totalDefn.createNode(); item.add(total); NumberAttributeDefinition discountDefn = (NumberAttributeDefinition) itemDefn.getChildDefinition("discount_percent"); IntegerAttribute discount = (IntegerAttribute) discountDefn.createNode(); item.add(discount); parentEntity.add(item); return item; }
public RealValue parseReal(NumberAttributeDefinition attrDef, String value) { Map<String, Object> map = parseJSONToMap(value, RealValue.VALUE_FIELD); if (map == null) { return null; } Integer unitId = getInteger(map, RealValue.UNIT_ID_FIELD); Unit unit = attrDef.getActualUnit(unitId); return new RealValue(getDouble(map, RealValue.VALUE_FIELD), unit); }
/** * Creates a test survey in which there is a bill with a list of items. For each item there is a * price, a quantity and a total (calculated using the an expression or a constant). * * @return */ private Survey createTestSurvey() { SurveyContext surveyContext = new TestSurveyContext(); Survey survey = surveyContext.createSurvey(); Schema schema = survey.getSchema(); EntityDefinition root = schema.createEntityDefinition(); root.setName("bill"); schema.addRootEntityDefinition(root); EntityDefinition item = schema.createEntityDefinition(); item.setName("item"); root.addChildDefinition(item); NumberAttributeDefinition qty = schema.createNumberAttributeDefinition(); qty.setType(Type.INTEGER); qty.setName("qty"); item.addChildDefinition(qty); NumberAttributeDefinition price = schema.createNumberAttributeDefinition(); price.setName("price"); item.addChildDefinition(price); NumberAttributeDefinition total = schema.createNumberAttributeDefinition(); total.setName("total"); total.setCalculated(true); total.addAttributeDefault( new AttributeDefault("qty * (price - (price * discount_percent div 100))")); item.addChildDefinition(total); NumberAttributeDefinition discountPercent = schema.createNumberAttributeDefinition(); discountPercent.setType(Type.INTEGER); discountPercent.setName("discount_percent"); discountPercent.setCalculated(true); discountPercent.addAttributeDefault(new AttributeDefault("30", "qty > 50")); discountPercent.addAttributeDefault(new AttributeDefault("20", "qty > 20")); discountPercent.addAttributeDefault(new AttributeDefault("10", "qty > 10")); discountPercent.addAttributeDefault(new AttributeDefault("0", "true()")); item.addChildDefinition(discountPercent); return survey; }