Search in sources :

Example 16 with GenericResponse

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

the class AuthorProcessApi method get.

@Get
public GenericResponse get(GenericRequest request) {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    // Fetching AppProperty
    String appPropertyId = "Api.AuthorProcess.ValidateData";
    AppProperty appProperty = dataAccessor.getAppProperty(appPropertyId);
    if (appProperty == null) {
        appProperty = dataAccessor.newAppProperty(appPropertyId);
        appProperty.setValue(new Date(0));
    }
    // Fetching list of author ids.
    AuthorFilter authorFilter = new AuthorFilter();
    authorFilter.setMinLastUpdate((Date) appProperty.getValue(), false);
    List<Long> authorIdList = dataAccessor.getAuthorIdList(authorFilter, null, 10000).getDataList();
    // Creating one task per author id.
    List<Task> taskList = new ArrayList<>(authorIdList.size());
    for (Long authorId : authorIdList) {
        Task task = TaskQueueFactory.newTask().setUrl("/author/process").addParam("authorId", authorId.toString()).addParam("validateData", "true");
        taskList.add(task);
    }
    TaskQueueFactory.getAuthorOfflineTaskQueue().addAll(taskList);
    logger.log(Level.INFO, "Added " + taskList.size() + " tasks.");
    // Updating AppProperty.
    if (authorIdList.size() > 0) {
        appProperty.setValue(dataAccessor.getAuthor(authorIdList.get(authorIdList.size() - 1)).getLastUpdated());
        appProperty = dataAccessor.createOrUpdateAppProperty(appProperty);
    }
    return new GenericResponse();
}
Also used : Task(com.pratilipi.taskqueue.Task) GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) AuthorFilter(com.pratilipi.common.util.AuthorFilter) ArrayList(java.util.ArrayList) AppProperty(com.pratilipi.data.type.AppProperty) Date(java.util.Date) Get(com.pratilipi.api.annotation.Get)

Example 17 with GenericResponse

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

the class PratilipiBackupApi method post.

@Post
public GenericResponse post(PostRequest request) throws UnexpectedServerException {
    Pratilipi pratilipi = DataAccessorFactory.getDataAccessor().getPratilipi(request.pratilipiId);
    Date dateTime = new Date(pratilipi.getLastUpdated().getTime() + TimeUnit.HOURS.toMillis(1L) - 1);
    DateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd-HH'.00'-z");
    dateTimeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
    String srcBucket = "static.pratilipi.com";
    String dstBucket = "backup.pratilipi.com";
    String srcPrefix = "pratilipi/" + request.pratilipiId + "/";
    String dstPrefix = srcBucket + "/pratilipi-" + dateTimeFormat.format(dateTime) + "/" + request.pratilipiId + "/";
    try {
        ListResult result = gcsService.list(srcBucket, new ListOptions.Builder().setPrefix(srcPrefix).build());
        while (result.hasNext()) {
            String srcName = result.next().getName();
            String dstName = dstPrefix + srcName.substring(srcPrefix.length());
            gcsService.copy(new GcsFilename(srcBucket, srcName), new GcsFilename(dstBucket, dstName));
        }
    } catch (IOException e) {
        logger.log(Level.SEVERE, "", e);
        throw new UnexpectedServerException();
    }
    return new GenericResponse();
}
Also used : GenericResponse(com.pratilipi.api.shared.GenericResponse) GcsService(com.google.appengine.tools.cloudstorage.GcsService) ListOptions(com.google.appengine.tools.cloudstorage.ListOptions) IOException(java.io.IOException) Date(java.util.Date) ListResult(com.google.appengine.tools.cloudstorage.ListResult) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Pratilipi(com.pratilipi.data.type.Pratilipi) SimpleDateFormat(java.text.SimpleDateFormat) GcsFilename(com.google.appengine.tools.cloudstorage.GcsFilename) Post(com.pratilipi.api.annotation.Post)

Example 18 with GenericResponse

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

the class NotificationProcessApi method get.

@Get
public GenericResponse get(GenericRequest request) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    List<Notification> notifList = dataAccessor.getNotificationListWithFcmPending(1000);
    logger.log(Level.INFO, "Total pending notifications = " + notifList.size());
    Map<Long, List<Notification>> userIdNotifListMap = new HashMap<>();
    for (Notification notif : notifList) {
        List<Notification> userNotifList = userIdNotifListMap.get(notif.getUserId());
        if (userNotifList == null) {
            userNotifList = new LinkedList<>();
            userIdNotifListMap.put(notif.getUserId(), userNotifList);
        }
        userNotifList.add(notif);
    }
    for (final Entry<Long, List<Notification>> entry : userIdNotifListMap.entrySet()) {
        Async async = new Async() {

            @Override
            public void exec() {
                for (Notification notif : entry.getValue()) {
                    Task task = TaskQueueFactory.newTask().setUrl("/notification/process").addParam("notificationId", notif.getId().toString());
                    TaskQueueFactory.getNotificationTaskQueue().add(task);
                    logger.log(Level.INFO, "Task created for notification id " + notif.getId());
                }
            }
        };
        NotificationDataUtil.updateFirebaseDb(entry.getKey(), entry.getValue(), async);
    }
    return new GenericResponse();
}
Also used : Task(com.pratilipi.taskqueue.Task) HashMap(java.util.HashMap) GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) Async(com.pratilipi.common.util.Async) List(java.util.List) LinkedList(java.util.LinkedList) Notification(com.pratilipi.data.type.Notification) Get(com.pratilipi.api.annotation.Get)

Example 19 with GenericResponse

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

the class AuthorBackupApi method get.

@Get
public GenericResponse get(GetRequest request) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessorBackup();
    Date backupDate = new Date();
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    DateFormat csvDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    DateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm-z");
    dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    csvDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    dateTimeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    StringBuilder backup = new StringBuilder();
    StringBuilder csv = new StringBuilder(CSV_HEADER + LINE_SEPARATOR);
    int count = 0;
    AuthorFilter authorFilter = new AuthorFilter();
    String cursor = null;
    Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new GsonIstDateAdapter()).create();
    while (true) {
        DataListCursorTuple<Author> authorListCursorTupe = dataAccessor.getAuthorList(authorFilter, cursor, 1000);
        List<Author> authorList = authorListCursorTupe.getDataList();
        for (Author author : authorList) {
            backup.append(gson.toJson(author) + LINE_SEPARATOR);
            if (request.generateCsv != null && request.generateCsv)
                csv.append("'" + author.getId()).append(CSV_SEPARATOR).append(author.getUserId() == null ? "" : "'" + author.getUserId()).append(CSV_SEPARATOR).append(author.getFirstName() == null ? "" : author.getFirstName()).append(CSV_SEPARATOR).append(author.getLastName() == null ? "" : author.getLastName()).append(CSV_SEPARATOR).append(author.getPenName() == null ? "" : author.getPenName()).append(CSV_SEPARATOR).append(author.getFirstNameEn() == null ? "" : author.getFirstNameEn()).append(CSV_SEPARATOR).append(author.getLastNameEn() == null ? "" : author.getLastNameEn()).append(CSV_SEPARATOR).append(author.getPenNameEn() == null ? "" : author.getPenNameEn()).append(CSV_SEPARATOR).append(author.getLanguage()).append(CSV_SEPARATOR).append(author.getSummary() != null && author.getSummary().trim().length() != 0).append(CSV_SEPARATOR).append(author.getContentPublished()).append(CSV_SEPARATOR).append(csvDateFormat.format(author.getRegistrationDate())).append(LINE_SEPARATOR);
        }
        count = count + authorList.size();
        if (authorList.size() < 1000)
            break;
        else
            cursor = authorListCursorTupe.getCursor();
    }
    String fileName = "datastore.author/" + dateFormat.format(backupDate) + "/" + "author-" + dateTimeFormat.format(backupDate);
    BlobEntry authorBackupEntry = blobAccessor.newBlob(fileName, backup.toString().getBytes(Charset.forName("UTF-8")), "text/plain");
    blobAccessor.createOrUpdateBlob(authorBackupEntry);
    if (request.generateCsv != null && request.generateCsv) {
        BlobEntry authorCsvEntry = blobAccessor.newBlob("datastore/author.csv", csv.toString().getBytes(Charset.forName("UTF-8")), "text/csv");
        blobAccessor.createOrUpdateBlob(authorCsvEntry);
    }
    logger.log(Level.INFO, "Backed up " + count + " Author Entities.");
    return new GenericResponse();
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) BlobEntry(com.pratilipi.data.type.BlobEntry) AuthorFilter(com.pratilipi.common.util.AuthorFilter) Gson(com.google.gson.Gson) Date(java.util.Date) GsonIstDateAdapter(com.pratilipi.common.util.GsonIstDateAdapter) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) BlobAccessor(com.pratilipi.data.BlobAccessor) Author(com.pratilipi.data.type.Author) SimpleDateFormat(java.text.SimpleDateFormat) Get(com.pratilipi.api.annotation.Get)

Example 20 with GenericResponse

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

the class AuthorCoverApi method post.

@Post
public GenericResponse post(PostRequest request) throws InsufficientAccessException, UnexpectedServerException {
    BlobEntry blobEntry = DataAccessorFactory.getBlobAccessor().newBlob(request.getName());
    blobEntry.setData(request.getData());
    blobEntry.setMimeType(request.getMimeType());
    blobEntry.setMetaName(request.getName());
    String coverImageUrl = AuthorDataUtil.saveAuthorCoverImage(request.authorId, blobEntry);
    return new Response(coverImageUrl);
}
Also used : GenericFileDownloadResponse(com.pratilipi.api.shared.GenericFileDownloadResponse) GenericResponse(com.pratilipi.api.shared.GenericResponse) BlobEntry(com.pratilipi.data.type.BlobEntry) 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