<commit_before>/**
 * This file is part of CMDit.
 *
 * CMDit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CMDit 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with CMDit.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.malaguna.cmdit.service.commands.usrmgt;

import java.util.Iterator;
import java.util.Set;

import org.malaguna.cmdit.model.usrmgt.ActionHelper;
import org.malaguna.cmdit.model.usrmgt.Participation;
import org.malaguna.cmdit.model.usrmgt.User;
import org.malaguna.cmdit.service.commands.Command;
import org.springframework.beans.factory.BeanFactory;

public class SaveRoles extends Command {
	private User usuario = null;
	private Set<String> roles = null;

	public SaveRoles(BeanFactory bf) {
		super(bf);
		setCanLog(true);
		setReadOnly(false);
		setAction(ActionHelper.MANAGE_USERS);
	}

	public void setUsuario(User usuario) {
		this.usuario = usuario;
	}

	public void setRoles(Set<String> roles) {
		this.roles = roles;
	}

	@Override 
	public boolean isValid(){
		return super.isValid() &&
				usuario != null &&
				roles != null;
	}
	
	@Override
	public Command runCommand() throws Exception {
		
		usuario = getUserDao().findById(usuario.getPid());
		Iterator<Participation> iPart = usuario.getParticipations().iterator();
		Iterator<String> iRoles = roles.iterator();
		if(usuario.getParticipations().size()<roles.size()){
			while(iPart.hasNext()){
				iPart.next().setRol(iRoles.next());
			}
			Participation p = null;
			while(iRoles.hasNext()){
				p = new Participation();
				p.setRol(iRoles.next());
				p.setCenter(usuario.getDefault_center());
				p.setUser(usuario);
				usuario.getParticipations().add(p);
			}
		}else if(usuario.getParticipations().size()==roles.size()){
			while(iPart.hasNext()){
				iPart.next().setRol(iRoles.next());
			}
		}else if(usuario.getParticipations().size()>roles.size()){
			while(iPart.hasNext()){
				usuario.getParticipations().remove(iPart.next());
			}
			Participation p = null;
			while(iRoles.hasNext()){
				p = new Participation();
				p.setRol(iRoles.next());
				p.setCenter(usuario.getDefault_center());
				p.setUser(usuario);
				usuario.getParticipations().add(p);
			}
			
		}
			

		getUserDao().persist(usuario);
		
		createLogComment("log.changeRol.ok", usuario.getPid(), roles);
		return this;
	}
}<commit_msg>Center in saveRoles<commit_after>/**
 * This file is part of CMDit.
 *
 * CMDit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CMDit 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with CMDit.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.malaguna.cmdit.service.commands.usrmgt;

import java.util.Iterator;
import java.util.Set;

import org.malaguna.cmdit.model.usrmgt.ActionHelper;
import org.malaguna.cmdit.model.usrmgt.Center;
import org.malaguna.cmdit.model.usrmgt.Participation;
import org.malaguna.cmdit.model.usrmgt.User;
import org.malaguna.cmdit.service.commands.Command;
import org.springframework.beans.factory.BeanFactory;

public class SaveRoles extends Command {
	private User usuario = null;
	private Center center = null;
	private Set<String> roles = null;

	public SaveRoles(BeanFactory bf) {
		super(bf);
		setCanLog(true);
		setReadOnly(false);
		setAction(ActionHelper.MANAGE_USERS);
	}

	public void setUsuario(User usuario) {
		this.usuario = usuario;
	}

	public Center getCenter() {
		return center;
	}

	public void setCenter(Center center) {
		this.center = center;
	}

	public void setRoles(Set<String> roles) {
		this.roles = roles;
	}

	@Override 
	public boolean isValid(){
		return super.isValid() &&
				usuario != null &&
				roles != null;
	}
	
	@Override
	public Command runCommand() throws Exception {
		
		usuario = getUserDao().findById(usuario.getPid());
		Iterator<Participation> iPart = usuario.getParticipations().iterator();
		Iterator<String> iRoles = roles.iterator();
		if(usuario.getParticipations().size()<roles.size()){
			while(iPart.hasNext()){
				iPart.next().setRol(iRoles.next());
			}
			Participation p = null;
			while(iRoles.hasNext()){
				p = new Participation();
				p.setRol(iRoles.next());
				if(center!=null){
					p.setCenter(center);
				}else{
					p.setCenter(usuario.getDefault_center());
				}
				p.setUser(usuario);
				usuario.getParticipations().add(p);
			}
		}else if(usuario.getParticipations().size()==roles.size()){
			while(iPart.hasNext()){
				iPart.next().setRol(iRoles.next());
			}
		}else if(usuario.getParticipations().size()>roles.size()){
			while(iPart.hasNext()){
				usuario.getParticipations().remove(iPart.next());
			}
			Participation p = null;
			while(iRoles.hasNext()){
				p = new Participation();
				p.setRol(iRoles.next());
				if(center!=null){
					p.setCenter(center);
				}else{
					p.setCenter(usuario.getDefault_center());
				}				p.setUser(usuario);
				usuario.getParticipations().add(p);
			}
			
		}
			

		getUserDao().persist(usuario);
		
		createLogComment("log.changeRol.ok", usuario.getPid(), roles);
		return this;
	}
}
{'fraction_non_alphanumeric': 0.1306617275635629, 'fraction_numerical': 0.0003367570298029971, 'mean_word_length': 4.025380710659898, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '7661720', 'n_tokens_mistral': 2219, 'n_tokens_neox': 1984, 'n_words': 459}