Search in sources :

Example 6 with GroovyScriptHelper

use of com.axelor.script.GroovyScriptHelper in project axelor-open-suite by axelor.

the class ConfiguratorFormulaServiceImpl method checkFormula.

@Override
public void checkFormula(ConfiguratorFormula formula, ConfiguratorCreator creator) throws AxelorException {
    ScriptBindings defaultValueBindings = Beans.get(ConfiguratorCreatorService.class).getTestingValues(creator);
    Object result = new GroovyScriptHelper(defaultValueBindings).eval(formula.getFormula());
    String wantedTypeName = formula.getMetaField().getTypeName();
    if (result == null) {
        throw new AxelorException(formula, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CONFIGURATOR_CREATOR_SCRIPT_ERROR));
    } else if (!Beans.get(ConfiguratorService.class).areCompatible(wantedTypeName, getCalculatedClassName(result)) && !wantedTypeName.equals("one-to-many") && !wantedTypeName.equals("many-to-many")) {
        throw new AxelorException(formula, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CONFIGURATOR_CREATOR_FORMULA_TYPE_ERROR), result.getClass().getSimpleName(), wantedTypeName);
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) ScriptBindings(com.axelor.script.ScriptBindings) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper)

Example 7 with GroovyScriptHelper

use of com.axelor.script.GroovyScriptHelper in project axelor-open-suite by axelor.

the class ValueMapperService method execute.

public Object execute(ValueMapper mapper, Model model) {
    if (model == null || mapper == null) {
        return null;
    }
    String modelName = getModelVariable(model);
    Bindings bindings = new SimpleBindings();
    bindings.put("$ctx", FullContextHelper.class);
    bindings.put(modelName, new FullContext(model));
    bindings.put(modelName + "Id", model.getId());
    Object result = new GroovyScriptHelper(bindings).eval(mapper.getScript());
    return result;
}
Also used : SimpleBindings(javax.script.SimpleBindings) FullContext(com.axelor.apps.tool.context.FullContext) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper)

Example 8 with GroovyScriptHelper

use of com.axelor.script.GroovyScriptHelper in project axelor-open-suite by axelor.

the class GlobalAuditTracker method onComplete.

/**
 * This method should be called from {@link
 * AuditInterceptor#beforeTransactionCompletion(Transaction)} method to finish change recording.
 *
 * @param tx the transaction in which the change tracking is being done
 * @param user the session user
 */
public void onComplete(Transaction tx, User user) {
    List<GlobalTrackingLog> logList = LOGS.get();
    LOGS.remove();
    if (CollectionUtils.isEmpty(logList)) {
        return;
    }
    MetaModelRepository modelRepo = Beans.get(MetaModelRepository.class);
    MetaFieldRepository fieldRepo = Beans.get(MetaFieldRepository.class);
    GlobalTrackingLogRepository logRepo = Beans.get(GlobalTrackingLogRepository.class);
    GlobalTrackingConfigurationLineRepository configLineRepo = Beans.get(GlobalTrackingConfigurationLineRepository.class);
    GlobalTrackingConfigurationLine configLine;
    List<GlobalTrackingConfigurationLine> configLineList;
    ScriptBindings bindings;
    for (GlobalTrackingLog log : logList) {
        configLineList = configLineRepo.all().filter("self.metaModel.name = ?", log.getMetaModelName()).fetch();
        if (configLineList.isEmpty()) {
            continue;
        }
        log.setMetaModel(modelRepo.findByName(log.getMetaModelName()));
        List<GlobalTrackingLogLine> logLinesToSave = new ArrayList<>();
        if ((CollectionUtils.isNotEmpty(log.getGlobalTrackingLogLineList()))) {
            try {
                bindings = new ScriptBindings(this.getContext(JPA.find((Class<Model>) Class.forName(log.getMetaModel().getFullName()), log.getRelatedId())));
            } catch (Exception e) {
                continue;
            }
            for (GlobalTrackingLogLine line : log.getGlobalTrackingLogLineList()) {
                configLine = configLineList.stream().filter(l -> l.getMetaField().getName().equals(line.getMetaFieldName())).findFirst().orElse(null);
                if (configLine == null || !this.canTrack(configLine, log.getTypeSelect()) || (!Strings.isNullOrEmpty(configLine.getTrackingCondition()) && !Boolean.TRUE.equals(new GroovyScriptHelper(bindings).eval(configLine.getTrackingCondition())))) {
                    continue;
                }
                line.setMetaField(fieldRepo.all().filter("self.metaModel.id = ? AND self.name = ?", log.getMetaModel().getId(), line.getMetaFieldName()).fetchOne());
                logLinesToSave.add(line);
            }
        }
        if (!logLinesToSave.isEmpty() || (GlobalTrackingLogRepository.TYPE_DELETE == log.getTypeSelect() && configLineList.stream().anyMatch(l -> Boolean.TRUE.equals(l.getTrackDeletion())))) {
            log.getGlobalTrackingLogLineList().stream().forEach(l -> l.setGlobalTrackingLog(null));
            logLinesToSave.stream().forEach(l -> l.setGlobalTrackingLog(log));
            log.setUser(user);
            logRepo.save(log);
        }
    }
}
Also used : GlobalTrackingConfigurationLine(com.axelor.apps.base.db.GlobalTrackingConfigurationLine) MetaModelRepository(com.axelor.meta.db.repo.MetaModelRepository) Arrays(java.util.Arrays) LocalDateTime(java.time.LocalDateTime) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper) HashMap(java.util.HashMap) ScriptBindings(com.axelor.script.ScriptBindings) PersistentSet(org.hibernate.collection.internal.PersistentSet) ArrayList(java.util.ArrayList) Transaction(org.hibernate.Transaction) Strings(com.google.common.base.Strings) Introspector(java.beans.Introspector) GlobalTrackingLog(com.axelor.apps.base.db.GlobalTrackingLog) CollectionUtils(org.apache.commons.collections.CollectionUtils) AuditInterceptor(com.axelor.auth.AuditInterceptor) BeanInfo(java.beans.BeanInfo) Map(java.util.Map) GlobalTrackingLogRepository(com.axelor.apps.base.db.repo.GlobalTrackingLogRepository) Method(java.lang.reflect.Method) JPA(com.axelor.db.JPA) PersistentBag(org.hibernate.collection.internal.PersistentBag) Model(com.axelor.db.Model) Collection(java.util.Collection) GlobalTrackingLogLine(com.axelor.apps.base.db.GlobalTrackingLogLine) Collectors(java.util.stream.Collectors) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GlobalTrackingConfigurationLineRepository(com.axelor.apps.base.db.repo.GlobalTrackingConfigurationLineRepository) List(java.util.List) Beans(com.axelor.inject.Beans) MetaFieldRepository(com.axelor.meta.db.repo.MetaFieldRepository) PropertyDescriptor(java.beans.PropertyDescriptor) AbstractPersistentCollection(org.hibernate.collection.internal.AbstractPersistentCollection) AuditableModel(com.axelor.auth.db.AuditableModel) AuthUtils(com.axelor.auth.AuthUtils) User(com.axelor.auth.db.User) MetaModelRepository(com.axelor.meta.db.repo.MetaModelRepository) GlobalTrackingLog(com.axelor.apps.base.db.GlobalTrackingLog) GlobalTrackingConfigurationLineRepository(com.axelor.apps.base.db.repo.GlobalTrackingConfigurationLineRepository) ArrayList(java.util.ArrayList) GlobalTrackingLogLine(com.axelor.apps.base.db.GlobalTrackingLogLine) MetaFieldRepository(com.axelor.meta.db.repo.MetaFieldRepository) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GlobalTrackingLogRepository(com.axelor.apps.base.db.repo.GlobalTrackingLogRepository) GlobalTrackingConfigurationLine(com.axelor.apps.base.db.GlobalTrackingConfigurationLine) ScriptBindings(com.axelor.script.ScriptBindings)

Example 9 with GroovyScriptHelper

use of com.axelor.script.GroovyScriptHelper in project axelor-open-suite by axelor.

the class ImportAdvancedImport method importGeneral.

@SuppressWarnings("unchecked")
public Object importGeneral(Object bean, Map<String, Object> values) throws ClassNotFoundException {
    if (bean == null) {
        return bean;
    }
    FileTab fileTab = fileTabRepo.find(Long.valueOf(values.get("fileTabId").toString()));
    ScriptHelper scriptHelper = new GroovyScriptHelper(new ScriptBindings(values));
    List<String> exprs = (List<String>) values.get("ifConditions" + fileTab.getId());
    if (!CollectionUtils.isEmpty(exprs)) {
        if ((boolean) scriptHelper.eval(String.join(" || ", exprs))) {
            return null;
        }
    }
    if (((Model) bean).getId() == null) {
        List<Property> propList = this.getProperties(bean);
        JPA.save((Model) bean);
        this.addJsonObjectRecord(bean, fileTab, fileTab.getMetaModel().getName(), values);
        int fieldSeq = 2;
        int btnSeq = 3;
        for (Property prop : propList) {
            validatorService.createCustomObjectSet(fileTab.getClass().getName(), prop.getTarget().getName(), fieldSeq);
            validatorService.createCustomButton(fileTab.getClass().getName(), prop.getTarget().getName(), btnSeq);
            this.addJsonObjectRecord(prop.get(bean), fileTab, StringUtils.substringAfterLast(prop.getTarget().getName(), "."), values);
            fieldSeq++;
            btnSeq++;
        }
    }
    final String ACTIONS_TO_APPLY = "actionsToApply" + fileTab.getId();
    if (!ObjectUtils.isEmpty(values.get(ACTIONS_TO_APPLY))) {
        bean = actionService.apply(values.get(ACTIONS_TO_APPLY).toString(), bean);
    }
    return bean;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) FileTab(com.axelor.apps.base.db.FileTab) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper) ScriptHelper(com.axelor.script.ScriptHelper) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper) ScriptBindings(com.axelor.script.ScriptBindings) Property(com.axelor.db.mapper.Property)

Example 10 with GroovyScriptHelper

use of com.axelor.script.GroovyScriptHelper in project axelor-open-suite by axelor.

the class BamlServiceImpl method execute.

@Override
@Transactional
public Model execute(BamlModel bamlModel, Map<String, Object> context) {
    Bindings bindings = new SimpleBindings();
    if (context != null) {
        bindings.putAll(context);
    }
    GroovyScriptHelper helper = new GroovyScriptHelper(bindings);
    Object object = helper.eval(bamlModel.getResultScript());
    if (object != null && object instanceof Model) {
        return JpaRepository.of(EntityHelper.getEntityClass((Model) object)).save((Model) object);
    }
    return null;
}
Also used : SimpleBindings(javax.script.SimpleBindings) Model(com.axelor.db.Model) BamlModel(com.axelor.apps.bpm.db.BamlModel) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper) Transactional(com.google.inject.persist.Transactional)

Aggregations

GroovyScriptHelper (com.axelor.script.GroovyScriptHelper)12 Context (com.axelor.rpc.Context)4 FullContext (com.axelor.apps.tool.context.FullContext)3 Model (com.axelor.db.Model)3 ScriptBindings (com.axelor.script.ScriptBindings)3 ScriptHelper (com.axelor.script.ScriptHelper)3 Bindings (javax.script.Bindings)3 SimpleBindings (javax.script.SimpleBindings)3 BamlModel (com.axelor.apps.bpm.db.BamlModel)2 User (com.axelor.auth.db.User)2 AxelorException (com.axelor.exception.AxelorException)2 Transactional (com.google.inject.persist.Transactional)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ProcessActionNode (com.axelor.apps.baml.xml.ProcessActionNode)1 ProcessActionRootNode (com.axelor.apps.baml.xml.ProcessActionRootNode)1 Company (com.axelor.apps.base.db.Company)1 FileTab (com.axelor.apps.base.db.FileTab)1 GlobalTrackingConfigurationLine (com.axelor.apps.base.db.GlobalTrackingConfigurationLine)1 GlobalTrackingLog (com.axelor.apps.base.db.GlobalTrackingLog)1