use of com.pratilipi.api.annotation.Get 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();
}
use of com.pratilipi.api.annotation.Get in project pratilipi by Pratilipi.
the class AuthorListApi method get.
@Get
public Response get(GetRequest request) throws InsufficientAccessException {
AuthorFilter authorFilter = new AuthorFilter();
authorFilter.setLanguage(request.getLanguage());
authorFilter.setOrderByContentPublished(request.getOrderByContentPublished());
DataListCursorTuple<AuthorData> authorListCursorTuple = AuthorDataUtil.getAuthorDataList(request.getSearchQuery(), authorFilter, request.getCursor(), request.getResultCount() == null ? 20 : request.getResultCount());
return new Response(authorListCursorTuple.getDataList(), authorListCursorTuple.getCursor());
}
use of com.pratilipi.api.annotation.Get in project pratilipi by Pratilipi.
the class EmailProcessApi method get.
@Get
public GenericResponse get(GenericRequest request) throws UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
RtdbAccessor rtdbAccessor = DataAccessorFactory.getRtdbAccessor();
Map<Long, UserPreferenceRtdb> userPreferences = null;
List<Email> emailList = new ArrayList<>();
DataListIterator<Email> it = dataAccessor.getEmailListIteratorForStatePending(null, false);
Set<Long> userIds = new HashSet<>();
while (it.hasNext()) {
Email email = it.next();
emailList.add(email);
userIds.add(email.getUserId());
}
userPreferences = rtdbAccessor.getUserPreferences(userIds);
Map<Long, User> users = dataAccessor.getUsers(userPreferences.keySet());
// Re-scheduling for all Emails
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss z");
dateFormat.setTimeZone(TimeZone.getTimeZone("IST"));
for (Email email : emailList) {
UserPreferenceRtdb preference = userPreferences.get(email.getUserId());
User user = users.get(email.getUserId());
if (email.getScheduledDate().before(new Date()))
continue;
if (email.getScheduledDate().equals(preference.getEmailFrequency().getNextSchedule(user.getLastEmailedDate())))
continue;
logger.log(Level.INFO, "Rescheduling email: " + email.getId() + " from " + dateFormat.format(email.getScheduledDate()) + " to " + dateFormat.format(preference.getEmailFrequency().getNextSchedule(user.getLastEmailedDate())));
email.setScheduledDate(preference.getEmailFrequency().getNextSchedule(user.getLastEmailedDate()));
if (// User changed setting to NEVER -> Scheduled Date will be null
email.getScheduledDate() == null)
email.setState(EmailState.DROPPED);
email.setLastUpdated(new Date());
}
// Creating Tasks
List<Task> taskList = new ArrayList<>();
Set<Long> userIdTaskSet = new HashSet<>();
for (Email email : emailList) {
if (// User changed setting to NEVER -> Email Dropped
email.getState() != EmailState.PENDING)
continue;
if (email.getScheduledDate().after(new Date()))
continue;
UserPreferenceRtdb preference = userPreferences.get(email.getUserId());
if (preference.getEmailFrequency() == EmailFrequency.IMMEDIATELY)
taskList.add(TaskQueueFactory.newTask().setUrl("/email/process").addParam("emailId", email.getId().toString()));
else
userIdTaskSet.add(email.getUserId());
email.setState(EmailState.IN_PROGRESS);
email.setLastUpdated(new Date());
}
for (Long userId : userIdTaskSet) taskList.add(TaskQueueFactory.newTask().setUrl("/email/process").addParam("userId", userId.toString()));
TaskQueueFactory.getEmailHpTaskQueue().addAll(taskList);
emailList = dataAccessor.createOrUpdateEmailList(emailList);
return new GenericResponse();
}
use of com.pratilipi.api.annotation.Get in project pratilipi by Pratilipi.
the class AccessTokenCleanupApi method get.
@Get
public GenericResponse get(GenericRequest request) {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
while (true) {
int count = dataAccessor.deleteExpiredAccessTokenList(1000);
logger.log(Level.INFO, "Deleted " + count + " AccessToken entities ...");
if (count < 1000)
break;
}
return new GenericResponse();
}
use of com.pratilipi.api.annotation.Get in project pratilipi by Pratilipi.
the class TagsApi method getTags.
@Get
public Response getTags(Request request) {
List<TagData> tagDataList = TagDataUtil.getTags(request.getLanguage(), request.getType());
List<Response.ResponseObject> responseObjList = new ArrayList<>();
List<TagData> storyTagList = new ArrayList<>();
List<TagData> articleTagList = new ArrayList<>();
List<TagData> poemTagList = new ArrayList<>();
for (TagData tagData : tagDataList) {
if (tagData.getPratilipiType().equals(PratilipiType.STORY))
storyTagList.add(tagData);
else if (tagData.getPratilipiType().equals(PratilipiType.ARTICLE))
articleTagList.add(tagData);
else if (tagData.getPratilipiType().equals(PratilipiType.POEM))
poemTagList.add(tagData);
}
if (storyTagList.size() > 0)
responseObjList.add(new Response.ResponseObject(PratilipiType.STORY, null, storyTagList));
if (articleTagList.size() > 0)
responseObjList.add(new Response.ResponseObject(PratilipiType.ARTICLE, null, articleTagList));
if (poemTagList.size() > 0)
responseObjList.add(new Response.ResponseObject(PratilipiType.POEM, null, poemTagList));
return new Response(responseObjList);
}
Aggregations