Search in sources :

Example 16 with PratilipiData

use of com.pratilipi.data.client.PratilipiData in project pratilipi by Pratilipi.

the class NotificationDataUtil method createNotificationDataList.

public static List<NotificationData> createNotificationDataList(List<Notification> notificationList, Language language, boolean plainText) throws UnexpectedServerException {
    // Pre-fetching required User and Pratilipi Entities
    List<Long> userIdList = new LinkedList<>();
    List<Long> pratilipiIdList = new LinkedList<>();
    for (Notification notification : notificationList) {
        userIdList.add(notification.getUserId());
        if (notification.getType() == NotificationType.PRATILIPI) {
            pratilipiIdList.add(notification.getSourceIdLong());
        } else if (notification.getType() == NotificationType.PRATILIPI_PUBLISHED_FOLLOWER) {
            pratilipiIdList.add(notification.getSourceIdLong());
        } else if (notification.getType() == NotificationType.AUTHOR_FOLLOW) {
            if (notification.getDataIds().size() <= 3)
                userIdList.addAll(notification.getDataIds());
            else
                userIdList.addAll(notification.getDataIds().subList(notification.getDataIds().size() - 3, notification.getDataIds().size()));
        }
    }
    Map<Long, UserData> users = UserDataUtil.createUserDataList(userIdList, true);
    List<PratilipiData> pratilipiDataList = PratilipiDataUtil.createPratilipiDataList(pratilipiIdList, true);
    Map<Long, PratilipiData> pratilipis = new HashMap<>(pratilipiDataList.size());
    for (PratilipiData pratilipiData : pratilipiDataList) pratilipis.put(pratilipiData.getId(), pratilipiData);
    // Creating Notification Data list
    List<NotificationData> notificationDataList = new ArrayList<>(notificationList.size());
    for (Notification notification : notificationList) {
        NotificationData notificationData = new NotificationData(notification.getId());
        notificationData.setUserId(notification.getUserId());
        Language notificationLanguage = language == null ? users.get(notification.getUserId()).getLanguage() : language;
        if (notificationLanguage == null)
            notificationLanguage = Language.ENGLISH;
        if (notification.getType() == NotificationType.PRATILIPI) {
            String createdBy = notification.getCreatedBy();
            if (createdBy.startsWith("BATCH_PROCESS::")) {
                Long batchProcessId = Long.parseLong(createdBy.substring(15));
                BatchProcess batchProcess = DataAccessorFactory.getDataAccessor().getBatchProcess(batchProcessId);
                JsonObject execDoc = new Gson().fromJson(batchProcess.getExecDoc(), JsonElement.class).getAsJsonObject();
                notificationData.setMessage(execDoc.get("message").getAsString());
            }
            PratilipiData pratilipiData = pratilipis.get(notification.getSourceIdLong());
            notificationData.setSourceUrl(pratilipiData.getPageUrl() + "?" + RequestParameter.NOTIFICATION_ID.getName() + "=" + notification.getId());
            notificationData.setSourceImageUrl(pratilipiData.getCoverImageUrl());
        } else if (notification.getType() == NotificationType.PRATILIPI_PUBLISHED_FOLLOWER) {
            PratilipiData pratilipiData = pratilipis.get(notification.getSourceIdLong());
            if (pratilipiData.getState() == PratilipiState.PUBLISHED) {
                notificationData.setMessage(createNotificationMessage(pratilipiData, notificationLanguage, plainText));
                notificationData.setSourceUrl(pratilipiData.getPageUrl() + "?" + RequestParameter.NOTIFICATION_ID.getName() + "=" + notification.getId());
                notificationData.setSourceImageUrl(pratilipiData.getCoverImageUrl());
                notificationData.setDisplayImageUrl(pratilipiData.getAuthor().getImageUrl());
            }
        } else if (notification.getType() == NotificationType.AUTHOR_FOLLOW) {
            notificationData.setMessage(createNotificationMessage(notification.getDataIds(), users, notificationLanguage, plainText));
            notificationData.setSourceUrl("/followers?" + RequestParameter.NOTIFICATION_ID.getName() + "=" + notification.getId());
            if (notification.getDataIds().size() != 0)
                notificationData.setDisplayImageUrl(users.get(notification.getDataIds().get(notification.getDataIds().size() - 1)).getProfileImageUrl());
        }
        notificationData.setSourceId(notification.getSourceId());
        notificationData.setState(notification.getState());
        notificationData.setNotificationType(notification.getType());
        notificationData.setLastUpdatedDate(notification.getLastUpdated());
        notificationDataList.add(notificationData);
    }
    return notificationDataList;
}
Also used : UserData(com.pratilipi.data.client.UserData) HashMap(java.util.HashMap) BatchProcess(com.pratilipi.data.type.BatchProcess) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) LinkedList(java.util.LinkedList) Notification(com.pratilipi.data.type.Notification) Language(com.pratilipi.common.type.Language) PratilipiData(com.pratilipi.data.client.PratilipiData) JsonElement(com.google.gson.JsonElement) NotificationData(com.pratilipi.data.client.NotificationData)

Example 17 with PratilipiData

use of com.pratilipi.data.client.PratilipiData 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 18 with PratilipiData

use of com.pratilipi.data.client.PratilipiData 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)

Example 19 with PratilipiData

use of com.pratilipi.data.client.PratilipiData in project pratilipi by Pratilipi.

the class SearchAccessorMockImpl method searchPratilipi.

@Override
public DataListCursorTuple<Long> searchPratilipi(String query, PratilipiFilter pratilipiFilter, String cursorStr, Integer offset, Integer resultCount) {
    List<Long> pratilipiIdList = new LinkedList<>();
    for (Object object : GLOBAL_INDEX) {
        if (object.getClass() != PratilipiData.class)
            continue;
        PratilipiData pratilipiData = (PratilipiData) object;
        if (pratilipiFilter.getLanguage() != null && !pratilipiFilter.getLanguage().equals(pratilipiData.getLanguage()))
            continue;
        if (pratilipiFilter.getAuthorId() != null && !pratilipiFilter.getAuthorId().equals(pratilipiData.getAuthorId()))
            continue;
        if (pratilipiFilter.getType() != null && !pratilipiFilter.getType().equals(pratilipiData.getType()))
            continue;
        pratilipiIdList.add(((PratilipiData) object).getId());
    }
    return new DataListCursorTuple<Long>(pratilipiIdList, null);
}
Also used : PratilipiData(com.pratilipi.data.client.PratilipiData) LinkedList(java.util.LinkedList)

Example 20 with PratilipiData

use of com.pratilipi.data.client.PratilipiData in project pratilipi by Pratilipi.

the class PratilipiV1Api method post.

@Post
public Response post(PostRequest request) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    Gson gson = new Gson();
    // Creating PratilipiData object.
    PratilipiData pratilipiData = gson.fromJson(gson.toJson(request), PratilipiData.class);
    // If not set already, setting AuthorId for new content.
    if (pratilipiData.getId() == null && !pratilipiData.hasAuthorId()) {
        DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
        Author author = dataAccessor.getAuthorByUserId(AccessTokenFilter.getAccessToken().getUserId());
        pratilipiData.setAuthorId(author.getId());
    }
    // Saving PratilipiData object.
    pratilipiData = PratilipiDataUtil.savePratilipiData(pratilipiData);
    // Creating PratilipiProcess task to process the data.
    Task task = TaskQueueFactory.newTask().setUrl("/pratilipi/process").addParam("pratilipiId", pratilipiData.getId().toString()).addParam("processData", "true");
    TaskQueueFactory.getPratilipiTaskQueue().add(task);
    // If PratilipiState has changed, creating AuthorProcess task to update Author stats.
    if (request.hasState && pratilipiData.getAuthorId() != null) {
        Task authorTask = TaskQueueFactory.newTask().setUrl("/author/process").addParam("authorId", pratilipiData.getAuthorId().toString()).addParam("updateStats", "true");
        TaskQueueFactory.getAuthorTaskQueue().add(authorTask);
    }
    return new Response(pratilipiData);
}
Also used : GenericResponse(com.pratilipi.api.shared.GenericResponse) Task(com.pratilipi.taskqueue.Task) PratilipiData(com.pratilipi.data.client.PratilipiData) DataAccessor(com.pratilipi.data.DataAccessor) Gson(com.google.gson.Gson) Author(com.pratilipi.data.type.Author) Post(com.pratilipi.api.annotation.Post)

Aggregations

PratilipiData (com.pratilipi.data.client.PratilipiData)20 DataAccessor (com.pratilipi.data.DataAccessor)15 HashMap (java.util.HashMap)9 Get (com.pratilipi.api.annotation.Get)5 UserPratilipiData (com.pratilipi.data.client.UserPratilipiData)5 Pratilipi (com.pratilipi.data.type.Pratilipi)5 LinkedList (java.util.LinkedList)5 Gson (com.google.gson.Gson)4 GenericResponse (com.pratilipi.api.shared.GenericResponse)4 PratilipiFilter (com.pratilipi.common.util.PratilipiFilter)4 PratilipiContentDoc (com.pratilipi.data.type.PratilipiContentDoc)4 JsonObject (com.google.gson.JsonObject)3 DocAccessor (com.pratilipi.data.DocAccessor)3 UserData (com.pratilipi.data.client.UserData)3 Author (com.pratilipi.data.type.Author)3 UserPratilipi (com.pratilipi.data.type.UserPratilipi)3 ArrayList (java.util.ArrayList)3 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)2 CommentData (com.pratilipi.data.client.CommentData)2 Event (com.pratilipi.data.type.Event)2