CodeValueFunctions.java

/*
 * Copyright 2022 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gringlobal.custom.jasper;

import org.gringlobal.component.elastic.AppContextHelper;
import org.springframework.context.i18n.LocaleContextHolder;

import net.sf.jasperreports.functions.annotations.Function;
import net.sf.jasperreports.functions.annotations.FunctionCategories;
import net.sf.jasperreports.functions.annotations.FunctionParameter;
import net.sf.jasperreports.functions.annotations.FunctionParameters;

/**
 * JasperReports helper functions.
 */
@FunctionCategories({ CustomFunctionCategory.class })
public class CodeValueFunctions {

	/**
	 * Returns the Code Value {@code title} in the active language of the report.
	 *
	 * @param groupName Code Value <b>group name</b>
	 * @param value The value
	 * @return Title of the code value in selected language, or {@code "?? " + code} if not available.
	 */
	@Function("CODEVALUE")
	@FunctionParameters({ @FunctionParameter("groupName"), @FunctionParameter("value")})
	public static String CODEVALUE(String groupName, String value) {
		var translatedCodeValue = AppContextHelper.findTitleOfCodeValueByLang(groupName, value, LocaleContextHolder.getLocale());
		if (translatedCodeValue.isPresent()) {
			return translatedCodeValue.get().title;
		}
		return "?? " + value; // "?? SD"
	}

//	/**
//	 * Returns the Code Value {@code title} in selected language.
//	 *
//	 * @param groupName Code Value <b>group name</b>
//	 * @param value The value
//	 * @param locale Target language locale (e.g. {@code java.util.Locale.FRENCH})
//	 * @return Title of the code value in selected language, or {@code "?? " + code} if not available.
//	 */
//	@Function("CODEVALUE")
//	@FunctionParameters({ @FunctionParameter("groupName"), @FunctionParameter("value"), @FunctionParameter("locale")})
//	public static String CODEVALUE(String groupName, String value, Locale locale) {
//		var translatedCodeValue = AppContextHelper.findTitleOfCodeValueByLang(groupName, value, locale);
//		if (translatedCodeValue.isPresent()) {
//			return translatedCodeValue.get().title;
//		}
//		return "?? " + value; // "?? SD"
//	}
}