Search in sources :

Example 41 with DataAccessor

use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.

the class UserPratilipiDataUtil method getUserPratilipi.

public static UserPratilipiData getUserPratilipi(Long userId, Long pratilipiId) throws UnexpectedServerException {
    if (userId.equals(0L))
        return null;
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    UserPratilipi userPratilipi = dataAccessor.getUserPratilipi(userId, pratilipiId);
    if (userPratilipi == null) {
        userPratilipi = dataAccessor.newUserPratilipi();
        userPratilipi.setUserId(userId);
        userPratilipi.setPratilipiId(pratilipiId);
    }
    return createUserPratilipiData(userPratilipi);
}
Also used : UserPratilipi(com.pratilipi.data.type.UserPratilipi) DataAccessor(com.pratilipi.data.DataAccessor)

Example 42 with DataAccessor

use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.

the class PratilipiProcessApi method postPratilipiProcess.

@Post
public GenericResponse postPratilipiProcess(PostPratilipiProcessRequest request) throws InvalidArgumentException, UnexpectedServerException, InsufficientAccessException {
    List<Long> pratilipiIdList = request.getPratilipiIdList() != null ? request.getPratilipiIdList() : new ArrayList<Long>(1);
    if (request.getPratilipiId() != null)
        pratilipiIdList.add(request.getPratilipiId());
    if (pratilipiIdList.size() == 0) {
        logger.log(Level.SEVERE, "0 pratilipiId found.");
        throw new InvalidArgumentException("'pratilipiId' or 'pratilipiIdList' must be set/non-empty.");
    }
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    if (request.validateData()) {
        for (Long pratilipiId : pratilipiIdList) {
            Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
            Author author = dataAccessor.getAuthor(pratilipi.getAuthorId());
            Page pratilipiPage = dataAccessor.getPage(PageType.PRATILIPI, pratilipiId);
            if (pratilipi.getLanguage() == null)
                throw new InvalidArgumentException("Language is null.");
            if (pratilipi.getAuthorId() != null && author == null)
                throw new InvalidArgumentException("Invalid Author id.");
            if (pratilipi.getState() == PratilipiState.PUBLISHED && author != null && author.getState() == AuthorState.DELETED)
                throw new InvalidArgumentException("Linked Author entity is DELETED.");
            if (pratilipi.getState() == PratilipiState.DELETED && pratilipiPage != null)
                throw new InvalidArgumentException("DELETED Pratilipi has non-deleted Page entity.");
            if (pratilipi.getState() != PratilipiState.DELETED && pratilipiPage == null)
                throw new InvalidArgumentException("Page entity is missing for the Pratilipi.");
            if (pratilipi.getState() == PratilipiState.DELETED)
                continue;
            if (pratilipi.getTitle() == null && pratilipi.getTitleEn() == null)
                throw new InvalidArgumentException("Title is missing for the Pratilipi.");
            if ((pratilipi.getTitle() != null && pratilipi.getTitle().trim().isEmpty()) || (pratilipi.getTitleEn() != null && pratilipi.getTitleEn().trim().isEmpty()))
                throw new InvalidArgumentException("Title has empty string.");
        }
    }
    if (request.processData()) {
        PratilipiDataUtil.updatePratilipiSearchIndex(pratilipiIdList);
        PratilipiDataUtil.updateFacebookScrape(pratilipiIdList);
    }
    if (request.processContent()) {
        for (Long pratilipiId : pratilipiIdList) {
            PratilipiDocUtil.updatePratilipiContent(pratilipiId);
            PratilipiDataUtil.updatePratilipiIndex(pratilipiId);
            boolean changed = PratilipiDocUtil.updateMeta(pratilipiId);
            if (changed)
                PratilipiDataUtil.updatePratilipiSearchIndex(pratilipiId, null);
        }
    }
    if (request.processContentDoc()) {
        for (Long pratilipiId : pratilipiIdList) {
            boolean changed = PratilipiDocUtil.updateMeta(pratilipiId);
            if (changed)
                PratilipiDataUtil.updatePratilipiSearchIndex(pratilipiId, null);
        }
    }
    if (request.updateReviewsDoc()) {
        for (long pratilipiId : pratilipiIdList) PratilipiDocUtil.updatePratilipiReviews(pratilipiId);
    }
    if (request.updateStats()) {
        List<Task> taskList = new ArrayList<>(pratilipiIdList.size());
        List<Pratilipi> pratilipiList = DataAccessorFactory.getDataAccessor().getPratilipiList(pratilipiIdList);
        for (int i = 0; i < pratilipiIdList.size(); i++) {
            Pratilipi pratilipi = pratilipiList.get(i);
            if (pratilipi == null || pratilipi.getState() != PratilipiState.PUBLISHED) {
                pratilipiList.remove(i);
                pratilipiIdList.remove(i);
                i--;
                continue;
            }
            PratilipiDataUtil.updatePratilipiStats(pratilipi.getId());
            if (pratilipi.getAuthorId() != null) {
                // Creating tasks to update author entities
                Task task = TaskQueueFactory.newTask().setUrl("/author/process").addParam("authorId", pratilipi.getAuthorId().toString()).addParam("updateStats", "true");
                taskList.add(task);
            }
        }
        PratilipiDataUtil.updatePratilipiSearchIndex(pratilipiIdList);
        TaskQueueFactory.getAuthorOfflineTaskQueue().addAll(taskList);
    }
    if (request.updateUserPratilipiStats()) {
        for (long pratilipiId : pratilipiIdList) PratilipiDataUtil.updateUserPratilipiStats(pratilipiId);
    }
    return new GenericResponse();
}
Also used : Task(com.pratilipi.taskqueue.Task) GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) Page(com.pratilipi.data.type.Page) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) Author(com.pratilipi.data.type.Author) Pratilipi(com.pratilipi.data.type.Pratilipi) Post(com.pratilipi.api.annotation.Post)

Example 43 with DataAccessor

use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.

the class PratilipiProcessApi method getPratilipiProcess.

@Get
public GenericResponse getPratilipiProcess(GenericRequest request) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    // START: Creating ValidateData Tasks
    // Fetching AppProperty
    String appPropertyId = "Api.PratilipiProcess.ValidateData";
    AppProperty appProperty = dataAccessor.getAppProperty(appPropertyId);
    if (appProperty == null) {
        appProperty = dataAccessor.newAppProperty(appPropertyId);
        appProperty.setValue(new Date(0));
    }
    // Fetching list of Pratilipi ids.
    PratilipiFilter pratilipiFilter = new PratilipiFilter();
    pratilipiFilter.setMinLastUpdate((Date) appProperty.getValue(), false);
    List<Long> pratilipiIdList = dataAccessor.getPratilipiIdList(pratilipiFilter, null, 0, 10000).getDataList();
    // Creating one task per Pratilipi id.
    List<Task> taskList = new ArrayList<>(pratilipiIdList.size());
    for (Long pratilipiId : pratilipiIdList) {
        Task task = TaskQueueFactory.newTask().setUrl("/pratilipi/process").addParam("pratilipiId", pratilipiId.toString()).addParam("validateData", "true");
        taskList.add(task);
    }
    TaskQueueFactory.getPratilipiOfflineTaskQueue().addAll(taskList);
    logger.log(Level.INFO, "Added " + taskList.size() + " ValidateData tasks.");
    // Updating AppProperty.
    if (pratilipiIdList.size() > 0) {
        Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiIdList.get(pratilipiIdList.size() - 1));
        Date lastUpdatedDate = pratilipi.getLastUpdated();
        logger.log(Level.INFO, "Processing all contents updated from " + (Date) appProperty.getValue() + " to " + lastUpdatedDate + ".");
        logger.log(Level.INFO, "Last Updated PratilipiID : " + pratilipi.getId());
        appProperty.setValue(lastUpdatedDate);
        appProperty = dataAccessor.createOrUpdateAppProperty(appProperty);
    }
    return new GenericResponse();
}
Also used : PratilipiFilter(com.pratilipi.common.util.PratilipiFilter) Task(com.pratilipi.taskqueue.Task) GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) Pratilipi(com.pratilipi.data.type.Pratilipi) AppProperty(com.pratilipi.data.type.AppProperty) Date(java.util.Date) Get(com.pratilipi.api.annotation.Get)

Example 44 with DataAccessor

use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.

the class PratilipiStatsApi method post.

@Post
public GenericResponse post(Request request) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(request.pratilipiId);
    if (pratilipi == null || pratilipi.getState() != PratilipiState.PUBLISHED)
        return new GenericResponse();
    if (request.fbLikeShareCount == null) {
        Page page = dataAccessor.getPage(PageType.PRATILIPI, pratilipi.getId());
        request.fbLikeShareCount = FacebookApi.getUrlShareCount("http://" + Website.ALL_LANGUAGE.getHostName() + page.getUri());
    }
    PratilipiDataUtil.updatePratilipiStats(request.pratilipiId, request.readCountOffset, request.readCount, request.fbLikeShareCountOffset, request.fbLikeShareCount);
    PratilipiDataUtil.updatePratilipiSearchIndex(request.pratilipiId, null);
    if (pratilipi.getAuthorId() != null && (request.readCountOffset != null || request.readCount != null)) {
        Task task = TaskQueueFactory.newTask().setUrl("/author/process").addParam("authorId", pratilipi.getAuthorId().toString()).addParam("updateStats", "true");
        TaskQueueFactory.getAuthorOfflineTaskQueue().add(task);
    }
    return new GenericResponse();
}
Also used : Task(com.pratilipi.taskqueue.Task) GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) Page(com.pratilipi.data.type.Page) Pratilipi(com.pratilipi.data.type.Pratilipi) Post(com.pratilipi.api.annotation.Post)

Example 45 with DataAccessor

use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.

the class PratilipiV1Api method get.

@Get
public Response get(GetRequest request) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(request.pratilipiId);
    Author author = pratilipi.getAuthorId() == null ? null : dataAccessor.getAuthor(pratilipi.getAuthorId());
    PratilipiData pratilipiData = PratilipiDataUtil.createPratilipiData(pratilipi, author);
    if (UxModeFilter.isAndroidApp()) {
        DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
        PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(request.pratilipiId);
        pratilipiData.setIndex(pcDoc == null ? null : pcDoc.getIndex());
    }
    return new Response(pratilipiData);
}
Also used : GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) PratilipiData(com.pratilipi.data.client.PratilipiData) DocAccessor(com.pratilipi.data.DocAccessor) Author(com.pratilipi.data.type.Author) Pratilipi(com.pratilipi.data.type.Pratilipi) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc) Get(com.pratilipi.api.annotation.Get)

Aggregations

DataAccessor (com.pratilipi.data.DataAccessor)162 Date (java.util.Date)53 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)37 Pratilipi (com.pratilipi.data.type.Pratilipi)34 Author (com.pratilipi.data.type.Author)33 AuditLog (com.pratilipi.data.type.AuditLog)32 UserPratilipi (com.pratilipi.data.type.UserPratilipi)32 ArrayList (java.util.ArrayList)30 Page (com.pratilipi.data.type.Page)28 HashMap (java.util.HashMap)25 GenericResponse (com.pratilipi.api.shared.GenericResponse)24 DocAccessor (com.pratilipi.data.DocAccessor)22 User (com.pratilipi.data.type.User)22 Get (com.pratilipi.api.annotation.Get)20 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)19 PratilipiData (com.pratilipi.data.client.PratilipiData)15 UserData (com.pratilipi.data.client.UserData)14 BlobAccessor (com.pratilipi.data.BlobAccessor)13 Email (com.pratilipi.data.type.Email)13 JsonObject (com.google.gson.JsonObject)12