use of com.pratilipi.data.type.AuditLog in project pratilipi by Pratilipi.
the class UserPratilipiDataUtil method saveUserPratilipiAddToLibrary.
public static UserPratilipiData saveUserPratilipiAddToLibrary(Long userId, Long pratilipiId, String lastOpenedPage, Boolean addedToLibrary) throws InsufficientAccessException, UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
UserPratilipi userPratilipi = dataAccessor.getUserPratilipi(userId, pratilipiId);
if (userPratilipi == null) {
userPratilipi = dataAccessor.newUserPratilipi();
userPratilipi.setUserId(userId);
userPratilipi.setPratilipiId(pratilipiId);
}
if (lastOpenedPage == null && addedToLibrary == null)
return createUserPratilipiData(userPratilipi);
if (!hasAccessToUpdateUserPratilipiData(userPratilipi, AccessType.USER_PRATILIPI_LIBRARY))
throw new InsufficientAccessException();
AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_PRATILIPI_LIBRARY, userPratilipi);
if (lastOpenedPage != null) {
userPratilipi.setLastOpenedPage(lastOpenedPage);
userPratilipi.setLastOpenedDate(new Date());
}
if (addedToLibrary != null) {
userPratilipi.setAddedToLib(addedToLibrary);
userPratilipi.setAddedToLibDate(new Date());
}
userPratilipi = dataAccessor.createOrUpdateUserPratilipi(userPratilipi, auditLog);
return createUserPratilipiData(userPratilipi);
}
use of com.pratilipi.data.type.AuditLog in project pratilipi by Pratilipi.
the class UserDataUtil method registerUser.
public static UserData registerUser(String firstName, String lastName, String email, String password, UserSignUpSource signUpSource) throws InvalidArgumentException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
User user = dataAccessor.getUserByEmail(email.toLowerCase());
if (user == null || user.getState() == UserState.DELETED) {
user = dataAccessor.newUser();
} else if (user.getState() != UserState.REFERRAL) {
JsonObject errorMessages = new JsonObject();
errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REGISTERED_ALREADY);
throw new InvalidArgumentException(errorMessages);
}
AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_ADD, user);
user.setPassword(PasswordUtil.getSaltedHash(password));
user.setEmail(email.toLowerCase());
user.setState(UserState.REGISTERED);
user.setSignUpDate(new Date());
user.setSignUpSource(signUpSource);
user = dataAccessor.createOrUpdateUser(user, auditLog);
UserData userData = createUserData(user);
userData.setFirstName(firstName);
userData.setLastName(lastName);
return userData;
}
use of com.pratilipi.data.type.AuditLog in project pratilipi by Pratilipi.
the class UserDataUtil method verifyUserEmail.
public static void verifyUserEmail(String email, String verificationToken) throws InvalidArgumentException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
User user = dataAccessor.getUserByEmail(email.toLowerCase());
if (user == null || user.getState() != UserState.REGISTERED)
return;
if (!_verifyToken(user, verificationToken))
throw new InvalidArgumentException(GenericRequest.ERR_VERIFICATION_TOKEN_INVALID_OR_EXPIRED);
AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_UPDATE, user);
user.setState(UserState.ACTIVE);
// Note: Do not clear VerificationToken
user = dataAccessor.createOrUpdateUser(user, auditLog);
}
use of com.pratilipi.data.type.AuditLog in project pratilipi by Pratilipi.
the class PratilipiDocUtil method updateMeta.
public static boolean updateMeta(Long pratilipiId) throws UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(pratilipiId);
PratilipiMetaDoc pmDoc = docAccessor.getPratilipiMetaDoc(pratilipiId);
if (pmDoc == null)
pmDoc = docAccessor.newPratilipiMetaDoc();
int wordCount = 0;
int imageCount = 0;
int chapterCount = 0;
Map<String, Integer> wordCounts = new HashMap<>();
for (PratilipiContentDoc.Chapter chapter : pcDoc.getChapterList()) {
String content = chapter.getTitle() == null ? "" : chapter.getTitle();
for (PratilipiContentDoc.Page page : chapter.getPageList()) {
for (PratilipiContentDoc.Pagelet pagelet : page.getPageletList()) {
if (pagelet.getType() == PratilipiContentDoc.PageletType.IMAGE)
imageCount++;
else
content += " " + pagelet.getDataAsString();
}
}
content = content.trim();
if (content.length() > 1) {
String[] words = content.replaceAll(nonKeywordsPattern, " ").split("[\\s]+");
for (String word : words) {
Integer count = wordCounts.get(word);
count = count == null ? 1 : count++;
wordCounts.put(word, count);
}
wordCount += words.length;
}
chapterCount++;
}
boolean isChanged = wordCounts.size() != pmDoc.getWordCounts().size();
if (!isChanged) {
for (Entry<String, Integer> entry : pmDoc.getWordCounts().entrySet()) {
if (!entry.getValue().equals(wordCounts.get(entry.getKey()))) {
isChanged = true;
break;
}
}
}
if (isChanged) {
// Update & Save Pratilipi Meta Doc
pmDoc.setWordCounts(wordCounts);
docAccessor.save(pratilipiId, pmDoc);
// Update counts in Pratilipi Entity
AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
pratilipi.setWordCount(wordCount);
pratilipi.setImageCount(imageCount);
pratilipi.setChapterCount(chapterCount);
pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
}
return isChanged;
}
use of com.pratilipi.data.type.AuditLog in project pratilipi by Pratilipi.
the class MailingListSubscriptionDataUtil method subscribe.
public static void subscribe(MailingList mailingList, Long userId, String email, String phone, Language language, String comment) throws InvalidArgumentException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
MailingListSubscription mailingListSubscription = null;
if (email != null) {
List<MailingListSubscription> mailingListSubscriptionList = dataAccessor.getMailingListSubscriptionList(mailingList, email, null);
for (MailingListSubscription subs : mailingListSubscriptionList) {
if (language != null && subs.getLanguage() != null && language != subs.getLanguage())
continue;
if (phone != null && subs.getPhone() != null && !phone.equals(subs.getPhone()))
continue;
if (comment != null && subs.getComment() != null && !comment.equals(subs.getComment()))
continue;
mailingListSubscription = subs;
break;
}
} else if (phone != null) {
List<MailingListSubscription> mailingListSubscriptionList = dataAccessor.getMailingListSubscriptionList(mailingList, null, phone);
for (MailingListSubscription subs : mailingListSubscriptionList) {
if (language != null && subs.getLanguage() != null && language != subs.getLanguage())
continue;
if (comment != null && subs.getComment() != null && !comment.equals(subs.getComment()))
continue;
mailingListSubscription = subs;
break;
}
} else {
throw new InvalidArgumentException("Either email or phone must be provided.");
}
if (mailingListSubscription == null)
mailingListSubscription = dataAccessor.newMailingListSubscription();
AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.MAILING_LIST_SUBSCRIPTION_ADD, mailingListSubscription);
boolean bool = true;
if (// New entry
mailingListSubscription.getMailingList() == null)
mailingListSubscription.setMailingList(mailingList);
if (userId != null && mailingListSubscription.getUserId() == null)
mailingListSubscription.setUserId(userId);
if (email != null && mailingListSubscription.getEmail() == null) {
mailingListSubscription.setEmail(email);
bool = false;
}
if (phone != null && mailingListSubscription.getPhone() == null) {
mailingListSubscription.setPhone(phone);
bool = false;
}
if (language != null && mailingListSubscription.getLanguage() == null) {
mailingListSubscription.setLanguage(language);
bool = false;
}
if (comment != null && mailingListSubscription.getComment() == null) {
mailingListSubscription.setComment(comment);
bool = false;
}
if (// New entry
mailingListSubscription.getSubscriptionDate() == null)
mailingListSubscription.setSubscriptionDate(new Date());
if (bool)
throw new InvalidArgumentException(GenericRequest.ERR_MAILING_LIST_SUBSCRIBED_ALREDY);
dataAccessor.createOrUpdateMailingListSubscription(mailingListSubscription, auditLog);
}
Aggregations