Search in sources :

Example 26 with MetaModel

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

the class FilterSqlService method addJoin.

private void addJoin(MetaJsonField field, List<String> joins, StringBuilder parent) {
    String targetModel = null;
    if (field.getTargetModel() != null) {
        targetModel = field.getTargetModel();
    } else if (field.getTargetJsonModel() != null) {
        targetModel = MetaJsonRecord.class.getName();
    }
    MetaModel metaModel = metaModelRepo.all().filter("self.fullName = ?1", targetModel).fetchOne();
    joins.add("left join " + metaModel.getTableName() + " " + "obj" + joins.size() + " on (obj" + joins.size() + ".id = " + "cast(" + parent + "." + getColumn(field.getModel(), field.getModelField()) + "->'" + field.getName() + "'->>'id' as integer))");
    parent.replace(0, parent.length(), "obj" + (joins.size() - 1));
}
Also used : MetaModel(com.axelor.meta.db.MetaModel)

Example 27 with MetaModel

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

the class ExportObserver method onExport.

void onExport(@Observes @Named(RequestEvent.EXPORT) PostRequest event) throws IOException {
    List<GlobalTrackingConfigurationLine> gtcLines = Beans.get(AppBaseService.class).getAppBase().getGlobalTrackingConfigurationLines();
    if (CollectionUtils.isEmpty(gtcLines)) {
        return;
    }
    MetaModel model = Beans.get(MetaModelRepository.class).all().filter("self.fullName = ?", event.getRequest().getBeanClass().getName()).fetchOne();
    if (gtcLines.stream().anyMatch(l -> l.getMetaModel().equals(model) && l.getTrackExport())) {
        @SuppressWarnings("unchecked") final Map<String, Object> data = (Map<String, Object>) event.getResponse().getData();
        if (data == null || data.get("fileName") == null) {
            return;
        }
        final String fileName = (String) data.get("fileName");
        final Path filePath = MetaFiles.findTempFile(fileName);
        MetaFile mf = new MetaFile();
        mf.setFileName(fileName);
        Beans.get(MetaFiles.class).upload(new FileInputStream(filePath.toFile()), mf);
        Beans.get(GlobalTrackingLogService.class).createExportLog(model, mf);
    }
}
Also used : Path(java.nio.file.Path) MetaFiles(com.axelor.meta.MetaFiles) MetaModelRepository(com.axelor.meta.db.repo.MetaModelRepository) FileInputStream(java.io.FileInputStream) MetaModel(com.axelor.meta.db.MetaModel) GlobalTrackingConfigurationLine(com.axelor.apps.base.db.GlobalTrackingConfigurationLine) MetaFile(com.axelor.meta.db.MetaFile) Map(java.util.Map)

Example 28 with MetaModel

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

the class AdvancedExportController method callAdvancedExportWizard.

@SuppressWarnings("unchecked")
public void callAdvancedExportWizard(ActionRequest request, ActionResponse response) throws ClassNotFoundException {
    LOG.debug("Call advanced export wizard for model : {} ", request.getModel());
    MetaModel metaModel = Beans.get(MetaModelRepository.class).all().filter("self.fullName = ?", request.getModel()).fetchOne();
    String criteria = "";
    if (request.getContext().get("_ids") != null)
        criteria = request.getContext().get("_ids").toString();
    else {
        Class<? extends Model> klass = (Class<? extends Model>) request.getBeanClass();
        Filter filter = Beans.get(AdvancedExportService.class).getJpaSecurityFilter(metaModel);
        int recordCount = (int) request.getCriteria().createQuery(klass, filter).count();
        if (recordCount > 0)
            criteria = String.valueOf(recordCount);
    }
    if (Strings.isNullOrEmpty(criteria))
        response.setError(I18n.get(IExceptionMessage.ADVANCED_EXPORT_2));
    else {
        String metaModelName = request.getModel();
        boolean isExist = Beans.get(AdvancedExportService.class).checkAdvancedExportExist(metaModelName);
        if (!isExist) {
            response.setView(ActionView.define(I18n.get("Advanced export")).model(AdvancedExport.class.getName()).add("form", "advanced-export-form").context("_metaModel", metaModel).map());
            return;
        }
        response.setView(ActionView.define(I18n.get("Advanced export")).model(AdvancedExport.class.getName()).add("form", "advanced-export-wizard-form").param("popup", "true").param("show-toolbar", "false").param("show-confirm", "false").param("popup-save", "false").context("_metaModel", metaModel).context("_criteria", criteria.startsWith("[") ? criteria : request).map());
    }
}
Also used : MetaModel(com.axelor.meta.db.MetaModel) MetaModelRepository(com.axelor.meta.db.repo.MetaModelRepository) AdvancedExport(com.axelor.apps.base.db.AdvancedExport) Filter(com.axelor.rpc.filter.Filter) Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel) AdvancedExportService(com.axelor.apps.base.service.advancedExport.AdvancedExportService)

Example 29 with MetaModel

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

the class DataBackupCreateService method checkErrors.

protected int checkErrors(DataBackup dataBackup, List<MetaModel> metaModelList, String tempDirectoryPath, Map<String, List<String>> subClassesMap) {
    int errorsCount = 0;
    for (MetaModel metaModel : metaModelList) {
        try {
            List<String> subClasses = subClassesMap.get(metaModel.getFullName());
            long totalRecord = getMetaModelDataCount(metaModel, subClasses);
            if (totalRecord > 0) {
                LOG.debug("Checking Model : " + metaModel.getFullName());
                CSVWriter csvWriter = new CSVWriter(new FileWriter(new File(tempDirectoryPath, metaModel.getName() + ".csv")), SEPARATOR, QUOTE_CHAR);
                writeCSVData(metaModel, csvWriter, dataBackup, 1, subClasses, tempDirectoryPath);
                csvWriter.close();
            }
        } catch (ClassNotFoundException e) {
        } catch (IOException e) {
            TraceBackService.trace(e, DataBackupService.class.getName());
        } catch (Exception e) {
            JPA.em().getTransaction().rollback();
            sb.append("\nError occured while processing model : " + metaModel.getFullName() + "\n");
            sb.append(e.getMessage() + "\n");
            JPA.em().getTransaction().begin();
            dataBackup = Beans.get(DataBackupRepository.class).find(dataBackup.getId());
            dataBackup.setFetchLimit(1);
            errorsCount++;
        }
    }
    return errorsCount;
}
Also used : MetaModel(com.axelor.meta.db.MetaModel) DataBackupRepository(com.axelor.apps.base.db.repo.DataBackupRepository) FileWriter(java.io.FileWriter) CSVWriter(com.opencsv.CSVWriter) IOException(java.io.IOException) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 30 with MetaModel

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

the class DataBackupCreateService method getSubClassesMap.

protected Map<String, List<String>> getSubClassesMap() {
    List<MetaModel> metaModels = getMetaModels();
    List<String> subClasses;
    Map<String, List<String>> subClassMap = new HashMap<>();
    for (MetaModel metaModel : metaModels) {
        try {
            subClasses = new ArrayList<>();
            @SuppressWarnings("unchecked") Class<AuditableModel> superClass = (Class<AuditableModel>) Class.forName(metaModel.getFullName()).getSuperclass();
            if (superClass != AuditableModel.class) {
                if (!subClassMap.isEmpty() && subClassMap.containsKey(superClass.getName())) {
                    subClasses = subClassMap.get(superClass.getName());
                }
                subClasses.add(metaModel.getName());
                subClassMap.put(superClass.getName(), subClasses);
            }
        } catch (ClassNotFoundException e) {
            String strError = "Class not found issue: ";
            sb.append(strError).append(e.getLocalizedMessage() + "-----------------------------------------\n");
            e.printStackTrace();
        }
    }
    return subClassMap;
}
Also used : HashMap(java.util.HashMap) AuditableModel(com.axelor.auth.db.AuditableModel) MetaModel(com.axelor.meta.db.MetaModel) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Aggregations

MetaModel (com.axelor.meta.db.MetaModel)41 MetaModelRepository (com.axelor.meta.db.repo.MetaModelRepository)12 AxelorException (com.axelor.exception.AxelorException)9 MetaField (com.axelor.meta.db.MetaField)9 ArrayList (java.util.ArrayList)8 Model (com.axelor.db.Model)7 Mapper (com.axelor.db.mapper.Mapper)6 IOException (java.io.IOException)6 Transactional (com.google.inject.persist.Transactional)5 List (java.util.List)5 App (com.axelor.apps.base.db.App)4 Property (com.axelor.db.mapper.Property)4 MetaJsonModel (com.axelor.meta.db.MetaJsonModel)4 Context (com.axelor.rpc.Context)4 AuditableModel (com.axelor.auth.db.AuditableModel)3 CSVInput (com.axelor.data.csv.CSVInput)3 MetaFile (com.axelor.meta.db.MetaFile)3 ActionViewBuilder (com.axelor.meta.schema.actions.ActionView.ActionViewBuilder)3 AppFilter (com.axelor.app.internal.AppFilter)2 AdvancedExport (com.axelor.apps.base.db.AdvancedExport)2