<commit_before>/*
 * Copyright (C) 2012 - 2012 NHN Corporation
 * All rights reserved.
 *
 * This file is part of The nGrinder software distribution. Refer to
 * the file LICENSE which is part of The nGrinder distribution for
 * licensing details. The nGrinder distribution is available on the
 * Internet at http://nhnopensource.org/ngrinder
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.ngrinder.agent.repository;

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

import net.grinder.message.console.AgentControllerState;

import org.ngrinder.agent.model.AgentInfo;
import org.ngrinder.model.User;
import org.springframework.data.jpa.domain.Specification;

/**
 * Class description.
 *
 * @author Mavlarn
 * @since 3.1
 */
public abstract class AgentManagerSpecification {

	public static Specification<AgentInfo> startWithRegion(final String region) {
		return new Specification<AgentInfo>() {
			@Override
			public Predicate toPredicate(Root<AgentInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				String queryStr = region + "%";
				return cb.like(root.get("region").as(String.class), queryStr);
			}
		};
	}

	public static Specification<AgentInfo> startWithRegionEqualStatusOfUser(final String region,
			final AgentControllerState status, final User user) {
		return new Specification<AgentInfo>() {
			@Override
			public Predicate toPredicate(Root<AgentInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				String regionQueryStr = region + "%";
				String userQueryStr = "%owned_" + user.getUserId() + "%";
				
				return cb.and(cb.and(cb.like(root.get("region").as(String.class), regionQueryStr),
									cb.like(root.get("region").as(String.class), userQueryStr)),
						cb.equal(root.get("status"), status));
			}
		};
	}
}<commit_msg>[NGRINDER-347] Implement ehcache cluster<commit_after>/*
 * Copyright (C) 2012 - 2012 NHN Corporation
 * All rights reserved.
 *
 * This file is part of The nGrinder software distribution. Refer to
 * the file LICENSE which is part of The nGrinder distribution for
 * licensing details. The nGrinder distribution is available on the
 * Internet at http://nhnopensource.org/ngrinder
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.ngrinder.agent.repository;

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

import net.grinder.message.console.AgentControllerState;

import org.ngrinder.agent.model.AgentInfo;
import org.ngrinder.model.User;
import org.springframework.data.jpa.domain.Specification;

/**
 * Class description.
 *
 * @author Mavlarn
 * @since 3.1
 */
public abstract class AgentManagerSpecification {

	public static Specification<AgentInfo> startWithRegion(final String region) {
		return new Specification<AgentInfo>() {
			@Override
			public Predicate toPredicate(Root<AgentInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				String queryStr = region + "%";
				return cb.like(root.get("region").as(String.class), queryStr);
			}
		};
	}

	/**
	 * query specification to get available agents for user.
	 * condition is:
			1. the ready agents in this region
			2. user specified agent, which name is: ${region} + * + "owned_${userId}"
	 * @param region
	 * 				agent region.
	 * @param status
	 * 				agent status
	 * @param user
	 * 				specified user.
	 * @return
	 */
	public static Specification<AgentInfo> startWithRegionEqualStatusOfUser(final String region,
			final AgentControllerState status, final User user) {
		return new Specification<AgentInfo>() {
			@Override
			public Predicate toPredicate(Root<AgentInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				String regionQueryStr = region + "%";
				String userQueryStr = region + "%owned_" + user.getUserId() + "%";
				
				return cb.and(cb.or(cb.like(root.get("region").as(String.class), regionQueryStr),
									cb.like(root.get("region").as(String.class), userQueryStr)),
						cb.equal(root.get("status"), status));
			}
		};
	}
}
{'fraction_non_alphanumeric': 0.09613027490807215, 'fraction_numerical': 0.0043775170723165824, 'mean_word_length': 4.460803059273423, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '2282199', 'n_tokens_mistral': 1788, 'n_tokens_neox': 1554, 'n_words': 543}