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

fi.evolver.basics.spring.messaging.entity.MessageLite Maven / Gradle / Ivy

package fi.evolver.basics.spring.messaging.entity;

import java.io.Serializable;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import fi.evolver.basics.spring.messaging.entity.Message.MessageState;
import jakarta.persistence.*;


@Entity
@Table(name="message")
public class MessageLite implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private long id;

	@Column(name="state")
	@Enumerated(EnumType.STRING)
	private MessageState state;

	@Column(name="message_chain_id")
	private Long messageChainId;

	@Column(name="creation_time")
	private Long creationTime;

	@Column(name="last_updated_time")
	private Long lastUpdatedTime;

	@Column(name="fail_count")
	private Integer failCount;

	@Column(name="message_group_id")
	private String messageGroupId;

	@Column(name="priority")
	private Long priority;

	@Column(name="message_data_id")
	private Long messageDataId;

	@ManyToOne
	@JoinColumn(name="message_target_config_id")
	private MessageTargetConfig messageTargetConfig;

	@OrderBy("key, id")
	@BatchSize(size=100)
	@Fetch(value = FetchMode.JOIN)
	@OneToMany(mappedBy="message", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
	private List metadata;


	public long getId() {
		return id;
	}

	public Instant getCreationTime() {
		return Instant.ofEpochMilli(creationTime);
	}

	public Integer getFailCount() {
		return failCount;
	}

	public Instant getLastUpdatedTime() {
		return Instant.ofEpochMilli(lastUpdatedTime);
	}

	public Long getMessageChainId() {
		return messageChainId;
	}

	public String getMessageGroupId() {
		return messageGroupId;
	}

	public MessageTargetConfig getMessageTargetConfig() {
		return messageTargetConfig;
	}

	public Long getPriority() {
		return priority;
	}

	public MessageState getState() {
		return state;
	}


	public void setState(MessageState state) {
		this.state = state;
		this.lastUpdatedTime = Instant.now().toEpochMilli();
	}


	public String getTargetUri() {
		return messageTargetConfig.getUri();
	}

	public Map getMetadata() {
		return metadata.stream().collect(Collectors.toMap(
				MessageMetadataLite::getKey,
				MessageMetadataLite::getValue,
				(u, v) -> u,
				LinkedHashMap::new));
	}


	public String getMessageType() {
		return messageTargetConfig.getMessageType();
	}

	public String getTargetSystem() {
		return messageTargetConfig.getTargetSystem();
	}


	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append(String.format("%s: %s (%s)", id, messageTargetConfig, state));
		getMetadata().entrySet().stream()
				.map(e -> String.format("\n  %s=%s", e.getKey(), e.getValue()))
				.forEach(builder::append);
		return builder.toString();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy