use of org.codelibs.fess.es.config.exentity.ElevateWord 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("Label");
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(",")));
addToList(list, entity.getSuggestWord());
addToList(list, entity.getReading());
addToList(list, permissions);
addToList(list, entity.getTargetLabel());
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);
}
}
use of org.codelibs.fess.es.config.exentity.ElevateWord in project fess by codelibs.
the class ApiAdminElevatewordAction method post$setting.
// POST /api/admin/elevateword/setting
@Execute
public JsonResponse<ApiResult> post$setting(final EditBody body) {
validateApi(body, messages -> {
});
body.crudMode = CrudMode.EDIT;
final ElevateWord entity = getElevateWord(body).orElseGet(() -> {
throwValidationErrorApi(messages -> {
messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id);
});
return null;
});
try {
elevateWordService.store(entity);
suggestHelper.deleteAllElevateWord();
suggestHelper.storeAllElevateWords();
} catch (final Exception e) {
throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
}
return asJson(new ApiResult.ApiUpdateResponse().id(entity.getId()).created(false).status(ApiResult.Status.OK).result());
}
use of org.codelibs.fess.es.config.exentity.ElevateWord in project fess by codelibs.
the class ApiAdminElevatewordAction method createEditBody.
protected EditBody createEditBody(final ElevateWord entity) {
final EditBody body = new EditBody();
body.id = entity.getId();
body.versionNo = entity.getVersionNo();
body.createdBy = entity.getCreatedBy();
body.createdTime = entity.getCreatedTime();
body.suggestWord = entity.getSuggestWord();
body.updatedBy = entity.getUpdatedBy();
body.updatedTime = entity.getUpdatedTime();
body.labelTypeIds = entity.getLabelTypeIds();
final PermissionHelper permissionHelper = ComponentUtil.getPermissionHelper();
body.permissions = stream(entity.getPermissions()).get(stream -> stream.map(s -> permissionHelper.decode(s)).filter(StringUtil::isNotBlank).distinct().collect(Collectors.joining("\n")));
body.targetLabel = entity.getTargetLabel();
body.reading = entity.getReading();
return body;
}
use of org.codelibs.fess.es.config.exentity.ElevateWord in project fess by codelibs.
the class SuggestHelper method deleteAllElevateWord.
public void deleteAllElevateWord() {
final List<ElevateWord> list = elevateWordBhv.selectList(cb -> {
cb.query().matchAll();
cb.fetchFirst(ComponentUtil.getFessConfig().getPageElevateWordMaxFetchSizeAsInteger());
});
for (final ElevateWord elevateWord : list) {
suggester.indexer().deleteElevateWord(elevateWord.getSuggestWord());
}
suggester.refresh();
}
use of org.codelibs.fess.es.config.exentity.ElevateWord 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 label = getValue(list, 3);
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())));
}
if (StringUtil.isNotBlank(label)) {
cb.query().setTargetLabel_Equal(label);
}
}).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.setTargetLabel(label);
elevateWord.setBoost(StringUtil.isBlank(boost) ? 1.0f : Float.parseFloat(boost));
elevateWord.setCreatedBy(Constants.SYSTEM_USER);
elevateWord.setCreatedTime(now);
elevateWordBhv.insert(elevateWord);
} else if (StringUtil.isBlank(reading) && StringUtil.isBlank(boost)) {
elevateWordBhv.delete(elevateWord);
} 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);
}
} 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);
}
}
Aggregations