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

pl.edu.icm.unity.stdext.attr.DateTimeAttributeSyntax Maven / Gradle / Ivy

Go to download

Standard plugins which are distributed with the system: attribute syntaxes, identity types, credentials

There is a newer version: 4.0.2
Show newest version
/*
 * Copyright (c) 2017 Bixbit - Krzysztof Benedyczak All rights reserved.
 * See LICENCE.txt file for licensing information.
 */
package pl.edu.icm.unity.stdext.attr;

import com.fasterxml.jackson.databind.JsonNode;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import pl.edu.icm.unity.base.Constants;
import pl.edu.icm.unity.base.attribute.IllegalAttributeValueException;
import pl.edu.icm.unity.base.exceptions.InternalException;
import pl.edu.icm.unity.base.utils.Log;
import pl.edu.icm.unity.engine.api.attributes.AbstractAttributeValueSyntaxFactory;
import pl.edu.icm.unity.engine.api.attributes.AttributeValueSyntax;
import pl.edu.icm.unity.engine.api.attributes.NullAttributeValueException;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

/**
 * DateTime attribute sytax. Accept datetime in various formats
 * 
 * @author P.Piernik
 *
 */
public class DateTimeAttributeSyntax implements AttributeValueSyntax
{
	public static final String ID = "datetime";
	public static List ACCEPTABLE_FORMATS = Arrays.asList(
			"yyyy-MM-dd['T'][ ]HH:mm:ss", "dd-MM-yyyy['T'][ ]HH-mm-ss",
			"ddMMyy['T'][ ]HHmmss", "dd.MM.yyyy['T'][ ]HH.mm.ss",
			"ddMMyyyy['T'][ ]HHmmss", "dd/MM/yyyy['T'][ ]HH/mm/ss");
	
	private static final Logger log = Log.getLogger(Log.U_SERVER_CORE, DateTimeAttributeSyntax.class);	
	
	@Override
	public String getValueSyntaxId()
	{
		return ID;
	}

	@Override
	public JsonNode getSerializedConfiguration()
	{
		return Constants.MAPPER.createObjectNode();
	}

	@Override
	public void setSerializedConfiguration(JsonNode json)
	{
		// OK
	}

	@Override
	public void validate(LocalDateTime value) throws IllegalAttributeValueException
	{
		if (value == null)
			throw new NullAttributeValueException();

	}

	@Override
	public boolean areEqual(LocalDateTime value, Object another)
	{
		return value == null ? null == another : value.equals(another);
	}

	@Override
	public int hashCode(Object value)
	{
		return value.hashCode();
	}

	@Override
	public LocalDateTime convertFromString(String stringRepresentation)
	{
		for (String format : ACCEPTABLE_FORMATS)
		{
			try
			{
				return LocalDateTime.parse(stringRepresentation,
						DateTimeFormatter.ofPattern(format));

			} catch (Exception e)
			{
				log.trace("Can not parse datetime " + stringRepresentation
						+ " using format: " + format, e);
			}
		}

		throw new InternalException("Can not parse datetime " + stringRepresentation
				+ " using standard date formats");
	}

	@Override
	public String convertToString(LocalDateTime value)
	{
		return value.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
	}

	@Override
	public boolean isEmailVerifiable()
	{
		return false;
	}

	@Override
	public boolean isUserVerifiable()
	{
		return false;
	}

	@Component
	public static class Factory extends AbstractAttributeValueSyntaxFactory
	{
		public Factory()
		{
			super(DateTimeAttributeSyntax.ID, DateTimeAttributeSyntax::new);
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy