Search in sources :

Example 26 with GenericResponse

use of com.pratilipi.api.shared.GenericResponse in project pratilipi by Pratilipi.

the class VoteApi method post.

@Post
public GenericResponse post(PostRequest request) throws InvalidArgumentException, InsufficientAccessException {
    VoteData voteData = VoteDataUtil.saveVoteData(AccessTokenFilter.getAccessToken().getUserId(), request.parentType, request.parentId, request.type);
    if (voteData.getReferenceType() == ReferenceType.PRATILIPI) {
        Task task = TaskQueueFactory.newTask().setUrl("/pratilipi/process").addParam("pratilipiId", voteData.getReferenceId()).addParam("updateReviewsDoc", "true");
        TaskQueueFactory.getPratilipiTaskQueue().add(task);
    }
    return new GenericResponse();
}
Also used : Task(com.pratilipi.taskqueue.Task) GenericResponse(com.pratilipi.api.shared.GenericResponse) VoteData(com.pratilipi.data.client.VoteData) Post(com.pratilipi.api.annotation.Post)

Example 27 with GenericResponse

use of com.pratilipi.api.shared.GenericResponse in project pratilipi by Pratilipi.

the class PratilipiIdfApi method get.

@Get
public GenericResponse get(GenericRequest request) throws UnexpectedServerException {
    Date idfGenerationDate = new Date();
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    PratilipiFilter pratilipiFilter = new PratilipiFilter();
    String cursor = null;
    DataListCursorTuple<Long> pratilipiIdListCursorTupe = dataAccessor.getPratilipiIdList(pratilipiFilter, cursor, null, null);
    List<Long> pratilipiIdList = pratilipiIdListCursorTupe.getDataList();
    // Populate Keyword-Frequency map.
    final HashMap<String, Integer> keywordFrequencyMap = new HashMap<>();
    for (Long pratilipiId : pratilipiIdList) {
        String[] keywords = PratilipiDataUtil.getPratilipiKeywords(pratilipiId).split("\\s+");
        if (keywords == null)
            continue;
        for (String keyword : keywords) {
            if (keywordFrequencyMap.containsKey(keyword))
                keywordFrequencyMap.put(keyword, keywordFrequencyMap.get(keyword) + 1);
            else
                keywordFrequencyMap.put(keyword, 1);
        }
    }
    // Sort Keyword-Frequency map in descending order of frequency
    Comparator<String> comparator = new Comparator<String>() {

        @Override
        public int compare(String a, String b) {
            return keywordFrequencyMap.get(a) >= keywordFrequencyMap.get(b) ? -1 : 1;
        }
    };
    TreeMap<String, Integer> sortedKeywordFrequencyMap = new TreeMap<>(comparator);
    sortedKeywordFrequencyMap.putAll(keywordFrequencyMap);
    // Transform sorted map to csv string
    StringBuilder csv = new StringBuilder();
    for (Map.Entry<String, Integer> entry : sortedKeywordFrequencyMap.entrySet()) {
        csv.append(entry.getKey() + ",");
        csv.append(entry.getValue().toString() + ",");
        csv.append("\n");
    }
    // Persist csv string in BlobStore
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
    BlobEntry blobEntry = blobAccessor.newBlob("pratilipi/" + new SimpleDateFormat("yyyy-MM-dd-HH:mm").format(idfGenerationDate) + "-idf.csv", null, "text/plain");
    blobEntry.setData(csv.toString().getBytes(Charset.forName("UTF-8")));
    blobAccessor.createOrUpdateBlob(blobEntry);
    logger.log(Level.INFO, "Generated IDF with " + keywordFrequencyMap.size() + " keywords.");
    return new GenericResponse();
}
Also used : HashMap(java.util.HashMap) GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) BlobEntry(com.pratilipi.data.type.BlobEntry) TreeMap(java.util.TreeMap) Date(java.util.Date) Comparator(java.util.Comparator) PratilipiFilter(com.pratilipi.common.util.PratilipiFilter) BlobAccessor(com.pratilipi.data.BlobAccessor) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SimpleDateFormat(java.text.SimpleDateFormat) Get(com.pratilipi.api.annotation.Get)

Example 28 with GenericResponse

use of com.pratilipi.api.shared.GenericResponse in project pratilipi by Pratilipi.

the class UserProcessApi method get.

@Get
public GenericResponse get(GenericRequest request) {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    // Fetching AppProperty
    AppProperty appProperty = dataAccessor.getAppProperty(appPropertyId);
    if (appProperty == null) {
        appProperty = dataAccessor.newAppProperty(appPropertyId);
        appProperty.setValue(new Date(0));
    }
    List<Key<UserEntity>> keyList = ObjectifyService.ofy().load().type(UserEntity.class).filter("SIGN_UP_DATE >", appProperty.getValue()).order("SIGN_UP_DATE").limit(10000).keys().list();
    // Creating task for each user.
    List<Task> taskList = new ArrayList<>(keyList.size());
    for (Key<UserEntity> key : keyList) {
        Task task = TaskQueueFactory.newTask().setUrl("/user/process").addParam("userId", key.getId() + "").addParam("validateData", "true");
        taskList.add(task);
    }
    TaskQueueFactory.getUserOfflineTaskQueue().addAll(taskList);
    logger.log(Level.INFO, "Added " + taskList.size() + " tasks.");
    // Updating AppProperty.
    if (taskList.size() > 0) {
        appProperty.setValue(dataAccessor.getUser(keyList.get(keyList.size() - 1).getId()).getSignUpDate());
        dataAccessor.createOrUpdateAppProperty(appProperty);
    }
    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) AppProperty(com.pratilipi.data.type.AppProperty) Date(java.util.Date) UserEntity(com.pratilipi.data.type.gae.UserEntity) Key(com.googlecode.objectify.Key) Get(com.pratilipi.api.annotation.Get)

Example 29 with GenericResponse

use of com.pratilipi.api.shared.GenericResponse in project pratilipi by Pratilipi.

the class PratilipiContentBatchApi method get.

@Post
public static GenericResponse get(PostRequest request) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    PratilipiDocUtil.saveContentPageBatch(request.pratilipiId, request.jsonObject);
    Task task = TaskQueueFactory.newTask().setUrl("/pratilipi/process").addParam("pratilipiId", request.pratilipiId.toString()).addParam("processContentDoc", "true");
    TaskQueueFactory.getPratilipiTaskQueue().add(task);
    return new GenericResponse();
}
Also used : Task(com.pratilipi.taskqueue.Task) GenericResponse(com.pratilipi.api.shared.GenericResponse) Post(com.pratilipi.api.annotation.Post)

Aggregations

GenericResponse (com.pratilipi.api.shared.GenericResponse)29 DataAccessor (com.pratilipi.data.DataAccessor)17 Post (com.pratilipi.api.annotation.Post)15 Get (com.pratilipi.api.annotation.Get)12 Task (com.pratilipi.taskqueue.Task)11 Date (java.util.Date)10 ArrayList (java.util.ArrayList)8 Gson (com.google.gson.Gson)7 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)6 BlobEntry (com.pratilipi.data.type.BlobEntry)6 AppProperty (com.pratilipi.data.type.AppProperty)5 Pratilipi (com.pratilipi.data.type.Pratilipi)5 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)4 Author (com.pratilipi.data.type.Author)4 SimpleDateFormat (java.text.SimpleDateFormat)4 GsonBuilder (com.google.gson.GsonBuilder)3 JsonObject (com.google.gson.JsonObject)3 GenericFileDownloadResponse (com.pratilipi.api.shared.GenericFileDownloadResponse)3 AuthorFilter (com.pratilipi.common.util.AuthorFilter)3 BlobAccessor (com.pratilipi.data.BlobAccessor)3