<commit_before>package org.intermine.web.logic.profile;

/*
 * Copyright (C) 2002-2011 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionMessage;
import org.intermine.api.InterMineAPI;
import org.intermine.api.profile.InterMineBag;
import org.intermine.api.profile.Profile;
import org.intermine.api.profile.ProfileManager;
import org.intermine.api.profile.SavedQuery;
import org.intermine.api.util.NameUtil;
import org.intermine.objectstore.ObjectStoreException;
import org.intermine.web.logic.Constants;
import org.intermine.web.logic.session.SessionMethods;
import org.intermine.web.struts.InterMineAction;

/**
 * @author <NAME>
 *
 */
public abstract class LoginHandler extends InterMineAction
{
    /**
     * Abstract class containing the methods for login in and copying current
     * history, bags,... into profile
     *
     * @param request The HttpServletRequest
     * @param response The HttpServletResponse
     * @param session The session
     * @param pm The profile manager
     * @param username The username
     * @param password The password
     * @return the map containing the renamed bags the user created before they were logged in
     */
    public Map<String, String> doLogin(HttpServletRequest request, HttpServletResponse response,
            HttpSession session, ProfileManager pm, String username,
            String password) {
        // Merge current history into loaded profile
        Profile currentProfile = SessionMethods.getProfile(request.getSession());
        Map<String, SavedQuery> mergeQueries = Collections.emptyMap();
        Map<String, InterMineBag> mergeBags = Collections.emptyMap();
        if (currentProfile != null && StringUtils.isEmpty(currentProfile.getUsername())) {
            mergeQueries = currentProfile.getHistory();
            mergeBags = currentProfile.getSavedBags();
        }

        Profile profile = setUpProfile(session, pm, username, password);
        InterMineAPI im = SessionMethods.getInterMineAPI(session);
        if (!im.getBagManager().isUserBagsCurrent(profile)) {
            new Thread(new UpgradeBagList(profile, im.getBagQueryRunner(), session)).start();
            recordMessage(new ActionMessage("login.upgradeListStarted"), request);
        }

        // Merge in anonymous query history
        for (SavedQuery savedQuery : mergeQueries.values()) {
            profile.saveHistory(savedQuery);
        }
        // Merge anonymous bags
        Map<String, String> renamedBags = new HashMap<String, String>();
        for (Map.Entry<String, InterMineBag> entry : mergeBags.entrySet()) {
            InterMineBag bag = entry.getValue();
            // Make sure the userId gets set to be the profile one
            try {
                bag.setProfileId(profile.getUserId());
                String name = NameUtil.validateName(profile.getSavedBags().keySet(),
                        entry.getKey());
                if (!entry.getKey().equals(name)) {
                    renamedBags.put(entry.getKey(), name);
                }
                bag.setName(name);
                profile.saveBag(name, bag);
            } catch (ObjectStoreException iex) {
                throw new RuntimeException(iex.getMessage());
            }
        }
        return renamedBags;
    }

    /**
     * Retrieves profile (creates or gets from ProfileManager) and saves it to session.
     *
     * @param session http session
     * @param pm profile manager
     * @param username user name
     * @param password password
     * @return profile
     */
    public static Profile setUpProfile(HttpSession session, ProfileManager pm,
            String username, String password) {
        Profile profile;
        InterMineAPI im = SessionMethods.getInterMineAPI(session);
        if (pm.hasProfile(username)) {
            profile = pm.getProfile(username, password, im.getClassKeys());
        } else {
            profile = new Profile(pm, username, null, password, new HashMap(), new HashMap(),
                    new HashMap());
            pm.saveProfile(profile);
        }
        SessionMethods.setProfile(session, profile);

        if (profile.getUsername().equals(pm.getSuperuser())) {
            session.setAttribute(Constants.IS_SUPERUSER, Boolean.TRUE);
        }
        return profile;
    }
}
<commit_msg>Added method to set up profile with a given profile, rather than looking for it again<commit_after>package org.intermine.web.logic.profile;

/*
 * Copyright (C) 2002-2011 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionMessage;
import org.intermine.api.InterMineAPI;
import org.intermine.api.profile.InterMineBag;
import org.intermine.api.profile.Profile;
import org.intermine.api.profile.ProfileManager;
import org.intermine.api.profile.SavedQuery;
import org.intermine.api.util.NameUtil;
import org.intermine.objectstore.ObjectStoreException;
import org.intermine.web.logic.Constants;
import org.intermine.web.logic.session.SessionMethods;
import org.intermine.web.struts.InterMineAction;

/**
 * @author <NAME>
 *
 */
public abstract class LoginHandler extends InterMineAction
{
    /**
     * Abstract class containing the methods for login in and copying current
     * history, bags,... into profile
     *
     * @param request The HttpServletRequest
     * @param response The HttpServletResponse
     * @param session The session
     * @param pm The profile manager
     * @param username The username
     * @param password <PASSWORD>
     * @return the map containing the renamed bags the user created before they were logged in
     */
    public Map<String, String> doLogin(HttpServletRequest request, HttpServletResponse response,
            HttpSession session, ProfileManager pm, String username,
            String password) {
        // Merge current history into loaded profile
        Profile currentProfile = SessionMethods.getProfile(request.getSession());
        Map<String, SavedQuery> mergeQueries = Collections.emptyMap();
        Map<String, InterMineBag> mergeBags = Collections.emptyMap();
        if (currentProfile != null && StringUtils.isEmpty(currentProfile.getUsername())) {
            mergeQueries = currentProfile.getHistory();
            mergeBags = currentProfile.getSavedBags();
        }

        Profile profile = setUpProfile(session, pm, username, password);
        InterMineAPI im = SessionMethods.getInterMineAPI(session);
        if (!im.getBagManager().isUserBagsCurrent(profile)) {
            new Thread(new UpgradeBagList(profile, im.getBagQueryRunner(), session)).start();
            recordMessage(new ActionMessage("login.upgradeListStarted"), request);
        }

        // Merge in anonymous query history
        for (SavedQuery savedQuery : mergeQueries.values()) {
            profile.saveHistory(savedQuery);
        }
        // Merge anonymous bags
        Map<String, String> renamedBags = new HashMap<String, String>();
        for (Map.Entry<String, InterMineBag> entry : mergeBags.entrySet()) {
            InterMineBag bag = entry.getValue();
            // Make sure the userId gets set to be the profile one
            try {
                bag.setProfileId(profile.getUserId());
                String name = NameUtil.validateName(profile.getSavedBags().keySet(),
                        entry.getKey());
                if (!entry.getKey().equals(name)) {
                    renamedBags.put(entry.getKey(), name);
                }
                bag.setName(name);
                profile.saveBag(name, bag);
            } catch (ObjectStoreException iex) {
                throw new RuntimeException(iex.getMessage());
            }
        }
        return renamedBags;
    }

    /**
     * Retrieves profile (creates or gets from ProfileManager) and saves it to session.
     *
     * @param session http session
     * @param pm profile manager
     * @param username user name
     * @param password password
     * @return profile
     */
    public static Profile setUpProfile(HttpSession session, ProfileManager pm,
            String username, String password) {
    	final InterMineAPI im = SessionMethods.getInterMineAPI(session);
    	Profile profile;
        if (pm.hasProfile(username)) {
            profile = pm.getProfile(username, password, im.getClassKeys());
        } else {
            profile = new Profile(pm, username, null, password, new HashMap(), new HashMap(),
                    new HashMap(), null);
            pm.saveProfile(profile);
        }
        return setUpProfile(session, pm, profile);
    }

    public static Profile setUpProfile(HttpSession session, ProfileManager pm, Profile profile) {
    	SessionMethods.setProfile(session, profile);

        if (profile.getUsername().equals(pm.getSuperuser())) {
            session.setAttribute(Constants.IS_SUPERUSER, Boolean.TRUE);
        }
        return profile;
    }
}

{'fraction_non_alphanumeric': 0.09554140127388536, 'fraction_numerical': 0.0016176321908805986, 'mean_word_length': 2.7784568372803666, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '831091', 'n_tokens_mistral': 2575, 'n_tokens_neox': 2543, 'n_words': 689}