Search in sources :

Example 76 with Model

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

the class ModelTool method apply.

/**
 * Apply consumer to each record found from collection of IDs.
 *
 * @param ids collection of IDs.
 * @param consumer to apply on each record.
 * @return the number of errors that occurred.
 */
public static <T extends Model> int apply(Class<? extends Model> modelClass, Collection<? extends Number> ids, ThrowConsumer<T> consumer) {
    Preconditions.checkNotNull(ids, I18n.get("The collection of IDs cannot be null."));
    Preconditions.checkNotNull(consumer, I18n.get("The consumer cannot be null."));
    int errorCount = 0;
    for (Number id : ids) {
        try {
            if (id != null) {
                Model model = JPA.find(modelClass, id.longValue());
                if (model != null) {
                    consumer.accept((T) model);
                    continue;
                }
            }
            throw new AxelorException(modelClass, TraceBackRepository.CATEGORY_NO_VALUE, I18n.get("Cannot find record #%s"), String.valueOf(id));
        } catch (Exception e) {
            ++errorCount;
            TraceBackService.trace(e);
        } finally {
            JPA.clear();
        }
    }
    return errorCount;
}
Also used : AxelorException(com.axelor.exception.AxelorException) Model(com.axelor.db.Model) InvocationTargetException(java.lang.reflect.InvocationTargetException) AxelorException(com.axelor.exception.AxelorException)

Example 77 with Model

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

the class ModelTool method checkUniqueFields.

/**
 * Get set of fields affected by unique constraint error.
 *
 * @param model
 * @return
 */
private static Set<Field> checkUniqueFields(Model model) {
    Set<Field> errors = new HashSet<>();
    Class<? extends Model> modelClass = EntityHelper.getEntityClass(model);
    for (Field field : modelClass.getDeclaredFields()) {
        Column column = field.getAnnotation(Column.class);
        if (column == null || !column.unique()) {
            continue;
        }
        String filter = String.format("self.%s = :value", field.getName());
        String getterName = fieldNameToGetter(field.getName());
        try {
            Method getter = modelClass.getMethod(getterName);
            Object value = getter.invoke(model);
            Model existing = JPA.all(modelClass).filter(filter).bind("value", value).fetchOne();
            if (existing != null && !existing.getId().equals(model.getId())) {
                errors.add(field);
            }
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            TraceBackService.trace(e);
        }
    }
    return errors;
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) Column(javax.persistence.Column) Model(com.axelor.db.Model) HashSet(java.util.HashSet)

Aggregations

Model (com.axelor.db.Model)77 MetaModel (com.axelor.meta.db.MetaModel)22 AxelorException (com.axelor.exception.AxelorException)19 ArrayList (java.util.ArrayList)18 HashMap (java.util.HashMap)16 Context (com.axelor.rpc.Context)15 Transactional (com.google.inject.persist.Transactional)15 List (java.util.List)14 Mapper (com.axelor.db.mapper.Mapper)13 IOException (java.io.IOException)13 File (java.io.File)12 Map (java.util.Map)12 Property (com.axelor.db.mapper.Property)11 MetaJsonRecord (com.axelor.meta.db.MetaJsonRecord)9 JsonContext (com.axelor.rpc.JsonContext)8 MetaModelRepository (com.axelor.meta.db.repo.MetaModelRepository)7 Strings (com.google.common.base.Strings)7 HashSet (java.util.HashSet)7 FullContext (com.axelor.apps.tool.context.FullContext)6 Beans (com.axelor.inject.Beans)6