Search in sources :

Example 1 with AdvancedExport

use of com.axelor.apps.base.db.AdvancedExport 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 AdvancedExport

use of com.axelor.apps.base.db.AdvancedExport in project axelor-open-suite by axelor.

the class AdvancedExportController method createCriteria.

@SuppressWarnings("unchecked")
private List<Long> createCriteria(ActionRequest request, AdvancedExport advancedExport) {
    if (request.getContext().get("_criteria") != null) {
        if (request.getContext().get("_criteria").toString().startsWith("[")) {
            String ids = request.getContext().get("_criteria").toString();
            return Splitter.on(", ").splitToList(ids.substring(1, ids.length() - 1)).stream().map(id -> Long.valueOf(id.toString())).collect(Collectors.toList());
        } else {
            ObjectMapper mapper = new ObjectMapper();
            ActionRequest parentRequest = mapper.convertValue(request.getContext().get("_criteria"), ActionRequest.class);
            Class<? extends Model> klass = (Class<? extends Model>) parentRequest.getBeanClass();
            Filter filter = Beans.get(AdvancedExportService.class).getJpaSecurityFilter(advancedExport.getMetaModel());
            Stream<? extends Model> listObj = parentRequest.getCriteria().createQuery(klass, filter).fetchSteam(advancedExport.getMaxExportLimit());
            return listObj.map(it -> it.getId()).collect(Collectors.toList());
        }
    }
    return null;
}
Also used : MetaModelRepository(com.axelor.meta.db.repo.MetaModelRepository) Filter(com.axelor.rpc.filter.Filter) ActionView(com.axelor.meta.schema.actions.ActionView) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Mapper(com.axelor.db.mapper.Mapper) AdvancedExportLine(com.axelor.apps.base.db.AdvancedExportLine) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) AdvancedExport(com.axelor.apps.base.db.AdvancedExport) AxelorException(com.axelor.exception.AxelorException) ActionResponse(com.axelor.rpc.ActionResponse) Map(java.util.Map) I18n(com.axelor.i18n.I18n) Inflector(com.axelor.common.Inflector) AdvancedExportRepository(com.axelor.apps.base.db.repo.AdvancedExportRepository) ActionRequest(com.axelor.rpc.ActionRequest) Splitter(com.google.common.base.Splitter) MetaFiles(com.axelor.meta.MetaFiles) Logger(org.slf4j.Logger) Model(com.axelor.db.Model) MetaField(com.axelor.meta.db.MetaField) MethodHandles(java.lang.invoke.MethodHandles) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TraceBackService(com.axelor.exception.service.TraceBackService) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) File(java.io.File) AdvancedExportService(com.axelor.apps.base.service.advancedExport.AdvancedExportService) MetaFile(com.axelor.meta.db.MetaFile) MetaModel(com.axelor.meta.db.MetaModel) List(java.util.List) Stream(java.util.stream.Stream) Beans(com.axelor.inject.Beans) MetaFieldRepository(com.axelor.meta.db.repo.MetaFieldRepository) IExceptionMessage(com.axelor.apps.base.exceptions.IExceptionMessage) Context(com.axelor.rpc.Context) Joiner(com.google.common.base.Joiner) Singleton(com.google.inject.Singleton) ActionRequest(com.axelor.rpc.ActionRequest) 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) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with AdvancedExport

use of com.axelor.apps.base.db.AdvancedExport in project axelor-open-suite by axelor.

the class AdvancedExportController method advancedExport.

private void advancedExport(ActionRequest request, ActionResponse response, String fileType) throws AxelorException, IOException {
    AdvancedExport advancedExport = request.getContext().asType(AdvancedExport.class);
    advancedExport = Beans.get(AdvancedExportRepository.class).find(advancedExport.getId());
    getAdvancedExportFile(request, response, advancedExport, fileType);
}
Also used : AdvancedExport(com.axelor.apps.base.db.AdvancedExport)

Example 4 with AdvancedExport

use of com.axelor.apps.base.db.AdvancedExport in project axelor-open-suite by axelor.

the class AdvancedExportController method generateExportFile.

@SuppressWarnings({ "rawtypes" })
public void generateExportFile(ActionRequest request, ActionResponse response) {
    try {
        if (request.getContext().get("_xAdvancedExport") == null || request.getContext().get("exportFormatSelect") == null) {
            response.setError(I18n.get(IExceptionMessage.ADVANCED_EXPORT_4));
            return;
        }
        AdvancedExport advancedExport = Beans.get(AdvancedExportRepository.class).find(Long.valueOf(((Map) request.getContext().get("_xAdvancedExport")).get("id").toString()));
        String fileType = request.getContext().get("exportFormatSelect").toString();
        getAdvancedExportFile(request, response, advancedExport, fileType);
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : AdvancedExport(com.axelor.apps.base.db.AdvancedExport) AdvancedExportRepository(com.axelor.apps.base.db.repo.AdvancedExportRepository) AxelorException(com.axelor.exception.AxelorException) IOException(java.io.IOException)

Example 5 with AdvancedExport

use of com.axelor.apps.base.db.AdvancedExport in project axelor-open-suite by axelor.

the class AdvancedExportGenerator method generateFile.

/**
 * This method is used to generate the export file.
 *
 * @param advancedExport
 * @param query
 * @return
 * @throws AxelorException
 * @throws IOException
 * @throws DocumentException
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public File generateFile(Query query) throws AxelorException {
    AdvancedExport advancedExport = getAdvancedExport();
    log.debug("Export file : {}", getFileName());
    generateHeader();
    int startPosition = 0;
    int reachLimit = 0;
    int maxExportLimit = advancedExport.getMaxExportLimit();
    int queryFetchLimit = advancedExport.getQueryFetchSize();
    List<List> dataList = new ArrayList<>();
    query.setMaxResults(queryFetchLimit);
    while (startPosition < maxExportLimit) {
        if ((maxExportLimit - startPosition) < queryFetchLimit) {
            query.setMaxResults((maxExportLimit - startPosition));
        }
        query.setFirstResult(startPosition);
        dataList = query.getResultList();
        if (dataList.isEmpty())
            break;
        generateBody(dataList);
        startPosition = startPosition + queryFetchLimit;
        reachLimit += dataList.size();
    }
    if (maxExportLimit == reachLimit) {
        isReachMaxExportLimit = true;
    }
    close();
    return getExportFile();
}
Also used : AdvancedExport(com.axelor.apps.base.db.AdvancedExport) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

AdvancedExport (com.axelor.apps.base.db.AdvancedExport)5 ArrayList (java.util.ArrayList)3 AdvancedExportRepository (com.axelor.apps.base.db.repo.AdvancedExportRepository)2 Mapper (com.axelor.db.mapper.Mapper)2 AxelorException (com.axelor.exception.AxelorException)2 MetaField (com.axelor.meta.db.MetaField)2 MetaModel (com.axelor.meta.db.MetaModel)2 MetaFieldRepository (com.axelor.meta.db.repo.MetaFieldRepository)2 MetaModelRepository (com.axelor.meta.db.repo.MetaModelRepository)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 AdvancedExportLine (com.axelor.apps.base.db.AdvancedExportLine)1 IExceptionMessage (com.axelor.apps.base.exceptions.IExceptionMessage)1 AdvancedExportService (com.axelor.apps.base.service.advancedExport.AdvancedExportService)1 Inflector (com.axelor.common.Inflector)1 Model (com.axelor.db.Model)1 TraceBackService (com.axelor.exception.service.TraceBackService)1