use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class UserPratilipiDataUtil method getUserPratilipi.
public static UserPratilipiData getUserPratilipi(Long userId, Long pratilipiId) throws UnexpectedServerException {
if (userId.equals(0L))
return null;
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
UserPratilipi userPratilipi = dataAccessor.getUserPratilipi(userId, pratilipiId);
if (userPratilipi == null) {
userPratilipi = dataAccessor.newUserPratilipi();
userPratilipi.setUserId(userId);
userPratilipi.setPratilipiId(pratilipiId);
}
return createUserPratilipiData(userPratilipi);
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class PratilipiProcessApi method postPratilipiProcess.
@Post
public GenericResponse postPratilipiProcess(PostPratilipiProcessRequest request) throws InvalidArgumentException, UnexpectedServerException, InsufficientAccessException {
List<Long> pratilipiIdList = request.getPratilipiIdList() != null ? request.getPratilipiIdList() : new ArrayList<Long>(1);
if (request.getPratilipiId() != null)
pratilipiIdList.add(request.getPratilipiId());
if (pratilipiIdList.size() == 0) {
logger.log(Level.SEVERE, "0 pratilipiId found.");
throw new InvalidArgumentException("'pratilipiId' or 'pratilipiIdList' must be set/non-empty.");
}
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
if (request.validateData()) {
for (Long pratilipiId : pratilipiIdList) {
Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
Author author = dataAccessor.getAuthor(pratilipi.getAuthorId());
Page pratilipiPage = dataAccessor.getPage(PageType.PRATILIPI, pratilipiId);
if (pratilipi.getLanguage() == null)
throw new InvalidArgumentException("Language is null.");
if (pratilipi.getAuthorId() != null && author == null)
throw new InvalidArgumentException("Invalid Author id.");
if (pratilipi.getState() == PratilipiState.PUBLISHED && author != null && author.getState() == AuthorState.DELETED)
throw new InvalidArgumentException("Linked Author entity is DELETED.");
if (pratilipi.getState() == PratilipiState.DELETED && pratilipiPage != null)
throw new InvalidArgumentException("DELETED Pratilipi has non-deleted Page entity.");
if (pratilipi.getState() != PratilipiState.DELETED && pratilipiPage == null)
throw new InvalidArgumentException("Page entity is missing for the Pratilipi.");
if (pratilipi.getState() == PratilipiState.DELETED)
continue;
if (pratilipi.getTitle() == null && pratilipi.getTitleEn() == null)
throw new InvalidArgumentException("Title is missing for the Pratilipi.");
if ((pratilipi.getTitle() != null && pratilipi.getTitle().trim().isEmpty()) || (pratilipi.getTitleEn() != null && pratilipi.getTitleEn().trim().isEmpty()))
throw new InvalidArgumentException("Title has empty string.");
}
}
if (request.processData()) {
PratilipiDataUtil.updatePratilipiSearchIndex(pratilipiIdList);
PratilipiDataUtil.updateFacebookScrape(pratilipiIdList);
}
if (request.processContent()) {
for (Long pratilipiId : pratilipiIdList) {
PratilipiDocUtil.updatePratilipiContent(pratilipiId);
PratilipiDataUtil.updatePratilipiIndex(pratilipiId);
boolean changed = PratilipiDocUtil.updateMeta(pratilipiId);
if (changed)
PratilipiDataUtil.updatePratilipiSearchIndex(pratilipiId, null);
}
}
if (request.processContentDoc()) {
for (Long pratilipiId : pratilipiIdList) {
boolean changed = PratilipiDocUtil.updateMeta(pratilipiId);
if (changed)
PratilipiDataUtil.updatePratilipiSearchIndex(pratilipiId, null);
}
}
if (request.updateReviewsDoc()) {
for (long pratilipiId : pratilipiIdList) PratilipiDocUtil.updatePratilipiReviews(pratilipiId);
}
if (request.updateStats()) {
List<Task> taskList = new ArrayList<>(pratilipiIdList.size());
List<Pratilipi> pratilipiList = DataAccessorFactory.getDataAccessor().getPratilipiList(pratilipiIdList);
for (int i = 0; i < pratilipiIdList.size(); i++) {
Pratilipi pratilipi = pratilipiList.get(i);
if (pratilipi == null || pratilipi.getState() != PratilipiState.PUBLISHED) {
pratilipiList.remove(i);
pratilipiIdList.remove(i);
i--;
continue;
}
PratilipiDataUtil.updatePratilipiStats(pratilipi.getId());
if (pratilipi.getAuthorId() != null) {
// Creating tasks to update author entities
Task task = TaskQueueFactory.newTask().setUrl("/author/process").addParam("authorId", pratilipi.getAuthorId().toString()).addParam("updateStats", "true");
taskList.add(task);
}
}
PratilipiDataUtil.updatePratilipiSearchIndex(pratilipiIdList);
TaskQueueFactory.getAuthorOfflineTaskQueue().addAll(taskList);
}
if (request.updateUserPratilipiStats()) {
for (long pratilipiId : pratilipiIdList) PratilipiDataUtil.updateUserPratilipiStats(pratilipiId);
}
return new GenericResponse();
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class PratilipiProcessApi method getPratilipiProcess.
@Get
public GenericResponse getPratilipiProcess(GenericRequest request) throws UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
// START: Creating ValidateData Tasks
// Fetching AppProperty
String appPropertyId = "Api.PratilipiProcess.ValidateData";
AppProperty appProperty = dataAccessor.getAppProperty(appPropertyId);
if (appProperty == null) {
appProperty = dataAccessor.newAppProperty(appPropertyId);
appProperty.setValue(new Date(0));
}
// Fetching list of Pratilipi ids.
PratilipiFilter pratilipiFilter = new PratilipiFilter();
pratilipiFilter.setMinLastUpdate((Date) appProperty.getValue(), false);
List<Long> pratilipiIdList = dataAccessor.getPratilipiIdList(pratilipiFilter, null, 0, 10000).getDataList();
// Creating one task per Pratilipi id.
List<Task> taskList = new ArrayList<>(pratilipiIdList.size());
for (Long pratilipiId : pratilipiIdList) {
Task task = TaskQueueFactory.newTask().setUrl("/pratilipi/process").addParam("pratilipiId", pratilipiId.toString()).addParam("validateData", "true");
taskList.add(task);
}
TaskQueueFactory.getPratilipiOfflineTaskQueue().addAll(taskList);
logger.log(Level.INFO, "Added " + taskList.size() + " ValidateData tasks.");
// Updating AppProperty.
if (pratilipiIdList.size() > 0) {
Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiIdList.get(pratilipiIdList.size() - 1));
Date lastUpdatedDate = pratilipi.getLastUpdated();
logger.log(Level.INFO, "Processing all contents updated from " + (Date) appProperty.getValue() + " to " + lastUpdatedDate + ".");
logger.log(Level.INFO, "Last Updated PratilipiID : " + pratilipi.getId());
appProperty.setValue(lastUpdatedDate);
appProperty = dataAccessor.createOrUpdateAppProperty(appProperty);
}
return new GenericResponse();
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class PratilipiStatsApi method post.
@Post
public GenericResponse post(Request request) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Pratilipi pratilipi = dataAccessor.getPratilipi(request.pratilipiId);
if (pratilipi == null || pratilipi.getState() != PratilipiState.PUBLISHED)
return new GenericResponse();
if (request.fbLikeShareCount == null) {
Page page = dataAccessor.getPage(PageType.PRATILIPI, pratilipi.getId());
request.fbLikeShareCount = FacebookApi.getUrlShareCount("http://" + Website.ALL_LANGUAGE.getHostName() + page.getUri());
}
PratilipiDataUtil.updatePratilipiStats(request.pratilipiId, request.readCountOffset, request.readCount, request.fbLikeShareCountOffset, request.fbLikeShareCount);
PratilipiDataUtil.updatePratilipiSearchIndex(request.pratilipiId, null);
if (pratilipi.getAuthorId() != null && (request.readCountOffset != null || request.readCount != null)) {
Task task = TaskQueueFactory.newTask().setUrl("/author/process").addParam("authorId", pratilipi.getAuthorId().toString()).addParam("updateStats", "true");
TaskQueueFactory.getAuthorOfflineTaskQueue().add(task);
}
return new GenericResponse();
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class PratilipiV1Api method get.
@Get
public Response get(GetRequest request) throws UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Pratilipi pratilipi = dataAccessor.getPratilipi(request.pratilipiId);
Author author = pratilipi.getAuthorId() == null ? null : dataAccessor.getAuthor(pratilipi.getAuthorId());
PratilipiData pratilipiData = PratilipiDataUtil.createPratilipiData(pratilipi, author);
if (UxModeFilter.isAndroidApp()) {
DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(request.pratilipiId);
pratilipiData.setIndex(pcDoc == null ? null : pcDoc.getIndex());
}
return new Response(pratilipiData);
}
Aggregations