package com.aemreunal.repository.beacon;

/*
 **************************
 * Copyright (c) 2014     *
 *                        *
 * This code belongs to:  *
 *                        *
 * Ahmet Emre Ünal        *
 * S001974                *
 *                        *
 * aemreunal@gmail.com    *
 * emre.unal@ozu.edu.tr   *
 *                        *
 * aemreunal.com          *
 **************************
 */

import java.util.ArrayList;
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 com.aemreunal.domain.Beacon;

public class BeaconSpecifications {
    /**
     * Creates the Beacon search specification from the given attributes
     *
     * @param projectId
     *     The project ID to search in
     * @param uuid
     *     The UUID attribute to search for
     * @param major
     *     The major attribute to search for
     * @param minor
     *     The minor attribute to search for
     *
     * @return The specification of the beacon
     */
    public static Specification<Beacon> beaconWithSpecification(final Long projectId, final String uuid, final String major, final String minor) {
        return new Specification<Beacon>() {
            public Predicate toPredicate(Root<Beacon> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                ArrayList<Predicate> predicates = new ArrayList<Predicate>();
                // Project specification
                predicates.add(builder.equal(root.get("project").get("projectId"), projectId));

                if (!uuid.equals("")) {
                    if (uuid.length() == Beacon.UUID_MAX_LENGTH) {
                        predicates.add(builder.equal(root.get("uuid"), uuid.toUpperCase()));
                    } else {
                        predicates.add(builder.like(root.get("uuid").as(String.class), "%" + uuid.toUpperCase() + "%"));
                    }
                }

                if (!major.equals("")) {
                    predicates.add(builder.equal(root.get("major"), major.toUpperCase()));
                }

                if (!minor.equals("")) {
                    predicates.add(builder.equal(root.get("minor"), minor.toUpperCase()));
                }

                return builder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
    }
}

@@ -2361,32 +2361,1042 @@
              %7D%0A%0A
+                return builder.and(predicates.toArray(new Predicate%5Bpredicates.size()%5D));%0A            %7D%0A        %7D;%0A    %7D%0A%0A    /**%0A     * Creates the 'Beacon exists' search specification from the given attributes%0A     *%0A     * @param projectId%0A     *     The project ID to search in%0A     * @param beaconId%0A     *     The ID of the beacon to find%0A     *%0A     * @return The specification of the beacon%0A     */%0A    public static Specification%3CBeacon%3E beaconExistsSpecification(final Long projectId, final Long beaconId) %7B%0A        return new Specification%3CBeacon%3E() %7B%0A            public Predicate toPredicate(Root%3CBeacon%3E root, CriteriaQuery%3C?%3E query, CriteriaBuilder builder) %7B%0A                ArrayList%3CPredicate%3E predicates = new ArrayList%3CPredicate%3E();%0A                // Project specification%0A                predicates.add(builder.equal(root.get(%22project%22).get(%22projectId%22), projectId));%0A%0A                // Beacon specification%0A                predicates.add(builder.equal(root.get(%22beaconId%22), beaconId));%0A%0A
                 

Add existance check specification