org.gravidence.gravifon.db.UsersDBClient.java Source code

Java tutorial

Introduction

Here is the source code for org.gravidence.gravifon.db.UsersDBClient.java

Source

/*
 * The MIT License
 *
 * Copyright 2013 Gravidence.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.gravidence.gravifon.db;

import com.fasterxml.jackson.databind.node.ArrayNode;
import java.util.List;
import javax.ws.rs.client.WebTarget;
import org.gravidence.gravifon.db.domain.UserDocument;
import org.gravidence.gravifon.db.domain.UserStatus;
import org.gravidence.gravifon.util.BasicUtils;
import org.gravidence.gravifon.util.DateTimeUtils;
import org.gravidence.gravifon.util.SharedInstanceHolder;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;

/**
 * CouchDB JAX-RS client to <code>/users</code> database.
 * 
 * @author Maksim Liauchuk <maksim_liauchuk@fastmail.fm>
 */
public class UsersDBClient extends BasicDBClient<UserDocument> implements InitializingBean {

    private static final Logger LOGGER = LoggerFactory.getLogger(UsersDBClient.class);

    /**
     * JAX-RS client target associated with <code>main/all_usernames</code> view.
     */
    private WebTarget viewMainAllUsernamesTarget;

    /**
     * JAX-RS client target associated with <code>main/all_emails</code> view.
     */
    private WebTarget viewMainAllEmailsTarget;

    /**
     * JAX-RS client target associated with <code>main/by_status</code> view.
     */
    private WebTarget viewMainByStatusTarget;

    @Override
    public void afterPropertiesSet() throws Exception {
        setDbTarget(getDbClient().getTarget().path("users"));

        viewMainAllUsernamesTarget = ViewUtils.getViewTarget(getDbTarget(), "main", "all_usernames");
        viewMainAllEmailsTarget = ViewUtils.getViewTarget(getDbTarget(), "main", "all_emails");
        viewMainByStatusTarget = ViewUtils.getViewTarget(getDbTarget(), "main", "by_status");
    }

    /**
     * Retrieves user amount.<p>
     * User amount is equal to <code>main/all_usernames</code> view size.
     * 
     * @return user amount
     * 
     * @see #viewMainAllUsernamesTarget
     * @see ViewQueryExecutor#querySize(javax.ws.rs.client.WebTarget)
     */
    public long retrieveUserAmount() {
        return ViewQueryExecutor.querySize(viewMainAllUsernamesTarget);
    }

    /**
     * Retrieves existing user {@link UserDocument document}.
     * 
     * @param id user identifier
     * @return user details document if found, <code>null</code> otherwise
     */
    public UserDocument retrieveUserByID(String id) {
        return retrieve(id, UserDocument.class);
    }

    /**
     * Retrieves existing user {@link UserDocument document}.<p>
     * Makes sure that <code>username</code> written in lower case
     * since <code>main/all_usernames</code> view is case sensitive.
     * 
     * @param username username
     * @return user details document if found, <code>null</code> otherwise
     */
    public UserDocument retrieveUserByUsername(String username) {
        ViewQueryArguments args = new ViewQueryArguments().addKey(BasicUtils.lowerCase(username))
                .addIncludeDocs(true);

        List<UserDocument> documents = ViewQueryExecutor.queryDocuments(viewMainAllUsernamesTarget, args,
                UserDocument.class);

        // usernames are unique, so don't care about multiple results
        return documents == null ? null : documents.get(0);
    }

    /**
     * Retrieves existing user {@link UserDocument document}.<p>
     * Makes sure that <code>email</code> written in lower case
     * since <code>main/all_emails</code> view is case sensitive.
     * 
     * @param email user email
     * @return user details document if found, <code>null</code> otherwise
     */
    public UserDocument retrieveUserByEmail(String email) {
        ViewQueryArguments args = new ViewQueryArguments().addKey(BasicUtils.lowerCase(email)).addIncludeDocs(true);

        List<UserDocument> documents = ViewQueryExecutor.queryDocuments(viewMainAllEmailsTarget, args,
                UserDocument.class);

        // emails are unique, so don't care about multiple results
        return documents == null ? null : documents.get(0);
    }

    /**
     * Retrieves user accounts that didn't manage to complete registration in time.
     * 
     * @param threshold max allowed amount of time to complete the registration
     * @return list of user details documents
     */
    public List<UserDocument> retrieveUsersFailedToCompleteRegistration(Duration threshold) {
        ArrayNode startkey = SharedInstanceHolder.OBJECT_MAPPER.getNodeFactory().arrayNode();
        startkey.add(UserStatus.CREATED.toString());

        ArrayNode endkey = startkey.deepCopy();

        startkey.add(DateTimeUtils.dateTimeToArrayNode(DateTime.now(DateTimeZone.UTC).minus(threshold)));

        ViewQueryArguments args = new ViewQueryArguments().addStartKey(startkey).addEndKey(endkey)
                .addIncludeDocs(true).addDescending();

        List<UserDocument> documents = ViewQueryExecutor.queryDocuments(viewMainByStatusTarget, args,
                UserDocument.class);

        return documents;
    }

}