Search in sources :

Example 21 with MetaJsonField

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

the class AppLoaderExportServiceImpl method addJsonModelData.

protected void addJsonModelData(AppDataLoader dataLoader, File parentDir) {
    try {
        if (!allowRead(MetaJsonRecord.class)) {
            return;
        }
        String modelName = dataLoader.getJsonModel().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());
        Map<String, Object> jsonFieldMap = MetaStore.findJsonFields(modelName);
        fixTargetName(jsonFieldMap);
        for (FullContext record : records) {
            if (!allowRead(MetaJsonRecord.class, (Long) record.get("id"))) {
                continue;
            }
            fileWriter.write("<" + dasherizeModel + ">\n");
            for (MetaJsonField jsonField : dataLoader.getJsonFieldSet()) {
                String field = jsonField.getName();
                Map<String, Object> fieldAttrs = (Map<String, Object>) jsonFieldMap.get(field);
                fileWriter.write("\t<" + field + ">" + extractJsonFieldValue(record, fieldAttrs) + "</" + field + ">\n");
            }
            fileWriter.write("</" + dasherizeModel + ">\n\n");
        }
        fileWriter.write("</" + dasherizeModel + "s>\n");
        fileWriter.close();
    } catch (IOException e) {
        TraceBackService.trace(e);
    }
}
Also used : FileWriter(java.io.FileWriter) FullContext(com.axelor.apps.tool.context.FullContext) MetaJsonField(com.axelor.meta.db.MetaJsonField) IOException(java.io.IOException) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) MetaJsonRecord(com.axelor.meta.db.MetaJsonRecord)

Example 22 with MetaJsonField

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

the class AppLoaderExportServiceImpl method fixTargetName.

private void fixTargetName(Map<String, Object> jsonFieldMap) {
    // NOTE: Issue in AOP, it always return name as targetname for custom model.
    for (String field : jsonFieldMap.keySet()) {
        Map<String, Object> fieldAttrs = (Map<String, Object>) jsonFieldMap.get(field);
        String targetModel = (String) fieldAttrs.get("jsonTarget");
        if (targetModel != null) {
            MetaJsonField nameField = metaJsonModelRepository.findNameField(metaJsonModelRepository.findByName(targetModel));
            String targetName = "id";
            if (nameField != null) {
                targetName = nameField.getName();
            }
            ((Map<String, Object>) jsonFieldMap.get(field)).put("targetName", targetName);
        }
    }
}
Also used : MetaJsonField(com.axelor.meta.db.MetaJsonField) Map(java.util.Map) HashMap(java.util.HashMap)

Example 23 with MetaJsonField

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

the class AppLoaderExportServiceImpl method geJsonFieldBinding.

protected List<XMLBind> geJsonFieldBinding(Map<String, Object> jsonFieldMap, AppDataLoader dataLoader, boolean relationalInput) {
    List<XMLBind> fieldBindings = new ArrayList<>();
    for (MetaJsonField jsonField : dataLoader.getJsonFieldSet()) {
        Map<String, Object> fieldAttrs = (Map<String, Object>) jsonFieldMap.get(jsonField.getName());
        log.debug("Json Field name: {}, Field attrs: {}", jsonField, fieldAttrs);
        String fieldType = jsonField.getType();
        fieldBindings.add(createDummyFieldBinding(jsonField.getName()));
        if (relationalInput && jsonField.getTargetJsonModel() == null && jsonField.getTargetModel() == null) {
            continue;
        }
        XMLBind xmlBind = new XMLBind();
        xmlBind.setNode(jsonField.getName());
        xmlBind.setField("$attrs." + jsonField.getName());
        if (jsonField.getTargetJsonModel() != null || jsonField.getTargetModel() != null) {
            addRelationaJsonFieldBind(jsonField, fieldAttrs, xmlBind);
        } else if (fieldType.equals("boolean")) {
            xmlBind.setAdapter("Boolean");
        }
        fieldBindings.add(xmlBind);
    }
    return fieldBindings;
}
Also used : ArrayList(java.util.ArrayList) MetaJsonField(com.axelor.meta.db.MetaJsonField) XMLBind(com.axelor.data.xml.XMLBind) Map(java.util.Map) HashMap(java.util.HashMap)

Example 24 with MetaJsonField

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

the class AppLoaderExportServiceImpl method getJsonSearchFields.

protected String getJsonSearchFields(AppDataLoader appDataLoader, Map<String, Object> jsonFieldMap) {
    StringBuilder fields = new StringBuilder();
    for (MetaJsonField field : appDataLoader.getSearchJsonFieldSet()) {
        if (fields.length() != 0) {
            fields.append(" AND ");
        }
        Map<String, Object> fieldAttrs = (Map<String, Object>) jsonFieldMap.get(field.getName());
        log.debug("Json search Field name: {}, Field attrs: {}", field, fieldAttrs);
        String jsonFunction = "json_extract_text";
        if (field.getTargetJsonModel() != null || field.getTargetModel() != null) {
            String targetName = getTargetName(fieldAttrs);
            if (targetName.equals("id")) {
                jsonFunction = "json_extract_integer";
            }
            fields.append(jsonFunction + "(self.attrs," + "'" + targetName + "') = :_" + field.getName());
        } else {
            if (JSON_EXTRACT_TYPES.contains(field.getType())) {
                jsonFunction = "json_extract_" + field.getType();
            }
            fields.append(jsonFunction + "(self.attrs,'" + field.getName() + "') = :_" + field.getName());
        }
    }
    return fields.toString();
}
Also used : MetaJsonField(com.axelor.meta.db.MetaJsonField) Map(java.util.Map) HashMap(java.util.HashMap)

Example 25 with MetaJsonField

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

the class FilterGroovyService method getJsonFieldType.

private String getJsonFieldType(MetaJsonField jsonField, String targetField) throws AxelorException {
    if (targetField == null || !targetField.contains(".")) {
        return jsonField.getType();
    }
    targetField = targetField.substring(targetField.indexOf(".") + 1);
    String targetName = targetField.contains(".") ? targetField.substring(0, targetField.indexOf(".")) : targetField;
    if (jsonField.getTargetJsonModel() != null) {
        MetaJsonField subJson = metaJsonFieldRepo.all().filter("self.name = ?1 and self.jsonModel = ?2", targetName, jsonField.getTargetJsonModel()).fetchOne();
        if (subJson == null) {
            throw new AxelorException(TraceBackRepository.CATEGORY_MISSING_FIELD, "No sub field found model: %s field %s ", jsonField.getTargetJsonModel().getName(), targetName);
        }
        return getJsonFieldType(subJson, targetField);
    } else {
        MetaField subMeta = filterSqlService.findMetaField(targetName, jsonField.getTargetModel());
        if (subMeta == null) {
            throw new AxelorException(TraceBackRepository.CATEGORY_MISSING_FIELD, "No sub field found model: %s field %s ", jsonField.getTargetModel(), targetName);
        }
        return getMetaFieldType(subMeta, targetField, true);
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) MetaField(com.axelor.meta.db.MetaField) MetaJsonField(com.axelor.meta.db.MetaJsonField)

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