Search in sources :

Example 26 with Property

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

the class DmnServiceImpl method mapToMetaModelFields.

private String mapToMetaModelFields(List<String> fields, String modelName, String searchOperator, boolean multiple, String resultVar) throws ClassNotFoundException {
    StringBuilder scriptBuilder = new StringBuilder();
    Mapper mapper = Mapper.of(EntityHelper.getEntityClass(Class.forName(modelName)));
    String varName = Beans.get(WkfCommonService.class).getVarName(modelName);
    scriptBuilder.append("def _query = null\n");
    for (String field : fields) {
        String resultField = resultVar + "?." + field;
        String targetField = varName + "." + field;
        Property property = mapper.getProperty(field);
        if (property == null) {
            MetaJsonField jsonField = Beans.get(MetaJsonFieldRepository.class).all().filter("self.model = ?1 and self.name = ?2", modelName, field).fetchOne();
            if (jsonField != null) {
                addJsonField(searchOperator, multiple, resultVar, scriptBuilder, varName, jsonField);
            }
            continue;
        }
        if (property.getTarget() != null) {
            addRelationalField(searchOperator, multiple, scriptBuilder, targetField, resultField, property.getTarget().getSimpleName(), property.getTargetName());
        } else {
            scriptBuilder.append(targetField + " = " + resultField);
        }
        scriptBuilder.append("\n");
    }
    return scriptBuilder.toString();
}
Also used : Mapper(com.axelor.db.mapper.Mapper) MetaJsonFieldRepository(com.axelor.meta.db.repo.MetaJsonFieldRepository) MetaJsonField(com.axelor.meta.db.MetaJsonField) Property(com.axelor.db.mapper.Property) WkfCommonService(com.axelor.apps.bpm.service.WkfCommonService)

Example 27 with Property

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

the class AppLoaderExportServiceImpl method addRelationalMetaFieldBind.

private void addRelationalMetaFieldBind(Mapper modelMapper, MetaField field, XMLBind fieldBind) {
    Property property = modelMapper.getProperty(field.getName());
    if (property.isCollection()) {
        fieldBind.setSearch("self." + property.getTargetName() + " in :_" + field.getName());
        fieldBind.setExpression(field.getName() + "?.split(';') as List");
    } else {
        fieldBind.setSearch("self." + property.getTargetName() + " = :_" + field.getName());
    }
    fieldBind.setUpdate(true);
    fieldBind.setCreate(false);
}
Also used : Property(com.axelor.db.mapper.Property)

Example 28 with Property

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

the class AppLoaderExportServiceImpl method getMetaSearchFields.

protected String getMetaSearchFields(Mapper modelMapper, AppDataLoader appDataLoader) throws ClassNotFoundException {
    StringBuilder fields = new StringBuilder();
    for (MetaField field : appDataLoader.getSearchMetaFieldSet()) {
        if (fields.length() != 0) {
            fields.append(" AND ");
        }
        if (field.getRelationship() != null) {
            Property property = modelMapper.getProperty(field.getName());
            fields.append("self." + field.getName() + "." + property.getTargetName() + " =  :_" + field.getName());
        } else {
            fields.append("self." + field.getName() + " =  :_" + field.getName());
        }
    }
    return fields.toString();
}
Also used : MetaField(com.axelor.meta.db.MetaField) Property(com.axelor.db.mapper.Property)

Example 29 with Property

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

the class ReportBuilderService method getNameColumn.

/**
 * Get nameColumn for given relational metaField.
 *
 * @param name Field name
 * @param metaField MetaField to check.
 * @return Name column of field.
 */
private String getNameColumn(String name, MetaField metaField) {
    String refModel = getRefModel(metaField.getTypeName());
    try {
        Mapper mapper = Mapper.of(Class.forName(refModel));
        boolean nameField = false;
        boolean codeField = false;
        for (Property property : Arrays.asList(mapper.getProperties())) {
            if (property.isNameColumn()) {
                return name + "." + property.getName();
            } else if (property.getName().equals("name")) {
                nameField = true;
            } else if (property.getName().equals("code")) {
                codeField = true;
            }
        }
        if (nameField) {
            return name + ".name";
        }
        if (codeField) {
            return name + ".code";
        }
        return name + "." + "id";
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return name;
}
Also used : Mapper(com.axelor.db.mapper.Mapper) Property(com.axelor.db.mapper.Property)

Example 30 with Property

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

the class AdvancedImportServiceImpl method removeSubRecords.

@SuppressWarnings("unchecked")
@Transactional
public void removeSubRecords(Class<? extends Model> klass, JsonContext jsonContext) throws ClassNotFoundException {
    for (Property prop : Mapper.of(klass).getProperties()) {
        if (prop.getTarget() == null || prop.isCollection()) {
            continue;
        }
        String simpleModelName = StringUtils.substringAfterLast(prop.getTarget().getName(), ".");
        String field = inflector.camelize(simpleModelName, true) + "Set";
        if (!jsonContext.containsKey(field)) {
            continue;
        }
        List<Object> recList = (List<Object>) jsonContext.get(field);
        String ids = recList.stream().map(obj -> {
            Map<String, Object> recordMap = Mapper.toMap(EntityHelper.getEntity(obj));
            return recordMap.get("id").toString();
        }).collect(Collectors.joining(","));
        JpaRepository<? extends Model> modelRepo = JpaRepository.of((Class<? extends Model>) Class.forName(prop.getTarget().getName()));
        modelRepo.all().filter("self.id IN (" + ids + ")").delete();
    }
}
Also used : MetaModelRepository(com.axelor.meta.db.repo.MetaModelRepository) Arrays(java.util.Arrays) EntityHelper(com.axelor.db.EntityHelper) JpaRepository(com.axelor.db.JpaRepository) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) Property(com.axelor.db.mapper.Property) HashMap(java.util.HashMap) Mapper(com.axelor.db.mapper.Mapper) StringUtils(org.apache.commons.lang3.StringUtils) JsonContext(com.axelor.rpc.JsonContext) Transactional(com.google.inject.persist.Transactional) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) FileField(com.axelor.apps.base.db.FileField) Strings(com.google.common.base.Strings) AxelorException(com.axelor.exception.AxelorException) AdvancedImportRepository(com.axelor.apps.base.db.repo.AdvancedImportRepository) Files(com.google.common.io.Files) CollectionUtils(org.apache.commons.collections.CollectionUtils) Map(java.util.Map) I18n(com.axelor.i18n.I18n) Inflector(com.axelor.common.Inflector) DataReaderFactory(com.axelor.apps.tool.reader.DataReaderFactory) Logger(org.slf4j.Logger) Model(com.axelor.db.Model) TraceBackRepository(com.axelor.exception.db.repo.TraceBackRepository) MetaField(com.axelor.meta.db.MetaField) MethodHandles(java.lang.invoke.MethodHandles) Set(java.util.Set) Collectors(java.util.stream.Collectors) DataReaderService(com.axelor.apps.tool.reader.DataReaderService) MetaModel(com.axelor.meta.db.MetaModel) List(java.util.List) Beans(com.axelor.inject.Beans) FileFieldRepository(com.axelor.apps.base.db.repo.FileFieldRepository) MetaFieldRepository(com.axelor.meta.db.repo.MetaFieldRepository) IExceptionMessage(com.axelor.apps.base.exceptions.IExceptionMessage) FileTab(com.axelor.apps.base.db.FileTab) AdvancedImport(com.axelor.apps.base.db.AdvancedImport) ArrayList(java.util.ArrayList) List(java.util.List) Property(com.axelor.db.mapper.Property) HashMap(java.util.HashMap) Map(java.util.Map) Transactional(com.google.inject.persist.Transactional)

Aggregations

Property (com.axelor.db.mapper.Property)40 Mapper (com.axelor.db.mapper.Mapper)21 Model (com.axelor.db.Model)12 ArrayList (java.util.ArrayList)11 AxelorException (com.axelor.exception.AxelorException)10 MetaModel (com.axelor.meta.db.MetaModel)8 List (java.util.List)8 Transactional (com.google.inject.persist.Transactional)7 MetaField (com.axelor.meta.db.MetaField)6 FileField (com.axelor.apps.base.db.FileField)5 FileTab (com.axelor.apps.base.db.FileTab)4 Inflector (com.axelor.common.Inflector)4 TraceBackRepository (com.axelor.exception.db.repo.TraceBackRepository)4 I18n (com.axelor.i18n.I18n)4 Inject (com.google.inject.Inject)4 Arrays (java.util.Arrays)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 AdvancedImport (com.axelor.apps.base.db.AdvancedImport)3 FileFieldRepository (com.axelor.apps.base.db.repo.FileFieldRepository)3