All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.imunity.vaadin.auth.services.tabs.GeneralTab Maven / Gradle / Ivy

There is a newer version: 4.0.3
Show newest version
/*
 * Copyright (c) 2019 Bixbit - Krzysztof Benedyczak. All rights reserved.
 * See LICENCE.txt file for licensing information.
 */

package io.imunity.vaadin.auth.services.tabs;

import static io.imunity.vaadin.elements.CSSVars.TEXT_FIELD_BIG;
import static io.imunity.vaadin.elements.CSSVars.TEXT_FIELD_MEDIUM;
import static io.imunity.vaadin.elements.CssClassNames.MEDIUM_VAADIN_FORM_ITEM_LABEL;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.formlayout.FormLayout;
import com.vaadin.flow.component.html.NativeLabel;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.data.binder.ValidationResult;

import io.imunity.vaadin.elements.LocalizedTextFieldDetails;
import io.imunity.vaadin.auth.services.DefaultServiceDefinition;
import io.imunity.vaadin.auth.services.ServiceEditorBase.EditorTab;
import io.imunity.vaadin.auth.services.ServiceEditorComponent.ServiceEditorTab;
import io.imunity.vaadin.auth.services.ServiceTypeInfoHelper;
import pl.edu.icm.unity.base.endpoint.EndpointTypeDescription;
import pl.edu.icm.unity.base.exceptions.WrongArgumentException;
import pl.edu.icm.unity.base.i18n.I18nString;
import pl.edu.icm.unity.base.message.MessageSource;
import pl.edu.icm.unity.engine.api.endpoint.EndpointPathValidator;

/**
 * Service general editor tab
 * 
 * @author P.Piernik
 *
 */
public class GeneralTab extends VerticalLayout implements EditorTab
{
	protected final MessageSource msg;
	private final EndpointTypeDescription type;
	private final List usedEndpointsPaths;
	private final List usedNames;
	private final Set serverContextPaths;

	public GeneralTab(MessageSource msg, EndpointTypeDescription type, List usedEndpointsPaths, List usedNames,
			Set serverContextPaths)
	{
		this.msg = msg;
		this.type = type;
		this.usedEndpointsPaths = usedEndpointsPaths;
		this.serverContextPaths = serverContextPaths;
		this.usedNames = usedNames;
	}

	public void initUI(Binder binder, boolean editMode)
	{
		setMargin(false);

		FormLayout mainGeneralLayout = new FormLayout();
		mainGeneralLayout.setResponsiveSteps(new FormLayout.ResponsiveStep("0", 1));
		mainGeneralLayout.addClassName(MEDIUM_VAADIN_FORM_ITEM_LABEL.getName());

		TextField name = new TextField();
		name.setReadOnly(editMode);
		binder.forField(name)
				.asRequired().withValidator((v,c) -> {
					
					if (!editMode && usedNames.contains(v))
					{
						return ValidationResult.error(msg.getMessage("ServiceEditorBase.usedName", v));
					}
					
					return ValidationResult.ok();
				})
				.bind("name");
		mainGeneralLayout.addFormItem(name, msg.getMessage("ServiceEditorBase.name"));

		NativeLabel binding = new NativeLabel();
		binding.setText(ServiceTypeInfoHelper.getBinding(msg, type.getSupportedBinding()));
		mainGeneralLayout.addFormItem(binding, msg.getMessage("ServiceEditorBase.binding"));
		if (type.getPaths() != null && !type.getPaths()
				.isEmpty() && !type.getPaths()
						.keySet()
						.iterator()
						.next()
						.isEmpty())

		{
			TextField paths = new TextField();
			paths.setWidth(TEXT_FIELD_MEDIUM.value());
			List pathsList = type.getPaths()
					.keySet()
					.stream()
					.collect(Collectors.toList());
			pathsList.set(0, pathsList.get(0)
					.isEmpty() ? "" : pathsList.get(0) + " (" + msg.getMessage("default") + ")");
			paths.setValue(String.join(",", pathsList));
			paths.setReadOnly(true);
			mainGeneralLayout.addFormItem(paths, msg.getMessage("ServiceEditorBase.paths"));
		}

		TextField contextPath = new TextField();
		contextPath.setReadOnly(editMode);
		contextPath.setWidth(TEXT_FIELD_MEDIUM.value());
		binder.forField(contextPath)
				.asRequired()
				.withValidator((v, c) ->
				{

					return editMode ? validatePathForEdit(v) : validatePathForAdd(v);

				})
				.bind("address");
		mainGeneralLayout.addFormItem(contextPath, msg.getMessage("ServiceEditorBase.contextPath"));

		LocalizedTextFieldDetails displayedName = new LocalizedTextFieldDetails(msg.getEnabledLocales()
				.values(), msg.getLocale());
		displayedName.setWidth(TEXT_FIELD_BIG.value());
		binder.forField(displayedName)
				.withConverter(I18nString::new, I18nString::getLocalizedMap)
				.bind("displayedName");
		mainGeneralLayout.addFormItem(displayedName, msg.getMessage("ServiceEditorBase.displayedName"));

		TextField description = new TextField();
		binder.forField(description)
				.bind("description");
		description.setWidth(TEXT_FIELD_BIG.value());
		mainGeneralLayout.addFormItem(description, msg.getMessage("ServiceEditorBase.description"));

		add(mainGeneralLayout);
	}

	private ValidationResult validatePathForAdd(String path)
	{
		if (path == null || path.isEmpty())
		{
			return ValidationResult.error(msg.getMessage("fieldRequired"));
		}

		if (usedEndpointsPaths.contains(path))
		{
			return ValidationResult.error(msg.getMessage("ServiceEditorBase.usedContextPath"));
		}

		try
		{
			EndpointPathValidator.validateEndpointPath(path, serverContextPaths);

		} catch (WrongArgumentException e)
		{
			return ValidationResult.error(msg.getMessage("ServiceEditorBase.invalidContextPath"));
		}

		return ValidationResult.ok();
	}

	private ValidationResult validatePathForEdit(String path)
	{
		try
		{
			EndpointPathValidator.validateEndpointPath(path);

		} catch (WrongArgumentException e)
		{
			return ValidationResult.error(msg.getMessage("ServiceEditorBase.invalidContextPath"));
		}

		return ValidationResult.ok();
	}

	@Override
	public String getType()
	{
		return ServiceEditorTab.GENERAL.toString();
	}

	@Override
	public Component getComponent()
	{
		return this;
	}

	@Override
	public VaadinIcon getIcon()
	{
		return VaadinIcon.COGS;
	}

	@Override
	public String getCaption()
	{
		return msg.getMessage("ServiceEditorBase.general");
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy