AppContextHelper.java

/*
 * Copyright 2020 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.component.elastic;

import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;

import org.genesys.blocks.security.model.AclSid;
import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.blocks.util.CurrentApplicationContext;
import org.gringlobal.model.Accession;
import org.gringlobal.model.AccessionInvName;
import org.gringlobal.model.Inventory;
import org.gringlobal.persistence.AccessionInvNameRepository;
import org.gringlobal.persistence.CodeValueLangRepository;
import org.gringlobal.service.CodeValueService;
import org.gringlobal.service.CodeValueTranslationService;
import org.gringlobal.service.CodeValueTranslationService.TranslatedCodeValue;

/**
 * The Class AppContextHelper.
 */
public class AppContextHelper {
	private volatile static AccessionInvNameRepository accessionInvNameRepository;
	private volatile static CodeValueLangRepository codeValueLangRepository;
	private volatile static CustomAclService aclService;
	private volatile static CodeValueService codeValueService;
	private volatile static CodeValueTranslationService codeValueTranslationService;

	public static List<AccessionInvName> loadNames(Accession accession, String formTypeCode) {
		if (accessionInvNameRepository == null) {
			synchronized (AppContextHelper.class) {
				if (accessionInvNameRepository == null) {
					accessionInvNameRepository = CurrentApplicationContext.getContext().getBean(AccessionInvNameRepository.class);
				}
			}
		}
		return accessionInvNameRepository.findAccessionNames(accession, formTypeCode);
	}

	public static List<AccessionInvName> loadNames(Inventory inventory) {
		if (accessionInvNameRepository == null) {
			synchronized (AppContextHelper.class) {
				if (accessionInvNameRepository == null) {
					accessionInvNameRepository = CurrentApplicationContext.getContext().getBean(AccessionInvNameRepository.class);
				}
			}
		}
		return accessionInvNameRepository.findInventoryNames(inventory);
	}

	public static AclSid ensureAuthoritySid(String authority) {
		if (aclService == null) {
			synchronized (AppContextHelper.class) {
				if (aclService == null) {
					aclService = CurrentApplicationContext.getContext().getBean(CustomAclService.class);
				}
			}
		}
		return aclService.ensureAuthoritySid(authority);
	}

	public static void removeAuthoritySid(String authority) {
		if (aclService == null) {
			synchronized (AppContextHelper.class) {
				if (aclService == null) {
					aclService = CurrentApplicationContext.getContext().getBean(CustomAclService.class);
				}
			}
		}
		aclService.removeAuthoritySid(authority);
	}

	public static boolean validateCodeValue(String groupName, String value) {
		if (codeValueService == null) {
			synchronized (AppContextHelper.class) {
				if (codeValueService == null) {
					codeValueService = CurrentApplicationContext.getContext().getBean(CodeValueService.class);
				}
			}
		}
		return codeValueService.validate(groupName, value);
	}

	public static Set<String> findAllTitlesOfCodeValue(String groupName, String value) {
		if (codeValueLangRepository == null) {
			synchronized (AppContextHelper.class) {
				if (codeValueLangRepository == null) {
					codeValueLangRepository = CurrentApplicationContext.getContext().getBean(CodeValueLangRepository.class);
				}
			}
		}
		return codeValueLangRepository.findAllTitles(groupName, value);
	}

	public static Optional<TranslatedCodeValue> findTitleOfCodeValueByLang(String groupName, String value, Locale locale) {
		if (codeValueTranslationService == null) {
			synchronized (AppContextHelper.class) {
				if (codeValueTranslationService == null) {
					codeValueTranslationService = CurrentApplicationContext.getContext().getBean(CodeValueTranslationService.class);
				}
			}
		}

		return codeValueTranslationService.findTranslatedCodeValue(groupName, value, locale);
	}

}