Search in sources :

Example 1 with ElevateWordToLabel

use of org.codelibs.fess.es.config.exentity.ElevateWordToLabel in project fess by codelibs.

the class ElevateWordService method store.

public void store(final ElevateWord elevateWord) {
    final boolean isNew = elevateWord.getId() == null;
    final String[] labelTypeIds = elevateWord.getLabelTypeIds();
    elevateWordBhv.insertOrUpdate(elevateWord, op -> {
        op.setRefreshPolicy(Constants.TRUE);
    });
    final String elevateWordId = elevateWord.getId();
    if (labelTypeIds != null) {
        if (isNew) {
            final List<ElevateWordToLabel> wctltmList = new ArrayList<>();
            for (final String id : labelTypeIds) {
                final ElevateWordToLabel mapping = new ElevateWordToLabel();
                mapping.setElevateWordId(elevateWordId);
                mapping.setLabelTypeId(id);
                wctltmList.add(mapping);
            }
            elevateWordToLabelBhv.batchInsert(wctltmList, op -> {
                op.setRefreshPolicy(Constants.TRUE);
            });
        } else {
            final List<ElevateWordToLabel> list = elevateWordToLabelBhv.selectList(wctltmCb -> {
                wctltmCb.query().setElevateWordId_Equal(elevateWordId);
                wctltmCb.fetchFirst(fessConfig.getPageLabeltypeMaxFetchSizeAsInteger());
            });
            final List<ElevateWordToLabel> newList = new ArrayList<>();
            final List<ElevateWordToLabel> matchedList = new ArrayList<>();
            for (final String id : labelTypeIds) {
                boolean exist = false;
                for (final ElevateWordToLabel mapping : list) {
                    if (mapping.getLabelTypeId().equals(id)) {
                        exist = true;
                        matchedList.add(mapping);
                        break;
                    }
                }
                if (!exist) {
                    // new
                    final ElevateWordToLabel mapping = new ElevateWordToLabel();
                    mapping.setElevateWordId(elevateWordId);
                    mapping.setLabelTypeId(id);
                    newList.add(mapping);
                }
            }
            list.removeAll(matchedList);
            elevateWordToLabelBhv.batchInsert(newList, op -> {
                op.setRefreshPolicy(Constants.TRUE);
            });
            elevateWordToLabelBhv.batchDelete(list, op -> {
                op.setRefreshPolicy(Constants.TRUE);
            });
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ElevateWordToLabel(org.codelibs.fess.es.config.exentity.ElevateWordToLabel)

Example 2 with ElevateWordToLabel

use of org.codelibs.fess.es.config.exentity.ElevateWordToLabel in project fess by codelibs.

the class ElevateWordService method importCsv.

public void importCsv(final Reader reader) {
    final PermissionHelper permissionHelper = ComponentUtil.getPermissionHelper();
    final CsvConfig cfg = new CsvConfig(',', '"', '"');
    cfg.setEscapeDisabled(false);
    cfg.setQuoteDisabled(false);
    @SuppressWarnings("resource") final CsvReader csvReader = new CsvReader(reader, cfg);
    try {
        List<String> list;
        // ignore header
        csvReader.readValues();
        while ((list = csvReader.readValues()) != null) {
            final String suggestWord = getValue(list, 0);
            if (StringUtil.isBlank(suggestWord)) {
                // skip
                continue;
            }
            try {
                final String[] permissions = split(getValue(list, 2), ",").get(stream -> stream.map(permissionHelper::encode).filter(StringUtil::isNotBlank).distinct().toArray(n -> new String[n]));
                final String[] labels = split(getValue(list, 3), ",").get(stream -> stream.filter(StringUtil::isNotBlank).distinct().toArray(n -> new String[n]));
                ElevateWord elevateWord = elevateWordBhv.selectEntity(cb -> {
                    cb.query().setSuggestWord_Equal(suggestWord);
                    if (permissions.length > 0) {
                        cb.query().setPermissions_InScope(stream(permissions).get(stream -> stream.collect(Collectors.toList())));
                    }
                }).orElse(null);
                final String reading = getValue(list, 1);
                final String boost = getValue(list, 4);
                final long now = ComponentUtil.getSystemHelper().getCurrentTimeAsLong();
                if (elevateWord == null) {
                    elevateWord = new ElevateWord();
                    elevateWord.setSuggestWord(suggestWord);
                    elevateWord.setReading(reading);
                    elevateWord.setPermissions(permissions);
                    elevateWord.setBoost(StringUtil.isBlank(boost) ? 1.0f : Float.parseFloat(boost));
                    elevateWord.setCreatedBy(Constants.SYSTEM_USER);
                    elevateWord.setCreatedTime(now);
                    elevateWordBhv.insert(elevateWord);
                    final String id = elevateWord.getId();
                    final List<ElevateWordToLabel> mappingList = stream(labels).get(stream -> stream.map(l -> labelTypeBhv.selectEntity(cb -> cb.query().setValue_Equal(l)).map(e -> {
                        final ElevateWordToLabel m = new ElevateWordToLabel();
                        m.setElevateWordId(id);
                        m.setLabelTypeId(e.getId());
                        return m;
                    }).orElse(null)).filter(e -> e != null).collect(Collectors.toList()));
                    if (!mappingList.isEmpty()) {
                        elevateWordToLabelBhv.batchInsert(mappingList);
                    }
                } else if (StringUtil.isBlank(reading) && StringUtil.isBlank(boost)) {
                    elevateWordBhv.delete(elevateWord);
                    final String id = elevateWord.getId();
                    elevateWordToLabelBhv.queryDelete(cb -> cb.query().setElevateWordId_Equal(id));
                } else {
                    elevateWord.setReading(reading);
                    elevateWord.setPermissions(permissions);
                    elevateWord.setBoost(StringUtil.isBlank(boost) ? 1.0f : Float.parseFloat(boost));
                    elevateWord.setUpdatedBy(Constants.SYSTEM_USER);
                    elevateWord.setUpdatedTime(now);
                    elevateWordBhv.update(elevateWord);
                    final String id = elevateWord.getId();
                    final List<ElevateWordToLabel> mappingList = stream(labels).get(stream -> stream.map(l -> labelTypeBhv.selectEntity(cb -> cb.query().setValue_Equal(l)).map(e -> {
                        final List<ElevateWordToLabel> mList = elevateWordToLabelBhv.selectList(cb -> {
                            cb.query().setElevateWordId_Equal(id);
                            cb.query().setLabelTypeId_Equal(e.getId());
                        });
                        if (!mList.isEmpty()) {
                            return null;
                        }
                        final ElevateWordToLabel m = new ElevateWordToLabel();
                        m.setElevateWordId(id);
                        m.setLabelTypeId(e.getId());
                        return m;
                    }).orElse(null)).filter(e -> e != null).collect(Collectors.toList()));
                    if (!mappingList.isEmpty()) {
                        elevateWordToLabelBhv.batchInsert(mappingList);
                    }
                }
            } catch (final Exception e) {
                logger.warn("Failed to read a sugget elevate word: {}", list, e);
            }
        }
        elevateWordBhv.refresh();
    } catch (final IOException e) {
        logger.warn("Failed to read 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) IOException(java.io.IOException) ElevateWordToLabel(org.codelibs.fess.es.config.exentity.ElevateWordToLabel) IOException(java.io.IOException) CsvReader(com.orangesignal.csv.CsvReader) 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)

Example 3 with ElevateWordToLabel

use of org.codelibs.fess.es.config.exentity.ElevateWordToLabel in project fess by codelibs.

the class ElevateWordService method getElevateWord.

public OptionalEntity<ElevateWord> getElevateWord(final String id) {
    return elevateWordBhv.selectByPK(id).map(entity -> {
        final List<ElevateWordToLabel> wctltmList = elevateWordToLabelBhv.selectList(wctltmCb -> {
            wctltmCb.query().setElevateWordId_Equal(entity.getId());
            wctltmCb.fetchFirst(fessConfig.getPageLabeltypeMaxFetchSizeAsInteger());
        });
        if (!wctltmList.isEmpty()) {
            final List<String> labelTypeIds = new ArrayList<>(wctltmList.size());
            for (final ElevateWordToLabel mapping : wctltmList) {
                labelTypeIds.add(mapping.getLabelTypeId());
            }
            entity.setLabelTypeIds(labelTypeIds.toArray(new String[labelTypeIds.size()]));
        }
        return entity;
    });
}
Also used : ArrayList(java.util.ArrayList) ElevateWordToLabel(org.codelibs.fess.es.config.exentity.ElevateWordToLabel)

Aggregations

ArrayList (java.util.ArrayList)3 ElevateWordToLabel (org.codelibs.fess.es.config.exentity.ElevateWordToLabel)3 CsvConfig (com.orangesignal.csv.CsvConfig)1 CsvReader (com.orangesignal.csv.CsvReader)1 CsvWriter (com.orangesignal.csv.CsvWriter)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 Writer (java.io.Writer)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Resource (javax.annotation.Resource)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 BeanUtil (org.codelibs.core.beans.util.BeanUtil)1 StringUtil (org.codelibs.core.lang.StringUtil)1 StreamUtil.split (org.codelibs.core.stream.StreamUtil.split)1 StreamUtil.stream (org.codelibs.core.stream.StreamUtil.stream)1 Constants (org.codelibs.fess.Constants)1 ElevateWordPager (org.codelibs.fess.app.pager.ElevateWordPager)1 SearchEngineClient (org.codelibs.fess.es.client.SearchEngineClient)1