<commit_before>package org.bbop.apollo

import grails.transaction.Transactional
import org.apache.shiro.SecurityUtils
import org.apache.shiro.session.Session
import org.bbop.apollo.gwt.shared.FeatureStringEnum

@Transactional
class PreferenceService {

    def permissionService

    Organism getCurrentOrganismForCurrentUser(){
        return permissionService.currentUser==null?null:getCurrentOrganism(permissionService.currentUser);
    }

    /**
     * Get the current user preference.
     * If no preference, then set one
     * @param user
     * @return
     */
    Organism getCurrentOrganism(User user){
        UserOrganismPreference userOrganismPreference = UserOrganismPreference.findByCurrentOrganismAndUser(true,user)
        if(!userOrganismPreference){
            userOrganismPreference = UserOrganismPreference.findByUser(user)

            if(!userOrganismPreference){
                Iterator i=permissionService.getOrganisms(user).iterator();
                if(i.hasNext()) {
                    Organism organism = i.next()
                    userOrganismPreference = new UserOrganismPreference(
                            user: user
                            , organism: organism
                            , sequence: Sequence.findByOrganism(organism)
                    ).save()
                }
                else {
                    throw new PermissionException("User has no access to any organisms!")<commit_msg>Fix the preference service to set the non-nullable field CurrentOrganism=true. See #329<commit_after>package org.bbop.apollo

import grails.transaction.Transactional
import org.apache.shiro.SecurityUtils
import org.apache.shiro.session.Session
import org.bbop.apollo.gwt.shared.FeatureStringEnum

@Transactional
class PreferenceService {

    def permissionService

    Organism getCurrentOrganismForCurrentUser(){
        return permissionService.currentUser==null?null:getCurrentOrganism(permissionService.currentUser);
    }

    /**
     * Get the current user preference.
     * If no preference, then set one
     * @param user
     * @return
     */
    Organism getCurrentOrganism(User user){
        UserOrganismPreference userOrganismPreference = UserOrganismPreference.findByCurrentOrganismAndUser(true,user)
        if(!userOrganismPreference){
            userOrganismPreference = UserOrganismPreference.findByUser(user)

            if(!userOrganismPreference){
                Iterator i=permissionService.getOrganisms(user).iterator();
                if(i.hasNext()) {
                    Organism organism = i.next()
                    userOrganismPreference = new UserOrganismPreference(
                            user: user
                            , organism: organism
                            , sequence: Sequence.findByOrganism(organism)
                            , currentOrganism: true
                    ).save()
                }
                else {
                    throw new PermissionException("User has no access to any organisms!")
{'fraction_non_alphanumeric': 0.07180500658761528, 'fraction_numerical': 0.0009881422924901185, 'mean_word_length': 2.2761596548004315, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4738481', 'n_tokens_mistral': 765, 'n_tokens_neox': 726, 'n_words': 150}