use of org.codelibs.fess.app.service.FavoriteLogService in project fess by codelibs.
the class JsonApiManager method processFavoriteRequest.
protected void processFavoriteRequest(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) {
if (!ComponentUtil.getFessConfig().isUserFavorite()) {
writeJsonResponse(9, null, "Unsupported operation.");
return;
}
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final UserInfoHelper userInfoHelper = ComponentUtil.getUserInfoHelper();
final SearchHelper searchHelper = ComponentUtil.getSearchHelper();
final FavoriteLogService favoriteLogService = ComponentUtil.getComponent(FavoriteLogService.class);
final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
try {
final String docId = request.getParameter("docId");
final String queryId = request.getParameter("queryId");
final String[] docIds = userInfoHelper.getResultDocIds(URLDecoder.decode(queryId, Constants.UTF_8));
if (docIds == null) {
throw new WebApiException(6, "No searched urls.");
}
searchHelper.getDocumentByDocId(docId, new String[] { fessConfig.getIndexFieldUrl(), fessConfig.getIndexFieldLang() }, OptionalThing.empty()).ifPresent(doc -> {
final String favoriteUrl = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
final String userCode = userInfoHelper.getUserCode();
if (StringUtil.isBlank(userCode)) {
throw new WebApiException(2, "No user session.");
}
if (StringUtil.isBlank(favoriteUrl)) {
throw new WebApiException(2, "URL is null.");
}
boolean found = false;
for (final String id : docIds) {
if (docId.equals(id)) {
found = true;
break;
}
}
if (!found) {
throw new WebApiException(5, "Not found: " + favoriteUrl);
}
if (!favoriteLogService.addUrl(userCode, (userInfo, favoriteLog) -> {
favoriteLog.setUserInfoId(userInfo.getId());
favoriteLog.setUrl(favoriteUrl);
favoriteLog.setDocId(docId);
favoriteLog.setQueryId(queryId);
favoriteLog.setCreatedAt(systemHelper.getCurrentTimeAsLocalDateTime());
})) {
throw new WebApiException(4, "Failed to add url: " + favoriteUrl);
}
final String id = DocumentUtil.getValue(doc, fessConfig.getIndexFieldId(), String.class);
searchHelper.update(id, builder -> {
final Script script = ComponentUtil.getLanguageHelper().createScript(doc, "ctx._source." + fessConfig.getIndexFieldFavoriteCount() + "+=1");
builder.setScript(script);
final Map<String, Object> upsertMap = new HashMap<>();
upsertMap.put(fessConfig.getIndexFieldFavoriteCount(), 1);
builder.setUpsert(upsertMap);
builder.setRefreshPolicy(Constants.TRUE);
});
writeJsonResponse(0, "\"result\":\"ok\"", (String) null);
}).orElse(() -> {
throw new WebApiException(6, "Not found: " + docId);
});
} catch (final Exception e) {
int status;
if (e instanceof WebApiException) {
status = ((WebApiException) e).getStatusCode();
} else {
status = 1;
}
writeJsonResponse(status, null, e);
if (logger.isDebugEnabled()) {
logger.debug("Failed to process a favorite request.", e);
}
}
}
use of org.codelibs.fess.app.service.FavoriteLogService in project fess by codelibs.
the class JsonApiManager method processFavoritesRequest.
protected void processFavoritesRequest(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) {
if (!ComponentUtil.getFessConfig().isUserFavorite()) {
writeJsonResponse(9, null, "Unsupported operation.");
return;
}
final UserInfoHelper userInfoHelper = ComponentUtil.getUserInfoHelper();
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final SearchHelper searchHelper = ComponentUtil.getSearchHelper();
final FavoriteLogService favoriteLogService = ComponentUtil.getComponent(FavoriteLogService.class);
int status = 0;
String body = null;
Exception err = null;
try {
final String queryId = request.getParameter("queryId");
final String userCode = userInfoHelper.getUserCode();
if (StringUtil.isBlank(userCode)) {
throw new WebApiException(2, "No user session.");
}
if (StringUtil.isBlank(queryId)) {
throw new WebApiException(3, "Query ID is null.");
}
final String[] docIds = userInfoHelper.getResultDocIds(queryId);
final List<Map<String, Object>> docList = searchHelper.getDocumentListByDocIds(docIds, new String[] { fessConfig.getIndexFieldUrl(), fessConfig.getIndexFieldDocId(), fessConfig.getIndexFieldFavoriteCount() }, OptionalThing.empty(), SearchRequestType.JSON);
List<String> urlList = new ArrayList<>(docList.size());
for (final Map<String, Object> doc : docList) {
final String urlObj = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
if (urlObj != null) {
urlList.add(urlObj);
}
}
urlList = favoriteLogService.getUrlList(userCode, urlList);
final List<String> docIdList = new ArrayList<>(urlList.size());
for (final Map<String, Object> doc : docList) {
final String urlObj = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
if (urlObj != null && urlList.contains(urlObj)) {
final String docIdObj = DocumentUtil.getValue(doc, fessConfig.getIndexFieldDocId(), String.class);
if (docIdObj != null) {
docIdList.add(docIdObj);
}
}
}
// TODO replace response stream
final StringBuilder buf = new StringBuilder(255);
buf.append("\"num\":").append(docIdList.size());
buf.append(", \"doc_ids\":[");
if (!docIdList.isEmpty()) {
for (int i = 0; i < docIdList.size(); i++) {
if (i > 0) {
buf.append(',');
}
buf.append(escapeJson(docIdList.get(i)));
}
}
buf.append(']');
body = buf.toString();
} catch (final Exception e) {
if (e instanceof WebApiException) {
status = ((WebApiException) e).getStatusCode();
} else {
status = 1;
}
err = e;
if (logger.isDebugEnabled()) {
logger.debug("Failed to process a favorites request.", e);
}
}
writeJsonResponse(status, body, err);
}
Aggregations