use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class PratilipiDocUtil method updatePratilipiGoogleAnalyticsPageViews.
public static List<Long> updatePratilipiGoogleAnalyticsPageViews(int year, int month, int day) throws UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
Gson gson = new Gson();
String dateStr = year + (month < 10 ? "-0" + month : "-" + month) + (day < 10 ? "-0" + day : "-" + day);
String fileName = "pratilipi-google-analytics/page-views/" + dateStr;
BlobEntry blobEntry = blobAccessor.getBlob(fileName);
if (blobEntry == null) {
try {
blobEntry = blobAccessor.newBlob(fileName, "{}".getBytes("UTF-8"), "application/json");
} catch (UnsupportedEncodingException e) {
logger.log(Level.SEVERE, e.getMessage());
throw new UnexpectedServerException();
}
}
@SuppressWarnings("serial") Map<String, Integer> oldPageViewsMap = gson.fromJson(new String(blobEntry.getData(), Charset.forName("UTF-8")), new TypeToken<Map<String, Integer>>() {
}.getType());
Map<String, Integer> newPageViewsMap = GoogleAnalyticsApi.getPageViews(dateStr);
Map<String, Integer> diffPageViewsMap = new HashMap<>();
for (Entry<String, Integer> entry : newPageViewsMap.entrySet()) if (!entry.getValue().equals(oldPageViewsMap.get(entry.getKey())))
diffPageViewsMap.put(entry.getKey(), entry.getValue());
Map<Long, Integer> pageViewsMap = new HashMap<>();
Map<Long, Integer> readPageViewsMap = new HashMap<>();
for (Entry<String, Integer> entry : diffPageViewsMap.entrySet()) {
String uri = entry.getKey();
if (!uri.startsWith("/read?id=")) {
if (uri.indexOf('?') != -1)
uri = uri.substring(0, uri.indexOf('?'));
Page page = dataAccessor.getPage(uri);
if (page != null && page.getType() == PageType.PRATILIPI) {
Long pratilpiId = page.getPrimaryContentId();
if (pageViewsMap.get(pratilpiId) == null)
pageViewsMap.put(pratilpiId, entry.getValue());
else
pageViewsMap.put(pratilpiId, pageViewsMap.get(pratilpiId) + entry.getValue());
}
} else {
// Reader
String patilipiIdStr = uri.indexOf('&') == -1 ? uri.substring("/read?id=".length()) : uri.substring("/read?id=".length(), uri.indexOf('&'));
try {
Long pratilpiId = Long.parseLong(patilipiIdStr);
if (readPageViewsMap.get(pratilpiId) == null)
readPageViewsMap.put(pratilpiId, entry.getValue());
else
readPageViewsMap.put(pratilpiId, readPageViewsMap.get(pratilpiId) + entry.getValue());
} catch (NumberFormatException e) {
logger.log(Level.SEVERE, "Exception while processing reader uri " + uri, e);
}
}
}
for (Entry<Long, Integer> entry : pageViewsMap.entrySet()) {
if (readPageViewsMap.get(entry.getKey()) == null) {
updatePratilipiGoogleAnalyticsPageViews(entry.getKey(), year, month, day, entry.getValue(), 0);
} else {
updatePratilipiGoogleAnalyticsPageViews(entry.getKey(), year, month, day, entry.getValue(), readPageViewsMap.get(entry.getKey()));
readPageViewsMap.remove(entry.getKey());
}
}
for (Entry<Long, Integer> entry : readPageViewsMap.entrySet()) updatePratilipiGoogleAnalyticsPageViews(entry.getKey(), year, month, day, 0, entry.getValue());
if (diffPageViewsMap.size() > 0) {
try {
blobEntry.setData(gson.toJson(newPageViewsMap).getBytes("UTF-8"));
blobAccessor.createOrUpdateBlob(blobEntry);
} catch (UnsupportedEncodingException e) {
logger.log(Level.SEVERE, e.getMessage());
throw new UnexpectedServerException();
}
}
ArrayList<Long> updatedPratilipiIdList = new ArrayList<>(pageViewsMap.size() + readPageViewsMap.size());
updatedPratilipiIdList.addAll(pageViewsMap.keySet());
updatedPratilipiIdList.addAll(readPageViewsMap.keySet());
return updatedPratilipiIdList;
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class PratilipiDocUtil method addContentChapter.
public static JsonArray addContentChapter(Long pratilipiId, Integer chapterNo) throws InsufficientAccessException, UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
if (!PratilipiDataUtil.hasAccessToUpdatePratilipiContent(pratilipi))
throw new InsufficientAccessException();
DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(pratilipiId);
if (pcDoc == null)
pcDoc = docAccessor.newPratilipiContentDoc();
pcDoc.addChapter(chapterNo, null);
docAccessor.save(pratilipiId, pcDoc);
return pcDoc.getIndex();
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class TagDataUtil method removeTags.
public static void removeTags(List<Long> ids) {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
logger.log(Level.INFO, "Number of ids : " + ids.size());
dataAccessor.deleteTags(ids);
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class UserAuthorDataUtil method hasAccessToUpdateUserAuthorData.
public static boolean hasAccessToUpdateUserAuthorData(UserAuthor userAuthor, AccessType accessType) {
AccessToken accessToken = AccessTokenFilter.getAccessToken();
if (!userAuthor.getUserId().equals(accessToken.getUserId()))
return false;
if (!UserAccessUtil.hasUserAccess(accessToken.getUserId(), null, accessType))
return false;
// User can not follow his/her own Author profile.
if (accessType == AccessType.USER_AUTHOR_FOLLOWING) {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Author author = dataAccessor.getAuthor(userAuthor.getAuthorId());
if (userAuthor.getUserId().equals(author.getUserId()))
return false;
}
return true;
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class UserAuthorDataUtil method saveUserAuthorFollow.
public static UserAuthorData saveUserAuthorFollow(Long userId, Long authorId, UserFollowState followState) throws InvalidArgumentException, InsufficientAccessException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
UserAuthor userAuthor = dataAccessor.getUserAuthor(userId, authorId);
if (userAuthor == null) {
userAuthor = dataAccessor.newUserAuthor();
userAuthor.setUserId(userId);
userAuthor.setAuthorId(authorId);
}
if (!hasAccessToUpdateUserAuthorData(userAuthor, AccessType.USER_AUTHOR_FOLLOWING))
throw new InsufficientAccessException();
AccessToken accessToken = AccessTokenFilter.getAccessToken();
AuditLog auditLog = dataAccessor.newAuditLog(accessToken, AccessType.USER_AUTHOR_FOLLOWING, userAuthor);
userAuthor.setFollowState(followState);
userAuthor.setFollowDate(new Date());
userAuthor = dataAccessor.createOrUpdateUserAuthor(userAuthor, auditLog);
return createUserAuthorData(userAuthor);
}
Aggregations