Search in sources :

Example 76 with DataAccessor

use of com.pratilipi.data.DataAccessor 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 77 with DataAccessor

use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.

the class UserDataUtil method _loginFederatedUser.

private static UserData _loginFederatedUser(UserData apiUserData, User user, UserSignUpSource signUpSource) throws InsufficientAccessException {
    // Do nothing if a user is already logged in
    if (!AccessTokenFilter.getAccessToken().getUserId().equals(0L))
        return getCurrentUser();
    // Note: Users having Facebook or Google Id can never be in GUEST or REFERRAL state.
    // Note: No action required for REGISTERED or ACTIVE users.
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    boolean isNew = false;
    if (user == null || user.getState() == UserState.DELETED) {
        if (apiUserData.getEmail() != null)
            user = dataAccessor.getUserByEmail(apiUserData.getEmail());
        AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), null, null);
        if (user == null || user.getState() == UserState.DELETED) {
            user = dataAccessor.newUser();
            auditLog.setAccessType(AccessType.USER_ADD);
            auditLog.setEventDataOld(user);
            user.setEmail(apiUserData.getEmail());
            // Counting on Facebook / Google for e-mail/user verification
            user.setState(UserState.ACTIVE);
            user.setSignUpDate(new Date());
            user.setSignUpSource(signUpSource);
            isNew = true;
        } else if (user.getState() == UserState.REFERRAL) {
            auditLog.setAccessType(AccessType.USER_ADD);
            auditLog.setEventDataOld(user);
            // Counting on Facebook / Google for e-mail/user verification
            user.setState(UserState.ACTIVE);
            user.setSignUpDate(new Date());
            user.setSignUpSource(signUpSource);
            isNew = true;
        } else if (user.getState() == UserState.REGISTERED) {
            auditLog.setAccessType(AccessType.USER_UPDATE);
            auditLog.setEventDataOld(user);
            // Counting on Facebook / google for e-mail/user verification
            user.setState(UserState.ACTIVE);
        } else {
            // user.getState() == UserState.ACTIVE || user.getState() == UserState.BLOCKED
            auditLog.setAccessType(AccessType.USER_UPDATE);
            auditLog.setEventDataOld(user);
        }
        if (apiUserData.getFacebookId() != null)
            user.setFacebookId(apiUserData.getFacebookId());
        if (apiUserData.getGoogleId() != null)
            user.setGoogleId(apiUserData.getGoogleId());
        user.setLastUpdated(new Date());
        user = dataAccessor.createOrUpdateUser(user, auditLog);
    }
    if (user.getState() == UserState.BLOCKED)
        throw new InsufficientAccessException(GenericRequest.ERR_ACCOUNT_BLOCKED);
    _loginUser(AccessTokenFilter.getAccessToken(), user);
    UserData userData = createUserData(user);
    if (isNew) {
        userData.setFirstName(apiUserData.getFirstName());
        userData.setLastName(apiUserData.getLastName());
        userData.setGender(apiUserData.getGender());
        userData.setDateOfBirth(apiUserData.getDateOfBirth());
    }
    return userData;
}
Also used : UserData(com.pratilipi.data.client.UserData) DataAccessor(com.pratilipi.data.DataAccessor) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 78 with DataAccessor

use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.

the class PratilipiDataUtil method updatePratilipiTags.

public static boolean updatePratilipiTags(long pratilipiId, PratilipiType pratilipiType, List<Long> tagIds, List<String> suggestedTags) throws InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (pratilipi == null)
        return false;
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
    PratilipiData pratilipiData = createPratilipiData(pratilipi);
    AccessToken accessToken = AccessTokenFilter.getAccessToken();
    logger.log(Level.INFO, "User Id : " + accessToken.getUserId());
    if (!hasAccessToUpdatePratilipiData(pratilipi, pratilipiData)) {
        throw new InsufficientAccessException();
    }
    if (pratilipiType != null)
        pratilipi.setType(pratilipiType);
    if (tagIds != null)
        pratilipi.setTagIds(tagIds);
    if (suggestedTags != null)
        pratilipi.setSuggestedTags(suggestedTags);
    dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
    return true;
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) PratilipiData(com.pratilipi.data.client.PratilipiData) AccessToken(com.pratilipi.data.type.AccessToken) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog)

Example 79 with DataAccessor

use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.

the class PratilipiDataUtil method createPratilipiDataList.

public static List<PratilipiData> createPratilipiDataList(List<Long> pratilipiIdList, boolean includeAuthorData, boolean includeMetaData) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    List<Pratilipi> pratilipiList = dataAccessor.getPratilipiList(pratilipiIdList);
    Map<Long, Page> pratilipiPages = dataAccessor.getPages(PageType.PRATILIPI, pratilipiIdList);
    Map<Long, AuthorData> authorIdToDataMap = null;
    if (includeAuthorData) {
        List<Long> authorIdList = new LinkedList<>();
        for (Pratilipi pratilipi : pratilipiList) if (pratilipi.getAuthorId() != null && !authorIdList.contains(pratilipi.getAuthorId()))
            authorIdList.add(pratilipi.getAuthorId());
        List<AuthorData> authorDataList = AuthorDataUtil.createAuthorDataList(authorIdList, false);
        authorIdToDataMap = new HashMap<>(authorDataList.size());
        for (AuthorData authorData : authorDataList) authorIdToDataMap.put(authorData.getId(), authorData);
    }
    List<PratilipiData> pratilipiDataList = new ArrayList<>(pratilipiList.size());
    for (Pratilipi pratilipi : pratilipiList) {
        PratilipiData pratilipiData = createPratilipiData(pratilipi, pratilipiPages.get(pratilipi.getId()), null, includeMetaData);
        if (includeAuthorData && pratilipi.getAuthorId() != null)
            pratilipiData.setAuthor(authorIdToDataMap.get(pratilipi.getAuthorId()));
        pratilipiData.setRelevance(calculateRelevance(pratilipi, dataAccessor.getAuthor(pratilipi.getAuthorId())));
        pratilipiDataList.add(pratilipiData);
    }
    // Fetching UserPratilipi list from DataStore
    List<UserPratilipi> userPratilipiList = dataAccessor.getUserPratilipiList(AccessTokenFilter.getAccessToken().getUserId(), pratilipiIdList);
    // Setting isAddedToLib flag for for each PratilipiData in the list
    for (int i = 0; i < userPratilipiList.size(); i++) {
        UserPratilipi userPratilipi = userPratilipiList.get(i);
        pratilipiDataList.get(i).setAddedToLib(userPratilipi != null && userPratilipi.isAddedToLib());
    }
    return pratilipiDataList;
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) Page(com.pratilipi.data.type.Page) LinkedList(java.util.LinkedList) UserPratilipi(com.pratilipi.data.type.UserPratilipi) PratilipiData(com.pratilipi.data.client.PratilipiData) AuthorData(com.pratilipi.data.client.AuthorData) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi)

Example 80 with DataAccessor

use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.

the class PratilipiDataUtil method updateUserPratilipiStats.

public static void updateUserPratilipiStats(Long pratilipiId) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (pratilipi.getState() != PratilipiState.PUBLISHED)
        return;
    PratilipiReviewsDoc reviewsDoc = docAccessor.getPratilipiReviewsDoc(pratilipiId);
    if (pratilipi.getRatingCount().equals(reviewsDoc.getRatingCount()) && pratilipi.getTotalRating().equals(reviewsDoc.getTotalRating()) && pratilipi.getReviewCount().equals(reviewsDoc.getReviewCount()))
        return;
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
    pratilipi.setRatingCount(reviewsDoc.getRatingCount());
    pratilipi.setTotalRating(reviewsDoc.getTotalRating());
    pratilipi.setReviewCount(reviewsDoc.getReviewCount());
    pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) AuditLog(com.pratilipi.data.type.AuditLog) PratilipiReviewsDoc(com.pratilipi.data.type.PratilipiReviewsDoc)

Aggregations

DataAccessor (com.pratilipi.data.DataAccessor)162 Date (java.util.Date)53 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)37 Pratilipi (com.pratilipi.data.type.Pratilipi)34 Author (com.pratilipi.data.type.Author)33 AuditLog (com.pratilipi.data.type.AuditLog)32 UserPratilipi (com.pratilipi.data.type.UserPratilipi)32 ArrayList (java.util.ArrayList)30 Page (com.pratilipi.data.type.Page)28 HashMap (java.util.HashMap)25 GenericResponse (com.pratilipi.api.shared.GenericResponse)24 DocAccessor (com.pratilipi.data.DocAccessor)22 User (com.pratilipi.data.type.User)22 Get (com.pratilipi.api.annotation.Get)20 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)19 PratilipiData (com.pratilipi.data.client.PratilipiData)15 UserData (com.pratilipi.data.client.UserData)14 BlobAccessor (com.pratilipi.data.BlobAccessor)13 Email (com.pratilipi.data.type.Email)13 JsonObject (com.google.gson.JsonObject)12