use of com.orangesignal.csv.CsvConfig in project fess by codelibs.
the class BadWordService method exportCsv.
public void exportCsv(final Writer writer) {
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("BadWord");
csvWriter.writeValues(list);
badWordBhv.selectCursor(cb -> cb.query().matchAll(), new EntityRowHandler<BadWord>() {
@Override
public void handle(final BadWord entity) {
final List<String> list = new ArrayList<>();
addToList(list, entity.getSuggestWord());
try {
csvWriter.writeValues(list);
} catch (final IOException e) {
logger.warn("Failed to write a sugget bad 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 bad word.", e);
}
}
use of com.orangesignal.csv.CsvConfig in project fess by codelibs.
the class BadWordService method importCsv.
public void importCsv(final Reader reader) {
@SuppressWarnings("resource") final CsvReader csvReader = new CsvReader(reader, new CsvConfig());
try {
List<String> list;
// ignore header
csvReader.readValues();
while ((list = csvReader.readValues()) != null) {
String targetWord = getValue(list, 0);
if (StringUtil.isBlank(targetWord)) {
// skip
continue;
}
try {
boolean isDelete = false;
if (targetWord.startsWith(DELETE_PREFIX)) {
isDelete = true;
targetWord = targetWord.substring(2);
}
final String target = targetWord;
// TODO
BadWord badWord = badWordBhv.selectEntity(cb -> cb.query().setSuggestWord_Equal(target)).orElse(null);
final long now = ComponentUtil.getSystemHelper().getCurrentTimeAsLong();
if (isDelete) {
badWordBhv.delete(badWord);
} else if (badWord == null) {
badWord = new BadWord();
badWord.setSuggestWord(targetWord);
badWord.setCreatedBy(Constants.SYSTEM_USER);
badWord.setCreatedTime(now);
badWordBhv.insert(badWord);
} else {
badWord.setUpdatedBy(Constants.SYSTEM_USER);
badWord.setUpdatedTime(now);
badWordBhv.update(badWord);
}
} catch (final Exception e) {
logger.warn("Failed to read a sugget elevate word: {}", list, e);
}
}
// TODO replace _all
searchEngineClient.refresh("_all");
} catch (final IOException e) {
logger.warn("Failed to read a sugget elevate word.", e);
}
}
use of com.orangesignal.csv.CsvConfig in project fess by codelibs.
the class CrawlingInfoService method exportCsv.
public void exportCsv(final Writer writer) {
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("SessionId");
list.add("SessionCreatedTime");
list.add("Key");
list.add("Value");
list.add("CreatedTime");
csvWriter.writeValues(list);
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(CoreLibConstants.DATE_FORMAT_ISO_8601_EXTEND);
crawlingInfoParamBhv.selectCursor(cb -> cb.query().matchAll(), new EntityRowHandler<CrawlingInfoParam>() {
@Override
public void handle(final CrawlingInfoParam entity) {
final List<String> list = new ArrayList<>();
entity.getCrawlingInfo().ifPresent(crawlingInfo -> {
addToList(list, crawlingInfo.getSessionId());
addToList(list, crawlingInfo.getCreatedTime());
});
// TODO
if (!entity.getCrawlingInfo().isPresent()) {
addToList(list, "");
addToList(list, "");
}
addToList(list, entity.getKey());
addToList(list, entity.getValue());
addToList(list, entity.getCreatedTime());
try {
csvWriter.writeValues(list);
} catch (final IOException e) {
logger.warn("Failed to write a crawling session info: {}", entity, e);
}
}
private void addToList(final List<String> list, final Object value) {
if (value == null) {
list.add(StringUtil.EMPTY);
} else if (value instanceof LocalDateTime) {
list.add(((LocalDateTime) value).format(formatter));
} else {
list.add(value.toString());
}
}
});
csvWriter.flush();
} catch (final IOException e) {
logger.warn("Failed to write a crawling session info.", e);
}
}
use of com.orangesignal.csv.CsvConfig 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);
}
}
Aggregations