use of com.qcadoo.model.api.DataDefinition in project qcadoo by qcadoo.
the class ManyToManyIntegrationTest method shouldSaveManyToManyField.
// http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html#performance-collections
@Test
@SuppressWarnings("unchecked")
public void shouldSaveManyToManyField() throws Exception {
// given
DataDefinition productDataDefinition = dataDefinitionService.get(PLUGIN_PRODUCTS_NAME, ENTITY_NAME_PRODUCT);
DataDefinition partDataDefinition = dataDefinitionService.get(PLUGIN_PRODUCTS_NAME, ENTITY_NAME_PART);
Entity firstProduct = productDataDefinition.save(createProduct("asd", "00001"));
Entity secondProduct = productDataDefinition.save(createProduct("fgh", "00002"));
Entity thirdProduct = productDataDefinition.save(createProduct("jkl", "00003"));
Entity firstPart = fromDb(save(createPart("qwe", firstProduct, Lists.newArrayList(firstProduct, secondProduct))));
Entity secondPart = fromDb(save(createPart("rty", secondProduct, Lists.newArrayList(firstProduct, thirdProduct))));
Entity thirdPart = fromDb(save(createPart("uiop", thirdProduct, Lists.newArrayList(firstProduct, secondProduct, thirdProduct))));
// when
firstProduct = productDataDefinition.get(firstProduct.getId());
secondProduct = productDataDefinition.get(secondProduct.getId());
thirdProduct = productDataDefinition.get(thirdProduct.getId());
// then
Collection<Entity> firstProductParts = (Collection<Entity>) firstProduct.getField("partsManyToMany");
assertNotNull(firstProductParts);
assertEquals(3, firstProductParts.size());
checkProxyCollection(firstProductParts, Lists.newArrayList(firstPart, secondPart, thirdPart));
Collection<Entity> secondProductParts = (Collection<Entity>) secondProduct.getField("partsManyToMany");
assertNotNull(secondProductParts);
assertEquals(2, secondProductParts.size());
checkProxyCollection(secondProductParts, Lists.newArrayList(firstPart, thirdPart));
Collection<Entity> thirdProductParts = (Collection<Entity>) thirdProduct.getField("partsManyToMany");
assertNotNull(thirdProductParts);
assertEquals(2, thirdProductParts.size());
checkProxyCollection(thirdProductParts, Lists.newArrayList(secondPart, thirdPart));
}
use of com.qcadoo.model.api.DataDefinition in project qcadoo by qcadoo.
the class ExpressionUtilTest method shouldGenerateValueOfTheMultiFieldColumn.
@Test
public void shouldGenerateValueOfTheMultiFieldColumn() throws Exception {
// given
DataDefinition dataDefinition = mock(DataDefinition.class, RETURNS_DEEP_STUBS);
Entity entity = new DefaultEntity(dataDefinition, 1L);
entity.setField("name", "Mr T");
entity.setField("age", 33);
entity.setField("sex", "F");
given(dataDefinition.getField(eq("name")).getType().toString(eq("Mr T"), eq(Locale.ENGLISH))).willReturn("Mr X");
given(dataDefinition.getField(eq("sex")).getType().toString(eq("F"), eq(Locale.ENGLISH))).willReturn("F");
given(dataDefinition.getField(eq("age")).getType().toString(eq(33), eq(Locale.ENGLISH))).willReturn("34");
// when
String value = expressionService.getValue(entity, "#name + \" -> (\" + (#age) + \") -> \" + (#sex == \"F\" ? \"female\" : \"male\") + \".\"", Locale.ENGLISH);
// then
assertEquals("Mr X -> (34) -> female.", value);
}
use of com.qcadoo.model.api.DataDefinition in project qcadoo by qcadoo.
the class ExpressionUtilTest method shouldGenerateValueOfTheSingleFieldColumn.
@Test
public void shouldGenerateValueOfTheSingleFieldColumn() throws Exception {
// given
DataDefinition dataDefinition = mock(DataDefinition.class, RETURNS_DEEP_STUBS);
Entity entity = new DefaultEntity(dataDefinition, 1L);
entity.setField("name", "Mr T");
given(dataDefinition.getField(eq("name")).getType().toString(eq("Mr T"), eq(Locale.ENGLISH))).willReturn("Mr X");
// when
String value = expressionService.getValue(entity, "#name.toUpperCase()", Locale.ENGLISH);
// then
assertEquals("MR X", value);
}
use of com.qcadoo.model.api.DataDefinition in project qcadoo by qcadoo.
the class LookupComponentPattern method initializeComponent.
@Override
protected void initializeComponent() throws JSONException {
super.initializeComponent();
for (ComponentOption option : getOptions()) {
if ("expression".equals(option.getType())) {
expression = option.getValue();
} else if ("fieldCode".equals(option.getType())) {
fieldCode = option.getValue();
} else if ("header".equals(option.getType())) {
header = Boolean.parseBoolean(option.getValue());
} else if ("prioritizable".equals(option.getType())) {
prioritizable = Boolean.parseBoolean(option.getValue());
} else if ("onlyActive".equals(option.getType())) {
onlyActive = Boolean.parseBoolean(option.getValue());
} else if ("textRepresentationOnDisabled".equals(option.getType())) {
textRepresentationOnDisabled = Boolean.parseBoolean(option.getValue());
} else if ("boldTextRepresentationOnDisabled".equals(option.getType())) {
Boolean optionValue = Boolean.parseBoolean(option.getValue());
textRepresentationOnDisabled = optionValue;
boldTextRepresentationOnDisabled = optionValue;
}
}
modalDimensions = ModalDimensions.parseFromOptions(getOptions());
checkState(hasText(fieldCode), "Missing fieldCode for lookup");
checkState(hasText(expression), "Missing expression for lookup");
String viewName = getViewName();
DataDefinition dataDefinition = getDataDefinition();
if (getScopeFieldDefinition() != null) {
dataDefinition = getScopeFieldDefinition().getDataDefinition();
}
lookupViewDefinition = new ViewDefinitionImpl(viewName, getViewDefinition().getPluginIdentifier(), dataDefinition, false, getTranslationService());
WindowComponentPattern window = createWindowComponentPattern(lookupViewDefinition);
GridComponentPattern grid = createGridComponentPattern(lookupViewDefinition, window);
for (ComponentOption option : getOptions()) {
if ("orderable".equals(option.getType())) {
Map<String, String> newAttributes = new HashMap<>();
newAttributes.put(L_VALUE, option.getValue() + ",lookupCode");
option = new ComponentOption("orderable", newAttributes);
grid.addOption(option);
} else if ("searchable".equals(option.getType())) {
Map<String, String> newAttributes = new HashMap<>();
newAttributes.put(L_VALUE, option.getValue() + ",lookupCode");
option = new ComponentOption("searchable", newAttributes);
grid.addOption(option);
} else if (!"expression".equals(option.getType()) && !"fieldCode".equals(option.getType()) && !"textRepresentationOnDisabled".equals(option.getType()) && !"labelWidth".equals(option.getType())) {
grid.addOption(option);
}
}
grid.addOption(new ComponentOption("lookup", Collections.singletonMap(L_VALUE, L_TRUE)));
window.addChild(grid);
lookupViewDefinition.addComponentPattern(window);
lookupViewDefinition.initialize();
}
use of com.qcadoo.model.api.DataDefinition in project mes by qcadoo.
the class OrderHooksMO method onSave.
public void onSave(final DataDefinition orderDD, final Entity order) {
Entity orderDb = null;
if (Objects.nonNull(order.getId())) {
orderDb = orderDD.get(order.getId());
}
Entity masterOrder = order.getBelongsToField(OrderFieldsMO.MASTER_ORDER);
if (Objects.nonNull(masterOrder) && (MasterOrderState.IN_EXECUTION.getStringValue().equals(masterOrder.getStringField(MasterOrderFields.STATE)) || MasterOrderState.NEW.getStringValue().equals(masterOrder.getStringField(MasterOrderFields.STATE))) && canChangeToCompleted(order, orderDb)) {
changeToCompleted(order);
} else if (canChangeMasterOrderStateToInExecution(order, orderDb)) {
changeToInExecution(order);
} else if (canChangeMasterOrderStateToNew(order, orderDb)) {
changeToNew(order);
}
if (Objects.nonNull(masterOrder)) {
BigDecimal plannedQuantity = BigDecimalUtils.convertNullToZero(order.getDecimalField(OrderFields.PLANNED_QUANTITY));
Entity masterOrderProduct = getMasterOrderProduct(masterOrder, order.getBelongsToField(OrderFields.PRODUCT));
if (Objects.nonNull(masterOrderProduct) && !MasterOrderPositionStatus.ORDERED.getStringValue().equals(masterOrderProduct.getStringField(MasterOrderProductFields.MASTER_ORDER_POSITION_STATUS))) {
BigDecimal masterOrderQuantity = BigDecimalUtils.convertNullToZero(masterOrderProduct.getDecimalField(MasterOrderProductFields.MASTER_ORDER_QUANTITY));
BigDecimal planned = masterOrder.getHasManyField(MasterOrderFields.ORDERS).stream().filter(o -> o.getBelongsToField(OrderFields.PRODUCT).getId().equals(order.getBelongsToField(OrderFields.PRODUCT).getId())).map(o -> o.getDecimalField(OrderFields.PLANNED_QUANTITY)).reduce(BigDecimal.ZERO, BigDecimal::add);
planned = planned.add(plannedQuantity, numberService.getMathContext());
if (planned.compareTo(masterOrderQuantity) >= 0) {
masterOrderProduct.setField(MasterOrderProductFields.MASTER_ORDER_POSITION_STATUS, MasterOrderPositionStatus.ORDERED.getText());
masterOrderProduct.getDataDefinition().save(masterOrderProduct);
}
}
}
}
Aggregations