Search in sources :

Example 1 with MetaModelRepository

use of com.axelor.meta.db.repo.MetaModelRepository in project axelor-open-suite by axelor.

the class AdvancedExportController method getModelAllFields.

public void getModelAllFields(ActionRequest request, ActionResponse response) throws ClassNotFoundException {
    AdvancedExport advancedExport = request.getContext().asType(AdvancedExport.class);
    inflector = Inflector.getInstance();
    if (advancedExport.getMetaModel() != null) {
        List<Map<String, Object>> allFieldList = new ArrayList<>();
        MetaModelRepository metaModelRepository = Beans.get(MetaModelRepository.class);
        MetaFieldRepository metaFieldRepository = Beans.get(MetaFieldRepository.class);
        for (MetaField field : advancedExport.getMetaModel().getMetaFields()) {
            Map<String, Object> allFieldMap = new HashMap<>();
            allFieldMap.put("currentDomain", advancedExport.getMetaModel().getName());
            Class<?> modelClass = Class.forName(advancedExport.getMetaModel().getFullName());
            Mapper modelMapper = Mapper.of(modelClass);
            if (modelMapper.getProperty(field.getName()) == null || modelMapper.getProperty(field.getName()).isTransient()) {
                continue;
            }
            if (!Strings.isNullOrEmpty(field.getRelationship())) {
                MetaModel metaModel = metaModelRepository.all().filter("self.name = ?", field.getTypeName()).fetchOne();
                Class<?> klass = Class.forName(metaModel.getFullName());
                Mapper mapper = Mapper.of(klass);
                String fieldName = mapper.getNameField() == null ? "id" : mapper.getNameField().getName();
                MetaField metaField = metaFieldRepository.all().filter("self.name = ?1 AND self.metaModel = ?2", fieldName, metaModel).fetchOne();
                allFieldMap.put("metaField", metaField);
                allFieldMap.put("targetField", field.getName() + "." + metaField.getName());
            } else {
                allFieldMap.put("metaField", field);
                allFieldMap.put("targetField", field.getName());
            }
            if (Strings.isNullOrEmpty(field.getLabel())) {
                allFieldMap.put("title", this.getFieldTitle(inflector, field.getName()));
            } else {
                allFieldMap.put("title", field.getLabel());
            }
            allFieldList.add(allFieldMap);
        }
        response.setAttr("advancedExportLineList", "value", allFieldList);
    }
}
Also used : MetaModelRepository(com.axelor.meta.db.repo.MetaModelRepository) MetaField(com.axelor.meta.db.MetaField) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MetaFieldRepository(com.axelor.meta.db.repo.MetaFieldRepository) Mapper(com.axelor.db.mapper.Mapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MetaModel(com.axelor.meta.db.MetaModel) AdvancedExport(com.axelor.apps.base.db.AdvancedExport) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with MetaModelRepository

use of com.axelor.meta.db.repo.MetaModelRepository in project axelor-open-suite by axelor.

the class GlobalAuditTracker method onComplete.

/**
 * This method should be called from {@link
 * AuditInterceptor#beforeTransactionCompletion(Transaction)} method to finish change recording.
 *
 * @param tx the transaction in which the change tracking is being done
 * @param user the session user
 */
public void onComplete(Transaction tx, User user) {
    List<GlobalTrackingLog> logList = LOGS.get();
    LOGS.remove();
    if (CollectionUtils.isEmpty(logList)) {
        return;
    }
    MetaModelRepository modelRepo = Beans.get(MetaModelRepository.class);
    MetaFieldRepository fieldRepo = Beans.get(MetaFieldRepository.class);
    GlobalTrackingLogRepository logRepo = Beans.get(GlobalTrackingLogRepository.class);
    GlobalTrackingConfigurationLineRepository configLineRepo = Beans.get(GlobalTrackingConfigurationLineRepository.class);
    GlobalTrackingConfigurationLine configLine;
    List<GlobalTrackingConfigurationLine> configLineList;
    ScriptBindings bindings;
    for (GlobalTrackingLog log : logList) {
        configLineList = configLineRepo.all().filter("self.metaModel.name = ?", log.getMetaModelName()).fetch();
        if (configLineList.isEmpty()) {
            continue;
        }
        log.setMetaModel(modelRepo.findByName(log.getMetaModelName()));
        List<GlobalTrackingLogLine> logLinesToSave = new ArrayList<>();
        if ((CollectionUtils.isNotEmpty(log.getGlobalTrackingLogLineList()))) {
            try {
                bindings = new ScriptBindings(this.getContext(JPA.find((Class<Model>) Class.forName(log.getMetaModel().getFullName()), log.getRelatedId())));
            } catch (Exception e) {
                continue;
            }
            for (GlobalTrackingLogLine line : log.getGlobalTrackingLogLineList()) {
                configLine = configLineList.stream().filter(l -> l.getMetaField().getName().equals(line.getMetaFieldName())).findFirst().orElse(null);
                if (configLine == null || !this.canTrack(configLine, log.getTypeSelect()) || (!Strings.isNullOrEmpty(configLine.getTrackingCondition()) && !Boolean.TRUE.equals(new GroovyScriptHelper(bindings).eval(configLine.getTrackingCondition())))) {
                    continue;
                }
                line.setMetaField(fieldRepo.all().filter("self.metaModel.id = ? AND self.name = ?", log.getMetaModel().getId(), line.getMetaFieldName()).fetchOne());
                logLinesToSave.add(line);
            }
        }
        if (!logLinesToSave.isEmpty() || (GlobalTrackingLogRepository.TYPE_DELETE == log.getTypeSelect() && configLineList.stream().anyMatch(l -> Boolean.TRUE.equals(l.getTrackDeletion())))) {
            log.getGlobalTrackingLogLineList().stream().forEach(l -> l.setGlobalTrackingLog(null));
            logLinesToSave.stream().forEach(l -> l.setGlobalTrackingLog(log));
            log.setUser(user);
            logRepo.save(log);
        }
    }
}
Also used : GlobalTrackingConfigurationLine(com.axelor.apps.base.db.GlobalTrackingConfigurationLine) MetaModelRepository(com.axelor.meta.db.repo.MetaModelRepository) Arrays(java.util.Arrays) LocalDateTime(java.time.LocalDateTime) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper) HashMap(java.util.HashMap) ScriptBindings(com.axelor.script.ScriptBindings) PersistentSet(org.hibernate.collection.internal.PersistentSet) ArrayList(java.util.ArrayList) Transaction(org.hibernate.Transaction) Strings(com.google.common.base.Strings) Introspector(java.beans.Introspector) GlobalTrackingLog(com.axelor.apps.base.db.GlobalTrackingLog) CollectionUtils(org.apache.commons.collections.CollectionUtils) AuditInterceptor(com.axelor.auth.AuditInterceptor) BeanInfo(java.beans.BeanInfo) Map(java.util.Map) GlobalTrackingLogRepository(com.axelor.apps.base.db.repo.GlobalTrackingLogRepository) Method(java.lang.reflect.Method) JPA(com.axelor.db.JPA) PersistentBag(org.hibernate.collection.internal.PersistentBag) Model(com.axelor.db.Model) Collection(java.util.Collection) GlobalTrackingLogLine(com.axelor.apps.base.db.GlobalTrackingLogLine) Collectors(java.util.stream.Collectors) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GlobalTrackingConfigurationLineRepository(com.axelor.apps.base.db.repo.GlobalTrackingConfigurationLineRepository) List(java.util.List) Beans(com.axelor.inject.Beans) MetaFieldRepository(com.axelor.meta.db.repo.MetaFieldRepository) PropertyDescriptor(java.beans.PropertyDescriptor) AbstractPersistentCollection(org.hibernate.collection.internal.AbstractPersistentCollection) AuditableModel(com.axelor.auth.db.AuditableModel) AuthUtils(com.axelor.auth.AuthUtils) User(com.axelor.auth.db.User) MetaModelRepository(com.axelor.meta.db.repo.MetaModelRepository) GlobalTrackingLog(com.axelor.apps.base.db.GlobalTrackingLog) GlobalTrackingConfigurationLineRepository(com.axelor.apps.base.db.repo.GlobalTrackingConfigurationLineRepository) ArrayList(java.util.ArrayList) GlobalTrackingLogLine(com.axelor.apps.base.db.GlobalTrackingLogLine) MetaFieldRepository(com.axelor.meta.db.repo.MetaFieldRepository) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GlobalTrackingLogRepository(com.axelor.apps.base.db.repo.GlobalTrackingLogRepository) GlobalTrackingConfigurationLine(com.axelor.apps.base.db.GlobalTrackingConfigurationLine) ScriptBindings(com.axelor.script.ScriptBindings)

Aggregations

MetaFieldRepository (com.axelor.meta.db.repo.MetaFieldRepository)2 MetaModelRepository (com.axelor.meta.db.repo.MetaModelRepository)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AdvancedExport (com.axelor.apps.base.db.AdvancedExport)1 GlobalTrackingConfigurationLine (com.axelor.apps.base.db.GlobalTrackingConfigurationLine)1 GlobalTrackingLog (com.axelor.apps.base.db.GlobalTrackingLog)1 GlobalTrackingLogLine (com.axelor.apps.base.db.GlobalTrackingLogLine)1 GlobalTrackingConfigurationLineRepository (com.axelor.apps.base.db.repo.GlobalTrackingConfigurationLineRepository)1 GlobalTrackingLogRepository (com.axelor.apps.base.db.repo.GlobalTrackingLogRepository)1 AuditInterceptor (com.axelor.auth.AuditInterceptor)1 AuthUtils (com.axelor.auth.AuthUtils)1 AuditableModel (com.axelor.auth.db.AuditableModel)1 User (com.axelor.auth.db.User)1 JPA (com.axelor.db.JPA)1 Model (com.axelor.db.Model)1 Mapper (com.axelor.db.mapper.Mapper)1 Beans (com.axelor.inject.Beans)1 MetaField (com.axelor.meta.db.MetaField)1