Search in sources :

Example 1 with CSVBind

use of com.axelor.data.csv.CSVBind in project axelor-open-suite by axelor.

the class DataImportServiceImpl method createBindForNotMatchWithFile.

private void createBindForNotMatchWithFile(String column, int importType, CSVBind dummyBind, String expression, String adapter, CSVBind parentBind, Property childProp) {
    dummyBind.setExpression(expression);
    dummyBind.setAdapter(adapter);
    if (importType == FileFieldRepository.IMPORT_TYPE_FIND_NEW || importType == FileFieldRepository.IMPORT_TYPE_NEW) {
        CSVBind subBind = this.createCSVBind(column, childProp.getName(), null, null, null, null);
        this.setImportIf(childProp, subBind, column);
        parentBind.getBindings().add(subBind);
    }
}
Also used : CSVBind(com.axelor.data.csv.CSVBind)

Example 2 with CSVBind

use of com.axelor.data.csv.CSVBind in project axelor-open-suite by axelor.

the class DataImportServiceImpl method createCSVBind.

private CSVBind createCSVBind(String column, String field, String search, String expression, String adapter, Boolean update) {
    CSVBind bind = new CSVBind();
    bind.setColumn(column);
    bind.setField(field);
    bind.setSearch(search);
    bind.setExpression(expression);
    bind.setAdapter(adapter);
    if (update != null) {
        bind.setUpdate(update);
    }
    return bind;
}
Also used : CSVBind(com.axelor.data.csv.CSVBind)

Example 3 with CSVBind

use of com.axelor.data.csv.CSVBind in project axelor-open-suite by axelor.

the class DataImportServiceImpl method createCSVBinding.

private List<CSVBind> createCSVBinding(String column, FileField fileField, Mapper mapper, List<CSVBind> allBindings) throws ClassNotFoundException {
    Property prop = mapper.getProperty(fileField.getImportField().getName());
    if (prop == null) {
        return allBindings;
    }
    CSVBind dummyBind = null;
    if (!fileField.getIsMatchWithFile()) {
        dummyBind = this.createCSVBind(null, column, null, null, null, null);
        allBindings.add(0, dummyBind);
    }
    if (Strings.isNullOrEmpty(fileField.getSubImportField())) {
        String expression = this.setExpression(column, fileField, prop);
        String adapter = null;
        String dateFormat = fileField.getDateFormat();
        if (Strings.isNullOrEmpty(expression) && !Strings.isNullOrEmpty(dateFormat)) {
            adapter = this.getAdapter(prop.getJavaType().getSimpleName(), dateFormat.trim());
        }
        CSVBind bind = null;
        if (!fileField.getIsMatchWithFile()) {
            dummyBind.setExpression(expression);
            dummyBind.setAdapter(adapter);
            bind = this.createCSVBind(column, prop.getName(), null, null, null, null);
            this.setImportIf(prop, bind, column);
        } else {
            bind = this.createCSVBind(column, prop.getName(), null, expression, adapter, null);
            this.setImportIf(prop, bind, column);
        }
        allBindings.add(bind);
    } else {
        CSVBind parentBind = null;
        boolean isSameParentExist = false;
        if (parentBindMap.containsKey(prop.getName())) {
            parentBind = parentBindMap.get(prop.getName());
            isSameParentExist = true;
        } else {
            parentBind = this.createCSVBind(null, prop.getName(), null, null, null, true);
            parentBind.setBindings(new ArrayList<>());
            allBindings.add(parentBind);
            parentBindMap.put(prop.getName(), parentBind);
        }
        fullFieldName = prop.getName();
        String[] subFields = fileField.getSubImportField().split("\\.");
        this.createCSVSubBinding(subFields, 0, column, prop, fileField, parentBind, dummyBind, isSameParentExist);
    }
    if (!Strings.isNullOrEmpty(fileField.getNoImportIf())) {
        String importIf = this.convertExpression(fileField.getNoImportIf().trim(), fileField.getTargetType(), column);
        ifList.add(importIf);
    }
    return allBindings;
}
Also used : CSVBind(com.axelor.data.csv.CSVBind) Property(com.axelor.db.mapper.Property)

Example 4 with CSVBind

use of com.axelor.data.csv.CSVBind in project axelor-open-suite by axelor.

the class DataBackupCreateService method getDateOrDateTimeHeader.

protected String getDateOrDateTimeHeader(Property property, CSVInput csvInput) {
    String propertyName = property.getName();
    CSVBind csvBind = new CSVBind();
    csvBind.setField(propertyName);
    csvBind.setColumn(propertyName);
    if (property.getType().toString().equals("DATE")) {
        csvBind.setExpression("call:com.axelor.csv.script.ImportDateTime:importDate(" + propertyName + ")");
    } else {
        csvBind.setExpression("call:com.axelor.csv.script.ImportDateTime:importDateTime(" + propertyName + ")");
    }
    csvInput.getBindings().add(csvBind);
    return propertyName;
}
Also used : CSVBind(com.axelor.data.csv.CSVBind)

Example 5 with CSVBind

use of com.axelor.data.csv.CSVBind in project axelor-open-suite by axelor.

the class AppServiceImpl method refreshApp.

@Override
public void refreshApp() throws IOException {
    File dataDir = Files.createTempDirectory(null).toFile();
    File imgDir = new File(dataDir, "img");
    imgDir.mkdir();
    CSVConfig csvConfig = new CSVConfig();
    csvConfig.setInputs(new ArrayList<>());
    List<MetaModel> metaModels = metaModelRepo.all().filter("self.name != 'App' and self.name like 'App%' and self.packageName =  ?1", App.class.getPackage().getName()).fetch();
    final List<String> appFieldTargetList = Stream.of(JPA.fields(App.class)).filter(p -> p.getType() == PropertyType.ONE_TO_ONE).filter(p -> p.getName().startsWith("app")).map(Property::getTarget).map(Class::getName).collect(Collectors.toList());
    log.debug("Total app models: {}", metaModels.size());
    for (MetaModel metaModel : metaModels) {
        if (!appFieldTargetList.contains(metaModel.getFullName())) {
            log.debug("Not a App class : {}", metaModel.getName());
            continue;
        }
        Class<?> klass;
        try {
            klass = Class.forName(metaModel.getFullName());
        } catch (ClassNotFoundException e) {
            continue;
        }
        Object obj = null;
        Query query = JPA.em().createQuery("SELECT id FROM " + metaModel.getName());
        try {
            obj = query.setMaxResults(1).getSingleResult();
        } catch (Exception ex) {
        }
        if (obj != null) {
            continue;
        }
        log.debug("App without app record: {}", metaModel.getName());
        String csvName = "base_" + inflector.camelize(klass.getSimpleName(), true) + ".csv";
        String pngName = inflector.dasherize(klass.getSimpleName()) + ".png";
        CSVInput input = new CSVInput();
        input.setFileName(csvName);
        input.setTypeName(App.class.getName());
        input.setCallable("com.axelor.csv.script.ImportApp:importApp");
        input.setSearch("self.code =:code");
        input.setSeparator(';');
        csvConfig.getInputs().add(input);
        CSVInput appInput = new CSVInput();
        appInput.setFileName(csvName);
        appInput.setTypeName(klass.getName());
        appInput.setSearch("self.app.code =:code");
        appInput.setSeparator(';');
        CSVBind appBind = new CSVBind();
        appBind.setColumn("code");
        appBind.setField("app");
        appBind.setSearch("self.code = :code");
        appInput.getBindings().add(appBind);
        csvConfig.getInputs().add(appInput);
        InputStream stream = klass.getResourceAsStream("/data-init/input/" + csvName);
        copyStream(stream, new File(dataDir, csvName));
        stream = klass.getResourceAsStream("/data-init/input/img/" + pngName);
        copyStream(stream, new File(imgDir, pngName));
    }
    if (!csvConfig.getInputs().isEmpty()) {
        CSVImporter importer = new CSVImporter(csvConfig, dataDir.getAbsolutePath());
        importer.run();
    }
}
Also used : App(com.axelor.apps.base.db.App) MetaModelRepository(com.axelor.meta.db.repo.MetaModelRepository) Arrays(java.util.Arrays) FileUtils(com.axelor.common.FileUtils) URL(java.net.URL) MetaScanner(com.axelor.meta.MetaScanner) Inject(com.google.inject.Inject) CSVInput(com.axelor.data.csv.CSVInput) LoggerFactory(org.slf4j.LoggerFactory) Property(com.axelor.db.mapper.Property) Scanner(java.util.Scanner) AppRepository(com.axelor.apps.base.db.repo.AppRepository) Mapper(com.axelor.db.mapper.Mapper) Transactional(com.google.inject.persist.Transactional) ArrayList(java.util.ArrayList) AxelorException(com.axelor.exception.AxelorException) App(com.axelor.apps.base.db.App) I18n(com.axelor.i18n.I18n) Inflector(com.axelor.common.Inflector) XMLImporter(com.axelor.data.xml.XMLImporter) PropertyType(com.axelor.db.mapper.PropertyType) JPA(com.axelor.db.JPA) Logger(org.slf4j.Logger) Model(com.axelor.db.Model) TraceBackRepository(com.axelor.exception.db.repo.TraceBackRepository) Files(java.nio.file.Files) Collection(java.util.Collection) CSVImporter(com.axelor.data.csv.CSVImporter) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) MetaModel(com.axelor.meta.db.MetaModel) List(java.util.List) Query(javax.persistence.Query) Stream(java.util.stream.Stream) IExceptionMessages(com.axelor.apps.base.exceptions.IExceptionMessages) CSVBind(com.axelor.data.csv.CSVBind) CSVConfig(com.axelor.data.csv.CSVConfig) AppSettings(com.axelor.app.AppSettings) ByteStreams(com.google.common.io.ByteStreams) Pattern(java.util.regex.Pattern) Importer(com.axelor.data.Importer) Singleton(com.google.inject.Singleton) InputStream(java.io.InputStream) Query(javax.persistence.Query) InputStream(java.io.InputStream) CSVInput(com.axelor.data.csv.CSVInput) CSVImporter(com.axelor.data.csv.CSVImporter) CSVConfig(com.axelor.data.csv.CSVConfig) AxelorException(com.axelor.exception.AxelorException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) MetaModel(com.axelor.meta.db.MetaModel) CSVBind(com.axelor.data.csv.CSVBind) File(java.io.File) Property(com.axelor.db.mapper.Property)

Aggregations

CSVBind (com.axelor.data.csv.CSVBind)9 Property (com.axelor.db.mapper.Property)3 ArrayList (java.util.ArrayList)3 Mapper (com.axelor.db.mapper.Mapper)2 AppSettings (com.axelor.app.AppSettings)1 App (com.axelor.apps.base.db.App)1 FileField (com.axelor.apps.base.db.FileField)1 AppRepository (com.axelor.apps.base.db.repo.AppRepository)1 IExceptionMessages (com.axelor.apps.base.exceptions.IExceptionMessages)1 FileUtils (com.axelor.common.FileUtils)1 Inflector (com.axelor.common.Inflector)1 Importer (com.axelor.data.Importer)1 CSVConfig (com.axelor.data.csv.CSVConfig)1 CSVImporter (com.axelor.data.csv.CSVImporter)1 CSVInput (com.axelor.data.csv.CSVInput)1 XMLImporter (com.axelor.data.xml.XMLImporter)1 JPA (com.axelor.db.JPA)1 Model (com.axelor.db.Model)1 PropertyType (com.axelor.db.mapper.PropertyType)1 AxelorException (com.axelor.exception.AxelorException)1