<commit_before>package gov.healthit.chpl.manager.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.JsonProcessingException;

import gov.healthit.chpl.dao.CertifiedProductDAO;
import gov.healthit.chpl.dao.CorrectiveActionPlanCertificationResultDAO;
import gov.healthit.chpl.dao.CorrectiveActionPlanDAO;
import gov.healthit.chpl.dao.CorrectiveActionPlanDocumentationDAO;
import gov.healthit.chpl.dao.EntityCreationException;
import gov.healthit.chpl.dao.EntityRetrievalException;
import gov.healthit.chpl.domain.ActivityConcept;
import gov.healthit.chpl.domain.CorrectiveActionPlanDetails;
import gov.healthit.chpl.dto.CertifiedProductDTO;
import gov.healthit.chpl.dto.CorrectiveActionPlanCertificationResultDTO;
import gov.healthit.chpl.dto.CorrectiveActionPlanDTO;
import gov.healthit.chpl.dto.CorrectiveActionPlanDocumentationDTO;
import gov.healthit.chpl.manager.ActivityManager;
import gov.healthit.chpl.manager.CorrectiveActionPlanManager;

@Service
public class CorrectiveActionPlanManagerImpl implements CorrectiveActionPlanManager {

	@Autowired CertifiedProductDAO cpDao;
	@Autowired CorrectiveActionPlanDAO capDao;
	@Autowired CorrectiveActionPlanCertificationResultDAO capCertDao;
	@Autowired CorrectiveActionPlanDocumentationDAO capDocDao;
	@Autowired public ActivityManager activityManager;
	
	@Override
	@Transactional
	@PreAuthorize("hasRole('ROLE_ADMIN') or "
			+ "(hasRole('ROLE_ACB_ADMIN') and hasPermission(#acbId, 'gov.healthit.chpl.dto.CertificationBodyDTO', admin))")
	public CorrectiveActionPlanDetails create(Long acbId, CorrectiveActionPlanDTO toCreate)
			throws EntityRetrievalException, EntityCreationException, JsonProcessingException {
		
		CorrectiveActionPlanDTO created = capDao.create(toCreate);
		
		CertifiedProductDTO cpDto = cpDao.getById(created.getCertifiedProductId());
		activityManager.addActivity(ActivityConcept.ACTIVITY_CONCEPT_CERTIFIED_PRODUCT, created.getId(), 
				"Corrective action plan for "+ cpDto.getChplProductNumberForActivity() +" was created.", null, created);

		return new CorrectiveActionPlanDetails(created, null);
	}


	@Override
	@Transactional
	@PreAuthorize("hasRole('ROLE_ADMIN') or "
			+ "(hasRole('ROLE_ACB_ADMIN') and hasPermission(#acbId, 'gov.healthit.chpl.dto.CertificationBodyDTO', admin))")
	public CorrectiveActionPlanDocumentationDTO addDocumentationToPlan(Long acbId, CorrectiveActionPlanDocumentationDTO doc)
		throws EntityRetrievalException, EntityCreationException, JsonProcessingException {
		CertifiedProductDTO cpDto = cpDao.getById(doc.getCorrectiveActionPlanId());
		CorrectiveActionPlanDocumentationDTO created = capDocDao.create(doc);
		
		activityManager.addActivity(ActivityConcept.ACTIVITY_CONCEPT_CERTIFIED_PRODUCT, created.getId(), 
				"A file " + doc.getFileName() + " was added to a corrective action plan for certified product "+ cpDto.getChplProductNumberForActivity(), null, created);
		return created;
	}
	
	@Override
	@Transactional
	@PreAuthorize("hasRole('ROLE_ADMIN') or "
			+ "(hasRole('ROLE_ACB_ADMIN') and hasPermission(#acbId, 'gov.healthit.chpl.dto.CertificationBodyDTO', admin))")
	public CorrectiveActionPlanDetails addCertificationsToPlan(Long acbId,<commit_msg>OCD-427: fix bug putting the wrong chpl product number in some activities<commit_after>package gov.healthit.chpl.manager.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.JsonProcessingException;

import gov.healthit.chpl.dao.CertifiedProductDAO;
import gov.healthit.chpl.dao.CorrectiveActionPlanCertificationResultDAO;
import gov.healthit.chpl.dao.CorrectiveActionPlanDAO;
import gov.healthit.chpl.dao.CorrectiveActionPlanDocumentationDAO;
import gov.healthit.chpl.dao.EntityCreationException;
import gov.healthit.chpl.dao.EntityRetrievalException;
import gov.healthit.chpl.domain.ActivityConcept;
import gov.healthit.chpl.domain.CorrectiveActionPlanDetails;
import gov.healthit.chpl.dto.CertifiedProductDTO;
import gov.healthit.chpl.dto.CorrectiveActionPlanCertificationResultDTO;
import gov.healthit.chpl.dto.CorrectiveActionPlanDTO;
import gov.healthit.chpl.dto.CorrectiveActionPlanDocumentationDTO;
import gov.healthit.chpl.manager.ActivityManager;
import gov.healthit.chpl.manager.CorrectiveActionPlanManager;

@Service
public class CorrectiveActionPlanManagerImpl implements CorrectiveActionPlanManager {

	@Autowired CertifiedProductDAO cpDao;
	@Autowired CorrectiveActionPlanDAO capDao;
	@Autowired CorrectiveActionPlanCertificationResultDAO capCertDao;
	@Autowired CorrectiveActionPlanDocumentationDAO capDocDao;
	@Autowired public ActivityManager activityManager;
	
	@Override
	@Transactional
	@PreAuthorize("hasRole('ROLE_ADMIN') or "
			+ "(hasRole('ROLE_ACB_ADMIN') and hasPermission(#acbId, 'gov.healthit.chpl.dto.CertificationBodyDTO', admin))")
	public CorrectiveActionPlanDetails create(Long acbId, CorrectiveActionPlanDTO toCreate)
			throws EntityRetrievalException, EntityCreationException, JsonProcessingException {
		
		CorrectiveActionPlanDTO created = capDao.create(toCreate);
		
		CertifiedProductDTO cpDto = cpDao.getById(created.getCertifiedProductId());
		activityManager.addActivity(ActivityConcept.ACTIVITY_CONCEPT_CERTIFIED_PRODUCT, created.getId(), 
				"Corrective action plan for "+ cpDto.getChplProductNumberForActivity() +" was created.", null, created);

		return new CorrectiveActionPlanDetails(created, null);
	}


	@Override
	@Transactional
	@PreAuthorize("hasRole('ROLE_ADMIN') or "
			+ "(hasRole('ROLE_ACB_ADMIN') and hasPermission(#acbId, 'gov.healthit.chpl.dto.CertificationBodyDTO', admin))")
	public CorrectiveActionPlanDocumentationDTO addDocumentationToPlan(Long acbId, CorrectiveActionPlanDocumentationDTO doc)
		throws EntityRetrievalException, EntityCreationException, JsonProcessingException {
		CorrectiveActionPlanDTO plan = capDao.getById(doc.getCorrectiveActionPlanId());
		CertifiedProductDTO cpDto = cpDao.getById(plan.getCertifiedProductId());
		
		CorrectiveActionPlanDocumentationDTO created = capDocDao.create(doc);
		
		activityManager.addActivity(ActivityConcept.ACTIVITY_CONCEPT_CERTIFIED_PRODUCT, created.getId(), 
				"A file " + doc.getFileName() + " was added to a corrective action plan for certified product "+ cpDto.getChplProductNumberForActivity(), null, created);
		return created;
	}
	
	@Override
	@Transactional
	@PreAuthorize("hasRole('ROLE_ADMIN') or "
			+ "(hasRole('ROLE_ACB_ADMIN') and hasPermission(#acbId, 'gov.healthit.chpl.dto.CertificationBodyDTO', admin))")
	public CorrectiveActionPlanDetails addCertificationsToPlan(Long acbId,
{'fraction_non_alphanumeric': 0.08887630908576281, 'fraction_numerical': 0.00042456835550523637, 'mean_word_length': 8.037084398976981, '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': '6445427', 'n_tokens_mistral': 2261, 'n_tokens_neox': 2205, 'n_words': 341}