use of org.codelibs.core.lang.StringUtil 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.core.lang.StringUtil in project fess by codelibs.
the class AdminAccesstokenAction method getAccessToken.
public static OptionalEntity<AccessToken> getAccessToken(final CreateForm form) {
final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
final String username = systemHelper.getUsername();
final long currentTime = systemHelper.getCurrentTimeAsLong();
return getEntity(form, username, currentTime).map(entity -> {
entity.setUpdatedBy(username);
entity.setUpdatedTime(currentTime);
BeanUtil.copyBeanToBean(form, entity, op -> op.exclude(Constants.COMMON_CONVERSION_RULE).exclude(TOKEN, Constants.PERMISSIONS, EXPIRED_TIME).dateConverter(Constants.DEFAULT_DATETIME_FORMAT, EXPIRES));
final PermissionHelper permissionHelper = ComponentUtil.getPermissionHelper();
entity.setPermissions(split(form.permissions, "\n").get(stream -> stream.map(s -> permissionHelper.encode(s)).filter(StringUtil::isNotBlank).distinct().toArray(n -> new String[n])));
return entity;
});
}
use of org.codelibs.core.lang.StringUtil in project fess by codelibs.
the class AdminWebconfigAction method createnew.
// ===================================================================================
// Edit Execute
// ============
// -----------------------------------------------------
// Entry Page
// ----------
@Execute
@Secured({ ROLE })
public HtmlResponse createnew() {
saveToken();
return asEditHtml().useForm(CreateForm.class, op -> {
op.setup(form -> {
form.initialize();
ComponentUtil.getCrawlingConfigHelper().getDefaultConfig(ConfigType.WEB).ifPresent(entity -> {
copyBeanToBean(entity, form, copyOp -> {
copyOp.excludeNull();
copyOp.exclude(Stream.concat(Stream.of(Constants.COMMON_CONVERSION_RULE), Stream.of(Constants.PERMISSIONS, Constants.VIRTUAL_HOSTS)).toArray(n -> new String[n]));
});
final PermissionHelper permissionHelper = ComponentUtil.getPermissionHelper();
form.permissions = stream(entity.getPermissions()).get(stream -> stream.map(s -> permissionHelper.decode(s)).filter(StringUtil::isNotBlank).distinct().collect(Collectors.joining("\n")));
form.virtualHosts = stream(entity.getVirtualHosts()).get(stream -> stream.filter(StringUtil::isNotBlank).map(String::trim).collect(Collectors.joining("\n")));
form.name = null;
});
form.crudMode = CrudMode.CREATE;
});
});
}
use of org.codelibs.core.lang.StringUtil in project fess by codelibs.
the class AdminWebconfigAction method getWebConfig.
public static OptionalEntity<WebConfig> getWebConfig(final CreateForm form) {
final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
final String username = systemHelper.getUsername();
final long currentTime = systemHelper.getCurrentTimeAsLong();
return getEntity(form, username, currentTime).map(entity -> {
entity.setUpdatedBy(username);
entity.setUpdatedTime(currentTime);
copyBeanToBean(form, entity, op -> op.exclude(Stream.concat(Stream.of(Constants.COMMON_CONVERSION_RULE), Stream.of(Constants.PERMISSIONS, Constants.VIRTUAL_HOSTS)).toArray(n -> new String[n])));
final PermissionHelper permissionHelper = ComponentUtil.getPermissionHelper();
entity.setPermissions(split(form.permissions, "\n").get(stream -> stream.map(s -> permissionHelper.encode(s)).filter(StringUtil::isNotBlank).distinct().toArray(n -> new String[n])));
entity.setVirtualHosts(split(form.virtualHosts, "\n").get(stream -> stream.filter(StringUtil::isNotBlank).distinct().map(String::trim).toArray(n -> new String[n])));
return entity;
});
}
use of org.codelibs.core.lang.StringUtil in project fess by codelibs.
the class ApiAdminAccesstokenAction method createEditBody.
protected EditBody createEditBody(final AccessToken entity) {
final EditBody body = new EditBody();
copyBeanToBean(entity, body, copyOp -> copyOp.exclude(Constants.PERMISSIONS, AdminAccesstokenAction.EXPIRED_TIME).excludeNull().dateConverter(Constants.DEFAULT_DATETIME_FORMAT, AdminAccesstokenAction.EXPIRES));
final PermissionHelper permissionHelper = ComponentUtil.getPermissionHelper();
body.permissions = stream(entity.getPermissions()).get(stream -> stream.map(permissionHelper::decode).filter(StringUtil::isNotBlank).distinct().collect(Collectors.joining("\n")));
body.crudMode = null;
return body;
}
Aggregations