<commit_before>package models;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.validation.Constraint;

import org.joda.time.DateTime;

import play.data.validation.Constraints;
import play.db.ebean.Model;

@Entity
public class Bed extends Model  {

	@Id
	public Long id;

	@Constraints.Required
	@ManyToOne
	public LargeCabin largeCabin;

	@ManyToMany
	public List<Booking> bookings;

	public boolean isAvailable(DateTime fromDate, DateTime toDate) {
		//if there exists a booking within fromDate and toDate, return false
		SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
		for(Booking booking: bookings) //check through all bookings related to this bed and see if daterange overlap
		{
			DateTime fromDate2 = new DateTime(booking.dateFrom);
			DateTime toDate2 = new DateTime(booking.dateTo);

			if((fromDate.isBefore(toDate2) && fromDate2.isBefore(toDate) ||
					(fmt.format(fromDate).equals(fmt.format(fromDate2)) && 
							(fmt.format(fromDate).equals(fmt.format(toDate)) || 
									fmt.format(fromDate2).equals(fmt.format(toDate2)))))) {
				return false;
			}
		}

		return true;
	}

	/** Used for calendar lookup of specific date **/
	public boolean isAvailable(Date date) {
		// TODO Auto-generated method stub
		//if there exists a booking that overlaps date, return false<commit_msg>Date overlap check created, tested for correct functionality as well<commit_after>package models;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.validation.Constraint;

import org.joda.time.DateTime;

import play.data.validation.Constraints;
import play.db.ebean.Model;

@Entity
public class Bed extends Model  {

	@Id
	public Long id;

	@Constraints.Required
	@ManyToOne
	public LargeCabin largeCabin;

	@ManyToMany
	public List<Booking> bookings;

	public boolean isAvailable(DateTime fromDate, DateTime toDate) {

		for(Booking booking: bookings) //check through all bookings related to this bed and see if daterange overlap
		{
			DateTime fromDate2 = new DateTime(booking.dateFrom);
			DateTime toDate2 = new DateTime(booking.dateTo);

			if(isOverlap(fromDate, toDate, fromDate2, toDate2))
				return false;
		}

		return true;
	}

	/**
	 * Checks if two date ranges overlap. If any of the dates are the same it is regarded as overlap
	 * If there is a need to change so that toDate and fromDate2 (or vice versa) can overlap, then
	 * remove the .equals OR lines at the end
	 * @return true if dates overlap
	 */
	private boolean isOverlap(DateTime fromDate, DateTime toDate,
			DateTime fromDate2, DateTime toDate2) {
		SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
		if((fromDate.isBefore(toDate2) && fromDate2.isBefore(toDate) ||
				((fmt.format(fromDate.toDate()).equals(fmt.format(fromDate2.toDate())) && 
						(fmt.format(fromDate.toDate()).equals(fmt.format(toDate2.toDate())))) || 
								fmt.format(fromDate2.toDate()).equals(fmt.format(toDate.toDate()))))) {
			return false;
		}
		return false;
	}

	/** Used for calendar lookup of specific date **/
	public boolean isAvailable(Date date) {
		// TODO Auto-generated method stub
		//if there exists a booking that overlaps date, return false
{'fraction_non_alphanumeric': 0.10008410428931876, 'fraction_numerical': 0.005326604990187833, 'mean_word_length': 4.523219814241486, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '466681', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1071, 'n_words': 313}