<commit_before>//
//CardBox library - framework for matchmaking networked games
//Copyright (C) 2005-2011 Three Rings Design, Inc., All Rights Reserved
//http://github.com/threerings/game-gardens
//
//This library is free software; you can redistribute it and/or modify it
//under the terms of the GNU Lesser General Public License as published
//by the Free Software Foundation; either version 2.1 of the License, or
//(at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.hextilla.cardbox.server.persist;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import com.samskivert.depot.DepotRepository;
import com.samskivert.depot.Exps;
import com.samskivert.depot.PersistenceContext;
import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.expression.FluentExp;
import com.samskivert.depot.clause.Limit;
import com.samskivert.depot.clause.OrderBy;
import com.samskivert.depot.clause.Where;

import com.samskivert.io.PersistenceException;

import com.hextilla.cardbox.server.CardBoxUserManager;

import static com.hextilla.cardbox.Log.log;

/**
* Provides an interface to our persistent repository of user metadata.
*/
public class FBUserRepository extends DepotRepository
 implements CardBoxUserManager.FacebookUserRepository
{
 /**
  * The database identifier used when establishing a database connection. This value being
  * <code>default</code>.
  */
 public static final String USER_DB_IDENT = "user";

 /**
  * Constructs a new repository with the specified persistence context.
  */
 public FBUserRepository (PersistenceContext ctx)
 {
     super(ctx);
 }
 
 /**
  * Loads information on a single user from the repository. Returns null if no user exists with
  * the specified user id.
  */
 public FBUserRecord loadUser (int userId)
     throws PersistenceException
 {
     return load(FBUserRecord.getKey(userId));
 }
 
 /**
  * Loads information on a single user from the repository. Returns null if no user exists for
  * the specified Facebook id.
  */
 public FBUserRecord loadUser (long fbId)
     throws PersistenceException
 {
	 FBUserMapRecord maptoUser = load(FBUserMapRecord.getKey(fbId));
	 if (maptoUser == null)
	 {
		 log.warning("Query for user by fbId returned 0 rows.", "fbId", fbId);
		 return null;
	 }
     return load(FBUserRecord.getKey(maptoUser.userId));
 }
 
 /**
  * Loads information on a single user from the repository. Returns null if no user exists for
  * the provided session hash.
  */
 public FBUserRecord loadUser (String sessionHash)
     throws PersistenceException
 {
	 SessionMapRecord maptoSession = load(SessionMapRecord.getKey(sessionHash));
	 if (maptoSession == null)
	 {
		 log.warning("Query for user by session returned 0 rows.", "session", sessionHash);
		 return null;
	 }
	 return load(FBUserRecord.getKey(maptoSession.userId));
 }

 /**
  * Inserts the supplied user into the repository. {@link GameRecord#gameId} will be filled in
  * by this method with the user's newly assigned unique identifier.<commit_msg>Fixing a bug where a user could log in with an expired session (which wouldn't work with restFB)<commit_after>//
//CardBox library - framework for matchmaking networked games
//Copyright (C) 2005-2011 Three Rings Design, Inc., All Rights Reserved
//http://github.com/threerings/game-gardens
//
//This library is free software; you can redistribute it and/or modify it
//under the terms of the GNU Lesser General Public License as published
//by the Free Software Foundation; either version 2.1 of the License, or
//(at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.hextilla.cardbox.server.persist;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import com.samskivert.depot.DepotRepository;
import com.samskivert.depot.Exps;
import com.samskivert.depot.PersistenceContext;
import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.expression.FluentExp;
import com.samskivert.depot.clause.Limit;
import com.samskivert.depot.clause.OrderBy;
import com.samskivert.depot.clause.Where;

import com.samskivert.io.PersistenceException;

import com.hextilla.cardbox.server.CardBoxUserManager;

import static com.hextilla.cardbox.Log.log;

/**
* Provides an interface to our persistent repository of user metadata.
*/
public class FBUserRepository extends DepotRepository
 implements CardBoxUserManager.FacebookUserRepository
{
 /**
  * The database identifier used when establishing a database connection. This value being
  * <code>default</code>.
  */
 public static final String USER_DB_IDENT = "user";

 /**
  * Constructs a new repository with the specified persistence context.
  */
 public FBUserRepository (PersistenceContext ctx)
 {
     super(ctx);
 }
 
 /**
  * Loads information on a single user from the repository. Returns null if no user exists with
  * the specified user id.
  */
 public FBUserRecord loadUser (int userId)
     throws PersistenceException
 {
     return load(FBUserRecord.getKey(userId));
 }
 
 /**
  * Loads information on a single user from the repository. Returns null if no user exists for
  * the specified Facebook id.
  */
 public FBUserRecord loadUser (long fbId)
     throws PersistenceException
 {
	 FBUserMapRecord maptoUser = load(FBUserMapRecord.getKey(fbId));
	 if (maptoUser == null)
	 {
		 log.warning("Query for user by fbId returned 0 rows.", "fbId", fbId);
		 return null;
	 }
     return load(FBUserRecord.getKey(maptoUser.userId));
 }
 
 /**
  * Loads information on a single user from the repository. Returns null if no user exists for
  * the provided session hash.
  */
 public FBUserRecord loadUser (String sessionHash)
     throws PersistenceException
 {
	 Calendar now = Calendar.getInstance();
	 SessionMapRecord maptoSession = load(SessionMapRecord.getKey(sessionHash));
	 if (maptoSession == null || maptoSession.expires < now.getTimeInMillis())
	 {
		 log.warning("Query for user by session returned 0 rows.", "session", sessionHash);
		 return null;
	 }
	 return load(FBUserRecord.getKey(maptoSession.userId));
 }

 /**
  * Inserts the supplied user into the repository. {@link GameRecord#gameId} will be filled in
  * by this method with the user's newly assigned unique identifier.
{'fraction_non_alphanumeric': 0.08831799591002044, 'fraction_numerical': 0.0066462167689161555, 'mean_word_length': 4.290736984448952, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1570276', 'n_tokens_mistral': 2287, 'n_tokens_neox': 2182, 'n_words': 785}