Search in sources :

Example 1 with FileTab

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

the class DataImportServiceImpl method process.

private List<CSVInput> process(DataReaderService reader, AdvancedImport advancedImport) throws AxelorException, IOException, ClassNotFoundException {
    String[] sheets = reader.getSheetNames();
    boolean isConfig = advancedImport.getIsConfigInFile();
    int linesToIgnore = advancedImport.getNbOfFirstLineIgnore();
    boolean isTabConfig = advancedImport.getIsFileTabConfigAdded();
    List<CSVInput> inputList = new ArrayList<CSVInput>();
    validatorService.sortFileTabList(advancedImport.getFileTabList());
    for (FileTab fileTab : advancedImport.getFileTabList()) {
        if (!Arrays.stream(sheets).anyMatch(sheet -> sheet.equals(fileTab.getName()))) {
            continue;
        }
        this.initializeVariables();
        String fileName = createDataFileName(fileTab);
        csvInput = this.createCSVInput(fileTab, fileName);
        ifList = new ArrayList<String>();
        try (CSVWriter csvWriter = new CSVWriter(new FileWriter(new File(dataDir, fileName)), CSV_SEPRATOR)) {
            int totalLines = reader.getTotalLines(fileTab.getName());
            if (totalLines == 0) {
                continue;
            }
            Mapper mapper = advancedImportService.getMapper(fileTab.getMetaModel().getFullName());
            List<String[]> allLines = new ArrayList<String[]>();
            int startIndex = isConfig ? 1 : linesToIgnore;
            String[] row = reader.read(fileTab.getName(), startIndex, 0);
            String[] headers = this.createHeader(row, fileTab, isConfig, mapper);
            allLines.add(headers);
            int tabConfigRowCount = 0;
            if (isTabConfig) {
                String[] objectRow = reader.read(fileTab.getName(), 0, 0);
                tabConfigRowCount = advancedImportService.getTabConfigRowCount(fileTab.getName(), reader, totalLines, objectRow);
            }
            startIndex = isConfig ? tabConfigRowCount + 3 : fileTab.getAdvancedImport().getIsHeader() ? linesToIgnore + 1 : linesToIgnore;
            for (int line = startIndex; line < totalLines; line++) {
                String[] dataRow = reader.read(fileTab.getName(), line, row.length);
                if (dataRow == null || Arrays.stream(dataRow).allMatch(StringUtils::isBlank)) {
                    continue;
                }
                String[] data = this.createData(dataRow, fileTab, isConfig, mapper);
                allLines.add(data);
            }
            csvWriter.writeAll(allLines);
            csvWriter.flush();
        }
        inputList.add(csvInput);
        importContext.put("ifConditions" + fileTab.getId(), ifList);
        importContext.put("jsonContextValues" + fileTab.getId(), createJsonContext(fileTab));
        importContext.put("actionsToApply" + fileTab.getId(), fileTab.getActions());
        XStream stream = XStreamUtils.createXStream();
        stream.processAnnotations(CSVConfig.class);
        LOG.debug("CSV Config created :" + "\n" + stream.toXML(csvInput));
    }
    return inputList;
}
Also used : XStream(com.thoughtworks.xstream.XStream) Arrays(java.util.Arrays) Date(java.util.Date) Inject(com.google.inject.Inject) CSVInput(com.axelor.data.csv.CSVInput) LoggerFactory(org.slf4j.LoggerFactory) Mapper(com.axelor.db.mapper.Mapper) JsonContext(com.axelor.rpc.JsonContext) FileField(com.axelor.apps.base.db.FileField) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) Inflector(com.axelor.common.Inflector) ZipEntry(java.util.zip.ZipEntry) TranslationService(com.axelor.apps.tool.service.TranslationService) ZipException(java.util.zip.ZipException) MethodHandles(java.lang.invoke.MethodHandles) CSVImporter(com.axelor.data.csv.CSVImporter) StringUtils(com.axelor.common.StringUtils) DataAdapter(com.axelor.data.adapter.DataAdapter) MetaModel(com.axelor.meta.db.MetaModel) List(java.util.List) CSVBind(com.axelor.data.csv.CSVBind) MetaSelectRepository(com.axelor.meta.db.repo.MetaSelectRepository) Entry(java.util.Map.Entry) FileTab(com.axelor.apps.base.db.FileTab) AdvancedImport(com.axelor.apps.base.db.AdvancedImport) ZipInputStream(java.util.zip.ZipInputStream) MetaSelectItem(com.axelor.meta.db.MetaSelectItem) Property(com.axelor.db.mapper.Property) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) ImporterListener(com.axelor.apps.base.service.imports.listener.ImporterListener) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) AxelorException(com.axelor.exception.AxelorException) Files(com.google.common.io.Files) CollectionUtils(org.apache.commons.collections.CollectionUtils) XStreamUtils(com.axelor.data.XStreamUtils) MetaSelectItemRepository(com.axelor.meta.db.repo.MetaSelectItemRepository) DataReaderFactory(com.axelor.apps.tool.reader.DataReaderFactory) MetaFiles(com.axelor.meta.MetaFiles) Logger(org.slf4j.Logger) Model(com.axelor.db.Model) FileWriter(java.io.FileWriter) TraceBackService(com.axelor.exception.service.TraceBackService) FileOutputStream(java.io.FileOutputStream) CSVWriter(com.opencsv.CSVWriter) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) JavaTimeAdapter(com.axelor.data.adapter.JavaTimeAdapter) FileInputStream(java.io.FileInputStream) File(java.io.File) DataReaderService(com.axelor.apps.tool.reader.DataReaderService) MetaFile(com.axelor.meta.db.MetaFile) FileFieldRepository(com.axelor.apps.base.db.repo.FileFieldRepository) CSVConfig(com.axelor.data.csv.CSVConfig) MetaSelect(com.axelor.meta.db.MetaSelect) Context(com.axelor.rpc.Context) XStream(com.thoughtworks.xstream.XStream) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) CSVInput(com.axelor.data.csv.CSVInput) CSVWriter(com.opencsv.CSVWriter) Mapper(com.axelor.db.mapper.Mapper) FileTab(com.axelor.apps.base.db.FileTab) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile)

Example 2 with FileTab

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

the class AdvancedImportServiceImpl method removeRecords.

@SuppressWarnings("unchecked")
public void removeRecords(List<FileTab> fileTabList) throws ClassNotFoundException {
    for (FileTab fileTab : fileTabList) {
        String targetModelName = fileTab.getMetaModel().getFullName();
        Map<String, Object> jsonContextMap = dataImportService.createJsonContext(fileTab);
        JsonContext jsonContext = (JsonContext) jsonContextMap.get("jsonContext");
        String fieldName = inflector.camelize(fileTab.getMetaModel().getName(), true) + "Set";
        List<Object> recordList = (List<Object>) jsonContext.get(fieldName);
        if (CollectionUtils.isEmpty(recordList)) {
            continue;
        }
        Class<? extends Model> modelKlass = (Class<? extends Model>) Class.forName(targetModelName);
        removeRecord(fileTab, modelKlass, recordList, fileTabList);
        removeSubRecords(modelKlass, jsonContext);
    }
}
Also used : JsonContext(com.axelor.rpc.JsonContext) Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel) ArrayList(java.util.ArrayList) List(java.util.List) FileTab(com.axelor.apps.base.db.FileTab)

Example 3 with FileTab

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

the class AdvancedImportServiceImpl method removeRecord.

@SuppressWarnings("unchecked")
@Transactional
public void removeRecord(FileTab fileTab, Class<? extends Model> modelKlass, List<Object> recordList, List<FileTab> fileTabList) throws ClassNotFoundException {
    JpaRepository<? extends Model> modelRepo = JpaRepository.of(modelKlass);
    for (FileTab tab : fileTabList) {
        Map<String, Object> jsonContextMap = dataImportService.createJsonContext(tab);
        JsonContext jsonContext = (JsonContext) jsonContextMap.get("jsonContext");
        String fieldName = inflector.camelize(tab.getMetaModel().getName(), true) + "Set";
        List<Object> recList = (List<Object>) jsonContext.get(fieldName);
        if (CollectionUtils.isEmpty(recList)) {
            continue;
        }
        Class<? extends Model> klass = (Class<? extends Model>) Class.forName(tab.getMetaModel().getFullName());
        Property[] props = Mapper.of(klass).getProperties();
        for (Property prop : props) {
            if (prop.getTarget() != null && prop.getTarget() == modelKlass && prop.isRequired()) {
                removeRecord(tab, klass, recList, fileTabList);
            }
        }
    }
    String ids = recordList.stream().map(obj -> {
        Map<String, Object> recordMap = Mapper.toMap(EntityHelper.getEntity(obj));
        return recordMap.get("id").toString();
    }).collect(Collectors.joining(","));
    modelRepo.all().filter("self.id IN (" + ids + ")").delete();
    fileTab.setAttrs(null);
    LOG.debug("Reset imported data : {}", modelKlass.getSimpleName());
}
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) JsonContext(com.axelor.rpc.JsonContext) Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel) ArrayList(java.util.ArrayList) List(java.util.List) FileTab(com.axelor.apps.base.db.FileTab) Property(com.axelor.db.mapper.Property) HashMap(java.util.HashMap) Map(java.util.Map) Transactional(com.google.inject.persist.Transactional)

Example 4 with FileTab

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

the class FileTabServiceImpl method getShowRecordIds.

@SuppressWarnings("unchecked")
@Override
public String getShowRecordIds(FileTab fileTab, String field) throws ClassNotFoundException {
    Context context = new Context(fileTab.getClass());
    Class<? extends Model> klass = (Class<? extends Model>) Class.forName(fileTab.getClass().getName());
    JsonContext jsonContext = new JsonContext(context, Mapper.of(klass).getProperty("attrs"), fileTab.getAttrs());
    List<Object> recordList = (List<Object>) jsonContext.get(field);
    if (CollectionUtils.isEmpty(recordList)) {
        return null;
    }
    String ids = recordList.stream().map(obj -> {
        Map<String, Object> recordMap = Mapper.toMap(EntityHelper.getEntity(obj));
        return recordMap.get("id").toString();
    }).collect(Collectors.joining(","));
    return ids;
}
Also used : JsonContext(com.axelor.rpc.JsonContext) Context(com.axelor.rpc.Context) MetaModelRepository(com.axelor.meta.db.repo.MetaModelRepository) EntityHelper(com.axelor.db.EntityHelper) Model(com.axelor.db.Model) MetaField(com.axelor.meta.db.MetaField) Inject(com.google.inject.Inject) Mapper(com.axelor.db.mapper.Mapper) Collectors(java.util.stream.Collectors) JsonContext(com.axelor.rpc.JsonContext) FileField(com.axelor.apps.base.db.FileField) Strings(com.google.common.base.Strings) 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) CollectionUtils(org.apache.commons.collections.CollectionUtils) Map(java.util.Map) FileTab(com.axelor.apps.base.db.FileTab) Context(com.axelor.rpc.Context) JsonContext(com.axelor.rpc.JsonContext) Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel) List(java.util.List) Map(java.util.Map)

Example 5 with FileTab

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

the class FileTabController method showRecord.

public void showRecord(ActionRequest request, ActionResponse response) {
    try {
        FileTab fileTab = request.getContext().asType(FileTab.class);
        fileTab = Beans.get(FileTabRepository.class).find(fileTab.getId());
        String btnName = request.getContext().get("_signal").toString();
        String fieldName = StringUtils.substringBetween(btnName, "show", "Btn");
        MetaJsonField jsonField = Beans.get(MetaJsonFieldRepository.class).all().filter("self.name = ?1 AND self.type = 'many-to-many' AND self.model = ?2 AND self.modelField = 'attrs'", fieldName, fileTab.getClass().getName()).fetchOne();
        if (jsonField == null) {
            return;
        }
        String ids = Beans.get(FileTabService.class).getShowRecordIds(fileTab, jsonField.getName());
        response.setView(ActionView.define(I18n.get(jsonField.getTitle())).model(jsonField.getTargetModel()).add("grid", jsonField.getGridView()).add("form", jsonField.getFormView()).domain("self.id IN (" + ids + ")").map());
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : MetaJsonFieldRepository(com.axelor.meta.db.repo.MetaJsonFieldRepository) FileTabService(com.axelor.apps.base.service.advanced.imports.FileTabService) MetaJsonField(com.axelor.meta.db.MetaJsonField) FileTab(com.axelor.apps.base.db.FileTab)

Aggregations

FileTab (com.axelor.apps.base.db.FileTab)14 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Model (com.axelor.db.Model)7 FileField (com.axelor.apps.base.db.FileField)6 FileFieldRepository (com.axelor.apps.base.db.repo.FileFieldRepository)5 Mapper (com.axelor.db.mapper.Mapper)5 Property (com.axelor.db.mapper.Property)5 Strings (com.google.common.base.Strings)5 Inject (com.google.inject.Inject)5 AdvancedImport (com.axelor.apps.base.db.AdvancedImport)4 DataReaderFactory (com.axelor.apps.tool.reader.DataReaderFactory)4 DataReaderService (com.axelor.apps.tool.reader.DataReaderService)4 Inflector (com.axelor.common.Inflector)4 AxelorException (com.axelor.exception.AxelorException)4 MetaField (com.axelor.meta.db.MetaField)4 MetaModel (com.axelor.meta.db.MetaModel)4 JsonContext (com.axelor.rpc.JsonContext)4 Files (com.google.common.io.Files)4 Arrays (java.util.Arrays)4