Search in sources :

Example 1 with MetaJsonField

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

the class ValidatorService method createCustomObjectSet.

@Transactional(rollbackOn = { Exception.class })
public void createCustomObjectSet(String modelName, String targetModelName, int sequence) {
    String simpleModelName = StringUtils.substringAfterLast(targetModelName, ".");
    String fieldName = Inflector.getInstance().camelize(simpleModelName, true) + "Set";
    String viewName = Inflector.getInstance().dasherize(simpleModelName);
    if (metaJsonFieldRepo.all().filter("self.type = ?1 AND self.model = ?2 AND self.targetModel = ?3", "many-to-many", modelName, targetModelName).count() > 0) {
        return;
    }
    MetaJsonField jsonField = new MetaJsonField();
    jsonField.setName(fieldName);
    jsonField.setType("many-to-many");
    jsonField.setTitle(Inflector.getInstance().titleize(simpleModelName));
    jsonField.setSequence(sequence);
    jsonField.setModel(modelName);
    jsonField.setModelField("attrs");
    jsonField.setTargetModel(targetModelName);
    jsonField.setGridView(viewName + "-grid");
    jsonField.setFormView(viewName + "-form");
    jsonField.setWidgetAttrs("{\"colSpan\": \"12\"}");
    jsonField.setShowIf(fieldName + " != null && $record.advancedImport.statusSelect > 0");
    metaJsonFieldRepo.save(jsonField);
}
Also used : MetaJsonField(com.axelor.meta.db.MetaJsonField) Transactional(com.google.inject.persist.Transactional)

Example 2 with MetaJsonField

use of com.axelor.meta.db.MetaJsonField 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)

Example 3 with MetaJsonField

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

the class FileTabController method showRecord.

public void showRecord(ActionRequest request, ActionResponse response) {
    try {
        FileTab fileTab = request.getContext().asType(FileTab.class);
        fileTab = Beans.get(FileTabRepository.class).find(fileTab.getId());
        String btnName = request.getContext().get("_signal").toString();
        String fieldName = StringUtils.substringBetween(btnName, "show", "Btn");
        MetaJsonField jsonField = Beans.get(MetaJsonFieldRepository.class).all().filter("self.name = ?1 AND self.type = 'many-to-many' AND self.model = ?2 AND self.modelField = 'attrs'", fieldName, fileTab.getClass().getName()).fetchOne();
        if (jsonField == null) {
            return;
        }
        String ids = Beans.get(FileTabService.class).getShowRecordIds(fileTab, jsonField.getName());
        response.setView(ActionView.define(I18n.get(jsonField.getTitle())).model(jsonField.getTargetModel()).add("grid", jsonField.getGridView()).add("form", jsonField.getFormView()).domain("self.id IN (" + ids + ")").map());
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : MetaJsonFieldRepository(com.axelor.meta.db.repo.MetaJsonFieldRepository) FileTabService(com.axelor.apps.base.service.advanced.imports.FileTabService) MetaJsonField(com.axelor.meta.db.MetaJsonField) FileTab(com.axelor.apps.base.db.FileTab)

Example 4 with MetaJsonField

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

the class ConfiguratorCreatorImportServiceImpl method updateOtherFieldsInAttribute.

/**
 * Update the configurator id in other fields of the attribute.
 *
 * @param creator
 * @param attribute attribute to update
 */
protected void updateOtherFieldsInAttribute(ConfiguratorCreator creator, MetaJsonField attribute) {
    try {
        List<Field> fieldsToUpdate = Arrays.stream(attribute.getClass().getDeclaredFields()).filter(field -> field.getType().equals(String.class)).collect(Collectors.toList());
        for (Field field : fieldsToUpdate) {
            Mapper mapper = Mapper.of(attribute.getClass());
            Method getter = mapper.getGetter(field.getName());
            String fieldString = (String) getter.invoke(attribute);
            if (fieldString != null && fieldString.contains("_")) {
                Method setter = mapper.getSetter(field.getName());
                String updatedFieldString = fieldString.substring(0, fieldString.lastIndexOf('_')) + '_' + creator.getId();
                setter.invoke(attribute, updatedFieldString);
            }
        }
    } catch (Exception e) {
        TraceBackService.trace(e);
    }
}
Also used : Arrays(java.util.Arrays) Inject(com.google.inject.Inject) Mapper(com.axelor.db.mapper.Mapper) ConfiguratorCreator(com.axelor.apps.sale.db.ConfiguratorCreator) Transactional(com.google.inject.persist.Transactional) AxelorException(com.axelor.exception.AxelorException) Files(com.google.common.io.Files) MetaJsonField(com.axelor.meta.db.MetaJsonField) Method(java.lang.reflect.Method) Path(java.nio.file.Path) XMLImporter(com.axelor.data.xml.XMLImporter) MetaFiles(com.axelor.meta.MetaFiles) IOUtil(org.apache.xmlbeans.impl.common.IOUtil) ConfiguratorFormula(com.axelor.apps.sale.db.ConfiguratorFormula) Model(com.axelor.db.Model) TraceBackService(com.axelor.exception.service.TraceBackService) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) Field(java.lang.reflect.Field) Listener(com.axelor.data.Listener) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) InputStream(java.io.InputStream) MetaJsonField(com.axelor.meta.db.MetaJsonField) Field(java.lang.reflect.Field) Mapper(com.axelor.db.mapper.Mapper) Method(java.lang.reflect.Method) AxelorException(com.axelor.exception.AxelorException) IOException(java.io.IOException)

Example 5 with MetaJsonField

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

the class ConfiguratorCreatorServiceImpl method updateIndicators.

@Transactional
public void updateIndicators(ConfiguratorCreator creator) throws AxelorException {
    List<MetaJsonField> indicators = Optional.ofNullable(creator.getIndicators()).orElse(Collections.emptyList());
    // add missing formulas
    List<? extends ConfiguratorFormula> formulas;
    if (creator.getGenerateProduct()) {
        formulas = creator.getConfiguratorProductFormulaList();
    } else {
        formulas = creator.getConfiguratorSOLineFormulaList();
    }
    for (ConfiguratorFormula formula : formulas) {
        addIfMissing(formula, creator);
    }
    // remove formulas
    List<MetaJsonField> fieldsToRemove = new ArrayList<>();
    for (MetaJsonField indicator : indicators) {
        if (isNotInFormulas(indicator, creator, formulas)) {
            fieldsToRemove.add(indicator);
        }
    }
    for (MetaJsonField indicatorToRemove : fieldsToRemove) {
        creator.removeIndicator(indicatorToRemove);
    }
    updateIndicatorsAttrs(creator, formulas);
    configuratorCreatorRepo.save(creator);
}
Also used : ConfiguratorFormula(com.axelor.apps.sale.db.ConfiguratorFormula) ArrayList(java.util.ArrayList) MetaJsonField(com.axelor.meta.db.MetaJsonField) Transactional(com.google.inject.persist.Transactional)

Aggregations

MetaJsonField (com.axelor.meta.db.MetaJsonField)30 MetaField (com.axelor.meta.db.MetaField)14 AxelorException (com.axelor.exception.AxelorException)7 HashMap (java.util.HashMap)5 MetaJsonFieldRepository (com.axelor.meta.db.repo.MetaJsonFieldRepository)4 Transactional (com.google.inject.persist.Transactional)4 Map (java.util.Map)4 Mapper (com.axelor.db.mapper.Mapper)3 WkfCommonService (com.axelor.apps.bpm.service.WkfCommonService)2 ConfiguratorFormula (com.axelor.apps.sale.db.ConfiguratorFormula)2 Property (com.axelor.db.mapper.Property)2 MetaModel (com.axelor.meta.db.MetaModel)2 Filter (com.axelor.studio.db.Filter)2 File (java.io.File)2 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 FileTab (com.axelor.apps.base.db.FileTab)1 FileTabService (com.axelor.apps.base.service.advanced.imports.FileTabService)1 Configurator (com.axelor.apps.sale.db.Configurator)1