<commit_before>/**
 * Axelor Business Solutions
 *
 * Copyright (C) 2005-2014 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.tools.x2j.pojo

import groovy.util.slurpersupport.NodeChild

import com.axelor.tools.x2j.Utils

class Finder {

	Entity entity

	String name

	String type

	String filter

	Boolean cacheable

	Boolean flush

	List<String> orderBy

	List<String> fields

	Finder(Entity entity, NodeChild node) {
		this.entity = entity
		this.name = node.@name.toString().trim()
		this.fields = node.@using.toString().trim().split(/\s*,\s*/)
		this.filter = node.@filter.toString().trim();
		this.orderBy = node.@orderBy.toString().trim().split(/\s*,\s*/)

		this.type = entity.name
		if (node.@all == "true") {
			type = "Query<" + type + ">"
		}

		fields = fields.findAll { s -> !s.empty }
		orderBy = orderBy.findAll { s -> !s.empty }

		if (node.@cacheable == "true") cacheable = true
		if (node.@flush == "false") flush = false
	}

	Finder(Entity entity, String field) {
		this.entity = entity
		this.name = "findBy" + Utils.firstUpper(field)
		this.type = entity.name
		this.fields = [field]
		this.filter = ""
	}

	private static final def TYPES = [
		"int"		: "int",
		"long"		: "long",
		"double"	: "double",
		"boolean"	: "boolean",
		"Integer"	: "Integer",
		"Long"		: "Long",
		"Double"	: "Double",
		"Boolean"	: "Boolean",
		"String"	: "String",
		"LocalDate"		: "org.joda.time.LocalDate",
		"LocalTime"		: "org.joda.time.LocalTime",
		"LocalDateTime"	: "org.joda.time.LocalDateTime",
		"DateTime"		: "org.joda.time.DateTime",
		"BigDecimal"	: "java.math.BigDecimal"
	]

	String getCode(boolean useStatic) {

		def query = []
		def params = []
		def args = []

		for(String field : fields) {
			def parts = field.split(/\:/)
			def n = Utils.firstLower(field)
			def p
			def t
			if (parts.length > 1) {
				if (filter.empty) return "" // filter must be provided
				t = parts[0]
				n = parts[1]
				if (TYPES[t]) {
					t = entity.importType(TYPES[t])
					t = entity.repository.importType(TYPES[t])
				} else {
					return ""
				}
			} else {
				p = entity.getField(n)
				if (!p && entity.baseEntity) p = entity.baseEntity.getField(n)
				if (!p) return ""<commit_msg>Allow all types/classes with finder method params<commit_after>/**
 * Axelor Business Solutions
 *
 * Copyright (C) 2005-2014 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.tools.x2j.pojo

import groovy.util.slurpersupport.NodeChild

import com.axelor.tools.x2j.Utils

class Finder {

	Entity entity

	String name

	String type

	String filter

	Boolean cacheable

	Boolean flush

	List<String> orderBy

	List<String> fields

	Finder(Entity entity, NodeChild node) {
		this.entity = entity
		this.name = node.@name.toString().trim()
		this.fields = node.@using.toString().trim().split(/\s*,\s*/)
		this.filter = node.@filter.toString().trim();
		this.orderBy = node.@orderBy.toString().trim().split(/\s*,\s*/)

		this.type = entity.name
		if (node.@all == "true") {
			type = "Query<" + type + ">"
		}

		fields = fields.findAll { s -> !s.empty }
		orderBy = orderBy.findAll { s -> !s.empty }

		if (node.@cacheable == "true") cacheable = true
		if (node.@flush == "false") flush = false
	}

	Finder(Entity entity, String field) {
		this.entity = entity
		this.name = "findBy" + Utils.firstUpper(field)
		this.type = entity.name
		this.fields = [field]
		this.filter = ""
	}

	private static final def TYPES = [
		"int"		: "int",
		"long"		: "long",
		"double"	: "double",
		"boolean"	: "boolean",
		"Integer"	: "Integer",
		"Long"		: "Long",
		"Double"	: "Double",
		"Boolean"	: "Boolean",
		"String"	: "String",
		"LocalDate"		: "org.joda.time.LocalDate",
		"LocalTime"		: "org.joda.time.LocalTime",
		"LocalDateTime"	: "org.joda.time.LocalDateTime",
		"DateTime"		: "org.joda.time.DateTime",
		"BigDecimal"	: "java.math.BigDecimal"
	]

	String getCode(boolean useStatic) {

		def query = []
		def params = []
		def args = []

		for(String field : fields) {
			def parts = field.split(/\:/)
			def n = Utils.firstLower(field)
			def p
			def t
			if (parts.length > 1) {
				if (filter.empty) return "" // filter must be provided
				t = parts[0]
				n = parts[1]
				if (TYPES[t]) {
					t = entity.importType(TYPES[t])
					t = entity.repository.importType(TYPES[t])
				} else {
					t = entity.importType(parts[0])
					t = entity.repository.importType(parts[0])
				}
			} else {
				p = entity.getField(n)
				if (!p && entity.baseEntity) p = entity.baseEntity.getField(n)
				if (!p) return ""
{'fraction_non_alphanumeric': 0.14634998253580161, 'fraction_numerical': 0.005239259517988124, 'mean_word_length': 3.1986803519061584, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 8, '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': '3751582', 'n_tokens_mistral': 2194, 'n_tokens_neox': 1962, 'n_words': 541}