Search in sources :

Example 11 with User

use of com.pratilipi.data.type.User 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;
}
Also used : User(com.pratilipi.data.type.User) HashMap(java.util.HashMap) DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) Page(com.pratilipi.data.type.Page) AuthorData(com.pratilipi.data.client.AuthorData) Author(com.pratilipi.data.type.Author)

Example 12 with User

use of com.pratilipi.data.type.User in project pratilipi by Pratilipi.

the class UserDataUtil method updateUserAuthorStats.

public static void updateUserAuthorStats(Long userId) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    User user = dataAccessor.getUser(userId);
    UserFollowsDoc followsDoc = docAccessor.getUserFollowsDoc(userId);
    if (user.getFollowCount().equals(followsDoc.getFollows(UserFollowState.FOLLOWING).size()))
        return;
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_UPDATE, user);
    user.setFollowCount((long) followsDoc.getFollows(UserFollowState.FOLLOWING).size());
    user = dataAccessor.createOrUpdateUser(user, auditLog);
}
Also used : User(com.pratilipi.data.type.User) DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) UserFollowsDoc(com.pratilipi.data.type.UserFollowsDoc) AuditLog(com.pratilipi.data.type.AuditLog)

Example 13 with User

use of com.pratilipi.data.type.User in project pratilipi by Pratilipi.

the class UserDataUtil method updateUserPassword.

public static void updateUserPassword(String email, String verificationToken, String newPassword) throws InvalidArgumentException, InsufficientAccessException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    User user = dataAccessor.getUserByEmail(email.toLowerCase());
    JsonObject errorMessages = new JsonObject();
    if (user == null || user.getState() == UserState.REFERRAL || user.getState() == UserState.DELETED) {
        errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_NOT_REGISTERED);
        throw new InvalidArgumentException(errorMessages);
    } else if (user.getState() == UserState.BLOCKED) {
        throw new InvalidArgumentException(GenericRequest.ERR_ACCOUNT_BLOCKED);
    }
    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.setPassword(PasswordUtil.getSaltedHash(newPassword));
    // Note: Do not clear VerificationToken
    user = dataAccessor.createOrUpdateUser(user, auditLog);
}
Also used : User(com.pratilipi.data.type.User) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) JsonObject(com.google.gson.JsonObject) AuditLog(com.pratilipi.data.type.AuditLog)

Example 14 with User

use of com.pratilipi.data.type.User in project pratilipi by Pratilipi.

the class UserDataUtil method _validateUserDataForSave.

private static void _validateUserDataForSave(UserData userData) throws InvalidArgumentException {
    boolean isNew = userData.getId() == null;
    JsonObject errorMessages = new JsonObject();
    // New user profile must have name.
    if (isNew && userData.getFirstName() == null)
        errorMessages.addProperty("name", GenericRequest.ERR_NAME_REQUIRED);
    // New user profile must have email.
    if (isNew && (!userData.hasEmail() || userData.getEmail() == null))
        errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REQUIRED);
    else // Email can not be un-set or set to null.
    if (!isNew && userData.hasEmail() && userData.getEmail() == null)
        errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REQUIRED);
    // For new user, user email should be not registered already.
    if (isNew && DataAccessorFactory.getDataAccessor().getUserByEmail(userData.getEmail()) != null)
        errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REGISTERED_ALREADY);
    else // Email, if provided, must not be registered with some other user.
    if (!isNew && userData.hasEmail() && userData.getEmail() != null) {
        User user = DataAccessorFactory.getDataAccessor().getUserByEmail(userData.getEmail());
        if (user != null && !user.getId().equals(userData.getId()))
            errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REGISTERED_ALREADY);
    }
    if (errorMessages.entrySet().size() > 0)
        throw new InvalidArgumentException(errorMessages);
}
Also used : User(com.pratilipi.data.type.User) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) JsonObject(com.google.gson.JsonObject)

Example 15 with User

use of com.pratilipi.data.type.User in project pratilipi by Pratilipi.

the class UserAuthorDataUtil method getUserFollowList.

public static DataListCursorTuple<AuthorData> getUserFollowList(Long userId, String cursor, Integer offset, Integer resultCount) {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    User user = dataAccessor.getUser(userId);
    if (user.getFollowCount() == 0L)
        return new DataListCursorTuple<>(new ArrayList<AuthorData>(0), null, 0L);
    DataListCursorTuple<Long> authorIdListCursorTuple = dataAccessor.getUserAuthorFollowList(userId, null, cursor, offset, resultCount);
    List<Long> authorIdList = authorIdListCursorTuple.getDataList();
    List<AuthorData> authorDataList = AuthorDataUtil.createAuthorDataList(authorIdList, true);
    // Setting AuthorData.isFollowing flag
    if (userId.equals(AccessTokenFilter.getAccessToken().getUserId())) {
        for (AuthorData authorData : authorDataList) authorData.setFollowing(true);
    } else {
        List<UserAuthor> userAuthorList = dataAccessor.getUserAuthorList(AccessTokenFilter.getAccessToken().getUserId(), authorIdList);
        if (userAuthorList.size() != 0)
            for (int i = 0; i < authorIdList.size(); i++) if (userAuthorList.get(i) != null && userAuthorList.get(i).getFollowState() == UserFollowState.FOLLOWING)
                authorDataList.get(i).setFollowing(true);
    }
    // Setting UserData.isFollowing flag
    Author author = dataAccessor.getAuthorByUserId(AccessTokenFilter.getAccessToken().getUserId());
    if (author != null) {
        List<Long> userIdList = new ArrayList<>(authorDataList.size());
        List<UserData> userDataList = new ArrayList<>(authorDataList.size());
        for (AuthorData authorData : authorDataList) {
            if (authorData.getUser().getId() != null) {
                userIdList.add(authorData.getUser().getId());
                userDataList.add(authorData.getUser());
            }
        }
        List<UserAuthor> userAuthorList = dataAccessor.getUserAuthorList(userIdList, author.getId());
        if (userAuthorList != null) {
            for (UserAuthor userAuthor : userAuthorList) if (userAuthor != null && userAuthor.getFollowState() == UserFollowState.FOLLOWING)
                userDataList.get(userIdList.indexOf(userAuthor.getUserId())).setFollowing(true);
        }
    }
    return new DataListCursorTuple<>(authorDataList, authorIdListCursorTuple.getCursor(), user.getFollowCount());
}
Also used : User(com.pratilipi.data.type.User) UserData(com.pratilipi.data.client.UserData) DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) DataListCursorTuple(com.pratilipi.data.DataListCursorTuple) UserAuthorData(com.pratilipi.data.client.UserAuthorData) AuthorData(com.pratilipi.data.client.AuthorData) UserAuthor(com.pratilipi.data.type.UserAuthor) UserAuthor(com.pratilipi.data.type.UserAuthor) Author(com.pratilipi.data.type.Author)

Aggregations

User (com.pratilipi.data.type.User)30 DataAccessor (com.pratilipi.data.DataAccessor)22 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)9 AuditLog (com.pratilipi.data.type.AuditLog)8 UserData (com.pratilipi.data.client.UserData)7 UserEntity (com.pratilipi.data.type.gae.UserEntity)7 Date (java.util.Date)7 Author (com.pratilipi.data.type.Author)6 ConversationUser (com.pratilipi.data.type.ConversationUser)5 ArrayList (java.util.ArrayList)5 JsonObject (com.google.gson.JsonObject)4 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)4 ConversationUserEntity (com.pratilipi.data.type.gae.ConversationUserEntity)4 GenericResponse (com.pratilipi.api.shared.GenericResponse)3 DateFormat (java.text.DateFormat)3 SimpleDateFormat (java.text.SimpleDateFormat)3 HashMap (java.util.HashMap)3 Get (com.pratilipi.api.annotation.Get)2 BlobAccessor (com.pratilipi.data.BlobAccessor)2 DocAccessor (com.pratilipi.data.DocAccessor)2