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);
}
}
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);
}
}
}
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;
}
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();
}
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);
}
}
Aggregations