<commit_before>package controllers;

import org.sagebionetworks.bridge.dao.ParticipantOption.SharingScope;
import org.sagebionetworks.bridge.models.SharingOption;
import org.sagebionetworks.bridge.models.User;
import org.sagebionetworks.bridge.models.UserSession;
import org.sagebionetworks.bridge.models.studies.ConsentSignature;
import org.sagebionetworks.bridge.models.studies.Study;
import org.sagebionetworks.bridge.services.ConsentService;
import org.sagebionetworks.bridge.services.ParticipantOptionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.fasterxml.jackson.databind.JsonNode;

import play.mvc.Result;

@Controller("consentController")
public class ConsentController extends BaseController {

    private ConsentService consentService;
    
    private ParticipantOptionsService optionsService;

    @Autowired
    public void setConsentService(ConsentService consentService) {
        this.consentService = consentService;
    }
    @Autowired
    public void setOptionsService(ParticipantOptionsService optionsService) {
        this.optionsService = optionsService;
    }

    public Result getConsentSignature() throws Exception {
        final UserSession session = getAuthenticatedAndConsentedSession();
        final Study study = studyService.getStudy(session.getStudyIdentifier());

        ConsentSignature sig = consentService.getConsentSignature(study, session.getUser());
        return okResult(sig);
    }

    public Result giveV1() throws Exception {
        return giveConsentForVersion(1);
    }
    
    public Result giveV2() throws Exception {
        return giveConsentForVersion(2);
    }

    public Result emailCopy() throws Exception {
        final UserSession session = getAuthenticatedAndConsentedSession();
        final Study study = studyService.getStudy(session.getStudyIdentifier());
        
        consentService.emailConsentAgreement(study, session.getUser());
        return okResult("Emailed consent.");
    }

    public Result suspendDataSharing() throws Exception {
        return changeSharingScope(SharingScope.NO_SHARING, 
                "Data sharing with the study researchers has been suspended.");
   }

    public Result resumeDataSharing() throws Exception {
        return changeSharingScope(SharingScope.SPONSORS_AND_PARTNERS,
                "Data sharing with the study researchers has been resumed.");
    }
    
    public Result changeSharingScope() throws Exception {
        SharingOption sharing = SharingOption.fromJson(requestToJSON(request()), 2);
        return changeSharingScope(sharing.getSharingScope(), "Data sharing has been changed.");
    }
    
    private Result changeSharingScope(SharingScope sharingScope, String message) {
        final UserSession session = getAuthenticatedAndConsentedSession();
        final User user = session.getUser();
        
        optionsService.setOption(session.getStudyIdentifier(), user.getHealthCode(), sharingScope);
        user.setSharingScope(sharingScope);
        updateSessionUser(session, user);
        return okResult(message);
    }

    private Result giveConsentForVersion(int version) throws Exception {
        final UserSession session = getAuthenticatedSession();
        final Study study = studyService.getStudy(session.getStudyIdentifier());
        final JsonNode node = requestToJSON(request());
        
        final ConsentSignature consent = ConsentSignature.createFromJson(node);
        final User user = consentService.consentToResearch(study, session.getUser(), consent, true);
        
        SharingOption sharing = SharingOption.fromJson(node, version);
        optionsService.setOption(study, session.getUser().getHealthCode(), sharing.getSharingScope());
        
        updateSessionUser(session, user);
        setSessionToken(session.getSessionToken());
        return createdResult("Consent to research has been recorded.");
    }
}<commit_msg>Refactoring<commit_after>package controllers;

import org.sagebionetworks.bridge.dao.ParticipantOption.SharingScope;
import org.sagebionetworks.bridge.models.SharingOption;
import org.sagebionetworks.bridge.models.User;
import org.sagebionetworks.bridge.models.UserSession;
import org.sagebionetworks.bridge.models.studies.ConsentSignature;
import org.sagebionetworks.bridge.models.studies.Study;
import org.sagebionetworks.bridge.services.ConsentService;
import org.sagebionetworks.bridge.services.ParticipantOptionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.fasterxml.jackson.databind.JsonNode;

import play.mvc.Result;

@Controller("consentController")
public class ConsentController extends BaseController {

    private ConsentService consentService;
    
    private ParticipantOptionsService optionsService;

    @Autowired
    public void setConsentService(ConsentService consentService) {
        this.consentService = consentService;
    }
    @Autowired
    public void setOptionsService(ParticipantOptionsService optionsService) {
        this.optionsService = optionsService;
    }

    public Result getConsentSignature() throws Exception {
        final UserSession session = getAuthenticatedAndConsentedSession();
        final Study study = studyService.getStudy(session.getStudyIdentifier());

        ConsentSignature sig = consentService.getConsentSignature(study, session.getUser());
        return okResult(sig);
    }

    public Result giveV1() throws Exception {
        return giveConsentForVersion(1);
    }
    
    public Result giveV2() throws Exception {
        return giveConsentForVersion(2);
    }

    public Result emailCopy() throws Exception {
        final UserSession session = getAuthenticatedAndConsentedSession();
        final Study study = studyService.getStudy(session.getStudyIdentifier());
        
        consentService.emailConsentAgreement(study, session.getUser());
        return okResult("Emailed consent.");
    }

    public Result suspendDataSharing() throws Exception {
        return changeSharingScope(SharingScope.NO_SHARING, 
                "Data sharing with the study researchers has been suspended.");
   }

    public Result resumeDataSharing() throws Exception {
        return changeSharingScope(SharingScope.SPONSORS_AND_PARTNERS,
                "Data sharing with the study researchers has been resumed.");
    }
    
    public Result changeSharingScope() throws Exception {
        SharingOption sharing = SharingOption.fromJson(requestToJSON(request()), 2);
        return changeSharingScope(sharing.getSharingScope(), "Data sharing has been changed.");
    }
    
    private Result changeSharingScope(SharingScope sharingScope, String message) {
        final UserSession session = getAuthenticatedAndConsentedSession();
        final User user = session.getUser();
        
        optionsService.setOption(session.getStudyIdentifier(), user.getHealthCode(), sharingScope);
        user.setSharingScope(sharingScope);
        updateSessionUser(session, user);
        return okResult(message);
    }

    private Result giveConsentForVersion(int version) throws Exception {
        final UserSession session = getAuthenticatedSession();
        final Study study = studyService.getStudy(session.getStudyIdentifier());
        final JsonNode node = requestToJSON(request());
        final ConsentSignature consent = ConsentSignature.createFromJson(node);
        final SharingOption sharing = SharingOption.fromJson(node, version);
        final User user = consentService.consentToResearch(study, session.getUser(), consent, true);
        optionsService.setOption(study, session.getUser().getHealthCode(), sharing.getSharingScope());
        
        updateSessionUser(session, user);
        setSessionToken(session.getSessionToken());
        return createdResult("Consent to research has been recorded.");
    }
}
{'fraction_non_alphanumeric': 0.08060075093867335, 'fraction_numerical': 0.0012515644555694619, 'mean_word_length': 3.9695273631840795, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '7376015', 'n_tokens_mistral': 2064, 'n_tokens_neox': 1998, 'n_words': 439}