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