Search in sources :

Example 1 with FavoriteLogService

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);
        }
    }
}
Also used : Arrays(java.util.Arrays) FilterChain(javax.servlet.FilterChain) Constants(org.codelibs.fess.Constants) DocumentUtil(org.codelibs.fess.util.DocumentUtil) IORuntimeException(org.codelibs.core.exception.IORuntimeException) UserInfoHelper(org.codelibs.fess.helper.UserInfoHelper) URLDecoder(java.net.URLDecoder) FavoriteLogService(org.codelibs.fess.app.service.FavoriteLogService) ServletException(javax.servlet.ServletException) OptionalThing(org.dbflute.optional.OptionalThing) HashMap(java.util.HashMap) SearchEngineClient(org.codelibs.fess.es.client.SearchEngineClient) WebApiException(org.codelibs.fess.exception.WebApiException) RelatedQueryHelper(org.codelibs.fess.helper.RelatedQueryHelper) ArrayList(java.util.ArrayList) GeoInfo(org.codelibs.fess.entity.GeoInfo) HttpServletRequest(javax.servlet.http.HttpServletRequest) SearchRequestType(org.codelibs.fess.entity.SearchRequestParams.SearchRequestType) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) PingResponse(org.codelibs.fess.entity.PingResponse) SearchRequestParams(org.codelibs.fess.entity.SearchRequestParams) Locale(java.util.Locale) PopularWordHelper(org.codelibs.fess.helper.PopularWordHelper) Map(java.util.Map) FacetInfo(org.codelibs.fess.entity.FacetInfo) SearchHelper(org.codelibs.fess.helper.SearchHelper) SearchRenderData(org.codelibs.fess.entity.SearchRenderData) Field(org.codelibs.fess.util.FacetResponse.Field) Script(org.opensearch.script.Script) HttpServletResponse(javax.servlet.http.HttpServletResponse) StringUtil(org.codelibs.core.lang.StringUtil) RelatedContentHelper(org.codelibs.fess.helper.RelatedContentHelper) IOException(java.io.IOException) FacetResponse(org.codelibs.fess.util.FacetResponse) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ComponentUtil(org.codelibs.fess.util.ComponentUtil) SystemHelper(org.codelibs.fess.helper.SystemHelper) LabelTypeHelper(org.codelibs.fess.helper.LabelTypeHelper) PostConstruct(javax.annotation.PostConstruct) HighlightInfo(org.codelibs.fess.entity.HighlightInfo) LogManager(org.apache.logging.log4j.LogManager) BaseJsonApiManager(org.codelibs.fess.api.BaseJsonApiManager) Script(org.opensearch.script.Script) FavoriteLogService(org.codelibs.fess.app.service.FavoriteLogService) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) SearchHelper(org.codelibs.fess.helper.SearchHelper) WebApiException(org.codelibs.fess.exception.WebApiException) IORuntimeException(org.codelibs.core.exception.IORuntimeException) ServletException(javax.servlet.ServletException) WebApiException(org.codelibs.fess.exception.WebApiException) IOException(java.io.IOException) SystemHelper(org.codelibs.fess.helper.SystemHelper) UserInfoHelper(org.codelibs.fess.helper.UserInfoHelper) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with FavoriteLogService

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);
}
Also used : FavoriteLogService(org.codelibs.fess.app.service.FavoriteLogService) ArrayList(java.util.ArrayList) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) SearchHelper(org.codelibs.fess.helper.SearchHelper) WebApiException(org.codelibs.fess.exception.WebApiException) IORuntimeException(org.codelibs.core.exception.IORuntimeException) ServletException(javax.servlet.ServletException) WebApiException(org.codelibs.fess.exception.WebApiException) IOException(java.io.IOException) UserInfoHelper(org.codelibs.fess.helper.UserInfoHelper) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ServletException (javax.servlet.ServletException)2 IORuntimeException (org.codelibs.core.exception.IORuntimeException)2 FavoriteLogService (org.codelibs.fess.app.service.FavoriteLogService)2 WebApiException (org.codelibs.fess.exception.WebApiException)2 SearchHelper (org.codelibs.fess.helper.SearchHelper)2 UserInfoHelper (org.codelibs.fess.helper.UserInfoHelper)2 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)2 URLDecoder (java.net.URLDecoder)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Locale (java.util.Locale)1 PostConstruct (javax.annotation.PostConstruct)1 FilterChain (javax.servlet.FilterChain)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 LogManager (org.apache.logging.log4j.LogManager)1