com.sqm.dashboard.dao.impl.UserDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.sqm.dashboard.dao.impl.UserDaoImpl.java

Source

/*
 * Copyright (c) 2008 - 2013 10gen, Inc. <http://10gen.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.sqm.dashboard.dao.impl;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoException;

import java.net.UnknownHostException;

import com.sqm.dashboard.dao.UserDAO;

public class UserDaoImpl implements UserDAO {

    // validates that username is unique and insert into db
    public boolean addUser(String firstname) throws UnknownHostException {

        // String passwordHash = makePasswordHash(password,
        // Integer.toString(random.nextInt()));

        BasicDBObject user = new BasicDBObject();
        System.out.println("In USerDAO: " + firstname);
        user.append("firstname", firstname);

        try {
            final DBCollection usersCollection;

            final MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost"));
            final DB blogDatabase = mongoClient.getDB("blog");
            usersCollection = blogDatabase.getCollection("user");

            usersCollection.insert(user);
            return true;
        } catch (MongoException.DuplicateKey e) {
            System.out.println("Username already in use: " + firstname);
            return false;
        }
    }

    /*
     * public DBObject validateLogin(String username, String password) {
     * DBObject user;
     * 
     * user = usersCollection.findOne(new BasicDBObject("_id", username));
     * 
     * if (user == null) { System.out.println("User not in database"); return
     * null; }
     * 
     * String hashedAndSalted = user.get("password").toString();
     * 
     * String salt = hashedAndSalted.split(",")[1];
     * 
     * if (!hashedAndSalted.equals(makePasswordHash(password, salt))) {
     * System.out.println("Submitted password is not a match"); return null; }
     * 
     * return user; }
     * 
     * 
     * private String makePasswordHash(String password, String salt) { try {
     * String saltedAndHashed = password + "," + salt; MessageDigest digest =
     * MessageDigest.getInstance("MD5");
     * digest.update(saltedAndHashed.getBytes()); BASE64Encoder encoder = new
     * BASE64Encoder(); byte hashedBytes[] = (new String(digest.digest(),
     * "UTF-8")).getBytes(); return encoder.encode(hashedBytes) + "," + salt; }
     * catch (NoSuchAlgorithmException e) { throw new
     * RuntimeException("MD5 is not available", e); } catch
     * (UnsupportedEncodingException e) { throw new
     * RuntimeException("UTF-8 unavailable?  Not a chance", e); } }
     */
}