Search in sources :

Example 51 with DataAccessor

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

the class UserBackupApi method get.

@Get
public GenericResponse get(GetRequest request) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessorBackup();
    Date backupDate = new Date();
    DateFormat yearFormat = new SimpleDateFormat("yyyy");
    DateFormat dayFormat = new SimpleDateFormat("dd");
    DateFormat hourFormat = new SimpleDateFormat("HH");
    DateFormat csvDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    yearFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    dayFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    hourFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    csvDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    StringBuilder backup = new StringBuilder();
    StringBuilder csv = new StringBuilder(CSV_HEADER + LINE_SEPARATOR);
    Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new GsonLongDateAdapter()).create();
    int batchSize = 1000;
    String cursor = null;
    int count = 0;
    while (true) {
        DataListCursorTuple<User> userListCursorTupe = dataAccessor.getUserList(cursor, batchSize);
        List<User> userList = userListCursorTupe.getDataList();
        for (User user : userList) {
            backup.append(gson.toJson(user) + LINE_SEPARATOR);
            if (request.generateCsv())
                csv.append("'" + user.getId().toString()).append(CSV_SEPARATOR).append(user.getFacebookId() == null ? "" : "'" + user.getFacebookId()).append(CSV_SEPARATOR).append(user.getEmail() == null ? "" : user.getEmail()).append(CSV_SEPARATOR).append(csvDateFormat.format(user.getSignUpDate())).append(LINE_SEPARATOR);
        }
        count = count + userList.size();
        if (userList.size() < batchSize)
            break;
        else
            cursor = userListCursorTupe.getCursor();
    }
    String year = yearFormat.format(backupDate);
    String day = dayFormat.format(backupDate);
    String hour = hourFormat.format(backupDate);
    String fileName = "datastore.user/" + year + "-mm-" + day + "/" + "user-" + year + "-mm-" + day + "-" + hour + ":xx-IST";
    BlobEntry userBackupEntry = blobAccessor.newBlob(fileName, backup.toString().getBytes(Charset.forName("UTF-8")), "text/plain");
    blobAccessor.createOrUpdateBlob(userBackupEntry);
    if (request.generateCsv()) {
        BlobEntry userCsvEntry = blobAccessor.newBlob("datastore/user.csv", csv.toString().getBytes(Charset.forName("UTF-8")), "text/csv");
        blobAccessor.createOrUpdateBlob(userCsvEntry);
    }
    logger.log(Level.INFO, "Backed up " + count + " User Entities.");
    return new GenericResponse();
}
Also used : User(com.pratilipi.data.type.User) GsonBuilder(com.google.gson.GsonBuilder) GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) BlobEntry(com.pratilipi.data.type.BlobEntry) Gson(com.google.gson.Gson) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) BlobAccessor(com.pratilipi.data.BlobAccessor) SimpleDateFormat(java.text.SimpleDateFormat) GsonLongDateAdapter(com.pratilipi.common.util.GsonLongDateAdapter) Get(com.pratilipi.api.annotation.Get)

Example 52 with DataAccessor

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

the class AuthorDataUtil method createAuthorDataList.

public static List<AuthorData> createAuthorDataList(List<Long> authorIdList, boolean includeUserData) {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    List<Author> authorList = dataAccessor.getAuthorList(authorIdList);
    Map<Long, Page> authorPages = dataAccessor.getPages(PageType.AUTHOR, authorIdList);
    List<AuthorData> authorDataList = new ArrayList<>(authorIdList.size());
    if (includeUserData) {
        List<Long> userIdList = new ArrayList<>(authorIdList.size());
        for (Author author : authorList) if (author.getUserId() != null)
            userIdList.add(author.getUserId());
        List<User> userList = dataAccessor.getUserList(userIdList);
        Map<Long, User> users = new HashMap<>(userIdList.size());
        for (User user : userList) users.put(user.getId(), user);
        for (Author author : authorList) authorDataList.add(createAuthorData(author, authorPages.get(author.getId()), users.get(author.getUserId())));
    } else {
        for (Author author : authorList) authorDataList.add(createAuthorData(author, authorPages.get(author.getId())));
    }
    return authorDataList;
}
Also used : User(com.pratilipi.data.type.User) HashMap(java.util.HashMap) DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) Page(com.pratilipi.data.type.Page) AuthorData(com.pratilipi.data.client.AuthorData) Author(com.pratilipi.data.type.Author)

Example 53 with DataAccessor

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

the class AuthorDataUtil method getRecommendedAuthorList.

public static DataListCursorTuple<AuthorData> getRecommendedAuthorList(Long userId, Language language, String cursorStr, Integer resultCount) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    UserFollowsDoc followsDoc = docAccessor.getUserFollowsDoc(userId);
    // Authors to ignore = Authors Following + Authors Ignored
    List<Long> authorIdsToIgnore = new ArrayList<Long>();
    if (followsDoc != null) {
        for (UserAuthorDoc userAuthorDoc : followsDoc.getFollows(UserFollowState.FOLLOWING)) authorIdsToIgnore.add(userAuthorDoc.getAuthorId());
        for (UserAuthorDoc userAuthorDoc : followsDoc.getFollows(UserFollowState.IGNORED)) if (new Date().getTime() - userAuthorDoc.getFollowDate().getTime() >= TimeUnit.DAYS.toMillis(30))
            authorIdsToIgnore.add(userAuthorDoc.getAuthorId());
    }
    // Get global list of recommended authors
    List<Long> recommendedList = _getRecommendAuthorGlobalList(language);
    // If cursor is passed, drop all items up till cursor
    Long cursor = cursorStr == null ? null : Long.parseLong(cursorStr);
    if (cursor != null && recommendedList.contains(cursor))
        while (!recommendedList.remove(0).equals(cursor)) continue;
    // Remove Author ids to be ignored
    recommendedList.removeAll(authorIdsToIgnore);
    // Drop items if recommendedList size is requested count
    if (resultCount != null)
        recommendedList = recommendedList.subList(0, Math.min(resultCount, recommendedList.size()));
    Map<Long, Author> authors = dataAccessor.getAuthors(recommendedList);
    Map<Long, Page> authorPages = dataAccessor.getPages(PageType.AUTHOR, recommendedList);
    List<AuthorData> recommendAuthorData = new ArrayList<>(recommendedList.size());
    for (Long authorId : recommendedList) recommendAuthorData.add(createAuthorData(authors.get(authorId), authorPages.get(authorId)));
    Collections.shuffle(recommendAuthorData);
    return new DataListCursorTuple<AuthorData>(recommendAuthorData, recommendedList.isEmpty() ? null : recommendedList.get(recommendedList.size() - 1).toString());
}
Also used : UserAuthorDoc(com.pratilipi.data.type.UserAuthorDoc) DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) ArrayList(java.util.ArrayList) Page(com.pratilipi.data.type.Page) Date(java.util.Date) DataListCursorTuple(com.pratilipi.data.DataListCursorTuple) AuthorData(com.pratilipi.data.client.AuthorData) Author(com.pratilipi.data.type.Author) UserFollowsDoc(com.pratilipi.data.type.UserFollowsDoc)

Example 54 with DataAccessor

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

the class AuthorDataUtil method createOrUpdateAuthorDashboardPageUrl.

public static boolean createOrUpdateAuthorDashboardPageUrl(Long authorId) {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Page page = dataAccessor.getPage(PageType.AUTHOR, authorId);
    Page dashboardPage = dataAccessor.getPage(PageType.AUTHOR_DASHBOARD, authorId);
    if (dashboardPage == null) {
        dashboardPage = dataAccessor.newPage();
        dashboardPage.setType(PageType.AUTHOR_DASHBOARD);
        dashboardPage.setUri(page.getUri() + "/dashboard");
        dashboardPage.setPrimaryContentId(authorId);
        dashboardPage.setCreationDate(new Date());
        if (page.getUriAlias() == null)
            dashboardPage = dataAccessor.createOrUpdatePage(dashboardPage);
    }
    if (page.getUriAlias() == null) {
        if (dashboardPage.getUriAlias() == null)
            return false;
        dashboardPage.setUriAlias(null);
    } else {
        String uriAlias = page.getUriAlias() + "/dashboard";
        if (uriAlias.equals(dashboardPage.getUriAlias()))
            return false;
        dashboardPage.setUriAlias(uriAlias);
    }
    dashboardPage = dataAccessor.createOrUpdatePage(dashboardPage);
    return true;
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) Page(com.pratilipi.data.type.Page) Date(java.util.Date)

Example 55 with DataAccessor

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

the class AuthorDataUtil method saveAuthorCoverImage.

public static String saveAuthorCoverImage(Long authorId, BlobEntry blobEntry) throws InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Author author = dataAccessor.getAuthor(authorId);
    if (!hasAccessToUpdateAuthorData(author, null))
        throw new InsufficientAccessException();
    String coverImageName = new Date().getTime() + "";
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
    blobEntry.setName("author/" + authorId + "/images/cover/" + coverImageName);
    blobAccessor.createOrUpdateBlob(blobEntry);
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.AUTHOR_UPDATE, author);
    author.setCoverImage(coverImageName);
    author.setLastUpdated(new Date());
    author = dataAccessor.createOrUpdateAuthor(author, auditLog);
    return createAuthorCoverImageUrl(author);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) BlobAccessor(com.pratilipi.data.BlobAccessor) Author(com.pratilipi.data.type.Author) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) Date(java.util.Date) AuditLog(com.pratilipi.data.type.AuditLog)

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