Search in sources :

Example 1 with MetaField

use of com.axelor.meta.db.MetaField in project axelor-open-suite by axelor.

the class FileFieldServiceImpl method getSubFieldType.

private String[] getSubFieldType(String[] subFields, int index, MetaField parentField) {
    String[] types = new String[2];
    if (index < subFields.length) {
        types[0] = parentField.getTypeName();
        types[1] = parentField.getRelationship();
        MetaModel childModel = metaModelRepo.findByName(parentField.getTypeName());
        if (childModel != null) {
            MetaField childField = metaFieldRepo.findByModel(subFields[index], childModel);
            if (childField == null) {
                return types;
            }
            if (childField.getRelationship() != null) {
                if (!types[0].equals("MetaFile")) {
                    types[0] = childField.getTypeName();
                }
                types[1] = childField.getRelationship();
                types = this.getSubFieldType(subFields, index + 1, childField);
            } else {
                if (!types[0].equals("MetaFile")) {
                    types[0] = childField.getTypeName();
                }
            }
        }
    } else {
        if ((!Strings.isNullOrEmpty(types[0]) && !types[0].equals("MetaFile")) || Strings.isNullOrEmpty(types[0])) {
            types[0] = parentField.getTypeName();
        }
        types[1] = parentField.getRelationship();
    }
    return types;
}
Also used : MetaModel(com.axelor.meta.db.MetaModel) MetaField(com.axelor.meta.db.MetaField)

Example 2 with MetaField

use of com.axelor.meta.db.MetaField in project axelor-open-suite by axelor.

the class AdvancedExportServiceImpl method createQueryParts.

/**
 * This method create query parts based on field.
 *
 * @param splitField
 * @param parentIndex
 * @param metaModel
 * @throws ClassNotFoundException
 */
private void createQueryParts(String[] splitField, int parentIndex, MetaModel metaModel) throws ClassNotFoundException {
    while (parentIndex <= splitField.length - 1) {
        MetaField relationalField = metaFieldRepo.all().filter("self.name = ?1 and self.metaModel = ?2", splitField[parentIndex], metaModel).fetchOne();
        MetaModel subMetaModel = metaModelRepo.all().filter("self.name = ?1", relationalField.getTypeName()).fetchOne();
        if (!Strings.isNullOrEmpty(relationalField.getRelationship())) {
            checkRelationalField(splitField, parentIndex);
        } else {
            checkSelectionField(splitField, parentIndex, metaModel);
            checkNormalField(splitField, parentIndex);
        }
        parentIndex += 1;
        metaModel = subMetaModel;
    }
}
Also used : MetaModel(com.axelor.meta.db.MetaModel) MetaField(com.axelor.meta.db.MetaField)

Example 3 with MetaField

use of com.axelor.meta.db.MetaField in project axelor-open-suite by axelor.

the class ProductStockRepository method save.

public Product save(Product product) {
    WeightedAveragePriceService weightedAveragePriceService = Beans.get(WeightedAveragePriceService.class);
    Set<MetaField> specificProductFieldSet = appBaseService.getAppBase().getCompanySpecificProductFieldsSet();
    if (!specificProductFieldSet.isEmpty() && appBaseService.getAppBase().getEnableMultiCompany()) {
        ArrayList<Company> productCompanyList = new ArrayList<>();
        if (product.getProductCompanyList() != null) {
            for (ProductCompany productCompany : product.getProductCompanyList()) {
                productCompanyList.add(productCompany.getCompany());
            }
        }
        Mapper mapper = Mapper.of(Product.class);
        List<StockConfig> stockConfigList = Beans.get(StockConfigRepository.class).all().fetch();
        for (StockConfig stockConfig : stockConfigList) {
            if (stockConfig.getCompany() != null && !productCompanyList.contains(stockConfig.getCompany()) && stockConfig.getReceiptDefaultStockLocation() != null && (stockConfig.getCompany().getArchived() == null || !stockConfig.getCompany().getArchived())) {
                ProductCompany productCompany = new ProductCompany();
                for (MetaField specificField : specificProductFieldSet) {
                    mapper.set(productCompany, specificField.getName(), mapper.get(product, specificField.getName()));
                }
                // specific case for avgPrice per company
                productCompany.setAvgPrice(weightedAveragePriceService.computeAvgPriceForCompany(product, stockConfig.getCompany()));
                productCompany.setCompany(stockConfig.getCompany());
                productCompany.setProduct(product);
                product.addProductCompanyListItem(productCompany);
            }
        }
    }
    return super.save(product);
}
Also used : Mapper(com.axelor.db.mapper.Mapper) Company(com.axelor.apps.base.db.Company) ProductCompany(com.axelor.apps.base.db.ProductCompany) StockConfig(com.axelor.apps.stock.db.StockConfig) MetaField(com.axelor.meta.db.MetaField) WeightedAveragePriceService(com.axelor.apps.stock.service.WeightedAveragePriceService) ArrayList(java.util.ArrayList) ProductCompany(com.axelor.apps.base.db.ProductCompany)

Example 4 with MetaField

use of com.axelor.meta.db.MetaField in project axelor-open-suite by axelor.

the class DuplicateObjectsService method removeDuplicate.

@Transactional
public void removeDuplicate(List<Long> selectedIds, String modelName) {
    List<Object> duplicateObjects = getDuplicateObject(selectedIds, modelName);
    Object originalObjct = getOriginalObject(selectedIds, modelName);
    List<MetaField> allField = metaFieldRepo.all().filter("(relationship = 'ManyToOne' AND typeName = ?1) OR (relationship = 'ManyToMany' AND (typeName = ?1 OR metaModel.name =?1))", modelName).fetch();
    for (MetaField metaField : allField) {
        if ("ManyToOne".equals(metaField.getRelationship())) {
            Query update = JPA.em().createQuery("UPDATE " + metaField.getMetaModel().getFullName() + " self SET self." + metaField.getName() + " = :value WHERE self." + metaField.getName() + " in (:duplicates)");
            update.setParameter("value", originalObjct);
            update.setParameter("duplicates", duplicateObjects);
            update.executeUpdate();
        } else if ("ManyToMany".equals(metaField.getRelationship())) {
            if (metaField.getTypeName().equals(modelName)) {
                Query select = JPA.em().createQuery("select self from " + metaField.getMetaModel().getFullName() + " self LEFT JOIN self." + metaField.getName() + " as x WHERE x IN (:ids)");
                select.setParameter("ids", duplicateObjects);
                List<?> list = select.getResultList();
                for (Object obj : list) {
                    Set<Object> items = (Set<Object>) Mapper.of(obj.getClass()).get(obj, metaField.getName());
                    for (Object dupObj : duplicateObjects) {
                        if (items.contains(dupObj)) {
                            items.remove(dupObj);
                        }
                    }
                    items.add(originalObjct);
                }
            }
            Mapper mapper = Mapper.of(originalObjct.getClass());
            Set<Object> existRelationalObjects = (Set<Object>) mapper.get(originalObjct, metaField.getName());
            for (int i = 0; i < duplicateObjects.size(); i++) {
                Set<Object> newRelationalObjects = (Set<Object>) mapper.get(duplicateObjects.get(i), metaField.getName());
                if (newRelationalObjects != null) {
                    existRelationalObjects.addAll(newRelationalObjects);
                    mapper.set(duplicateObjects.get(i), metaField.getName(), new HashSet<>());
                }
            }
        }
    }
    JPA.em().flush();
    JPA.em().clear();
    for (Object obj : getDuplicateObject(selectedIds, modelName)) {
        JPA.em().remove(obj);
    }
}
Also used : Mapper(com.axelor.db.mapper.Mapper) Set(java.util.Set) HashSet(java.util.HashSet) Query(javax.persistence.Query) MetaField(com.axelor.meta.db.MetaField) List(java.util.List) HashSet(java.util.HashSet) Transactional(com.google.inject.persist.Transactional)

Example 5 with MetaField

use of com.axelor.meta.db.MetaField in project axelor-open-suite by axelor.

the class ChartBuilderService method addSearchField.

/**
 * Method set default value for search-fields(parameters). It will add field and expression in
 * onNew for chart.
 *
 * @param fieldName Name of field of search-field.
 * @param typeName Type of field.
 * @param defaultValue Default value input in chart filter.
 * @param modelField It is for relational field. String array with first element as Model name and
 *     second as its field.
 */
// private void setDefaultValue(String fieldName, String typeName,
// String defaultValue, String[] modelField) {
// 
// if (defaultValue == null) {
// return;
// }
// 
// RecordField field = new RecordField();
// field.setName(fieldName);
// 
// defaultValue = filterCommonService.getTagValue(defaultValue, false);
// 
// if (modelField != null) {
// if (typeName.equals("STRING")) {
// defaultValue = "__repo__(" + modelField[0]
// + ").all().filter(\"LOWER(" + modelField[1] + ") LIKE "
// + defaultValue + "\").fetchOne()";
// } else {
// defaultValue = "__repo__(" + modelField[0]
// + ").all().filter(\"" + modelField[1] + " = "
// + defaultValue + "\").fetchOne()";
// }
// 
// }
// 
// log.debug("Default value: {}", defaultValue);
// 
// field.setExpression("eval:" + defaultValue);
// 
// onNewFields.add(field);
// }
/**
 * It will create onNew action from onNew fields.
 *
 * @param viewBuilder ViewBuilder use to get model name also used in onNew action name creation.
 */
// private void setOnNewAction(ChartBuilder chartBuilder) {
// 
// if (!onNewFields.isEmpty()) {
// onNewAction = new ActionRecord();
// onNewAction.setName("action-" + chartBuilder.getName() + "-default");
// onNewAction.setModel(chartBuilder.getModel());
// onNewAction.setFields(onNewFields);
// }
// 
// }
private void addSearchField(List<Filter> filters) throws AxelorException {
    if (filters == null) {
        return;
    }
    for (Filter filter : filters) {
        if (!filter.getIsParameter()) {
            continue;
        }
        String fieldStr = "param" + filter.getId();
        Object object = null;
        StringBuilder parent = new StringBuilder("self");
        if (filter.getIsJson()) {
            object = filterSqlService.parseJsonField(filter.getMetaJsonField(), filter.getTargetField(), null, parent);
        } else {
            object = filterSqlService.parseMetaField(filter.getMetaField(), filter.getTargetField(), null, parent, true);
        }
        if (object instanceof MetaField) {
            fieldStr = getMetaSearchField(fieldStr, (MetaField) object);
        } else {
            fieldStr = getJsonSearchField(fieldStr, (MetaJsonField) object);
        }
        searchFields.add(fieldStr + "\" x-required=\"true\" />");
    }
}
Also used : Filter(com.axelor.studio.db.Filter) MetaField(com.axelor.meta.db.MetaField) MetaJsonField(com.axelor.meta.db.MetaJsonField)

Aggregations

MetaField (com.axelor.meta.db.MetaField)45 MetaJsonField (com.axelor.meta.db.MetaJsonField)15 MetaModel (com.axelor.meta.db.MetaModel)10 Mapper (com.axelor.db.mapper.Mapper)9 AxelorException (com.axelor.exception.AxelorException)8 ArrayList (java.util.ArrayList)6 Property (com.axelor.db.mapper.Property)5 Filter (com.axelor.studio.db.Filter)4 Model (com.axelor.db.Model)3 MetaFieldRepository (com.axelor.meta.db.repo.MetaFieldRepository)3 MetaModelRepository (com.axelor.meta.db.repo.MetaModelRepository)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Company (com.axelor.apps.base.db.Company)2 FileField (com.axelor.apps.base.db.FileField)2 ProductCompany (com.axelor.apps.base.db.ProductCompany)2 Context (com.axelor.rpc.Context)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Transactional (com.google.inject.persist.Transactional)2 BigInteger (java.math.BigInteger)2