<commit_before>package gov.healthit.chpl.web.controller;


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

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.acls.domain.PrincipalSid;
import org.springframework.security.acls.model.Permission;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;

import gov.healthit.chpl.auth.Util;
import gov.healthit.chpl.auth.dto.UserDTO;
import gov.healthit.chpl.auth.dto.UserPermissionDTO;
import gov.healthit.chpl.auth.json.User;
import gov.healthit.chpl.auth.manager.UserManager;
import gov.healthit.chpl.auth.user.UserRetrievalException;
import gov.healthit.chpl.dao.EntityCreationException;
import gov.healthit.chpl.dao.EntityRetrievalException;
import gov.healthit.chpl.domain.CertificationBody;
import gov.healthit.chpl.domain.CertificationBodyPermission;
import gov.healthit.chpl.domain.CertificationBodyUser;
import gov.healthit.chpl.domain.UpdateUserAndAcbRequest;
import gov.healthit.chpl.dto.AddressDTO;
import gov.healthit.chpl.dto.CertificationBodyDTO;
import gov.healthit.chpl.manager.CertificationBodyManager;
import gov.healthit.chpl.web.controller.results.CertificationBodyResults;
import gov.healthit.chpl.web.controller.results.CertificationBodyUserResults;

@RestController
@RequestMapping("/acbs")
public class CertificationBodyController {
	
	@Autowired CertificationBodyManager acbManager;
	@Autowired UserManager userManager;
	
	private static final Logger logger = LogManager.getLogger(CertificationBodyController.class);
	
	@RequestMapping(value="/", method=RequestMethod.GET,
			produces="application/json; charset=utf-8")
	public @ResponseBody CertificationBodyResults getAcbs() {
		List<CertificationBodyDTO> acbs = acbManager.getAllForUser();
		
		CertificationBodyResults results = new CertificationBodyResults();
		if(acbs != null) {
			for(CertificationBodyDTO acb : acbs) {
				results.getAcbs().add(new CertificationBody(acb));
			}
		}
		return results;
	}
	
	@RequestMapping(value="/{acbId}", method=RequestMethod.GET,
			produces="application/json; charset=utf-8")
	public @ResponseBody CertificationBody getAcbById(@PathVariable("acbId") Long acbId)
		throws EntityRetrievalException {
		CertificationBodyDTO acb = acbManager.getById(acbId);
		
		return new CertificationBody(acb);
	}
	
	@RequestMapping(value="/create", method= RequestMethod.POST, 
			consumes= MediaType.APPLICATION_JSON_VALUE,
			produces="application/json; charset=utf-8")
	public CertificationBody createAcb(@RequestBody CertificationBody acbInfo) throws UserRetrievalException, EntityRetrievalException, EntityCreationException, JsonProcessingException {
		CertificationBodyDTO toCreate = new CertificationBodyDTO();
		toCreate.setName(acbInfo.getName());
		toCreate.setWebsite(acbInfo.getWebsite());
		AddressDTO address = null;
		if(acbInfo.getAddress() != null) {
			address = new AddressDTO();
			address.setId(acbInfo.getAddress().getAddressId());
			address.setStreetLineOne(acbInfo.getAddress().getLine1());
			address.setStreetLineTwo(acbInfo.getAddress().getLine2());
			address.setCity(acbInfo.getAddress().getCity());
			address.setState(acbInfo.getAddress().getState());
			address.setZipcode(acbInfo.getAddress().getZipcode());
			address.setCountry(acbInfo.getAddress().getCountry());
		}
		toCreate.setAddress(address);
		toCreate = acbManager.create(toCreate);
		return new CertificationBody(toCreate);
	}
	

	@RequestMapping(value="/update", method= RequestMethod.POST, 
			consumes= MediaType.APPLICATION_JSON_VALUE,
			produces="application/json; charset=utf-8")
	public CertificationBody updateAcb(@RequestBody CertificationBody acbInfo) throws EntityRetrievalException, JsonProcessingException, EntityCreationException {
		CertificationBodyDTO toUpdate = new CertificationBodyDTO();
		toUpdate.setId(acbInfo.getId());
		toUpdate.setName(acbInfo.getName());
		toUpdate.setWebsite(acbInfo.getWebsite());
		AddressDTO address = null;
		if(acbInfo.getAddress() != null) {
			address = new AddressDTO();
			address.setId(acbInfo.getAddress().getAddressId());
			address.setStreetLineOne(acbInfo.getAddress().getLine1());
			address.setStreetLineTwo(acbInfo.getAddress().getLine2());
			address.setCity(acbInfo.getAddress().getCity());
			address.setState(acbInfo.getAddress().getState());
			address.setZipcode(acbInfo.getAddress().getZipcode());
			address.setCountry(acbInfo.getAddress().getCountry());
		}
		toUpdate.setAddress(address);
		
		CertificationBodyDTO result = acbManager.update(toUpdate);
		return new CertificationBody(result);
	}
	
	
	@RequestMapping(value="/{acbId}/delete", method= RequestMethod.POST,<commit_msg>OCD-324: make address and website required for acbs<commit_after>package gov.healthit.chpl.web.controller;


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

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.acls.domain.PrincipalSid;
import org.springframework.security.acls.model.Permission;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;

import gov.healthit.chpl.auth.Util;
import gov.healthit.chpl.auth.dto.UserDTO;
import gov.healthit.chpl.auth.dto.UserPermissionDTO;
import gov.healthit.chpl.auth.json.User;
import gov.healthit.chpl.auth.manager.UserManager;
import gov.healthit.chpl.auth.user.UserRetrievalException;
import gov.healthit.chpl.dao.EntityCreationException;
import gov.healthit.chpl.dao.EntityRetrievalException;
import gov.healthit.chpl.domain.CertificationBody;
import gov.healthit.chpl.domain.CertificationBodyPermission;
import gov.healthit.chpl.domain.CertificationBodyUser;
import gov.healthit.chpl.domain.UpdateUserAndAcbRequest;
import gov.healthit.chpl.dto.AddressDTO;
import gov.healthit.chpl.dto.CertificationBodyDTO;
import gov.healthit.chpl.manager.CertificationBodyManager;
import gov.healthit.chpl.web.controller.results.CertificationBodyResults;
import gov.healthit.chpl.web.controller.results.CertificationBodyUserResults;

@RestController
@RequestMapping("/acbs")
public class CertificationBodyController {
	
	@Autowired CertificationBodyManager acbManager;
	@Autowired UserManager userManager;
	
	private static final Logger logger = LogManager.getLogger(CertificationBodyController.class);
	
	@RequestMapping(value="/", method=RequestMethod.GET,
			produces="application/json; charset=utf-8")
	public @ResponseBody CertificationBodyResults getAcbs() {
		List<CertificationBodyDTO> acbs = acbManager.getAllForUser();
		
		CertificationBodyResults results = new CertificationBodyResults();
		if(acbs != null) {
			for(CertificationBodyDTO acb : acbs) {
				results.getAcbs().add(new CertificationBody(acb));
			}
		}
		return results;
	}
	
	@RequestMapping(value="/{acbId}", method=RequestMethod.GET,
			produces="application/json; charset=utf-8")
	public @ResponseBody CertificationBody getAcbById(@PathVariable("acbId") Long acbId)
		throws EntityRetrievalException {
		CertificationBodyDTO acb = acbManager.getById(acbId);
		
		return new CertificationBody(acb);
	}
	
	@RequestMapping(value="/create", method= RequestMethod.POST, 
			consumes= MediaType.APPLICATION_JSON_VALUE,
			produces="application/json; charset=utf-8")
	public CertificationBody createAcb(@RequestBody CertificationBody acbInfo) throws InvalidArgumentsException, UserRetrievalException, EntityRetrievalException, EntityCreationException, JsonProcessingException {
		CertificationBodyDTO toCreate = new CertificationBodyDTO();
		toCreate.setName(acbInfo.getName());
		if(StringUtils.isEmpty(acbInfo.getWebsite())) {
			throw new InvalidArgumentsException("A website is required for a new certification body");
		}
		toCreate.setWebsite(acbInfo.getWebsite());
		
		if(acbInfo.getAddress() == null) {
			throw new InvalidArgumentsException("An address is required for a new certification body");
		}
		AddressDTO address = new AddressDTO();
		address.setId(acbInfo.getAddress().getAddressId());
		address.setStreetLineOne(acbInfo.getAddress().getLine1());
		address.setStreetLineTwo(acbInfo.getAddress().getLine2());
		address.setCity(acbInfo.getAddress().getCity());
		address.setState(acbInfo.getAddress().getState());
		address.setZipcode(acbInfo.getAddress().getZipcode());
		address.setCountry(acbInfo.getAddress().getCountry());
		toCreate.setAddress(address);
		toCreate = acbManager.create(toCreate);
		return new CertificationBody(toCreate);
	}
	

	@RequestMapping(value="/update", method= RequestMethod.POST, 
			consumes= MediaType.APPLICATION_JSON_VALUE,
			produces="application/json; charset=utf-8")
	public CertificationBody updateAcb(@RequestBody CertificationBody acbInfo) throws InvalidArgumentsException, EntityRetrievalException, JsonProcessingException, EntityCreationException {
		CertificationBodyDTO toUpdate = new CertificationBodyDTO();
		toUpdate.setId(acbInfo.getId());
		toUpdate.setName(acbInfo.getName());
		if(StringUtils.isEmpty(acbInfo.getWebsite())) {
			throw new InvalidArgumentsException("A website is required to update the certification body");
		}
		toUpdate.setWebsite(acbInfo.getWebsite());
		
		if(acbInfo.getAddress() == null) {
			throw new InvalidArgumentsException("An address is required to update the certification body");
		}
		AddressDTO address = new AddressDTO();
		address.setId(acbInfo.getAddress().getAddressId());
		address.setStreetLineOne(acbInfo.getAddress().getLine1());
		address.setStreetLineTwo(acbInfo.getAddress().getLine2());
		address.setCity(acbInfo.getAddress().getCity());
		address.setState(acbInfo.getAddress().getState());
		address.setZipcode(acbInfo.getAddress().getZipcode());
		address.setCountry(acbInfo.getAddress().getCountry());
		toUpdate.setAddress(address);
		
		CertificationBodyDTO result = acbManager.update(toUpdate);
		return new CertificationBody(result);
	}
	
	
	@RequestMapping(value="/{acbId}/delete", method= RequestMethod.POST,
{'fraction_non_alphanumeric': 0.11403428361903016, 'fraction_numerical': 0.002108350902924191, 'mean_word_length': 6.826398852223816, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5941150', 'n_tokens_mistral': 3504, 'n_tokens_neox': 3295, 'n_words': 490}