<commit_before>package gov.nih.nci.cananolab.service.admin.impl;

import gov.nih.nci.cananolab.domain.common.Protocol;
import gov.nih.nci.cananolab.domain.common.Publication;
import gov.nih.nci.cananolab.domain.particle.Characterization;
import gov.nih.nci.cananolab.domain.particle.ChemicalAssociation;
import gov.nih.nci.cananolab.domain.particle.FunctionalizingEntity;
import gov.nih.nci.cananolab.domain.particle.NanomaterialEntity;
import gov.nih.nci.cananolab.domain.particle.Sample;
import gov.nih.nci.cananolab.domain.particle.SampleComposition;
import gov.nih.nci.cananolab.dto.common.AccessibilityBean;
import gov.nih.nci.cananolab.exception.AdministrationException;
import gov.nih.nci.cananolab.exception.NoAccessException;
import gov.nih.nci.cananolab.service.BaseService;
import gov.nih.nci.cananolab.service.BaseServiceLocalImpl;
import gov.nih.nci.cananolab.service.admin.OwnershipTransferService;
import gov.nih.nci.cananolab.service.community.CommunityService;
import gov.nih.nci.cananolab.service.community.impl.CommunityServiceLocalImpl;
import gov.nih.nci.cananolab.service.protocol.ProtocolService;
import gov.nih.nci.cananolab.service.protocol.helper.ProtocolServiceHelper;
import gov.nih.nci.cananolab.service.protocol.impl.ProtocolServiceLocalImpl;
import gov.nih.nci.cananolab.service.publication.PublicationService;
import gov.nih.nci.cananolab.service.publication.helper.PublicationServiceHelper;
import gov.nih.nci.cananolab.service.publication.impl.PublicationServiceLocalImpl;
import gov.nih.nci.cananolab.service.sample.SampleService;
import gov.nih.nci.cananolab.service.sample.impl.SampleServiceLocalImpl;
import gov.nih.nci.cananolab.service.security.SecurityService;
import gov.nih.nci.cananolab.service.security.UserBean;
import gov.nih.nci.cananolab.system.applicationservice.CustomizedApplicationService;
import gov.nih.nci.system.client.ApplicationServiceProvider;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * Service methods for transfer ownership.
 *
 * @author lethai, pansu
 */
public class OwnershipTransferServiceImpl implements OwnershipTransferService {

	private static Logger logger = Logger
			.getLogger(OwnershipTransferServiceImpl.class);

	private int transferOwner(SampleService sampleService,
			List<String> sampleIds, String currentOwner,
			Boolean currentOwnerIsCurator, String newOwner,
			Boolean newOwnerIsCurator) throws AdministrationException,
			NoAccessException {
		SecurityService securityService = ((SampleServiceLocalImpl) sampleService)
				.getSecurityService();
		// user needs to be both curator and admin
		if (!(securityService.getUserBean().isCurator() && securityService
				.getUserBean().isAdmin())) {
			throw new NoAccessException();
		}
		int i = 0;
		try {
			CustomizedApplicationService appService = (CustomizedApplicationService) ApplicationServiceProvider
					.getApplicationService();
			for (String sampleId : sampleIds) {
				try {
					Sample domain = sampleService.findSampleById(sampleId,
							false).getDomain();
					domain.setCreatedBy(newOwner);
					SampleComposition sampleComposition = domain
							.getSampleComposition();
					appService.saveOrUpdate(domain);
					Collection<ChemicalAssociation> chemicalAssociation = new ArrayList<ChemicalAssociation>();
					Collection<FunctionalizingEntity> functionalizingEntity = new ArrayList<FunctionalizingEntity>();
					Collection<NanomaterialEntity> nanomaterialEntity = new ArrayList<NanomaterialEntity>();
					Collection<Characterization> characterization = new ArrayList<Characterization>();

					if (sampleComposition != null) {
						chemicalAssociation = sampleComposition
								.getChemicalAssociationCollection();
						functionalizingEntity = sampleComposition
								.getFunctionalizingEntityCollection();
						nanomaterialEntity = sampleComposition
								.getNanomaterialEntityCollection();
						characterization = domain
								.getCharacterizationCollection();

						for (ChemicalAssociation ca : chemicalAssociation) {
							ca.setCreatedBy(newOwner);
							appService.saveOrUpdate(ca);
						}
						for (FunctionalizingEntity fe : functionalizingEntity) {
							fe.setCreatedBy(newOwner);
							appService.saveOrUpdate(fe);
						}
						for (NanomaterialEntity ne : nanomaterialEntity) {
							ne.setCreatedBy(newOwner);
							appService.saveOrUpdate(ne);
						}

						for (Characterization c : characterization) {
							c.setCreatedBy(newOwner);
							appService.saveOrUpdate(c);
						}
					}
					appService.saveOrUpdate(domain);
					this.assignAndRemoveAccessForSample(sampleService, domain,
							currentOwner, currentOwnerIsCurator, newOwner,
							newOwnerIsCurator);

				} catch (Exception e) {
					i++;
					String error = "Error transferring ownership for sample: "
							+ sampleId;
					logger.error(error, e);
				}
			}
		} catch (Exception e) {
			String error = "Error transferring ownership for samples";
			logger.error(error, e);
			throw new AdministrationException(error, e);
		}
		return i;
	}

	private AccessibilityBean[] assignAndRemoveAccess(BaseService service,
			String dataId, String currentOwner, Boolean currentOwnerIsCurator,
			String newOwner, Boolean newOwnerIsCurator) throws Exception {
		SecurityService securityService = ((BaseServiceLocalImpl) service)
				.getSecurityService();
		List<AccessibilityBean> userAccesses = service
				.findUserAccessibilities(dataId);
		List<AccessibilityBean> groupAccesses = service
				.findGroupAccessibilities(dataId);
		// an array to store assign and remove accessibility for the data
		AccessibilityBean[] assignRemoveAccesses = new AccessibilityBean[2];
		// ----assign access to the owner
		// if newOwner is a curator, don't need to assign access to new owner
		if (newOwnerIsCurator) {
			assignRemoveAccesses[0] = null;
		}
		// if newOwner is not a curator, need to assign user access to new owner
		else {
			if (userAccesses != null && !userAccesses.isEmpty()) {
				assignRemoveAccesses[0] = this
						.getNewUserAccessFromUserAccesses(userAccesses,
								currentOwner, newOwner);
			} else {
				assignRemoveAccesses[0] = this
						.getNewUserAccessFromGroupAccesses(securityService,
								groupAccesses, currentOwner, newOwner);
			}
		}

		// ---remove access from the owner
		// if currentOwner is a curator, don't need to remove access from
		// current owner
		if (currentOwnerIsCurator) {
			assignRemoveAccesses[1] = null;
		} else {
			// remove currentOwner access if user accesses contains user
			if (userAccesses != null && !userAccesses.isEmpty()) {
				for (AccessibilityBean access : userAccesses) {
					if (access.getUserBean().getLoginName()
							.equals(currentOwner)) {
						assignRemoveAccesses[1] = access;
						break;
					}
				}
			}
			// if only group accesses don't need to remove.
			else {
				assignRemoveAccesses[1] = null;
			}
		}
		return assignRemoveAccesses;<commit_msg>updated assignAndRemoveAccess<commit_after>package gov.nih.nci.cananolab.service.admin.impl;

import gov.nih.nci.cananolab.domain.common.Protocol;
import gov.nih.nci.cananolab.domain.common.Publication;
import gov.nih.nci.cananolab.domain.particle.Characterization;
import gov.nih.nci.cananolab.domain.particle.ChemicalAssociation;
import gov.nih.nci.cananolab.domain.particle.FunctionalizingEntity;
import gov.nih.nci.cananolab.domain.particle.NanomaterialEntity;
import gov.nih.nci.cananolab.domain.particle.Sample;
import gov.nih.nci.cananolab.domain.particle.SampleComposition;
import gov.nih.nci.cananolab.dto.common.AccessibilityBean;
import gov.nih.nci.cananolab.exception.AdministrationException;
import gov.nih.nci.cananolab.exception.NoAccessException;
import gov.nih.nci.cananolab.service.BaseService;
import gov.nih.nci.cananolab.service.BaseServiceLocalImpl;
import gov.nih.nci.cananolab.service.admin.OwnershipTransferService;
import gov.nih.nci.cananolab.service.community.CommunityService;
import gov.nih.nci.cananolab.service.community.impl.CommunityServiceLocalImpl;
import gov.nih.nci.cananolab.service.protocol.ProtocolService;
import gov.nih.nci.cananolab.service.protocol.helper.ProtocolServiceHelper;
import gov.nih.nci.cananolab.service.protocol.impl.ProtocolServiceLocalImpl;
import gov.nih.nci.cananolab.service.publication.PublicationService;
import gov.nih.nci.cananolab.service.publication.helper.PublicationServiceHelper;
import gov.nih.nci.cananolab.service.publication.impl.PublicationServiceLocalImpl;
import gov.nih.nci.cananolab.service.sample.SampleService;
import gov.nih.nci.cananolab.service.sample.impl.SampleServiceLocalImpl;
import gov.nih.nci.cananolab.service.security.SecurityService;
import gov.nih.nci.cananolab.service.security.UserBean;
import gov.nih.nci.cananolab.system.applicationservice.CustomizedApplicationService;
import gov.nih.nci.system.client.ApplicationServiceProvider;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * Service methods for transfer ownership.
 *
 * @author lethai, pansu
 */
public class OwnershipTransferServiceImpl implements OwnershipTransferService {

	private static Logger logger = Logger
			.getLogger(OwnershipTransferServiceImpl.class);

	private int transferOwner(SampleService sampleService,
			List<String> sampleIds, String currentOwner,
			Boolean currentOwnerIsCurator, String newOwner,
			Boolean newOwnerIsCurator) throws AdministrationException,
			NoAccessException {
		SecurityService securityService = ((SampleServiceLocalImpl) sampleService)
				.getSecurityService();
		// user needs to be both curator and admin
		if (!(securityService.getUserBean().isCurator() && securityService
				.getUserBean().isAdmin())) {
			throw new NoAccessException();
		}
		int i = 0;
		try {
			CustomizedApplicationService appService = (CustomizedApplicationService) ApplicationServiceProvider
					.getApplicationService();
			for (String sampleId : sampleIds) {
				try {
					Sample domain = sampleService.findSampleById(sampleId,
							false).getDomain();
					domain.setCreatedBy(newOwner);
					SampleComposition sampleComposition = domain
							.getSampleComposition();
					appService.saveOrUpdate(domain);
					Collection<ChemicalAssociation> chemicalAssociation = new ArrayList<ChemicalAssociation>();
					Collection<FunctionalizingEntity> functionalizingEntity = new ArrayList<FunctionalizingEntity>();
					Collection<NanomaterialEntity> nanomaterialEntity = new ArrayList<NanomaterialEntity>();
					Collection<Characterization> characterization = new ArrayList<Characterization>();

					if (sampleComposition != null) {
						chemicalAssociation = sampleComposition
								.getChemicalAssociationCollection();
						functionalizingEntity = sampleComposition
								.getFunctionalizingEntityCollection();
						nanomaterialEntity = sampleComposition
								.getNanomaterialEntityCollection();
						characterization = domain
								.getCharacterizationCollection();

						for (ChemicalAssociation ca : chemicalAssociation) {
							ca.setCreatedBy(newOwner);
							appService.saveOrUpdate(ca);
						}
						for (FunctionalizingEntity fe : functionalizingEntity) {
							fe.setCreatedBy(newOwner);
							appService.saveOrUpdate(fe);
						}
						for (NanomaterialEntity ne : nanomaterialEntity) {
							ne.setCreatedBy(newOwner);
							appService.saveOrUpdate(ne);
						}

						for (Characterization c : characterization) {
							c.setCreatedBy(newOwner);
							appService.saveOrUpdate(c);
						}
					}
					appService.saveOrUpdate(domain);
					this.assignAndRemoveAccessForSample(sampleService, domain,
							currentOwner, currentOwnerIsCurator, newOwner,
							newOwnerIsCurator);

				} catch (Exception e) {
					i++;
					String error = "Error transferring ownership for sample: "
							+ sampleId;
					logger.error(error, e);
				}
			}
		} catch (Exception e) {
			String error = "Error transferring ownership for samples";
			logger.error(error, e);
			throw new AdministrationException(error, e);
		}
		return i;
	}

	private AccessibilityBean[] assignAndRemoveAccess(BaseService service,
			String dataId, String currentOwner, Boolean currentOwnerIsCurator,
			String newOwner, Boolean newOwnerIsCurator) throws Exception {
		SecurityService securityService = ((BaseServiceLocalImpl) service)
				.getSecurityService();
		List<AccessibilityBean> userAccesses = service
				.findUserAccessibilities(dataId);
		List<AccessibilityBean> groupAccesses = service
				.findGroupAccessibilities(dataId);
		// an array to store assign and remove accessibility for the data
		AccessibilityBean[] assignRemoveAccesses = new AccessibilityBean[2];
		// ----assign access to the owner
		// if newOwner is a curator, don't need to assign access to new owner
		if (newOwnerIsCurator) {
			assignRemoveAccesses[0] = null;
		}
		// if newOwner is not a curator, need to assign user access to new owner
		else {
			if (userAccesses != null && !userAccesses.isEmpty()) {
				assignRemoveAccesses[0] = this
						.getNewUserAccessFromUserAccesses(userAccesses,
								currentOwner, newOwner);
			}
			if (assignRemoveAccesses[0]==null) {
				assignRemoveAccesses[0] = this
						.getNewUserAccessFromGroupAccesses(securityService,
								groupAccesses, currentOwner, newOwner);
			}
		}

		// ---remove access from the owner
		// if currentOwner is a curator, don't need to remove access from
		// current owner
		if (currentOwnerIsCurator) {
			assignRemoveAccesses[1] = null;
		} else {
			// remove currentOwner access if user accesses contains user
			if (userAccesses != null && !userAccesses.isEmpty()) {
				for (AccessibilityBean access : userAccesses) {
					if (access.getUserBean().getLoginName()
							.equals(currentOwner)) {
						assignRemoveAccesses[1] = access;
						break;
					}
				}
			}
			// if only group accesses don't need to remove.
			else {
				assignRemoveAccesses[1] = null;
			}
		}
		return assignRemoveAccesses;
{'fraction_non_alphanumeric': 0.0931592838290891, 'fraction_numerical': 0.001355303516655967, 'mean_word_length': 4.446775446775447, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '6546774', 'n_tokens_mistral': 4938, 'n_tokens_neox': 3994, 'n_words': 791}