Search in sources :

Example 1 with GsonIstDateAdapter

use of com.pratilipi.common.util.GsonIstDateAdapter 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)

Aggregations

Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 Get (com.pratilipi.api.annotation.Get)1 GenericResponse (com.pratilipi.api.shared.GenericResponse)1 AuthorFilter (com.pratilipi.common.util.AuthorFilter)1 GsonIstDateAdapter (com.pratilipi.common.util.GsonIstDateAdapter)1 BlobAccessor (com.pratilipi.data.BlobAccessor)1 DataAccessor (com.pratilipi.data.DataAccessor)1 Author (com.pratilipi.data.type.Author)1 BlobEntry (com.pratilipi.data.type.BlobEntry)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1