use of com.axelor.meta.db.MetaField in project axelor-open-suite by axelor.
the class ObjectDataAnonymizeServiceImpl method getDefaultValues.
private Map<String, Object> getDefaultValues(Mapper mapper, Set<MetaField> fields) {
Map<String, Object> defaultValues = new HashMap<>();
LocalDate defaultDate = LocalDate.of(1900, 01, 01);
for (MetaField field : fields) {
String name = field.getName();
Property property = mapper.getProperty(name);
if (!property.isRequired()) {
defaultValues.put(name, null);
continue;
}
if (property.getTarget() != null) {
continue;
}
switch(field.getTypeName()) {
case "String":
{
defaultValues.put(name, "anonymous");
break;
}
case "BigDecimal":
{
defaultValues.put(name, BigDecimal.ZERO);
break;
}
case "Integer":
{
defaultValues.put(name, Integer.valueOf(0));
break;
}
case "Boolean":
{
defaultValues.put(name, Boolean.FALSE);
break;
}
case "Long":
{
defaultValues.put(name, Long.valueOf(0));
break;
}
case "LocalTime":
{
defaultValues.put(name, LocalTime.MIDNIGHT);
break;
}
case "LocalDateTime":
{
defaultValues.put(name, defaultDate.atStartOfDay());
break;
}
case "ZonedDateTime":
{
defaultValues.put(name, defaultDate.atStartOfDay(ZoneId.systemDefault()));
break;
}
default:
{
break;
}
}
}
return defaultValues;
}
use of com.axelor.meta.db.MetaField in project axelor-open-suite by axelor.
the class ConfiguratorCreatorServiceImpl method completeSelection.
/**
* Fill {@link MetaJsonField#selection} searching in java class code.
*
* @param metaField a meta field.
* @param newField a meta json field.
*/
protected void completeSelection(MetaField metaField, MetaJsonField newField) {
try {
Field correspondingField = Class.forName(metaField.getMetaModel().getPackageName() + "." + metaField.getMetaModel().getName()).getDeclaredField(metaField.getName());
Widget widget = correspondingField.getAnnotation(Widget.class);
if (widget == null) {
return;
}
String selection = widget.selection();
if (!Strings.isNullOrEmpty(selection)) {
newField.setSelection(selection);
}
} catch (ClassNotFoundException | NoSuchFieldException e) {
TraceBackService.trace(e);
}
}
use of com.axelor.meta.db.MetaField in project axelor-open-suite by axelor.
the class ConfiguratorCreatorServiceImpl method addIfMissing.
/**
* Add the {@link ConfiguratorFormula} in {@link ConfiguratorCreator#indicators} if the formula is
* not represented by an existing indicator.
*
* @param formula
* @param creator
*/
protected void addIfMissing(ConfiguratorFormula formula, ConfiguratorCreator creator) throws AxelorException {
MetaField formulaMetaField = formula.getMetaField();
List<MetaJsonField> fields = Optional.ofNullable(creator.getIndicators()).orElse(Collections.emptyList());
for (MetaJsonField field : fields) {
if (field.getName().equals(formulaMetaField.getName() + "_" + creator.getId())) {
return;
}
}
String metaModelName = formulaMetaField.getMetaModel().getName();
MetaJsonField newField = new MetaJsonField();
newField.setModel(Configurator.class.getName());
newField.setModelField("indicators");
MetaField metaField = Beans.get(MetaFieldRepository.class).all().filter("self.metaModel.name = :metaModelName AND self.name = :name").bind("metaModelName", metaModelName).bind("name", formulaMetaField.getName()).fetchOne();
String typeName;
if (!Strings.isNullOrEmpty(metaField.getRelationship())) {
typeName = metaField.getRelationship();
completeDefaultGridAndForm(metaField, newField);
} else {
typeName = metaField.getTypeName();
}
completeSelection(metaField, newField);
newField.setType(MetaTool.typeToJsonType(typeName));
newField.setName(formulaMetaField.getName() + "_" + creator.getId());
newField.setTitle(formulaMetaField.getLabel());
creator.addIndicator(newField);
}
use of com.axelor.meta.db.MetaField in project axelor-open-suite by axelor.
the class ConfiguratorServiceImpl method computeIndicatorValue.
/**
* Compute the value of one indicator. Using the corresponding formula and the values in {@link
* Configurator#attributes}
*
* @param configurator
* @param indicatorName
* @param jsonAttributes
* @return
*/
protected Object computeIndicatorValue(Configurator configurator, String indicatorName, JsonContext jsonAttributes) {
ConfiguratorCreator creator = configurator.getConfiguratorCreator();
List<? extends ConfiguratorFormula> formulas;
if (creator.getGenerateProduct()) {
formulas = creator.getConfiguratorProductFormulaList();
} else {
formulas = creator.getConfiguratorSOLineFormulaList();
}
String groovyFormula = null;
for (ConfiguratorFormula formula : formulas) {
String fieldName = indicatorName;
fieldName = fieldName.substring(0, fieldName.indexOf('_'));
MetaField metaField = formula.getMetaField();
if (metaField.getName().equals(fieldName)) {
groovyFormula = formula.getFormula();
break;
}
}
if (groovyFormula == null || jsonAttributes == null) {
return null;
}
return computeFormula(groovyFormula, jsonAttributes);
}
use of com.axelor.meta.db.MetaField in project axelor-open-suite by axelor.
the class ConfiguratorServiceImpl method computeMappedByMethod.
/**
* Find the method used to fill the mapped by many-to-one of a one-to-many relationship. Example:
* for a one-to-many "purchaseProductMultipleQtyList" with a mapped by many-to-one called
* "purchaseProduct", this method will return the method "setPurchaseProduct"
*
* @param oneToManyFormula a ConfiguratorFormula used to fill a one-to-many
* @return the found method
* @throws AxelorException if the mapped by field in meta field is empty.
*/
protected Method computeMappedByMethod(ConfiguratorFormula oneToManyFormula) throws AxelorException, ClassNotFoundException {
MetaField metaField = oneToManyFormula.getMetaField();
String mappedBy = metaField.getMappedBy();
if (mappedBy == null || "".equals(mappedBy)) {
throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.CONFIGURATOR_ONE_TO_MANY_WITHOUT_MAPPED_BY_UNSUPPORTED));
}
return Mapper.of(Class.forName(MetaTool.computeFullClassName(metaField))).getSetter(mappedBy);
}
Aggregations