CommunityAppSettings.java

/*
 * Copyright 2021 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.model.community;

import java.lang.reflect.Modifier;
import java.util.List;

import com.google.common.collect.Lists;
import org.gringlobal.model.AppSetting;
import org.springframework.util.ReflectionUtils;

/**
 * CommunityAppSettings declares {@link AppSetting} required for the operation of
 * GGCE.
 */
public abstract class CommunityAppSettings {

	public static final String CATEGORY_LABEL = "LABEL";
	public static final AppSettingDef INVENTORYVIABILITY_REPLICATE_LABEL = new AppSettingDef(CATEGORY_LABEL, "INVENTORYVIABILITY_REPLICATE",
			"^XA\n" +
			"^CF0,30\n" +
			"^FO50,10^FDViability {{ inventoryViability.id }}.{{ replicate }}^FS\n" +
			"^FO50,40^FDINV {{ inventory.inventoryNumber }}^FS\n" +
			"^FO50,90^FDACC {{ accession.accessionNumber }}^FS\n" +
			"^FO50,120^FD{{ taxon }}^FS\n" +
			"^FX Third section with bar code.\n" +
			"^FO50,150^BQN,2,6^FDQA,{{ inventoryViability.id }}.{{ replicate }}^FS\n" +
			"^XZ");

	public static final String CATEGORY_MCPD = "MCPD";
	public static final AppSettingDef MCPD_ACCEURL = new AppSettingDef(CATEGORY_MCPD, "ACCEURL", "https://PUBLIC-WEBSITE/{{#_urlpath}}{{{ accession.accessionNumber }}}{{/_urlpath}}");

	public static final String CATEGORY_PASSWORD_POLICY = "PASSWORD_POLICY";
	public static final AppSettingDef PASSWORD_POLICY_MIN_LENGTH = new AppSettingDef(CATEGORY_PASSWORD_POLICY, "MIN_LENGTH", "8");
	public static final AppSettingDef PASSWORD_POLICY_MIN_DIGITS = new AppSettingDef(CATEGORY_PASSWORD_POLICY, "MIN_DIGITS", "1");
	public static final AppSettingDef PASSWORD_POLICY_MIN_SPECIAL_CHARS = new AppSettingDef(CATEGORY_PASSWORD_POLICY, "MIN_SPECIAL_CHARS", "1");

	public static final String CATEGORY_BARCODE = "BARCODE";
	public static final AppSettingDef BARCODE_INVENTORY = new AppSettingDef(CATEGORY_BARCODE, "inventoryBarcode", "I:{{ inventory.id }}");

	/**
	 * Get the list of app settings required for GGCE
	 *
	 * @return List of app settings
	 */
	public static List<AppSettingDef> listCommunityAppSettings() {
		List<AppSettingDef> communitySettings = Lists.newArrayList();

		ReflectionUtils.doWithFields(CommunityAppSettings.class, field -> {
			if (Modifier.isStatic(field.getModifiers())) {
				if (field.getType() == AppSettingDef.class) {
					communitySettings.add((AppSettingDef) field.get(CommunityAppSettings.class));
				}
			}
		});

		return communitySettings;
	}

	public static final class AppSettingDef {
		public final String categoryTag;
		public final String name;
		public final String value;

		public AppSettingDef(String categoryTag, String name, String value) {
			this.categoryTag = categoryTag;
			this.name = name;
			this.value = value;
		}
	}
}