Search in sources :

Example 6 with PratilipiFilter

use of com.pratilipi.common.util.PratilipiFilter in project pratilipi by Pratilipi.

the class PratilipiSite method createDataModelForSearchPage.

private Map<String, Object> createDataModelForSearchPage(boolean basicMode, Language language, HttpServletRequest request) throws InsufficientAccessException, UnexpectedServerException {
    String searchQuery = request.getParameter(RequestParameter.SEARCH_QUERY.getName());
    if (searchQuery == null || searchQuery.trim().isEmpty())
        searchQuery = null;
    Long authorId = null;
    String authorIdString = request.getParameter(RequestParameter.AUTHOR_ID.getName());
    if (authorIdString != null && !authorIdString.trim().isEmpty())
        authorId = Long.parseLong(authorIdString);
    PratilipiState pratilipiState = PratilipiState.PUBLISHED;
    String state = request.getParameter(RequestParameter.PRATILIPI_STATE.getName());
    if (state != null && !state.trim().isEmpty())
        pratilipiState = PratilipiState.valueOf(state);
    Integer resultCount = 20;
    String resultCountString = request.getParameter(RequestParameter.RESULT_COUNT.getName());
    if (resultCountString != null && !resultCountString.trim().isEmpty())
        resultCount = Integer.parseInt(resultCountString);
    Integer pageCurr = 1;
    String pageNoString = request.getParameter(RequestParameter.LIST_PAGE_NUMBER.getName());
    if (pageNoString != null && !pageNoString.trim().isEmpty())
        pageCurr = Integer.parseInt(pageNoString);
    PratilipiListV2Api.GetRequest PratilipiListV1ApiRequest = new PratilipiListV2Api.GetRequest();
    PratilipiListV1ApiRequest.setLanguage(language);
    PratilipiListV1ApiRequest.setState(pratilipiState);
    PratilipiListV1ApiRequest.setResultCount(resultCount);
    PratilipiListV1ApiRequest.setOffset((pageCurr - 1) * resultCount);
    if (searchQuery != null)
        PratilipiListV1ApiRequest.setSearchQuery(searchQuery);
    if (authorId != null)
        PratilipiListV1ApiRequest.setAuthorId(authorId);
    PratilipiListV2Api.Response response = ApiRegistry.getApi(PratilipiListV2Api.class).get(PratilipiListV1ApiRequest);
    PratilipiFilter pratilipiFilter = new PratilipiFilter();
    pratilipiFilter.setLanguage(language);
    pratilipiFilter.setState(pratilipiState);
    Gson gson = new Gson();
    Map<String, Object> dataModel = new HashMap<String, Object>();
    dataModel.put("title", SEOTitleUtil.getSearchPageTitle(language));
    if (basicMode) {
        dataModel.put("pratilipiList", response.getPratilipiList());
        dataModel.put("pratilipiListSearchQuery", searchQuery);
        dataModel.put("pratilipiListPageCurr", pageCurr);
        Integer pageMax = response.getNumberFound() != null ? (int) Math.ceil(((double) response.getNumberFound()) / resultCount) : 1;
        dataModel.put("pratilipiListPageMax", pageMax);
    } else {
        dataModel.put("pratilipiListJson", gson.toJson(response.getPratilipiList()));
        dataModel.put("pratilipiListSearchQuery", searchQuery);
        dataModel.put("pratilipiListFilterJson", gson.toJson(pratilipiFilter));
        dataModel.put("pratilipiListCursor", response.getCursor());
    }
    return dataModel;
}
Also used : PratilipiListV2Api(com.pratilipi.api.impl.pratilipi.PratilipiListV2Api) HashMap(java.util.HashMap) PratilipiState(com.pratilipi.common.type.PratilipiState) Gson(com.google.gson.Gson) PratilipiFilter(com.pratilipi.common.util.PratilipiFilter) JsonObject(com.google.gson.JsonObject)

Example 7 with PratilipiFilter

use of com.pratilipi.common.util.PratilipiFilter in project pratilipi by Pratilipi.

the class PratilipiDataUtil method updatePratilipiSearchIndex.

public static void updatePratilipiSearchIndex(Long pratilipiId, Long authorId) throws InvalidArgumentException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    List<Pratilipi> pratilipiList = null;
    if (authorId != null) {
        PratilipiFilter pratilipiFilter = new PratilipiFilter();
        pratilipiFilter.setAuthorId(authorId);
        pratilipiList = dataAccessor.getPratilipiList(pratilipiFilter, null, null).getDataList();
    } else if (pratilipiId != null) {
        pratilipiList = new ArrayList<>(1);
        pratilipiList.add(dataAccessor.getPratilipi(pratilipiId));
    } else {
        logger.log(Level.SEVERE, "Neither AuthorId, nor PratilipiId is provided !");
        throw new InvalidArgumentException("Neither AuthorId, nor PratilipiId is provided !");
    }
    _updatePratilipiSearchIndex(pratilipiList);
}
Also used : PratilipiFilter(com.pratilipi.common.util.PratilipiFilter) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi)

Example 8 with PratilipiFilter

use of com.pratilipi.common.util.PratilipiFilter in project pratilipi by Pratilipi.

the class AuthorProcessApi method _validateAuthorData.

private void _validateAuthorData(Long authorId) throws InvalidArgumentException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Author author = dataAccessor.getAuthor(authorId);
    Page page = dataAccessor.getPage(PageType.AUTHOR, authorId);
    // DELETED Author can not have a Page entity linked.
    if (author.getState() == AuthorState.DELETED && page != null)
        throw new InvalidArgumentException("DELETED Author has a non-deleted Page entity.");
    // Non-DELETED Author must have a page entity linked.
    if (author.getState() != AuthorState.DELETED && page == null)
        throw new InvalidArgumentException("Page entity is missing for the Author.");
    // Pratilipi Entities linked with the Author.
    PratilipiFilter pratilipiFilter = new PratilipiFilter();
    pratilipiFilter.setAuthorId(authorId);
    pratilipiFilter.setState(PratilipiState.PUBLISHED);
    List<Pratilipi> pratilipiList = dataAccessor.getPratilipiList(pratilipiFilter, null, null).getDataList();
    // DELETED Author cannot have non-DELETED Pratilipi entities linked.
    if (author.getState() == AuthorState.DELETED) {
        if (pratilipiList.size() != 0)
            throw new InvalidArgumentException("DELETED Author has " + pratilipiList.size() + " non-deleted Pratilipi Entities linked.");
        return;
    }
    // Author having Pratilipi entites linked, can not have his/her language set to null.
    if (author.getLanguage() == null && pratilipiList.size() != 0)
        throw new InvalidArgumentException("Author has " + pratilipiList.size() + " non-deleted Pratilipi Entities linked but his/her language is not set.");
    // Count of Pratilipi Entities in each language.
    Map<Language, Integer> langCount = new HashMap<>();
    for (Pratilipi pratilipi : pratilipiList) {
        if (pratilipi.getState() != PratilipiState.PUBLISHED)
            continue;
        Integer count = langCount.get(pratilipi.getLanguage());
        count = count == null ? 1 : count++;
        langCount.put(pratilipi.getLanguage(), count);
    }
    // Author, having Pratilipi entities in just one language, must have the same set as his/her profile language.
    if (langCount.keySet().size() == 1) {
        Language language = langCount.keySet().iterator().next();
        if (langCount.get(language) > 1 && author.getLanguage() != language)
            throw new InvalidArgumentException("Author has " + author.getLanguage() + " as his/her profile language but all his/her content pieces are in " + language + ".");
    }
    // At least one of four name fields must be set.
    if (author.getFirstName() == null && author.getLastName() == null && author.getFirstNameEn() == null && author.getLastNameEn() == null)
        throw new InvalidArgumentException("Author name is missing.");
    // Name fields must not have empty string.
    if ((author.getFirstName() != null && author.getFirstName().trim().isEmpty()) || (author.getLastName() != null && author.getLastName().trim().isEmpty()) || (author.getFirstNameEn() != null && author.getFirstNameEn().trim().isEmpty()) || (author.getLastNameEn() != null && author.getLastNameEn().trim().isEmpty()))
        throw new InvalidArgumentException("Author name has empty string.");
}
Also used : PratilipiFilter(com.pratilipi.common.util.PratilipiFilter) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) Language(com.pratilipi.common.type.Language) HashMap(java.util.HashMap) DataAccessor(com.pratilipi.data.DataAccessor) Author(com.pratilipi.data.type.Author) Page(com.pratilipi.data.type.Page) Pratilipi(com.pratilipi.data.type.Pratilipi)

Example 9 with PratilipiFilter

use of com.pratilipi.common.util.PratilipiFilter in project pratilipi by Pratilipi.

the class InitV1Api method get.

@Get
public Response get(GetRequest request) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    List<Response.Section> sectionList = new LinkedList<>();
    List<Long> pratilipiIdMasterList = new LinkedList<>();
    for (String listName : dataAccessor.getHomeSectionList(request.language)) {
        String title = dataAccessor.getPratilipiListTitle(listName, request.language);
        if (title == null)
            continue;
        if (title.indexOf('|') != -1)
            title = title.substring(0, title.indexOf('|')).trim();
        PratilipiFilter pratilipiFilter = new PratilipiFilter();
        pratilipiFilter.setLanguage(request.language);
        pratilipiFilter.setListName(listName);
        pratilipiFilter.setState(PratilipiState.PUBLISHED);
        List<Long> pratilipiIdList = dataAccessor.getPratilipiIdList(pratilipiFilter, null, null, 6).getDataList();
        if (pratilipiIdList.size() < 6)
            continue;
        sectionList.add(new Response.Section(title, "/" + listName));
        pratilipiIdMasterList.addAll(pratilipiIdList);
    }
    List<PratilipiData> pratilipiDataMasterList = PratilipiDataUtil.createPratilipiDataList(pratilipiIdMasterList, true);
    if (UxModeFilter.isAndroidApp()) {
        DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
        for (PratilipiData pratilipiData : pratilipiDataMasterList) {
            PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(pratilipiData.getId());
            if (pcDoc == null)
                continue;
            pratilipiData.setIndex(pcDoc.getIndex());
        }
    }
    for (int i = 0; i < sectionList.size(); i++) sectionList.get(i).setPratilipiList(pratilipiDataMasterList.subList(i * 6, i * 6 + 6));
    return new Response(sectionList);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) LinkedList(java.util.LinkedList) GenericResponse(com.pratilipi.api.shared.GenericResponse) PratilipiFilter(com.pratilipi.common.util.PratilipiFilter) PratilipiData(com.pratilipi.data.client.PratilipiData) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc) Get(com.pratilipi.api.annotation.Get)

Example 10 with PratilipiFilter

use of com.pratilipi.common.util.PratilipiFilter in project pratilipi by Pratilipi.

the class InitV2Api method get.

@Get
public Response get(GetRequest request) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    List<Response.Section> sectionList = new LinkedList<>();
    List<Long> pratilipiIdMasterList = new LinkedList<>();
    for (String listName : dataAccessor.getHomeSectionList(request.language)) {
        String title = dataAccessor.getPratilipiListTitle(listName, request.language);
        if (title == null)
            continue;
        if (title.indexOf('|') != -1)
            title = title.substring(0, title.indexOf('|')).trim();
        PratilipiFilter pratilipiFilter = new PratilipiFilter();
        pratilipiFilter.setLanguage(request.language);
        pratilipiFilter.setListName(listName);
        pratilipiFilter.setState(PratilipiState.PUBLISHED);
        List<Long> pratilipiIdList = dataAccessor.getPratilipiIdList(pratilipiFilter, null, null, 6).getDataList();
        if (pratilipiIdList.size() < 6)
            continue;
        sectionList.add(new Response.Section(title, "/" + listName));
        pratilipiIdMasterList.addAll(pratilipiIdList);
    }
    List<PratilipiData> pratilipiDataMasterList = PratilipiDataUtil.createPratilipiDataList(pratilipiIdMasterList, true);
    for (int i = 0; i < sectionList.size(); i++) sectionList.get(i).setPratilipiList(pratilipiDataMasterList.subList(i * 6, i * 6 + 6));
    return new Response(sectionList);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) LinkedList(java.util.LinkedList) PratilipiFilter(com.pratilipi.common.util.PratilipiFilter) PratilipiData(com.pratilipi.data.client.PratilipiData) Get(com.pratilipi.api.annotation.Get)

Aggregations

PratilipiFilter (com.pratilipi.common.util.PratilipiFilter)11 DataAccessor (com.pratilipi.data.DataAccessor)7 Get (com.pratilipi.api.annotation.Get)6 GenericResponse (com.pratilipi.api.shared.GenericResponse)4 PratilipiData (com.pratilipi.data.client.PratilipiData)4 Pratilipi (com.pratilipi.data.type.Pratilipi)4 HashMap (java.util.HashMap)4 Gson (com.google.gson.Gson)2 JsonObject (com.google.gson.JsonObject)2 PratilipiListV2Api (com.pratilipi.api.impl.pratilipi.PratilipiListV2Api)2 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)2 DocAccessor (com.pratilipi.data.DocAccessor)2 Author (com.pratilipi.data.type.Author)2 Event (com.pratilipi.data.type.Event)2 PratilipiContentDoc (com.pratilipi.data.type.PratilipiContentDoc)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 LinkedList (java.util.LinkedList)2 Language (com.pratilipi.common.type.Language)1 PratilipiState (com.pratilipi.common.type.PratilipiState)1