Search in sources :

Example 96 with DataAccessor

use of com.pratilipi.data.DataAccessor 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;
}
Also used : User(com.pratilipi.data.type.User) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) UserData(com.pratilipi.data.client.UserData) DataAccessor(com.pratilipi.data.DataAccessor) JsonObject(com.google.gson.JsonObject) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 97 with DataAccessor

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

the class UserDataUtil method _sendEmailVerificationMail.

private static void _sendEmailVerificationMail(User user, Language language) throws InvalidArgumentException, UnexpectedServerException {
    String verificationToken = _getNextToken(user.getVerificationToken());
    if (!verificationToken.equals(user.getVerificationToken())) {
        DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
        user.setVerificationToken(verificationToken);
        user = dataAccessor.createOrUpdateUser(user);
    }
    Map<String, String> dataModel = new HashMap<>();
    String verificationLink = "/" + "?" + "email=" + user.getEmail() + "&" + "token=" + verificationToken.substring(0, verificationToken.indexOf("|")) + "&" + "verifyUser=" + Boolean.TRUE;
    dataModel.put("emailVerificationUrl", verificationLink);
    EmailUtil.sendMail(createUserData(user).getDisplayName(), user.getEmail(), "verification", language, dataModel);
}
Also used : HashMap(java.util.HashMap) DataAccessor(com.pratilipi.data.DataAccessor)

Example 98 with DataAccessor

use of com.pratilipi.data.DataAccessor 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);
}
Also used : User(com.pratilipi.data.type.User) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) AuditLog(com.pratilipi.data.type.AuditLog)

Example 99 with DataAccessor

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

the class NotificationDataUtil method saveNotificationState.

public static void saveNotificationState(Map<Long, NotificationState> notificationStates) throws InsufficientAccessException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Map<Long, Notification> notifications = dataAccessor.getNotifications(notificationStates.keySet());
    for (Map.Entry<Long, NotificationState> entry : notificationStates.entrySet()) {
        Notification notification = notifications.get(entry.getKey());
        if (notification.getState() == entry.getValue())
            continue;
        if (!hasAccessToUpdateData(notification))
            throw new InsufficientAccessException();
        notification.setState(entry.getValue());
    }
    dataAccessor.createOrUpdateNotificationList(new ArrayList<Notification>(notifications.values()));
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) HashMap(java.util.HashMap) Map(java.util.Map) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) Notification(com.pratilipi.data.type.Notification) NotificationState(com.pratilipi.common.type.NotificationState)

Example 100 with DataAccessor

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

the class NotificationDataUtil method sendFcm.

public static void sendFcm(Long notifId) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Notification notif = dataAccessor.getNotification(notifId);
    if (!notif.isFcmPending())
        return;
    if (notif.getState() == NotificationState.UNREAD) {
        List<Notification> notifList = new ArrayList<>(1);
        notifList.add(notif);
        // TODO: Implement createNotificationData
        NotificationData notifData = createNotificationDataList(notifList, null, true).get(0);
        if (notifData.getMessage() != null) {
            // TODO: Cancel previously sent FCM if notifData.getMessage() == null
            List<String> fcmTokenList = dataAccessor.getFcmTokenList(notifData.getUserId());
            if (fcmTokenList.size() != 0) {
                String fcmResponse = FirebaseApi.sendCloudMessage2(fcmTokenList, notifData.getMessage(), notifData.getId().toString(), notifData.getNotificationType().getAndroidHandler(), notifData.getSourceId(), notifData.getSourceImageUrl(), notifData.getDisplayImageUrl());
                if (notif.getFcmResponse() == null)
                    notif.setFcmResponse(fcmResponse);
                else
                    notif.setFcmResponse(notif.getFcmResponse() + "\n" + fcmResponse);
            }
        }
    }
    notif.setFcmPending(false);
    notif = dataAccessor.createOrUpdateNotification(notif);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) Notification(com.pratilipi.data.type.Notification) NotificationData(com.pratilipi.data.client.NotificationData)

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