Search in sources :

Example 81 with DataDefinition

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));
}
Also used : Entity(com.qcadoo.model.api.Entity) ProxyEntity(com.qcadoo.model.internal.ProxyEntity) Collection(java.util.Collection) DataDefinition(com.qcadoo.model.api.DataDefinition) Test(org.junit.Test)

Example 82 with DataDefinition

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);
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DataDefinition(com.qcadoo.model.api.DataDefinition) Test(org.junit.Test)

Example 83 with DataDefinition

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);
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DataDefinition(com.qcadoo.model.api.DataDefinition) Test(org.junit.Test)

Example 84 with DataDefinition

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();
}
Also used : ViewDefinitionImpl(com.qcadoo.view.internal.internal.ViewDefinitionImpl) GridComponentPattern(com.qcadoo.view.internal.components.grid.GridComponentPattern) WindowComponentPattern(com.qcadoo.view.internal.components.window.WindowComponentPattern) HashMap(java.util.HashMap) DataDefinition(com.qcadoo.model.api.DataDefinition) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 85 with DataDefinition

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);
            }
        }
    }
}
Also used : DataDefinitionService(com.qcadoo.model.api.DataDefinitionService) SearchRestrictions(com.qcadoo.model.api.search.SearchRestrictions) OrderFields(com.qcadoo.mes.orders.constants.OrderFields) Autowired(org.springframework.beans.factory.annotation.Autowired) BigDecimalUtils(com.qcadoo.model.api.BigDecimalUtils) DataDefinition(com.qcadoo.model.api.DataDefinition) Collectors(java.util.stream.Collectors) MasterOrdersConstants(com.qcadoo.mes.masterOrders.constants.MasterOrdersConstants) Objects(java.util.Objects) BigDecimal(java.math.BigDecimal) MasterOrderFields(com.qcadoo.mes.masterOrders.constants.MasterOrderFields) Entity(com.qcadoo.model.api.Entity) List(java.util.List) MasterOrderProductFields(com.qcadoo.mes.masterOrders.constants.MasterOrderProductFields) Service(org.springframework.stereotype.Service) NumberService(com.qcadoo.model.api.NumberService) MasterOrderState(com.qcadoo.mes.masterOrders.constants.MasterOrderState) MasterOrderPositionDtoFields(com.qcadoo.mes.masterOrders.constants.MasterOrderPositionDtoFields) OrderFieldsMO(com.qcadoo.mes.masterOrders.constants.OrderFieldsMO) Entity(com.qcadoo.model.api.Entity) BigDecimal(java.math.BigDecimal)

Aggregations

DataDefinition (com.qcadoo.model.api.DataDefinition)415 Entity (com.qcadoo.model.api.Entity)285 Test (org.junit.Test)165 BigDecimal (java.math.BigDecimal)53 FieldDefinition (com.qcadoo.model.api.FieldDefinition)48 List (java.util.List)32 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)32 Service (org.springframework.stereotype.Service)31 Autowired (org.springframework.beans.factory.annotation.Autowired)27 Date (java.util.Date)26 Map (java.util.Map)26 Collectors (java.util.stream.Collectors)26 FormComponent (com.qcadoo.view.api.components.FormComponent)25 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)23 DataDefinitionService (com.qcadoo.model.api.DataDefinitionService)22 IOException (java.io.IOException)21 Objects (java.util.Objects)21 GridComponent (com.qcadoo.view.api.components.GridComponent)20 InternalViewDefinition (com.qcadoo.view.internal.api.InternalViewDefinition)20 Lists (com.google.common.collect.Lists)16