Search in sources :

Example 21 with MetaField

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

the class FilterSqlService method getSqlField.

public String[] getSqlField(Object target, String source, List<String> joins) {
    String field = null;
    String type = null;
    String selection = null;
    if (target instanceof MetaField) {
        MetaField metaField = (MetaField) target;
        field = source + "." + getColumn(metaField);
        type = metaField.getTypeName();
        try {
            selection = Mapper.of(Class.forName(metaField.getMetaModel().getFullName())).getProperty(metaField.getName()).getSelection();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    } else {
        MetaJsonField metaJsonField = (MetaJsonField) target;
        selection = metaJsonField.getSelection();
        String jsonColumn = getColumn(metaJsonField.getModel(), metaJsonField.getModelField());
        field = "cast(" + source + "." + jsonColumn + "->>'" + metaJsonField.getName() + "' as " + getSqlType(metaJsonField.getType()) + ")";
        type = metaJsonField.getType();
    }
    log.debug("Selection for field :{} : {}", field, selection);
    if (joins != null && !Strings.isNullOrEmpty(selection)) {
        int join = joins.size();
        joins.add("left join meta_select obj" + join + " on (obj" + join + ".name = '" + selection + "')");
        join = joins.size();
        joins.add("left join meta_select_item obj" + join + " on (obj" + join + ".select_id = obj" + (join - 1) + ".id and obj" + join + ".value = cast(" + field + " as varchar))");
        join = joins.size();
        joins.add("left join meta_translation obj" + join + " on (obj" + join + ".message_key = obj" + (join - 1) + ".title and obj" + join + ".language = (select language from auth_user where id = :__user__))");
        field = "COALESCE(nullif(obj" + join + ".message_value,''), obj" + (join - 1) + ".title)";
    }
    return new String[] { field, type };
}
Also used : MetaField(com.axelor.meta.db.MetaField) MetaJsonField(com.axelor.meta.db.MetaJsonField)

Example 22 with MetaField

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

Example 23 with MetaField

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

the class FilterController method updateTargetField.

public void updateTargetField(ActionRequest request, ActionResponse response) {
    Filter filter = request.getContext().asType(Filter.class);
    MetaField metaField = filter.getMetaField();
    MetaJsonField metaJson = filter.getMetaJsonField();
    Boolean isJson = filter.getIsJson() != null ? filter.getIsJson() : false;
    if (!isJson && metaField != null) {
        String type = metaField.getRelationship() != null ? metaField.getRelationship() : metaField.getTypeName();
        response.setValue("targetType", type);
        response.setValue("targetField", metaField.getName());
        response.setValue("targetTitle", metaField.getLabel() != null && !metaField.getLabel().isEmpty() ? metaField.getLabel() : metaField.getName());
    } else if ((isJson || !isJson) && metaJson != null) {
        response.setValue("targetType", Inflector.getInstance().camelize(metaJson.getType()));
        response.setValue("targetField", metaJson.getName());
    } else {
        response.setValue("targetField", null);
        response.setValue("targetType", null);
    }
    response.setValue("operator", null);
}
Also used : Filter(com.axelor.studio.db.Filter) MetaField(com.axelor.meta.db.MetaField) MetaJsonField(com.axelor.meta.db.MetaJsonField)

Example 24 with MetaField

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

the class FilterController method updateTargetMetaField.

public void updateTargetMetaField(ActionRequest request, ActionResponse response) {
    Filter filter = request.getContext().asType(Filter.class);
    if (request.getContext().get("targetMetaField") != null) {
        Integer id = (Integer) ((Map) request.getContext().get("targetMetaField")).get("id");
        MetaField targetMetaField = Beans.get(MetaFieldRepository.class).find(Long.valueOf(id));
        String targetTitle = targetMetaField.getLabel() != null && !targetMetaField.getLabel().isEmpty() ? targetMetaField.getLabel() : targetMetaField.getName();
        response.setValue("targetField", filter.getTargetField() + "." + targetMetaField.getName());
        response.setValue("targetTitle", filter.getTargetTitle() + "." + targetTitle);
        response.setValue("targetType", targetMetaField.getRelationship() != null ? targetMetaField.getRelationship() : targetMetaField.getTypeName());
        if (targetMetaField.getRelationship() != null) {
            response.setValue("metaTargetFieldDomain", targetMetaField.getTypeName());
            response.setValue("targetMetaField", null);
        }
    }
    response.setValue("operator", null);
}
Also used : Filter(com.axelor.studio.db.Filter) MetaField(com.axelor.meta.db.MetaField) MetaFieldRepository(com.axelor.meta.db.repo.MetaFieldRepository)

Example 25 with MetaField

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

the class ObjectDataExportServiceImpl method createFieldsData.

private String[][] createFieldsData(Set<MetaField> metaFields, ResourceBundle bundle) throws ClassNotFoundException {
    List<String> names = new ArrayList<>();
    List<String> labels = new ArrayList<>();
    for (MetaField field : metaFields) {
        String label = field.getLabel();
        String name = field.getName();
        if (Strings.isNullOrEmpty(label)) {
            label = inflector.humanize(name);
        }
        if (field.getRelationship() != null) {
            name = name + "." + ObjectDataCommonService.getNameColumn(field);
        }
        names.add(name);
        String translated = bundle.getString(label);
        if (!Strings.isNullOrEmpty(translated)) {
            labels.add(translated);
        } else {
            labels.add(label);
        }
    }
    logger.debug("Fields: {}", names);
    logger.debug("Labels: {}", labels);
    return new String[][] { names.toArray(new String[names.size()]), labels.toArray(new String[labels.size()]) };
}
Also used : MetaField(com.axelor.meta.db.MetaField) ArrayList(java.util.ArrayList)

Aggregations

MetaField (com.axelor.meta.db.MetaField)45 MetaJsonField (com.axelor.meta.db.MetaJsonField)15 MetaModel (com.axelor.meta.db.MetaModel)10 Mapper (com.axelor.db.mapper.Mapper)9 AxelorException (com.axelor.exception.AxelorException)8 ArrayList (java.util.ArrayList)6 Property (com.axelor.db.mapper.Property)5 Filter (com.axelor.studio.db.Filter)4 Model (com.axelor.db.Model)3 MetaFieldRepository (com.axelor.meta.db.repo.MetaFieldRepository)3 MetaModelRepository (com.axelor.meta.db.repo.MetaModelRepository)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Company (com.axelor.apps.base.db.Company)2 FileField (com.axelor.apps.base.db.FileField)2 ProductCompany (com.axelor.apps.base.db.ProductCompany)2 Context (com.axelor.rpc.Context)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Transactional (com.google.inject.persist.Transactional)2 BigInteger (java.math.BigInteger)2