Skip to content

Commit

Permalink
feat: add message-header entity read-only rest api
Browse files Browse the repository at this point in the history
  • Loading branch information
bbortt committed Oct 21, 2023
1 parent 54f7e49 commit 10dcab3
Show file tree
Hide file tree
Showing 36 changed files with 1,950 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,9 @@ public B lastModifiedDate(Instant lastModifiedDate) {
}

protected abstract E getEntity();

public E build() {
return getEntity();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,6 @@ public static class MessageBuilder extends AuditingEntityBuilder<MessageBuilder,

private final Message message = new Message();

public Message build() {
return message;
}

@Override
protected Message getEntity() {
return message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import jakarta.persistence.ManyToOne;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;

import java.io.Serial;
import java.io.Serializable;

Expand Down Expand Up @@ -55,22 +56,30 @@ public class MessageHeader extends AbstractAuditingEntity<MessageHeader, Long> i
@NotNull
@ManyToOne(optional = false)
@JoinColumn(nullable = false)
@JsonIgnoreProperties(value = { "headers", "scenarioExecution" }, allowSetters = true)
@JsonIgnoreProperties(value = {"headers", "scenarioExecution"}, allowSetters = true)
private Message message;

public MessageHeader() {
// Hibernate constructor
}

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

public static MessageHeaderBuilder builder() {
return new MessageHeaderBuilder();
}

public Long getHeaderId() {
return headerId;
}

void setHeaderId(Long headerId) {
this.headerId = headerId;
}

public String getName() {
return name;
}
Expand All @@ -95,13 +104,55 @@ public void setMessage(Message message) {
this.message = message;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o instanceof MessageHeader messageHeader) {
return headerId != null && headerId.equals(messageHeader.headerId);
}
return false;
}

@Override
public int hashCode() {
// see https://vladmihalcea.com/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
return getClass().hashCode();
}

@Override
public String toString() {
return "MessageHeader{" +
"headerId='" + getHeaderId() + "'" +
", createdDate='" + getCreatedDate() + "'" +
", name='" + getName() + "'" +
", value='" + getValue() + "'" +
"}";
"headerId='" + getHeaderId() + "'" +
", createdDate='" + getCreatedDate() + "'" +
", name='" + getName() + "'" +
", value='" + getValue() + "'" +
"}";
}

public static class MessageHeaderBuilder extends AuditingEntityBuilder<MessageHeaderBuilder, MessageHeader, Long> {

private final MessageHeader messageHeader = new MessageHeader();

public MessageHeaderBuilder name(String name) {
messageHeader.setName(name);
return this;
}

public MessageHeaderBuilder value(String value) {
messageHeader.setValue(value);
return this;
}

public MessageHeaderBuilder message(Message message) {
messageHeader.setMessage(message);
return this;
}

@Override
protected MessageHeader getEntity() {
return messageHeader;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,6 @@ public static class TestParameterBuilder extends AuditingEntityBuilder<TestParam

private final TestParameter testParameter = new TestParameter();

public TestParameter build() {
return testParameter;
}

public TestParameterBuilder key(String key) {
if (Objects.isNull(testParameter.testParameterId)) {
testParameter.testParameterId = new TestParameterId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,6 @@ public static class TestResultBuilder extends AuditingEntityBuilder<TestResultBu

private final TestResult testResult = new TestResult();

public TestResult build() {
return testResult;
}

public TestResultBuilder id(Long id) {
testResult.id = id;
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.citrusframework.simulator.repository;

import org.citrusframework.simulator.model.MessageHeader;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
* Spring Data JPA repository for the MessageHeader entity.
*/
@Repository
public interface MessageHeaderRepository extends JpaRepository<MessageHeader, Long>, JpaSpecificationExecutor<MessageHeader> {
default Optional<MessageHeader> findOneWithEagerRelationships(Long id) {
return this.findOneWithToOneRelationships(id);
}

default List<MessageHeader> findAllWithEagerRelationships() {
return this.findAllWithToOneRelationships();
}

default Page<MessageHeader> findAllWithEagerRelationships(Pageable pageable) {
return this.findAllWithToOneRelationships(pageable);
}

@Query(
value = "select messageHeader from MessageHeader messageHeader left join fetch messageHeader.message",
countQuery = "select count(messageHeader) from MessageHeader messageHeader"
)
Page<MessageHeader> findAllWithToOneRelationships(Pageable pageable);

@Query("select messageHeader from MessageHeader messageHeader left join fetch messageHeader.message")
List<MessageHeader> findAllWithToOneRelationships();

@Query("select messageHeader from MessageHeader messageHeader left join fetch messageHeader.message where messageHeader.id =:id")
Optional<MessageHeader> findOneWithToOneRelationships(@Param("id") Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package org.citrusframework.simulator.service;

import jakarta.persistence.criteria.JoinType;
import org.citrusframework.simulator.model.MessageHeader;
import org.citrusframework.simulator.model.MessageHeader_;
import org.citrusframework.simulator.model.Message_;
import org.citrusframework.simulator.repository.MessageHeaderRepository;
import org.citrusframework.simulator.service.criteria.MessageHeaderCriteria;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* Service for executing complex queries for {@link MessageHeader} entities in the database.
* The main input is a {@link MessageHeaderCriteria} which gets converted to {@link Specification},
* in a way that all the filters must apply.
* It returns a {@link List} of {@link MessageHeader} or a {@link Page} of {@link MessageHeader} which fulfills the criteria.
*/
@Service
@Transactional(readOnly = true)
public class MessageHeaderQueryService extends QueryService<MessageHeader> {

private final Logger log = LoggerFactory.getLogger(MessageHeaderQueryService.class);

private final MessageHeaderRepository messageHeaderRepository;

public MessageHeaderQueryService(MessageHeaderRepository messageHeaderRepository) {
this.messageHeaderRepository = messageHeaderRepository;
}

/**
* Return a {@link List} of {@link MessageHeader} which matches the criteria from the database.
* @param criteria The object which holds all the filters, which the entities should match.
* @return the matching entities.
*/
@Transactional(readOnly = true)
public List<MessageHeader> findByCriteria(MessageHeaderCriteria criteria) {
log.debug("find by criteria : {}", criteria);
final Specification<MessageHeader> specification = createSpecification(criteria);
return messageHeaderRepository.findAll(specification);
}

/**
* Return a {@link Page} of {@link MessageHeader} which matches the criteria from the database.
* @param criteria The object which holds all the filters, which the entities should match.
* @param page The page, which should be returned.
* @return the matching entities.
*/
@Transactional(readOnly = true)
public Page<MessageHeader> findByCriteria(MessageHeaderCriteria criteria, Pageable page) {
log.debug("find by criteria : {}, page: {}", criteria, page);
final Specification<MessageHeader> specification = createSpecification(criteria);
return messageHeaderRepository.findAll(specification, page);
}

/**
* Return the number of matching entities in the database.
* @param criteria The object which holds all the filters, which the entities should match.
* @return the number of matching entities.
*/
@Transactional(readOnly = true)
public long countByCriteria(MessageHeaderCriteria criteria) {
log.debug("count by criteria : {}", criteria);
final Specification<MessageHeader> specification = createSpecification(criteria);
return messageHeaderRepository.count(specification);
}

/**
* Function to convert {@link MessageHeaderCriteria} to a {@link Specification}
* @param criteria The object which holds all the filters, which the entities should match.
* @return the matching {@link Specification} of the entity.
*/
protected Specification<MessageHeader> createSpecification(MessageHeaderCriteria criteria) {
Specification<MessageHeader> specification = Specification.where(null);
if (criteria != null) {
// This has to be called first, because the distinct method returns null
if (criteria.getDistinct() != null) {
specification = specification.and(distinct(criteria.getDistinct()));
}
if (criteria.getHeaderId() != null) {
specification = specification.and(buildRangeSpecification(criteria.getHeaderId(), MessageHeader_.headerId));
}
if (criteria.getName() != null) {
specification = specification.and(buildStringSpecification(criteria.getName(), MessageHeader_.name));
}
if (criteria.getValue() != null) {
specification = specification.and(buildStringSpecification(criteria.getValue(), MessageHeader_.value));
}
if (criteria.getMessageId() != null) {
specification =
specification.and(
buildSpecification(
criteria.getMessageId(),
root -> root.join(MessageHeader_.message, JoinType.LEFT).get(Message_.messageId)
)
);
}
}
return specification;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.citrusframework.simulator.service;

import org.citrusframework.simulator.model.MessageHeader;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.Optional;

/**
* Service Interface for managing {@link MessageHeader}.
*/
public interface MessageHeaderService {
/**
* Save a messageHeader.
*
* @param messageHeader the entity to save.
* @return the persisted entity.
*/
MessageHeader save(MessageHeader messageHeader);

/**
* Updates a messageHeader.
*
* @param messageHeader the entity to update.
* @return the persisted entity.
*/
MessageHeader update(MessageHeader messageHeader);

/**
* Get all the messageHeaders.
*
* @param pageable the pagination information.
* @return the list of entities.
*/
Page<MessageHeader> findAll(Pageable pageable);

/**
* Get all the messageHeaders with eager load of many-to-many relationships.
*
* @param pageable the pagination information.
* @return the list of entities.
*/
Page<MessageHeader> findAllWithEagerRelationships(Pageable pageable);

/**
* Get the "id" messageHeader.
*
* @param id the id of the entity.
* @return the entity.
*/
Optional<MessageHeader> findOne(Long id);

/**
* Delete the "id" messageHeader.
*
* @param id the id of the entity.
*/
void delete(Long id);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.citrusframework.simulator.service;

import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Root;
import org.citrusframework.simulator.model.Message;
import org.citrusframework.simulator.model.Message_;
import org.citrusframework.simulator.repository.MessageRepository;
Expand All @@ -15,7 +13,6 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.function.Function;

/**
* Service for executing complex queries for {@link Message} entities in the database.
Expand Down
Loading

0 comments on commit 10dcab3

Please sign in to comment.