Search in sources :

Example 1 with UserPreferenceRtdb

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

the class AuditLogProcessApi method _updateEmailTable.

private void _updateEmailTable(List<Email> emailList) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Set<Long> userIds = new HashSet<>(emailList.size());
    for (Email email : emailList) userIds.add(email.getUserId());
    Map<Long, UserPreferenceRtdb> userPreferenceMap = DataAccessorFactory.getRtdbAccessor().getUserPreferences(userIds);
    Map<Long, User> users = dataAccessor.getUsers(userIds);
    List<Email> emailsToUpdate = new ArrayList<>(emailList.size());
    for (Email email : emailList) {
        UserPreferenceRtdb preference = userPreferenceMap.get(email.getUserId());
        User user = users.get(email.getUserId());
        if (user.getEmail() == null)
            continue;
        if (preference.getEmailFrequency() == EmailFrequency.NEVER)
            continue;
        email.setScheduledDate(preference.getEmailFrequency().getNextSchedule(user.getLastEmailedDate()));
        emailsToUpdate.add(email);
    }
    emailsToUpdate = dataAccessor.createOrUpdateEmailList(emailsToUpdate);
}
Also used : Email(com.pratilipi.data.type.Email) User(com.pratilipi.data.type.User) DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) UserPreferenceRtdb(com.pratilipi.data.type.UserPreferenceRtdb) HashSet(java.util.HashSet)

Example 2 with UserPreferenceRtdb

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

the class RtdbAccessorFirebaseImpl method getUserPreferences.

@Override
public Map<Long, UserPreferenceRtdb> getUserPreferences(Collection<Long> userIds) throws UnexpectedServerException {
    if (userIds == null || userIds.isEmpty())
        return new HashMap<>();
    userIds = new HashSet<>(userIds);
    List<String> targetUrlList = new ArrayList<>(userIds.size());
    for (Long userId : userIds) targetUrlList.add(_getUserPreferenceDbUrl(userId));
    Map<String, BlobEntry> blobEntries = HttpUtil.doGet(targetUrlList, headersMap);
    Map<Long, UserPreferenceRtdb> userPreferences = new HashMap<>(userIds.size());
    try {
        for (Long userId : userIds) {
            BlobEntry blobEntry = blobEntries.get(_getUserPreferenceDbUrl(userId));
            String jsonStr = new String(blobEntry.getData(), "UTF-8");
            userPreferences.put(userId, _getUserPreferenceRtdb(jsonStr));
        }
    } catch (UnsupportedEncodingException e) {
        logger.log(Level.SEVERE, "Failed to parse response from Firebase.", e);
        throw new UnexpectedServerException();
    }
    return userPreferences;
}
Also used : HashMap(java.util.HashMap) BlobEntry(com.pratilipi.data.type.BlobEntry) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) UserPreferenceRtdb(com.pratilipi.data.type.UserPreferenceRtdb)

Example 3 with UserPreferenceRtdb

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

the class RtdbAccessorWithMemcache method getUserPreferences.

@Override
public Map<Long, UserPreferenceRtdb> getUserPreferences(Collection<Long> userIds) throws UnexpectedServerException {
    if (userIds == null || userIds.isEmpty())
        return new HashMap<>();
    userIds = new HashSet<>(userIds);
    Set<String> memcacheIds = new HashSet<>(userIds.size());
    for (Long userId : userIds) memcacheIds.add(_getUserPreferenceMemcacheId(userId));
    Map<String, UserPreferenceRtdb> userPrefs = memcache.getAll(memcacheIds);
    Map<Long, UserPreferenceRtdb> userPreferences = new HashMap<>(userIds.size());
    List<Long> missingUserIdList = new ArrayList<>();
    for (Long userId : userIds) {
        UserPreferenceRtdb userPref = userPrefs.get(_getUserPreferenceMemcacheId(userId));
        if (userPref != null)
            userPreferences.put(userId, userPref);
        else
            missingUserIdList.add(userId);
    }
    if (!missingUserIdList.isEmpty()) {
        Map<Long, UserPreferenceRtdb> missingUserPreferences = rtdbAccessor.getUserPreferences(missingUserIdList);
        userPreferences.putAll(missingUserPreferences);
        Map<String, UserPreferenceRtdb> missingUserPrefs = new HashMap<>(missingUserIdList.size());
        for (Long userId : missingUserIdList) missingUserPrefs.put(_getUserPreferenceMemcacheId(userId), missingUserPreferences.get(userId));
        memcache.putAll(missingUserPrefs, expirationDeltaMinutes);
    }
    return userPreferences;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UserPreferenceRtdb(com.pratilipi.data.type.UserPreferenceRtdb) HashSet(java.util.HashSet)

Example 4 with UserPreferenceRtdb

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

the class RtdbAccessorFirebaseImpl method _getUserPreferences.

private Map<Long, UserPreferenceRtdb> _getUserPreferences(Map<String, String> paramsMap) throws UnexpectedServerException {
    try {
        BlobEntry blobEntry = HttpUtil.doGet(_getUserPreferenceDbUrl(), headersMap, paramsMap);
        String jsonStr = new String(blobEntry.getData(), "UTF-8");
        JsonObject json = new Gson().fromJson(jsonStr, JsonElement.class).getAsJsonObject();
        Map<Long, UserPreferenceRtdb> userPreferenceMap = new HashMap<>();
        for (Entry<String, JsonElement> entry : json.entrySet()) userPreferenceMap.put(Long.parseLong(entry.getKey()), _getUserPreferenceRtdb(entry.getValue().getAsJsonObject()));
        return userPreferenceMap;
    } catch (UnsupportedEncodingException e) {
        logger.log(Level.SEVERE, e.getMessage());
        throw new UnexpectedServerException();
    }
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) HashMap(java.util.HashMap) JsonElement(com.google.gson.JsonElement) BlobEntry(com.pratilipi.data.type.BlobEntry) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UserPreferenceRtdb(com.pratilipi.data.type.UserPreferenceRtdb)

Example 5 with UserPreferenceRtdb

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

the class RtdbAccessorWithMemcache method getUserPreference.

// PREFERENCE Table
@Override
public UserPreferenceRtdb getUserPreference(Long userId) throws UnexpectedServerException {
    if (userId == null)
        return null;
    UserPreferenceRtdb userPref = memcache.get(_getUserPreferenceMemcacheId(userId));
    if (userPref == null) {
        userPref = rtdbAccessor.getUserPreference(userId);
        memcache.put(_getUserPreferenceMemcacheId(userId), userPref, expirationDeltaMinutes);
    }
    return userPref;
}
Also used : UserPreferenceRtdb(com.pratilipi.data.type.UserPreferenceRtdb)

Aggregations

UserPreferenceRtdb (com.pratilipi.data.type.UserPreferenceRtdb)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)2 DataAccessor (com.pratilipi.data.DataAccessor)2 BlobEntry (com.pratilipi.data.type.BlobEntry)2 Email (com.pratilipi.data.type.Email)2 User (com.pratilipi.data.type.User)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Gson (com.google.gson.Gson)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 Get (com.pratilipi.api.annotation.Get)1 GenericResponse (com.pratilipi.api.shared.GenericResponse)1 RtdbAccessor (com.pratilipi.data.RtdbAccessor)1 Task (com.pratilipi.taskqueue.Task)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1