<commit_before>package com.blstream.myhoard.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.blstream.myhoard.biz.enums.RequestMethodEnum;
import com.blstream.myhoard.biz.model.CollectionDTO;
import com.blstream.myhoard.biz.service.CollectionService;
import com.blstream.myhoard.biz.validator.CollectionValidator;
import com.blstream.myhoard.biz.validator.RequestValidator;
import com.blstream.myhoard.exception.ErrorCodeEnum;
import com.blstream.myhoard.exception.MyHoardException;
import com.blstream.myhoard.exception.MyHoardRestException;
import com.blstream.myhoard.exception.NotFoundException;
import com.blstream.myhoard.exception.ResourceAlreadyExistException;

@Controller
@RequestMapping("/collections")
public class CollectionController {

	private static final Logger logger = Logger
			.getLogger(CollectionController.class.getCanonicalName());

	@Autowired
	private CollectionService collectionService;

	@Autowired
	private CollectionValidator collectionValidator;

	@Autowired
	private RequestValidator requestValidator;

	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	@ResponseStatus(HttpStatus.CREATED)
	public CollectionDTO addCollection(@RequestBody CollectionDTO collection)
			throws MyHoardException {

		collectionValidator.validate(collection, RequestMethodEnum.POST);

		return collectionService.create(collection);
	}

	@RequestMapping(value = "/{collectionId}", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	@ResponseBody
	public CollectionDTO getCollection(
			@PathVariable("collectionId") String idStr) {

		requestValidator.validId(idStr);
		try {
			return collectionService.get(Integer.parseInt(idStr));
		} catch (MyHoardException e) {
			logger.error(e.getMessage(), e);
			throw new NotFoundException(String.format(
					"Collection with id = %s not exist", idStr));
		}
	}

	@RequestMapping(method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	@ResponseBody
	public List<CollectionDTO> getCollections(
			@RequestParam(value = "sort_by", required = false) List<String> sortBy,
			@RequestParam(value = "sort_direction", required = false) String sortDirection) {

		if (sortBy != null || sortDirection != null) {
			if (sortBy == null || sortDirection == null) {
				throw new MyHoardRestException(400);
			} else {

				String[] availableFields = { "name", "description",
						"created_date", "modified_date", "owner" };
				String[] availableDirection = { "asc", "desc" };

				for (String sortByElem : sortBy) {
					boolean everythingOk = false;
					for (String available : availableFields) {
						if (sortByElem.equals(available)) {
							everythingOk = true;
							break;
						}
					}
					if (!everythingOk) {
						throw new MyHoardRestException(1000);
					}
					everythingOk = false;
				}

				if (!sortDirection.equals(availableDirection[0])
						&& !sortDirection.equals(availableDirection[1])) {
					throw new MyHoardRestException(1000);
				}

				for (int i = 0; i < sortBy.size(); i++) {
					if (sortBy.get(i).equals("modified_date")) {
						sortBy.set(i, "modifiedDate");
					} else if (sortBy.get(i).equals("created_date")) {
						sortBy.set(i, "createdDate");
					}
				}

				try {
					return collectionService.getList(sortBy, sortDirection); // TODO
																				// Matuesz
																				// -
																				// owner
				} catch (MyHoardException e) {
					logger.error("getList error", e);
				}
			}
		}

		try {
			return collectionService.getList();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new MyHoardRestException(ErrorCodeEnum.READ.getValue());
		}
	}

	@RequestMapping(value = "/{collectionId}", method = RequestMethod.DELETE)
	@ResponseStatus(HttpStatus.NO_CONTENT)
	@ResponseBody
	public void deleteCollection(@PathVariable("collectionId") String idStr) {

		requestValidator.validId(idStr);

		try {
			collectionService.remove(Integer.parseInt(idStr));
		} catch (MyHoardException e) {
			logger.error(e.getMessage(), e);
			throw new NotFoundException(String.format(
					"Collection with id = %s not exist", idStr));
		}

	}

	@RequestMapping(value = "/{collectionId}", method = RequestMethod.PUT)
	@ResponseStatus(HttpStatus.OK)
	@ResponseBody
	public CollectionDTO updateCollection(
			@PathVariable("collectionId") String idStr,
			@RequestBody CollectionDTO collection) {

		requestValidator.validId(idStr);		
		collectionValidator.validate(collection, RequestMethodEnum.PUT);
		
		try {
			collection.setId(idStr);
			return collectionService.update(collection);
		} catch (ResourceAlreadyExistException ex) {
			logger.error(ex.getMessage(), ex);
			throw new ResourceAlreadyExistException(String.format(
					"Collection with name: %s already exist!",
					collection.getName()));
		} catch (Exception e) {
			throw new NotFoundException(String.format(
					"Collection with id = %s not exist", idStr));
		}
	}
}<commit_msg>items<commit_after>package com.blstream.myhoard.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.blstream.myhoard.biz.enums.RequestMethodEnum;
import com.blstream.myhoard.biz.model.CollectionDTO;
import com.blstream.myhoard.biz.service.CollectionService;
import com.blstream.myhoard.biz.validator.CollectionValidator;
import com.blstream.myhoard.biz.validator.RequestValidator;
import com.blstream.myhoard.exception.ErrorCodeEnum;
import com.blstream.myhoard.exception.MyHoardException;
import com.blstream.myhoard.exception.MyHoardRestException;
import com.blstream.myhoard.exception.NotFoundException;
import com.blstream.myhoard.exception.ResourceAlreadyExistException;

@Controller
@RequestMapping("/collections")
public class CollectionController {

	private static final Logger logger = Logger
			.getLogger(CollectionController.class.getCanonicalName());

	@Autowired
	private CollectionService collectionService;

	@Autowired
	private CollectionValidator collectionValidator;

	@Autowired
	private RequestValidator requestValidator;

	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	@ResponseStatus(HttpStatus.CREATED)
	public CollectionDTO addCollection(@RequestBody CollectionDTO collection)
			throws MyHoardException {

		collectionValidator.validate(collection, RequestMethodEnum.POST);

		return collectionService.create(collection);
	}

	@RequestMapping(value = "/{collectionId}", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	@ResponseBody
	public CollectionDTO getCollection(
			@PathVariable("collectionId") String idStr) {

		requestValidator.validId(idStr);
		try {
			return collectionService.get(Integer.parseInt(idStr));
		} catch (MyHoardException e) {
			logger.error(e.getMessage(), e);
			throw new NotFoundException(String.format(
					"Collection with id = %s not exist", idStr));
		}
	}

	@RequestMapping(method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	@ResponseBody
	public List<CollectionDTO> getCollections(
			@RequestParam(value = "sort_by", required = false) List<String> sortBy,
			@RequestParam(value = "sort_direction", required = false) String sortDirection) {

		if (sortBy != null || sortDirection != null) {
			if (sortBy == null || sortDirection == null) {
				throw new MyHoardRestException(400);
			} else {

				String[] availableFields = { "name", "description",
						"created_date", "modified_date", "owner" };
				String[] availableDirection = { "asc", "desc" };

				for (String sortByElem : sortBy) {
					boolean everythingOk = false;
					for (String available : availableFields) {
						if (sortByElem.equals(available)) {
							everythingOk = true;
							break;
						}
					}
					if (!everythingOk) {
						throw new MyHoardRestException(1000);
					}
					everythingOk = false;
				}

				if (!sortDirection.equals(availableDirection[0])
						&& !sortDirection.equals(availableDirection[1])) {
					throw new MyHoardRestException(1000);
				}

				for (int i = 0; i < sortBy.size(); i++) {
					if (sortBy.get(i).equals("modified_date")) {
						sortBy.set(i, "modifiedDate");
					} else if (sortBy.get(i).equals("created_date")) {
						sortBy.set(i, "createdDate");
					}
				}

				try {
					return collectionService.getList(sortBy, sortDirection); // TODO
																				// Matuesz
																				// -
																				// owner
				} catch (MyHoardException e) {
					logger.error("getList error", e);
				}
			}
		}

		try {
			return collectionService.getList();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new MyHoardRestException(ErrorCodeEnum.READ.getValue());
		}
	}

	@RequestMapping(value = "/{collectionId}", method = RequestMethod.DELETE)
	@ResponseStatus(HttpStatus.NO_CONTENT)
	@ResponseBody
	public void deleteCollection(@PathVariable("collectionId") String idStr) {

		requestValidator.validId(idStr);

		try {
			collectionService.remove(Integer.parseInt(idStr));
		} catch (MyHoardException e) {
			logger.error(e.getMessage(), e);
			throw new NotFoundException(String.format(
					"Collection with id = %s not exist", idStr));
		}

	}

	@RequestMapping(value = "/{collectionId}", method = RequestMethod.PUT)
	@ResponseStatus(HttpStatus.OK)
	@ResponseBody
	public CollectionDTO updateCollection(
			@PathVariable("collectionId") String idStr,
			@RequestBody CollectionDTO collection) {

		requestValidator.validId(idStr);		
		collectionValidator.validate(collection, RequestMethodEnum.PUT);
		
		try {
			collection.setId(idStr);
			return collectionService.update(collection);
		} catch (ResourceAlreadyExistException ex) {
			logger.error(ex.getMessage(), ex);
			throw new ResourceAlreadyExistException(String.format(
					"Collection with name: %s already exist!",
					collection.getName()));
		} catch (Exception e) {
			throw new NotFoundException(String.format(
					"Collection with id = %s not exist", idStr));
		}
	}
	
	
	@RequestMapping(value = "/{collectionId}/items", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	@ResponseBody
	public List<CollectionDTO> getCollectionsItems(
			@PathVariable("collectionId") String idStr) {
		
		return null;
	}
	
}
{'fraction_non_alphanumeric': 0.11510536568089251, 'fraction_numerical': 0.002656277669559058, 'mean_word_length': 4.542198233562316, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 8, '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': '259991', 'n_tokens_mistral': 3745, 'n_tokens_neox': 3250, 'n_words': 630}