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();
}
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;
}
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());
}
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;
}
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);
}
Aggregations