/**
 * Axelor Business Solutions
 *
 * Copyright (C) 2005-2017 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.db.internal.hibernate.type;

import com.google.common.base.Preconditions;

public final class JsonFunction {

	public static final String DEFAULT_TYPE = "text";

	private String field;

	private String attribute;

	private String type;

	public JsonFunction(String field, String attribute, String type) {
		this.field = field;
		this.attribute = attribute;
		this.type = type;
	}

	public JsonFunction(String field, String attribute) {
		this(field, attribute, DEFAULT_TYPE);
	}

	public static JsonFunction fromPath(String path) {
		Preconditions.checkArgument(path != null, "name cannot be null");
		Preconditions.checkArgument(path.indexOf('.') > -1, "not a json path");

		final int dot = path.indexOf('.');
		final int col = path.indexOf("::");

		final String type = col == -1 ? DEFAULT_TYPE : path.substring(col + 2);
		final String rest = col == -1 ? path : path.substring(0, col);
		final String field = rest.substring(0, dot);
		final String attribute = rest.substring(dot + 1);

		return new JsonFunction(field, attribute, type);
	}

	public String getField() {
		return field;
	}

	public String getAttribute() {
		return attribute;
	}

	public String getType() {
		return type;
	}

	@Override
	public String toString() {
		return String.format("json_extract_%s(self.%s, '%s')", type, field, attribute);
	}
}

@@ -1957,85 +1957,299 @@
 %7B%0A%09%09
-return String.format(%22json_extract_%25s(self.%25s, '%25s')%22, type, field, attribute
+final StringBuilder builder = new StringBuilder()%0A%09%09%09%09.append(%22json_extract_%22).append(type).append(%22(%22)%0A%09%09%09%09.append(%22self.%22).append(field);%0A%09%09for (String item : attribute.split(%22%5C%5C.%22)) %7B%0A%09%09%09builder.append(%22, %22).append(%22'%22).append(item).append(%22'%22);%0A%09%09%7D%0A%09%09return builder.append(%22)%22).toString(
 );%0A%09

Allow to select nested json values