Android Open Source - Learn-From-Me User Dao






From Project

Back to project page Learn-From-Me.

License

The source code is released under:

Apache License

If you think the Android project Learn-From-Me listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.overnightApps.myapplication.app.dao;
// w  ww.  j  ava  2  s .c om
import android.graphics.Bitmap;
import android.util.Log;

import com.overnightApps.myapplication.app.core.User;
import com.overnightApps.myapplication.app.core.helper.SignUpForm;
import com.overnightApps.myapplication.app.dao.exceptions.SavedUserIsNotFoundOnBackEndException;
import com.overnightApps.myapplication.app.dao.exceptions.DataClassNotFoundException;
import com.overnightApps.myapplication.app.dao.exceptions.UnableToSignUpException;
import com.overnightApps.myapplication.app.util.BitmapUtil;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseQuery;
import com.parse.ParseUser;

import java.io.Serializable;

/**
 * Created by andre on 3/21/14.
 */
public class UserDao extends Dao<User, ParseUser,ParseQuery<ParseUser>> implements Serializable {
    public static final String FULL_NAME = "fullName";
    public static final String EXPERIENCE = "experience";
    public static final String EMAIL = "email";
    public static final String PRIVATE_SIGNATURE = "privateSignature";
    public static final String PUBLIC_SIGNATURE = "publicSignature";
    public static final String PROFILE_PICTURE = "profilePicture";
    public static final String IS_ACTIVE = "isActive";
    public static final String SIGNATURE = "Signature";
    private static UserDao userDao;

    //TODO - class too big, break it down
    private UserDao() {
    }

    public static UserDao instance() {
        if (userDao == null) {
            userDao = new UserDao();
        }
        return userDao;
    }

    @Override
    protected ParseUser getNewDataClassInstance() {
        return new ParseUser();
    }

    @Override
    protected void saveDataClassFromDomain(ParseUser parseUser, User user) {
        parseUser.setEmail(user.getEmail());
        parseUser.setUsername(user.getEmail());
        parseUser.put(EXPERIENCE,user.getExperience());
        parseUser.put(FULL_NAME, user.getFullName());
        parseUser.put(PUBLIC_SIGNATURE,user.getPublicSignature());
        parseUser.put(PRIVATE_SIGNATURE,user.getPrivateSignature());
    }

    @Override
    protected ParseQuery getNewUniqueResultQueryInstance(User user) {
        ParseQuery query = getNewQueryInstance();
        query.whereEqualTo(EMAIL, user.getEmail());
        return query;
    }

    protected ParseQuery getNewQueryInstance() {
        return ParseUser.getQuery();
    }

    public User getCurrentUser() {
        ParseUser currentUser = ParseUser.getCurrentUser();
        if (currentUser != null) {
            return convertToDomainObject(currentUser);
        } else {
            return null;
        }
    }

    public boolean isEmailAddressUsed(String email) {
        ParseQuery<ParseUser> parseQuery = ParseUser.getQuery();
        parseQuery.whereEqualTo(EMAIL,email);
        boolean isEmailAddressUsed;
        try { //TODO - replace with null object pattern
            findSingleObject(parseQuery);
            isEmailAddressUsed = true;
        } catch (DataClassNotFoundException dataClassNotFoundException) {
            isEmailAddressUsed = false;
        }
        return isEmailAddressUsed;
    }

    public User logIn(String email, String password) {
        ParseUser parseUser = null;
        try {
            parseUser = ParseUser.logIn(email, password);
        } catch (ParseException e) {
            Log.e("Tag", "Parse Error", e);
        }
        if(parseUser != null){
            return convertToDomainObject(parseUser);
        }else{
            return null;
        }
    }

    public boolean signUserUp(SignUpForm signUpForm, Bitmap profilePicture) throws UnableToSignUpException {
        ParseUser user = new ParseUser();
        user.setUsername(signUpForm.getEmail());
        user.setPassword(signUpForm.getPassword());
        user.setEmail(signUpForm.getEmail());
        user.put(FULL_NAME,signUpForm.getFullName());
        user.put(IS_ACTIVE,true);
        user.put(EXPERIENCE,0);
        user.put(PUBLIC_SIGNATURE, signUpForm.getPublicSignature());
        user.put(PRIVATE_SIGNATURE, signUpForm.getPrivateSignature());
        try {
            user.signUp();
            uploadUserPicture(profilePicture);
        } catch (ParseException e) {
            throw new UnableToSignUpException("User unable to sing up even after submitting correcting" +
                    " informtation. Info from parse: " + e.getMessage());
        }
        return true;
    }

    public static Bitmap getUserProfilePicture(ParseUser parseUser) {
        ParseFile profilePicture = parseUser.getParseFile(PROFILE_PICTURE);
        Bitmap bitmap = null;
        try {
            bitmap =  BitmapUtil.byteArrayToBitmap(profilePicture.getData());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    public static void uploadUserPicture(Bitmap bitmap) {
        ParseFile parseFile = convertBitmapToParseFile(bitmap);
        ParseUser parseUser = ParseUser.getCurrentUser();
        parseUser.put(PROFILE_PICTURE, parseFile);
        parseUser.saveInBackground();
    }

    public static ParseFile convertBitmapToParseFile(Bitmap bitmap) {
        byte[] bytes = BitmapUtil.toByteArray(bitmap);
        return new ParseFile(PROFILE_PICTURE, bytes);
    }

    public boolean isPublicSignatureUsed(String publicSignature) {
        ParseQuery<ParseUser> userParseQuery = ParseUser.getQuery();
        userParseQuery.whereEqualTo(PUBLIC_SIGNATURE,publicSignature);
        boolean isSignatureUsed;
        try {
            findSingleObject(userParseQuery);
            isSignatureUsed = true;
        } catch (DataClassNotFoundException dataClassNotFoundException) {
            isSignatureUsed = false;
            dataClassNotFoundException.printStackTrace();
        }
        return isSignatureUsed;
    }

    public boolean isPrivateSignatureUsed(String privateSignature){
        ParseQuery<ParseUser> userParseQuery = ParseUser.getQuery();
        userParseQuery.whereEqualTo(PRIVATE_SIGNATURE,privateSignature);
        boolean isSignatureUsed;
        try {
            findSingleObject(userParseQuery);
            isSignatureUsed = true;
        } catch (DataClassNotFoundException dataClassNotFoundException) {
            isSignatureUsed = false;
            dataClassNotFoundException.printStackTrace();
        }
        return isSignatureUsed;
    }

    public ParseUser findUserByPublicSignature(String signature) throws DataClassNotFoundException {
        ParseQuery<ParseUser> userParseQuery = ParseUser.getQuery();
        userParseQuery.whereEqualTo(PUBLIC_SIGNATURE,signature);
        return findSingleObject(userParseQuery);
    }

    public User convertToDomainObject(ParseUser parseUser) {
        ParseUser parseToCreate = null;
        try {
            parseToCreate = parseUser.fetchIfNeeded();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new User(parseToCreate.getEmail(),parseToCreate.getString(FULL_NAME)
                ,parseToCreate.getCreatedAt().toString(), parseToCreate.getInt(EXPERIENCE),
                parseToCreate.getString(PUBLIC_SIGNATURE),parseToCreate.getString(PRIVATE_SIGNATURE));
    }

    public Bitmap getSignaturePicture(String signature) throws DataClassNotFoundException {
        ParseUser user = findUserByPublicSignature(signature);
        if(user != null) {
            return getUserProfilePicture(user);
        }else{
            return null;
        }
    }

    public User searchForUserByEmail(String email) throws SavedUserIsNotFoundOnBackEndException {
        User user = new User(email,"","",0,"","");
        ParseUser searchResult;
        try {
            searchResult = find(user);
        } catch (DataClassNotFoundException dataClassNotFoundException) {
            throw new SavedUserIsNotFoundOnBackEndException("Could not find user on back end, most" +
                    " likely the user has been deleted on back-end");
        }
        return convertToDomainObject(searchResult);
    }

    public User getSavedUser() {
        ParseUser currentDataUserFound = ParseUser.getCurrentUser();
        if(currentDataUserFound == null){
            throw new DataClassNotFoundException("Could not find the current user logged in");
        }
        return convertToDomainObject(currentDataUserFound);
    }

    public void logUserOut(){
        ParseUser.logOut();
    }
}




Java Source Code List

com.overnightApps.myapplication.app.CustomApplication.java
com.overnightApps.myapplication.app.core.Comment.java
com.overnightApps.myapplication.app.core.FriendShipRequest.java
com.overnightApps.myapplication.app.core.Friendship.java
com.overnightApps.myapplication.app.core.LetterRecommendation.java
com.overnightApps.myapplication.app.core.LetterVote.java
com.overnightApps.myapplication.app.core.Letter.java
com.overnightApps.myapplication.app.core.User.java
com.overnightApps.myapplication.app.core.helper.LetterWithUserVote.java
com.overnightApps.myapplication.app.core.helper.SignUpForm.java
com.overnightApps.myapplication.app.dao.CommentDao.java
com.overnightApps.myapplication.app.dao.Dao.java
com.overnightApps.myapplication.app.dao.FriendshipDao.java
com.overnightApps.myapplication.app.dao.FriendshipRequestDao.java
com.overnightApps.myapplication.app.dao.LetterDao.java
com.overnightApps.myapplication.app.dao.LetterRecommendationDao.java
com.overnightApps.myapplication.app.dao.LetterVoteDao.java
com.overnightApps.myapplication.app.dao.UserDao.java
com.overnightApps.myapplication.app.dao.exceptions.DataClassNotFoundException.java
com.overnightApps.myapplication.app.dao.exceptions.SavedUserIsNotFoundOnBackEndException.java
com.overnightApps.myapplication.app.dao.exceptions.UnableToSignUpException.java
com.overnightApps.myapplication.app.service.FriendshipSession.java
com.overnightApps.myapplication.app.service.LetterSender.java
com.overnightApps.myapplication.app.service.LetterVoter.java
com.overnightApps.myapplication.app.service.PrivateLetterSender.java
com.overnightApps.myapplication.app.service.PublicLetterSender.java
com.overnightApps.myapplication.app.service.RecommendationCreator.java
com.overnightApps.myapplication.app.service.SignUpFormVerifier.java
com.overnightApps.myapplication.app.service.SignUpSubmissionBuilder.java
com.overnightApps.myapplication.app.service.SignUpSubmission.java
com.overnightApps.myapplication.app.service.UserLogIn.java
com.overnightApps.myapplication.app.service.UserSession.java
com.overnightApps.myapplication.app.ui.FragmentIds.java
com.overnightApps.myapplication.app.ui.HomeActivity.java
com.overnightApps.myapplication.app.ui.homeFragments.CommentsFragment.java
com.overnightApps.myapplication.app.ui.homeFragments.CreateLetterFragment.java
com.overnightApps.myapplication.app.ui.homeFragments.FriendRequestsFragment.java
com.overnightApps.myapplication.app.ui.homeFragments.FriendsFragment.java
com.overnightApps.myapplication.app.ui.homeFragments.LearnFragment.java
com.overnightApps.myapplication.app.ui.homeFragments.LogInDialogFragment.java
com.overnightApps.myapplication.app.ui.homeFragments.ReceiveRecommendationsFragment.java
com.overnightApps.myapplication.app.ui.homeFragments.SendRecommendationFragment.java
com.overnightApps.myapplication.app.ui.homeFragments.SettingsFragment.java
com.overnightApps.myapplication.app.ui.homeFragments.UserLogInStateDependentFragments.LetterDetailFragmentUserNotLoggedIn.java
com.overnightApps.myapplication.app.ui.homeFragments.UserLogInStateDependentFragments.LetterDetailFragment_UserLoggedIn.java
com.overnightApps.myapplication.app.ui.homeFragments.UserLogInStateDependentFragments.LetterDetailFragment.java
com.overnightApps.myapplication.app.ui.homeFragments.UserLogInStateDependentFragments.UserFragment_UserLoggedIn.java
com.overnightApps.myapplication.app.ui.homeFragments.UserLogInStateDependentFragments.UserFragment_UserLoggedOut.java
com.overnightApps.myapplication.app.ui.homeFragments.UserLogInStateDependentFragments.UserFragment.java
com.overnightApps.myapplication.app.ui.homeFragments.absFragments.FilterListFragment.java
com.overnightApps.myapplication.app.ui.homeFragments.absFragments.HomeFragment.java
com.overnightApps.myapplication.app.ui.homeFragments.absFragments.MyListFragment.java
com.overnightApps.myapplication.app.ui.homeFragments.asyncTasks.LoadDetailFragmentTask.java
com.overnightApps.myapplication.app.ui.homeFragments.homeActivityInteractionInterfaces.OnActiveFragmentListener.java
com.overnightApps.myapplication.app.ui.homeFragments.homeActivityInteractionInterfaces.OnCreateLetterFragmentListener.java
com.overnightApps.myapplication.app.ui.homeFragments.homeActivityInteractionInterfaces.OnLetterDetailActionListener.java
com.overnightApps.myapplication.app.ui.homeFragments.homeActivityInteractionInterfaces.OnLetterDetailFragmentListener.java
com.overnightApps.myapplication.app.ui.homeFragments.homeActivityInteractionInterfaces.OnLoginFragmentListener.java
com.overnightApps.myapplication.app.ui.homeFragments.homeActivityInteractionInterfaces.OnSignUpActivityListener.java
com.overnightApps.myapplication.app.ui.homeFragments.homeActivityInteractionInterfaces.OnUserFragmentListener.java
com.overnightApps.myapplication.app.ui.homeFragments.homeActivityInteractionInterfaces.OnUserLogInListener.java
com.overnightApps.myapplication.app.ui.homeFragments.homeActivityInteractionInterfaces.OnUserLogOutListener.java
com.overnightApps.myapplication.app.ui.optionsMenus.MenuItem.java
com.overnightApps.myapplication.app.ui.optionsMenus.MenuOptionsBuilder.java
com.overnightApps.myapplication.app.ui.optionsMenus.MenuOptions.java
com.overnightApps.myapplication.app.ui.optionsMenus.menuOptionFactories.FriendMenuOptionsFactory.java
com.overnightApps.myapplication.app.ui.optionsMenus.menuOptionFactories.HomeMenuOptionsFactory.java
com.overnightApps.myapplication.app.ui.optionsMenus.menuOptionFactories.LetterMenuOptionsFactory.java
com.overnightApps.myapplication.app.ui.optionsMenus.menuOptionFactories.MenuOptionsFactory.java
com.overnightApps.myapplication.app.ui.optionsMenus.menuOptionFactories.RecommendationMenuOptionsFactory.java
com.overnightApps.myapplication.app.ui.signUp.NoSwipingViewPager.java
com.overnightApps.myapplication.app.ui.signUp.OnSignUpListener.java
com.overnightApps.myapplication.app.ui.signUp.OnUserSelectSignUpMethodListener.java
com.overnightApps.myapplication.app.ui.signUp.SelectSignUpMethodFragment.java
com.overnightApps.myapplication.app.ui.signUp.SignUpActivity.java
com.overnightApps.myapplication.app.ui.signUp.UploadProfilePictureFragment.java
com.overnightApps.myapplication.app.ui.signUp.UserSignUpFormFragment.java
com.overnightApps.myapplication.app.ui.slidingMenu.OptionsFragment.java
com.overnightApps.myapplication.app.util.AUtil.java
com.overnightApps.myapplication.app.util.BitmapUtil.java
com.overnightApps.myapplication.app.util.Logger.java
com.overnightApps.myapplication.app.util.MyAssert.java