Search in sources :

Example 26 with BlobEntry

use of com.pratilipi.data.type.BlobEntry 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 27 with BlobEntry

use of com.pratilipi.data.type.BlobEntry 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)

Example 28 with BlobEntry

use of com.pratilipi.data.type.BlobEntry in project pratilipi by Pratilipi.

the class RtdbAccessorFirebaseImpl method _getUserPreferences.

private Map<Long, UserPreferenceRtdb> _getUserPreferences(Map<String, String> paramsMap) throws UnexpectedServerException {
    try {
        BlobEntry blobEntry = HttpUtil.doGet(_getUserPreferenceDbUrl(), headersMap, paramsMap);
        String jsonStr = new String(blobEntry.getData(), "UTF-8");
        JsonObject json = new Gson().fromJson(jsonStr, JsonElement.class).getAsJsonObject();
        Map<Long, UserPreferenceRtdb> userPreferenceMap = new HashMap<>();
        for (Entry<String, JsonElement> entry : json.entrySet()) userPreferenceMap.put(Long.parseLong(entry.getKey()), _getUserPreferenceRtdb(entry.getValue().getAsJsonObject()));
        return userPreferenceMap;
    } catch (UnsupportedEncodingException e) {
        logger.log(Level.SEVERE, e.getMessage());
        throw new UnexpectedServerException();
    }
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) HashMap(java.util.HashMap) JsonElement(com.google.gson.JsonElement) BlobEntry(com.pratilipi.data.type.BlobEntry) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UserPreferenceRtdb(com.pratilipi.data.type.UserPreferenceRtdb)

Example 29 with BlobEntry

use of com.pratilipi.data.type.BlobEntry in project pratilipi by Pratilipi.

the class RtdbAccessorFirebaseImpl method getUserPreference.

// PREFERENCE Table
@Override
public UserPreferenceRtdb getUserPreference(Long userId) throws UnexpectedServerException {
    try {
        BlobEntry blobEntry = HttpUtil.doGet(_getUserPreferenceDbUrl(userId), headersMap, null);
        String jsonStr = new String(blobEntry.getData(), "UTF-8");
        return _getUserPreferenceRtdb(jsonStr);
    } catch (UnsupportedEncodingException e) {
        logger.log(Level.SEVERE, "Failed to parse response from Firebase.", e);
        throw new UnexpectedServerException();
    }
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) BlobEntry(com.pratilipi.data.type.BlobEntry) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 30 with BlobEntry

use of com.pratilipi.data.type.BlobEntry in project pratilipi by Pratilipi.

the class DataStoreBackupUtil method csvAuthor.

public static void csvAuthor() throws UnexpectedServerException {
    String CSV_HEADER = "AuthorId,UserId," + "FirstName,LastName,PenName,FirstNameEN,LastNameEN,PenNameEN," + "Language,HasSummary,ContentsPublished,FollowCount,RegistrationDate";
    String CSV_SEPARATOR = ",";
    String LINE_SEPARATOR = "\n";
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessorBackup();
    DateFormat csvDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    csvDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    StringBuilder csv = new StringBuilder(CSV_HEADER + LINE_SEPARATOR);
    int count = 0;
    QueryResultIterator<AuthorEntity> itr = ObjectifyService.ofy().cache(false).load().type(AuthorEntity.class).chunk(1000).iterator();
    while (itr.hasNext()) {
        Author author = itr.next();
        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(author.getFollowCount()).append(CSV_SEPARATOR).append(csvDateFormat.format(author.getRegistrationDate())).append(LINE_SEPARATOR);
        count++;
        if (count % 1000 == 0)
            System.out.println(count + " ...");
    }
    System.out.println(count + " ...");
    BlobEntry authorCsvEntry = blobAccessor.newBlob("datastore/author.csv", csv.toString().getBytes(Charset.forName("UTF-8")), "text/csv");
    blobAccessor.createOrUpdateBlob(authorCsvEntry);
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) BlobEntry(com.pratilipi.data.type.BlobEntry) BlobAccessor(com.pratilipi.data.BlobAccessor) Author(com.pratilipi.data.type.Author) SimpleDateFormat(java.text.SimpleDateFormat) AuthorEntity(com.pratilipi.data.type.gae.AuthorEntity)

Aggregations

BlobEntry (com.pratilipi.data.type.BlobEntry)31 BlobAccessor (com.pratilipi.data.BlobAccessor)12 DataAccessor (com.pratilipi.data.DataAccessor)8 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)7 Pratilipi (com.pratilipi.data.type.Pratilipi)7 GenericResponse (com.pratilipi.api.shared.GenericResponse)6 UserPratilipi (com.pratilipi.data.type.UserPratilipi)6 SimpleDateFormat (java.text.SimpleDateFormat)6 JsonObject (com.google.gson.JsonObject)5 Post (com.pratilipi.api.annotation.Post)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 DateFormat (java.text.DateFormat)5 Date (java.util.Date)5 Gson (com.google.gson.Gson)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 GsonBuilder (com.google.gson.GsonBuilder)3 Get (com.pratilipi.api.annotation.Get)3 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)3 PratilipiContentUtil (com.pratilipi.common.util.PratilipiContentUtil)3