Search in sources :

Example 36 with Model

use of com.axelor.db.Model 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 37 with Model

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

the class ConfiguratorServiceImpl method fillOneToManyFields.

protected void fillOneToManyFields(Configurator configurator, Model model, JsonContext jsonAttributes) throws AxelorException {
    try {
        ConfiguratorCreator creator = configurator.getConfiguratorCreator();
        List<? extends ConfiguratorFormula> configuratorFormulaList;
        Class[] methodArg = new Class[1];
        if (creator.getGenerateProduct()) {
            configuratorFormulaList = creator.getConfiguratorProductFormulaList();
            methodArg[0] = Product.class;
        } else {
            configuratorFormulaList = creator.getConfiguratorSOLineFormulaList();
            methodArg[0] = SaleOrderLine.class;
        }
        configuratorFormulaList = configuratorFormulaList.stream().filter(configuratorFormula -> "OneToMany".equals(configuratorFormula.getMetaField().getRelationship())).collect(Collectors.toList());
        for (ConfiguratorFormula formula : configuratorFormulaList) {
            List<? extends Model> computedValue = (List<? extends Model>) computeFormula(formula.getFormula(), jsonAttributes);
            if (computedValue == null) {
                continue;
            }
            Method setMappedByMethod = computeMappedByMethod(formula);
            for (Model listElement : computedValue) {
                setMappedByMethod.invoke(listElement, model);
                JPA.save(listElement);
            }
        }
    } catch (InvocationTargetException | IllegalAccessException | ClassNotFoundException e) {
        throw new AxelorException(e, TraceBackRepository.CATEGORY_INCONSISTENCY);
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) ConfiguratorCreator(com.axelor.apps.sale.db.ConfiguratorCreator) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConfiguratorFormula(com.axelor.apps.sale.db.ConfiguratorFormula) Model(com.axelor.db.Model) List(java.util.List)

Example 38 with Model

use of com.axelor.db.Model 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 39 with Model

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

the class MessageServiceImpl method regenerateMessage.

@Override
@Transactional(rollbackOn = { Exception.class })
public Message regenerateMessage(Message message) throws Exception {
    Preconditions.checkNotNull(message.getTemplate(), I18n.get("Cannot regenerate message without template associated to message."));
    Preconditions.checkNotNull(message.getRelatedTo1Select(), I18n.get("Cannot regenerate message without related model."));
    Class m = Class.forName(message.getRelatedTo1Select());
    Model model = JPA.all(m).filter("self.id = ?", message.getRelatedTo1SelectId()).fetchOne();
    Message newMessage = Beans.get(TemplateMessageService.class).generateMessage(model, message.getTemplate());
    newMessage.setRelatedTo2Select(message.getRelatedTo2Select());
    newMessage.setRelatedTo2SelectId(message.getRelatedTo2SelectId());
    message.setArchived(true);
    return newMessage;
}
Also used : Message(com.axelor.apps.message.db.Message) IExceptionMessage(com.axelor.apps.message.exception.IExceptionMessage) Model(com.axelor.db.Model) Transactional(com.google.inject.persist.Transactional)

Example 40 with Model

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

the class TemplateMessageServiceImpl method computeTemplateContexts.

@SuppressWarnings("unchecked")
@Override
public Map<String, Object> computeTemplateContexts(List<TemplateContext> templateContextList, long objectId, String model, boolean isJson, Map<String, Object> templatesContext) throws ClassNotFoundException {
    if (templateContextList == null) {
        return templatesContext;
    }
    Context context = null;
    if (isJson) {
        context = new com.axelor.rpc.Context(objectId, MetaJsonRecord.class);
    } else {
        Class<? extends Model> myClass = (Class<? extends Model>) Class.forName(model);
        context = new com.axelor.rpc.Context(objectId, myClass);
    }
    for (TemplateContext templateContext : templateContextList) {
        Object result = templateContextService.computeTemplateContext(templateContext, context);
        templatesContext.put(templateContext.getName(), result);
    }
    return templatesContext;
}
Also used : TemplateContext(com.axelor.apps.message.db.TemplateContext) Context(com.axelor.rpc.Context) MetaJsonModel(com.axelor.meta.db.MetaJsonModel) Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel) TemplateContext(com.axelor.apps.message.db.TemplateContext) Context(com.axelor.rpc.Context) MetaJsonRecord(com.axelor.meta.db.MetaJsonRecord)

Aggregations

Model (com.axelor.db.Model)77 MetaModel (com.axelor.meta.db.MetaModel)22 AxelorException (com.axelor.exception.AxelorException)19 ArrayList (java.util.ArrayList)18 HashMap (java.util.HashMap)16 Context (com.axelor.rpc.Context)15 Transactional (com.google.inject.persist.Transactional)15 List (java.util.List)14 Mapper (com.axelor.db.mapper.Mapper)13 IOException (java.io.IOException)13 File (java.io.File)12 Map (java.util.Map)12 Property (com.axelor.db.mapper.Property)11 MetaJsonRecord (com.axelor.meta.db.MetaJsonRecord)9 JsonContext (com.axelor.rpc.JsonContext)8 MetaModelRepository (com.axelor.meta.db.repo.MetaModelRepository)7 Strings (com.google.common.base.Strings)7 HashSet (java.util.HashSet)7 FullContext (com.axelor.apps.tool.context.FullContext)6 Beans (com.axelor.inject.Beans)6