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);
});
}
}
}
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);
}
}
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;
});
}
Aggregations