Search in sources :

Example 21 with Mapper

use of com.axelor.db.mapper.Mapper in project axelor-open-suite by axelor.

the class ConfiguratorServiceImpl method fetchManyToManyFields.

/**
 * Fix relational fields of a product or a sale order line generated from a configurator. This
 * method may become useless on a future ADK update.
 *
 * @param model
 */
protected void fetchManyToManyFields(Model model) throws AxelorException {
    // get all many to many fields
    List<MetaField> manyToManyFields = metaFieldRepository.all().filter("self.metaModel.name = :name " + "AND self.relationship = 'ManyToMany'").bind("name", model.getClass().getSimpleName()).fetch();
    Mapper mapper = Mapper.of(model.getClass());
    for (MetaField manyToManyField : manyToManyFields) {
        Set<? extends Model> manyToManyValue = (Set<? extends Model>) mapper.get(model, manyToManyField.getName());
        fetchManyToManyField(model, manyToManyValue, manyToManyField);
    }
}
Also used : Mapper(com.axelor.db.mapper.Mapper) HashSet(java.util.HashSet) Set(java.util.Set) MetaField(com.axelor.meta.db.MetaField) Model(com.axelor.db.Model)

Example 22 with Mapper

use of com.axelor.db.mapper.Mapper in project axelor-open-suite by axelor.

the class ConfiguratorServiceImpl method generateProduct.

@Override
@Transactional(rollbackOn = { Exception.class })
public void generateProduct(Configurator configurator, JsonContext jsonAttributes, JsonContext jsonIndicators) throws AxelorException {
    cleanIndicators(jsonIndicators);
    Mapper mapper = Mapper.of(Product.class);
    Product product = new Product();
    for (String key : jsonIndicators.keySet()) {
        mapper.set(product, key, jsonIndicators.get(key));
    }
    fixRelationalFields(product);
    fetchManyToManyFields(product);
    fillOneToManyFields(configurator, product, jsonAttributes);
    if (product.getProductTypeSelect() == null) {
        product.setProductTypeSelect(ProductRepository.PRODUCT_TYPE_STORABLE);
    }
    if (product.getCode() == null) {
        throw new AxelorException(configurator, TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.CONFIGURATOR_PRODUCT_MISSING_CODE));
    }
    if (product.getName() == null) {
        throw new AxelorException(configurator, TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.CONFIGURATOR_PRODUCT_MISSING_NAME));
    }
    configurator.setProduct(product);
    product.setConfigurator(configurator);
    Beans.get(ProductRepository.class).save(product);
}
Also used : Mapper(com.axelor.db.mapper.Mapper) AxelorException(com.axelor.exception.AxelorException) ProductRepository(com.axelor.apps.base.db.repo.ProductRepository) Product(com.axelor.apps.base.db.Product) Transactional(com.google.inject.persist.Transactional)

Example 23 with Mapper

use of com.axelor.db.mapper.Mapper in project axelor-open-suite by axelor.

the class ConfiguratorServiceImpl method fixRelationalFields.

protected void fixRelationalFields(Model model) throws AxelorException {
    // get all many to one fields
    List<MetaField> manyToOneFields = metaFieldRepository.all().filter("self.metaModel.name = :name " + "AND self.relationship = 'ManyToOne'").bind("name", model.getClass().getSimpleName()).fetch();
    Mapper mapper = Mapper.of(model.getClass());
    for (MetaField manyToOneField : manyToOneFields) {
        Model manyToOneValue = (Model) mapper.get(model, manyToOneField.getName());
        fixRelationalField(model, manyToOneValue, manyToOneField);
    }
}
Also used : Mapper(com.axelor.db.mapper.Mapper) MetaField(com.axelor.meta.db.MetaField) Model(com.axelor.db.Model)

Example 24 with Mapper

use of com.axelor.db.mapper.Mapper in project axelor-open-suite by axelor.

the class AppLoaderExportServiceImpl method createMetaModelInput.

protected XMLInput createMetaModelInput(AppDataLoader dataLoader, boolean relationalInput) throws ClassNotFoundException {
    XMLInput xmlInput = createXmlInput(dataLoader);
    Mapper modelMapper = Mapper.of(Class.forName(dataLoader.getMetaModel().getFullName()));
    String nodeName = getBindNodeName(xmlInput.getRoot());
    XMLBind xmlBind = new XMLBind();
    xmlBind.setTypeName(dataLoader.getMetaModel().getFullName());
    xmlBind.setNode(nodeName);
    if (!CollectionUtils.isEmpty(dataLoader.getSearchMetaFieldSet())) {
        xmlBind.setSearch(getMetaSearchFields(modelMapper, dataLoader));
        xmlBind.setUpdate(true);
    }
    if (relationalInput) {
        xmlBind.setCreate(false);
    }
    xmlBind.setBindings(getMetaFieldBinding(modelMapper, dataLoader, relationalInput));
    List<XMLBind> rootBindings = new ArrayList<XMLBind>();
    rootBindings.add(xmlBind);
    xmlInput.setBindings(rootBindings);
    return xmlInput;
}
Also used : Mapper(com.axelor.db.mapper.Mapper) ArrayList(java.util.ArrayList) XMLInput(com.axelor.data.xml.XMLInput) XMLBind(com.axelor.data.xml.XMLBind)

Example 25 with Mapper

use of com.axelor.db.mapper.Mapper in project axelor-open-suite by axelor.

the class AppLoaderExportServiceImpl method addMetaModelData.

protected void addMetaModelData(AppDataLoader dataLoader, File parentDir) {
    try {
        Class klass = Class.forName(dataLoader.getMetaModel().getFullName());
        if (!allowRead(klass)) {
            return;
        }
        Mapper modelMapper = Mapper.of(klass);
        String modelName = dataLoader.getMetaModel().getName();
        String dasherizeModel = Inflector.getInstance().dasherize(modelName);
        File dataFile = new File(parentDir, modelName + ".xml");
        FileWriter fileWriter = createHeader(dasherizeModel, dataFile);
        List<FullContext> records = FullContextHelper.filter(modelName, dataLoader.getFilterQuery());
        for (FullContext record : records) {
            if (!allowRead(klass, (Long) record.get("id"))) {
                continue;
            }
            fileWriter.write("<" + dasherizeModel + ">\n");
            for (MetaField metaField : dataLoader.getMetaFieldSet()) {
                String field = metaField.getName();
                fileWriter.write("\t<" + field + ">" + extractMetaFieldValue(record, modelMapper.getProperty(field)) + "</" + field + ">\n");
            }
            fileWriter.write("</" + dasherizeModel + ">\n\n");
        }
        fileWriter.write("</" + dasherizeModel + "s>\n");
        fileWriter.close();
    } catch (IOException | ClassNotFoundException e) {
        TraceBackService.trace(e);
    }
}
Also used : Mapper(com.axelor.db.mapper.Mapper) MetaField(com.axelor.meta.db.MetaField) FileWriter(java.io.FileWriter) FullContext(com.axelor.apps.tool.context.FullContext) IOException(java.io.IOException) File(java.io.File)

Aggregations

Mapper (com.axelor.db.mapper.Mapper)44 Property (com.axelor.db.mapper.Property)19 AxelorException (com.axelor.exception.AxelorException)13 Model (com.axelor.db.Model)12 MetaField (com.axelor.meta.db.MetaField)10 ArrayList (java.util.ArrayList)10 MetaModel (com.axelor.meta.db.MetaModel)7 File (java.io.File)6 IOException (java.io.IOException)6 FileField (com.axelor.apps.base.db.FileField)5 Transactional (com.google.inject.persist.Transactional)5 TraceBackService (com.axelor.exception.service.TraceBackService)4 MetaFiles (com.axelor.meta.MetaFiles)4 MetaModelRepository (com.axelor.meta.db.repo.MetaModelRepository)4 List (java.util.List)4 Inflector (com.axelor.common.Inflector)3 MetaFile (com.axelor.meta.db.MetaFile)3 MetaJsonField (com.axelor.meta.db.MetaJsonField)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Strings (com.google.common.base.Strings)3