package ca.corefacility.bioinformatics.irida.repositories.specification;

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

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;

import ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin;
import ca.corefacility.bioinformatics.irida.model.project.Project;

import com.google.common.base.Strings;

/**
 * Specification for searching {@link ProjectSampleJoin} properties
 *
 * @author Josh Adam <josh.adam@phac-aspc.gc.ca>
 */
public class ProjectSampleFilterSpecification {
	public static Specification<ProjectSampleJoin> searchProjectSamples(Project project, String name, String organism, Date minDate, Date maxDate) {
		return new Specification<ProjectSampleJoin>() {
			@Override public Predicate toPredicate(Root<ProjectSampleJoin> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
				List<Predicate> predicateList = new ArrayList<>();
				predicateList.add(criteriaBuilder.equal(root.get("project"), project));
				if (!Strings.isNullOrEmpty(name)) {
					predicateList.add(criteriaBuilder.like(root.get("sample").get("sampleName"), "%" + name + "%"));
				}
				if (!Strings.isNullOrEmpty(organism)) {
					predicateList.add(criteriaBuilder.like(root.get("sample").get("organism"), "%" + organism + "%"));
				}
				if (minDate != null) {
					predicateList.add(criteriaBuilder.greaterThanOrEqualTo(
							root.get("createdDate"), minDate));
				}
				if (maxDate != null) {
					predicateList.add(criteriaBuilder
							.lessThanOrEqualTo(root.get("createdDate"), maxDate));
				}
				if (predicateList.size() > 0) {
					return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
				} else {
					return null;
				}
			}
		};
	}
}

@@ -910,168 +910,29 @@
 urn 
-new Specification%3CProjectSampleJoin%3E() %7B%0A%09%09%09@Override public Predicate toPredicate(Root%3CProjectSampleJoin%3E root, CriteriaQuery%3C?%3E criteriaQuery, CriteriaBuilder
+(root, criteriaQuery,
  cri
@@ -944,23 +944,25 @@
 Builder)
+ -%3E
  %7B%0A%09%09%09
-%09
 List%3CPre
@@ -1004,17 +1004,16 @@
 st%3C%3E();%0A
-%09
 %09%09%09predi
@@ -1079,17 +1079,16 @@
 ject));%0A
-%09
 %09%09%09if (!
@@ -1114,33 +1114,32 @@
 ty(name)) %7B%0A%09%09%09%09
-%09
 predicateList.ad
@@ -1215,33 +1215,32 @@
 me + %22%25%22));%0A
-%09
 %09%09%09%7D%0A%09%09%09
 %09if (!String
@@ -1223,25 +1223,24 @@
 ));%0A%09%09%09%7D%0A%09%09%09
-%09
 if (!Strings
@@ -1263,25 +1263,24 @@
 rganism)) %7B%0A
-%09
 %09%09%09%09predicat
@@ -1366,33 +1366,32 @@
 sm + %22%25%22));%0A
-%09
 %09%09%09%7D%0A%09%09%09
 %09if (minDate
@@ -1374,25 +1374,24 @@
 ));%0A%09%09%09%7D%0A%09%09%09
-%09
 if (minDate 
@@ -1393,33 +1393,32 @@
 Date != null) %7B%0A
-%09
 %09%09%09%09predicateLis
@@ -1461,17 +1461,16 @@
 qualTo(%0A
-%09
 %09%09%09%09%09%09ro
@@ -1502,28 +1502,26 @@
 nDate));%0A%09%09%09
-%09
 %7D%0A
-%09
 %09%09%09if (maxDa
@@ -1530,25 +1530,24 @@
  != null) %7B%0A
-%09
 %09%09%09%09predicat
@@ -1578,17 +1578,16 @@
 r%0A%09%09%09%09%09%09
-%09
 .lessTha
@@ -1633,25 +1633,24 @@
 Date));%0A
-%09
 %09%09%09%7D%0A%09%09%09
 %09if (pre
@@ -1641,17 +1641,16 @@
 %09%09%09%7D%0A%09%09%09
-%09
 if (pred
@@ -1673,17 +1673,16 @@
  %3E 0) %7B%0A
-%09
 %09%09%09%09retu
@@ -1768,17 +1768,16 @@
 %5D));%0A%09%09%09
-%09
 %7D else %7B
@@ -1777,17 +1777,16 @@
  else %7B%0A
-%09
 %09%09%09%09retu
@@ -1798,14 +1798,8 @@
 ll;%0A
-%09%09%09%09%7D%0A
 %09%09%09%7D

Clean up specification.