Search in sources :

Example 56 with Model

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

the class PrintTemplateLineServiceImpl method checkExpression.

@SuppressWarnings("unchecked")
@Transactional
@Override
public void checkExpression(Long objectId, MetaModel metaModel, PrintTemplateLine printTemplateLine) throws ClassNotFoundException, AxelorException {
    if (metaModel == null) {
        return;
    }
    String model = metaModel.getFullName();
    String simpleModel = metaModel.getName();
    PrintTemplateLineTest test = printTemplateLine.getPrintTemplateLineTest();
    Context scriptContext = null;
    if (StringUtils.notEmpty(model)) {
        Class<? extends Model> modelClass = (Class<? extends Model>) Class.forName(model);
        Model modelObject = JPA.find(modelClass, objectId);
        if (ObjectUtils.notEmpty(modelObject)) {
            scriptContext = new Context(Mapper.toMap(modelObject), modelClass);
        }
    }
    String resultOfTitle = null;
    String resultOfContent = null;
    Locale locale = Optional.ofNullable(printTemplateLine.getPrintTemplate().getLanguage()).map(Language::getCode).map(Locale::new).orElseGet(AppFilter::getLocale);
    TemplateMaker maker = initMaker(objectId, model, simpleModel, locale);
    try {
        if (StringUtils.notEmpty(printTemplateLine.getTitle())) {
            maker.setTemplate(printTemplateLine.getTitle());
            resultOfTitle = maker.make();
        }
    } catch (Exception e) {
        resultOfTitle = "Error in title";
    }
    try {
        if (StringUtils.notEmpty(printTemplateLine.getContent())) {
            maker.setTemplate(printTemplateLine.getContent());
            resultOfContent = maker.make();
        }
    } catch (Exception e) {
        resultOfContent = "Error in content";
    }
    test.setContentResult(resultOfTitle + "<br>" + resultOfContent);
    Boolean present = Boolean.TRUE;
    if (StringUtils.notEmpty(printTemplateLine.getConditions())) {
        Object evaluation = null;
        try {
            evaluation = templateContextService.computeTemplateContext(printTemplateLine.getConditions(), scriptContext);
        } catch (Exception e) {
            throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.PRINT_TEMPLATE_CONDITION_MUST_BE_BOOLEAN));
        }
        if (evaluation instanceof Boolean) {
            present = (Boolean) evaluation;
        }
    }
    test.setConditionsResult(present);
    printTemplateLineRepo.save(printTemplateLine);
}
Also used : Context(com.axelor.rpc.Context) Locale(java.util.Locale) AxelorException(com.axelor.exception.AxelorException) PrintTemplateLineTest(com.axelor.apps.base.db.PrintTemplateLineTest) AxelorException(com.axelor.exception.AxelorException) AppFilter(com.axelor.app.internal.AppFilter) Language(com.axelor.apps.base.db.Language) TemplateMaker(com.axelor.tool.template.TemplateMaker) Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel) Transactional(com.google.inject.persist.Transactional)

Example 57 with Model

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

the class ConfiguratorServiceImpl method fixRelationalField.

protected void fixRelationalField(Model parentModel, Model value, MetaField metaField) throws AxelorException {
    if (value != null) {
        Mapper mapper = Mapper.of(parentModel.getClass());
        try {
            String className = MetaTool.computeFullClassName(metaField);
            Model manyToOneDbValue = JPA.find((Class<Model>) Class.forName(className), value.getId());
            mapper.set(parentModel, metaField.getName(), manyToOneDbValue);
        } catch (Exception e) {
            throw new AxelorException(Configurator.class, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, e.getMessage());
        }
    }
}
Also used : Mapper(com.axelor.db.mapper.Mapper) AxelorException(com.axelor.exception.AxelorException) Configurator(com.axelor.apps.sale.db.Configurator) Model(com.axelor.db.Model) AxelorException(com.axelor.exception.AxelorException) MissingPropertyException(groovy.lang.MissingPropertyException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 58 with Model

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

the class ConfiguratorCreatorImportServiceImpl method importConfiguratorCreators.

@Transactional(rollbackOn = { Exception.class })
@Override
public String importConfiguratorCreators(InputStream xmlInputStream, String configFilePath) throws IOException {
    InputStream inputStream = this.getClass().getResourceAsStream(configFilePath);
    File configFile = File.createTempFile("config", ".xml");
    FileOutputStream fout = new FileOutputStream(configFile);
    IOUtil.copyCompletely(inputStream, fout);
    File tempDir = Files.createTempDir();
    File importFile = new File(tempDir, "configurator-creator.xml");
    FileUtils.copyInputStreamToFile(xmlInputStream, importFile);
    XMLImporter importer = new XMLImporter(configFile.getAbsolutePath(), tempDir.getAbsolutePath());
    final StringBuilder importLog = new StringBuilder();
    Listener listener = new Listener() {

        @Override
        public void imported(Integer imported, Integer total) {
            importLog.append("Total records: " + total + ", Total imported: " + total);
        }

        @Override
        public void imported(Model arg0) {
            try {
                completeAfterImport(arg0);
            } catch (AxelorException e) {
                importLog.append("Error in import: " + Arrays.toString(e.getStackTrace()));
            }
        }

        @Override
        public void handle(Model arg0, Exception err) {
            importLog.append("Error in import: " + Arrays.toString(err.getStackTrace()));
        }
    };
    importer.addListener(listener);
    importer.run();
    FileUtils.forceDelete(configFile);
    FileUtils.forceDelete(tempDir);
    return importLog.toString();
}
Also used : AxelorException(com.axelor.exception.AxelorException) Listener(com.axelor.data.Listener) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) Model(com.axelor.db.Model) XMLImporter(com.axelor.data.xml.XMLImporter) File(java.io.File) AxelorException(com.axelor.exception.AxelorException) IOException(java.io.IOException) Transactional(com.google.inject.persist.Transactional)

Example 59 with Model

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

the class BamlModelController method execute.

public void execute(ActionRequest request, ActionResponse response) {
    Context context = request.getContext();
    String model = (String) context.get("modelName");
    Model entity = null;
    if (context.get("recordId") != null && model != null) {
        Long recordId = Long.parseLong(context.get("recordId").toString());
        entity = WkfContextHelper.getRepository(model).find(recordId);
    }
    Map<String, Object> bamlModelMap = (Map<String, Object>) context.get("bamlModel");
    BamlModel bamlModel = Beans.get(BamlModelRepository.class).find(Long.parseLong(bamlModelMap.get("id").toString()));
    Model object = Beans.get(BamlService.class).execute(bamlModel, entity);
    String modelName = object.getClass().getSimpleName();
    String dasherizeModel = Inflector.getInstance().dasherize(modelName);
    String title = object.getClass().getSimpleName();
    String formView = dasherizeModel + "-form";
    String gridView = dasherizeModel + "-grid";
    String jsonModel = null;
    if (object instanceof MetaJsonRecord) {
        jsonModel = ((MetaJsonRecord) object).getJsonModel();
        title = Beans.get(MetaJsonModelRepository.class).findByName(jsonModel).getTitle();
        if (Strings.isNullOrEmpty(title)) {
            title = jsonModel;
        }
        formView = "custom-model-" + jsonModel + "-form";
        gridView = "custom-model-" + jsonModel + "-grid";
    }
    response.setCanClose(true);
    ActionViewBuilder builder = ActionView.define(I18n.get(title)).model(object.getClass().getName()).add("form", formView).add("grid", gridView).context("_showRecord", object.getId());
    if (jsonModel != null) {
        builder.context("jsonModel", jsonModel);
        builder.domain("self.jsonModel = :jsonModel");
    }
    response.setView(builder.map());
}
Also used : Context(com.axelor.rpc.Context) BamlModelRepository(com.axelor.apps.bpm.db.repo.BamlModelRepository) MetaJsonModelRepository(com.axelor.meta.db.repo.MetaJsonModelRepository) ActionViewBuilder(com.axelor.meta.schema.actions.ActionView.ActionViewBuilder) BamlModel(com.axelor.apps.bpm.db.BamlModel) Model(com.axelor.db.Model) BamlModel(com.axelor.apps.bpm.db.BamlModel) Map(java.util.Map) BamlService(com.axelor.apps.baml.service.BamlService) MetaJsonRecord(com.axelor.meta.db.MetaJsonRecord)

Example 60 with Model

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

the class WkfDmnModelController method executeDmn.

public void executeDmn(ActionRequest request, ActionResponse response) throws AxelorException {
    Context context = request.getContext();
    String decisionId = (String) ((Map<String, Object>) context.get("dmnTable")).get("decisionId");
    String ctxModel = (String) context.get("ctxModel");
    Long ctxRecordId = Long.parseLong(context.get("ctxRecordId").toString());
    if (ctxRecordId == null || ctxModel == null) {
        return;
    }
    Model model = WkfContextHelper.getRepository(ctxModel).find(ctxRecordId);
    Beans.get(DmnService.class).executeDmn(decisionId, model);
    response.setCanClose(true);
}
Also used : Context(com.axelor.rpc.Context) DmnService(com.axelor.apps.dmn.service.DmnService) Model(com.axelor.db.Model) WkfDmnModel(com.axelor.apps.bpm.db.WkfDmnModel)

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