GGCE.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;

import java.text.MessageFormat;

import org.apache.commons.lang3.StringUtils;
import org.gringlobal.model.Accession;
import org.gringlobal.model.Inventory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;

import lombok.extern.slf4j.Slf4j;

/**
 * The Class GGCE.
 */
@Slf4j
public class GGCE implements InitializingBean {

	private static GGCE _instance;

	@Value("${accession.format1}")
	private String accessionPart1Format;
	@Value("${accession.format2}")
	private String accessionPart2Format;
	@Value("${accession.format3}")
	private String accessionPart3Format;

	@Value("${inventory.format1}")
	private String inventoryPart1Format;
	@Value("${inventory.format2}")
	private String inventoryPart2Format;
	@Value("${inventory.format3}")
	private String inventoryPart3Format;
	@Value("${inventory.format4}")
	private String inventoryPart4Format;

	@Override
	public void afterPropertiesSet() throws Exception {
		log.warn("Accession number format: {}", this.accessionNumber("ACC", 99999L, "A"));
		log.warn("Inventory number format: {}", this.inventoryNumber("INV", 313313L, "B", "SD"));
		GGCE._instance = this;
	}

	public static String accessionNumber(Accession accession) {
		if (accession == null) {
			return null;
		}

		if (_instance != null) {
			return _instance.accessionNumber(accession.getAccessionNumberPart1(), accession.getAccessionNumberPart2(), accession.getAccessionNumberPart3());
		}

		StringBuilder sb = new StringBuilder(accession.getAccessionNumberPart1());
		if (accession.getAccessionNumberPart2() != null) {
			sb.append(" ").append(accession.getAccessionNumberPart2());
		}
		if (StringUtils.isNotBlank(accession.getAccessionNumberPart3())) {
			sb.append(" ").append(accession.getAccessionNumberPart3());
		}
		return sb.toString();
	}

	private String accessionNumber(String accessionNumberPart1, Long accessionNumberPart2, String accessionNumberPart3) {
		StringBuilder sb = new StringBuilder(MessageFormat.format(accessionPart1Format, accessionNumberPart1));

		if (accessionNumberPart2 != null) {
			sb.append(MessageFormat.format(accessionPart2Format, accessionNumberPart2));
		}
		if (StringUtils.isNotBlank(accessionNumberPart3)) {
			sb.append(MessageFormat.format(accessionPart3Format, accessionNumberPart3));
		}
		
		return sb.toString().replaceAll("\\s+", " ").trim();
	}

	public static String inventoryNumber(Inventory inventory) {
		if (inventory == null) {
			return null;
		}

		if (_instance != null) {
			return _instance.inventoryNumber(inventory.getInventoryNumberPart1(), inventory.getInventoryNumberPart2(), inventory.getInventoryNumberPart3(), inventory
				.getFormTypeCode());
		}

		StringBuilder sb = new StringBuilder(inventory.getInventoryNumberPart1());

		if (inventory.getInventoryNumberPart2() != null) {
			sb.append(" ").append(inventory.getInventoryNumberPart2());
		}

		if (StringUtils.isNotBlank(inventory.getInventoryNumberPart3())) {
			sb.append(" ").append(inventory.getInventoryNumberPart3());
		}

		sb.append(" ").append(inventory.getFormTypeCode());
		return sb.toString();
	}

	private String inventoryNumber(String inventoryNumberPart1, Long inventoryNumberPart2, String inventoryNumberPart3, String formTypeCode) {
		StringBuilder sb = new StringBuilder(MessageFormat.format(inventoryPart1Format, inventoryNumberPart1));

		if (inventoryNumberPart2 != null) {
			sb.append(MessageFormat.format(inventoryPart2Format, inventoryNumberPart2));
		}
		if (StringUtils.isNotBlank(inventoryNumberPart3)) {
			sb.append(MessageFormat.format(inventoryPart3Format, inventoryNumberPart3));
		}
		sb.append(MessageFormat.format(inventoryPart4Format, formTypeCode));
		
		return sb.toString().replaceAll("\\s+", " ").trim();
	}

}