Search in sources :

Example 1 with CsvConfig

use of com.orangesignal.csv.CsvConfig in project fess by codelibs.

the class AdminBackupAction method writeCsvResponse.

private StreamResponse writeCsvResponse(final String id, final Consumer<CsvWriter> writeCall) {
    return asStream(id).contentTypeOctetStream().header("Pragma", "no-cache").header("Cache-Control", "no-cache").header("Expires", "Thu, 01 Dec 1994 16:00:00 GMT").stream(out -> {
        final CsvConfig cfg = new CsvConfig(',', '"', '"');
        cfg.setEscapeDisabled(false);
        cfg.setQuoteDisabled(false);
        try (final CsvWriter writer = new CsvWriter(new BufferedWriter(new OutputStreamWriter(out.stream(), fessConfig.getCsvFileEncoding())), cfg)) {
            writeCall.accept(writer);
            writer.flush();
        } catch (final Exception e) {
            logger.warn("Failed to write " + id + " to response.", e);
        }
    });
}
Also used : CsvWriter(com.orangesignal.csv.CsvWriter) OutputStreamWriter(java.io.OutputStreamWriter) CsvConfig(com.orangesignal.csv.CsvConfig) IORuntimeException(org.codelibs.core.exception.IORuntimeException) IOException(java.io.IOException) RuntimeIOException(com.healthmarketscience.jackcess.RuntimeIOException) BufferedWriter(java.io.BufferedWriter)

Example 2 with CsvConfig

use of com.orangesignal.csv.CsvConfig in project fess by codelibs.

the class ApiAdminBackupAction method writeCsvResponse.

private StreamResponse writeCsvResponse(final String id, final Consumer<CsvWriter> writeCall) {
    return asStream(id).contentTypeOctetStream().header("Pragma", "no-cache").header("Cache-Control", "no-cache").header("Expires", "Thu, 01 Dec 1994 16:00:00 GMT").stream(out -> {
        final CsvConfig cfg = new CsvConfig(',', '"', '"');
        cfg.setEscapeDisabled(false);
        cfg.setQuoteDisabled(false);
        try (final CsvWriter writer = new CsvWriter(new BufferedWriter(new OutputStreamWriter(out.stream(), fessConfig.getCsvFileEncoding())), cfg)) {
            writeCall.accept(writer);
            writer.flush();
        }
    });
}
Also used : CsvWriter(com.orangesignal.csv.CsvWriter) OutputStreamWriter(java.io.OutputStreamWriter) CsvConfig(com.orangesignal.csv.CsvConfig) BufferedWriter(java.io.BufferedWriter)

Example 3 with CsvConfig

use of com.orangesignal.csv.CsvConfig in project fess by codelibs.

the class ElevateWordService method exportCsv.

public void exportCsv(final Writer writer) {
    final PermissionHelper permissionHelper = ComponentUtil.getPermissionHelper();
    final CsvConfig cfg = new CsvConfig(',', '"', '"');
    cfg.setEscapeDisabled(false);
    cfg.setQuoteDisabled(false);
    @SuppressWarnings("resource") final CsvWriter csvWriter = new CsvWriter(writer, cfg);
    try {
        final List<String> list = new ArrayList<>();
        list.add("SuggestWord");
        list.add("Reading");
        list.add("Permissions");
        list.add("Labels");
        list.add("Boost");
        csvWriter.writeValues(list);
        elevateWordBhv.selectCursor(cb -> cb.query().matchAll(), new EntityRowHandler<ElevateWord>() {

            @Override
            public void handle(final ElevateWord entity) {
                final List<String> list = new ArrayList<>();
                final String permissions = stream(entity.getPermissions()).get(stream -> stream.map(s -> permissionHelper.decode(s)).filter(StringUtil::isNotBlank).distinct().collect(Collectors.joining(",")));
                final String labels = elevateWordToLabelBhv.selectList(cb -> cb.query().setElevateWordId_Equal(entity.getId())).stream().map(e -> labelTypeBhv.selectByPK(e.getLabelTypeId()).map(LabelType::getValue).filter(StringUtil::isNotBlank).orElse(null)).distinct().sorted().collect(Collectors.joining(","));
                addToList(list, entity.getSuggestWord());
                addToList(list, entity.getReading());
                addToList(list, permissions);
                addToList(list, labels);
                addToList(list, entity.getBoost());
                try {
                    csvWriter.writeValues(list);
                } catch (final IOException e) {
                    logger.warn("Failed to write a sugget elevate word: {}", entity, e);
                }
            }

            private void addToList(final List<String> list, final Object value) {
                if (value == null) {
                    list.add(StringUtil.EMPTY);
                } else {
                    list.add(value.toString());
                }
            }
        });
        csvWriter.flush();
    } catch (final IOException e) {
        logger.warn("Failed to write a sugget elevate word.", e);
    }
}
Also used : BeanUtil(org.codelibs.core.beans.util.BeanUtil) EntityRowHandler(org.dbflute.bhv.readable.EntityRowHandler) Constants(org.codelibs.fess.Constants) ElevateWordPager(org.codelibs.fess.app.pager.ElevateWordPager) SearchEngineClient(org.codelibs.fess.es.client.SearchEngineClient) PermissionHelper(org.codelibs.fess.helper.PermissionHelper) LabelType(org.codelibs.fess.es.config.exentity.LabelType) ArrayList(java.util.ArrayList) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) StreamUtil.split(org.codelibs.core.stream.StreamUtil.split) PagingResultBean(org.dbflute.cbean.result.PagingResultBean) LabelTypeBhv(org.codelibs.fess.es.config.exbhv.LabelTypeBhv) ElevateWordBhv(org.codelibs.fess.es.config.exbhv.ElevateWordBhv) ElevateWordCB(org.codelibs.fess.es.config.cbean.ElevateWordCB) ElevateWordToLabel(org.codelibs.fess.es.config.exentity.ElevateWordToLabel) StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) OptionalEntity(org.dbflute.optional.OptionalEntity) Resource(javax.annotation.Resource) StringUtil(org.codelibs.core.lang.StringUtil) CsvWriter(com.orangesignal.csv.CsvWriter) IOException(java.io.IOException) Reader(java.io.Reader) Collectors(java.util.stream.Collectors) CsvConfig(com.orangesignal.csv.CsvConfig) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ComponentUtil(org.codelibs.fess.util.ComponentUtil) ElevateWordToLabelBhv(org.codelibs.fess.es.config.exbhv.ElevateWordToLabelBhv) Writer(java.io.Writer) ElevateWord(org.codelibs.fess.es.config.exentity.ElevateWord) LogManager(org.apache.logging.log4j.LogManager) CsvReader(com.orangesignal.csv.CsvReader) CsvWriter(com.orangesignal.csv.CsvWriter) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ElevateWord(org.codelibs.fess.es.config.exentity.ElevateWord) PermissionHelper(org.codelibs.fess.helper.PermissionHelper) ArrayList(java.util.ArrayList) List(java.util.List) CsvConfig(com.orangesignal.csv.CsvConfig) StringUtil(org.codelibs.core.lang.StringUtil)

Example 4 with CsvConfig

use of com.orangesignal.csv.CsvConfig in project fess by codelibs.

the class CrawlingInfoService method importCsv.

public void importCsv(final Reader reader) {
    @SuppressWarnings("resource") final CsvReader csvReader = new CsvReader(reader, new CsvConfig());
    final DateFormat formatter = new SimpleDateFormat(CoreLibConstants.DATE_FORMAT_ISO_8601_EXTEND);
    try {
        List<String> list;
        // ignore header
        csvReader.readValues();
        while ((list = csvReader.readValues()) != null) {
            try {
                final String sessionId = list.get(0);
                CrawlingInfo crawlingInfo = crawlingInfoBhv.selectEntity(cb -> {
                    cb.query().setSessionId_Equal(sessionId);
                    cb.specify().columnSessionId();
                }).orElse(// TODO
                null);
                if (crawlingInfo == null) {
                    crawlingInfo = new CrawlingInfo();
                    crawlingInfo.setSessionId(list.get(0));
                    crawlingInfo.setCreatedTime(formatter.parse(list.get(1)).getTime());
                    crawlingInfoBhv.insert(crawlingInfo, op -> op.setRefreshPolicy(Constants.TRUE));
                }
                final CrawlingInfoParam entity = new CrawlingInfoParam();
                entity.setCrawlingInfoId(crawlingInfo.getId());
                entity.setKey(list.get(2));
                entity.setValue(list.get(3));
                entity.setCreatedTime(formatter.parse(list.get(4)).getTime());
                crawlingInfoParamBhv.insert(entity, op -> op.setRefreshPolicy(Constants.TRUE));
            } catch (final Exception e) {
                logger.warn("Failed to read a click log: {}", list, e);
            }
        }
    } catch (final IOException e) {
        logger.warn("Failed to read a click log.", e);
    }
}
Also used : CsvReader(com.orangesignal.csv.CsvReader) CrawlingInfo(org.codelibs.fess.es.config.exentity.CrawlingInfo) BeanUtil(org.codelibs.core.beans.util.BeanUtil) EntityRowHandler(org.dbflute.bhv.readable.EntityRowHandler) Constants(org.codelibs.fess.Constants) FessSystemException(org.codelibs.fess.exception.FessSystemException) CrawlingInfoBhv(org.codelibs.fess.es.config.exbhv.CrawlingInfoBhv) ListResultBean(org.dbflute.cbean.result.ListResultBean) LocalDateTime(java.time.LocalDateTime) SimpleDateFormat(java.text.SimpleDateFormat) ArrayList(java.util.ArrayList) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) CrawlingInfoParam(org.codelibs.fess.es.config.exentity.CrawlingInfoParam) PagingResultBean(org.dbflute.cbean.result.PagingResultBean) DateFormat(java.text.DateFormat) CrawlingInfoPager(org.codelibs.fess.app.pager.CrawlingInfoPager) OptionalEntity(org.dbflute.optional.OptionalEntity) CrawlingInfoParamBhv(org.codelibs.fess.es.config.exbhv.CrawlingInfoParamBhv) Resource(javax.annotation.Resource) StringUtil(org.codelibs.core.lang.StringUtil) CsvWriter(com.orangesignal.csv.CsvWriter) Set(java.util.Set) IOException(java.io.IOException) Reader(java.io.Reader) CoreLibConstants(org.codelibs.core.CoreLibConstants) Collectors(java.util.stream.Collectors) CrawlingInfoCB(org.codelibs.fess.es.config.cbean.CrawlingInfoCB) CsvConfig(com.orangesignal.csv.CsvConfig) List(java.util.List) Logger(org.apache.logging.log4j.Logger) CrawlingInfo(org.codelibs.fess.es.config.exentity.CrawlingInfo) ComponentUtil(org.codelibs.fess.util.ComponentUtil) DateTimeFormatter(java.time.format.DateTimeFormatter) Writer(java.io.Writer) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) CsvReader(com.orangesignal.csv.CsvReader) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) CsvConfig(com.orangesignal.csv.CsvConfig) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) CrawlingInfoParam(org.codelibs.fess.es.config.exentity.CrawlingInfoParam) FessSystemException(org.codelibs.fess.exception.FessSystemException) IOException(java.io.IOException)

Example 5 with CsvConfig

use of com.orangesignal.csv.CsvConfig in project fess by codelibs.

the class CsvDataStoreImpl method storeData.

@Override
protected void storeData(final DataConfig dataConfig, final IndexUpdateCallback callback, final Map<String, String> paramMap, final Map<String, String> scriptMap, final Map<String, Object> defaultDataMap) {
    final long readInterval = getReadInterval(paramMap);
    final List<File> csvFileList = getCsvFileList(paramMap);
    if (csvFileList.isEmpty()) {
        logger.warn("No CSV file.");
        return;
    }
    final String csvFileEncoding = getCsvFileEncoding(paramMap);
    final boolean hasHeaderLine = hasHeaderLine(paramMap);
    final CsvConfig csvConfig = buildCsvConfig(paramMap);
    for (final File csvFile : csvFileList) {
        processCsv(dataConfig, callback, paramMap, scriptMap, defaultDataMap, csvConfig, csvFile, readInterval, csvFileEncoding, hasHeaderLine);
    }
}
Also used : CsvConfig(com.orangesignal.csv.CsvConfig) File(java.io.File)

Aggregations

CsvConfig (com.orangesignal.csv.CsvConfig)9 CsvWriter (com.orangesignal.csv.CsvWriter)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 CsvReader (com.orangesignal.csv.CsvReader)5 Reader (java.io.Reader)5 Writer (java.io.Writer)5 Resource (javax.annotation.Resource)5 LogManager (org.apache.logging.log4j.LogManager)5 Logger (org.apache.logging.log4j.Logger)5 BeanUtil (org.codelibs.core.beans.util.BeanUtil)5 StringUtil (org.codelibs.core.lang.StringUtil)5 Constants (org.codelibs.fess.Constants)5 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)5 ComponentUtil (org.codelibs.fess.util.ComponentUtil)5 EntityRowHandler (org.dbflute.bhv.readable.EntityRowHandler)5 PagingResultBean (org.dbflute.cbean.result.PagingResultBean)5 OptionalEntity (org.dbflute.optional.OptionalEntity)5 Collectors (java.util.stream.Collectors)4