Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

#4 Demonstrate the usage of MapStruct for bean mappings #249

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions oasp4j-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<!-- MapStruct bean mapping -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.0.0.CR1</version>
</dependency>

<!-- *** optional TEST DEPENDENCIES *** -->
<!-- JUnit for Test-Case definition and execution -->
Expand Down
36 changes: 35 additions & 1 deletion oasp4j-samples/oasp4j-sample-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@
<artifactId>hibernate-validator</artifactId>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
</dependency>

<!-- CXF for REST and Webservices -->
<dependency>
<groupId>org.apache.cxf</groupId>
Expand Down Expand Up @@ -196,7 +201,36 @@
<exclude>config/env/*</exclude>
</excludes>
</configuration>
</plugin>
</plugin>
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>2.2.4</version>
<configuration>
<defaultOutputDirectory>
${project.build.directory}/generated-sources
</defaultOutputDirectory>
<processors>
<processor>org.mapstruct.ap.MappingProcessor</processor>
</processors>
</configuration>
<executions>
<execution>
<id>process</id>
<phase>generate-sources</phase>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.0.0.CR1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@ public AbstractEto() {
super();
}

@Override
public void setId(Long id) {

super.setId(id);
}

@Override
public Long getId() {

return super.getId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.oasp.gastronomy.restaurant.tablemanagement.logic.api.Tablemanagement;
import io.oasp.gastronomy.restaurant.tablemanagement.logic.api.to.TableEto;
import io.oasp.gastronomy.restaurant.tablemanagement.logic.api.to.TableSearchCriteriaTo;
import io.oasp.gastronomy.restaurant.tablemanagement.logic.mapper.TableMapper;
import io.oasp.module.jpa.common.api.to.PaginatedListTo;

import java.util.List;
Expand Down Expand Up @@ -45,6 +46,8 @@ public class TablemanagementImpl extends AbstractComponentFacade implements Tabl

private Staffmanagement staffmanagement;

private TableMapper tableMapper;

/**
* The constructor.
*/
Expand All @@ -58,7 +61,7 @@ public TablemanagementImpl() {
public TableEto findTable(Long id) {

LOG.debug("Get table with id '" + id + "' from database.");
return getBeanMapper().map(getTableDao().findOne(id), TableEto.class);
return getTableMapper().toTableEto(getTableDao().findOne(id));
}

@Override
Expand All @@ -67,7 +70,7 @@ public List<TableEto> findAllTables() {

LOG.debug("Get all restaurant tables from database.");
List<TableEntity> tables = getTableDao().findAll();
return getBeanMapper().mapList(tables, TableEto.class);
return getTableMapper().toTableEtos(tables);
}

@Override
Expand All @@ -77,7 +80,7 @@ public List<TableEto> findFreeTables() {
LOG.debug("Get all free restaurant tables from database.");

List<TableEntity> tables = getTableDao().getFreeTables();
return getBeanMapper().mapList(tables, TableEto.class);
return getTableMapper().toTableEtos(tables);
}

@Override
Expand All @@ -87,7 +90,7 @@ public PaginatedListTo<TableEto> findTableEtos(TableSearchCriteriaTo criteria) {
criteria.limitMaximumPageSize(MAXIMUM_HIT_LIMIT);
PaginatedListTo<TableEntity> tables = getTableDao().findTables(criteria);

return mapPaginatedEntityList(tables, TableEto.class);
return new PaginatedListTo<>(getTableMapper().toTableEtos(tables.getResult()), tables.getPagination());
}

@Override
Expand All @@ -111,7 +114,7 @@ public TableEto saveTable(@Valid TableEto table) {

Long tableId = table.getId();

TableEntity tableEntity = getBeanMapper().map(table, TableEntity.class);
TableEntity tableEntity = getTableMapper().toTableEntity(table);
// initialize
if (tableEntity.getState() == null) {
tableEntity.setState(TableState.FREE);
Expand All @@ -127,7 +130,7 @@ public TableEto saveTable(@Valid TableEto table) {

getTableDao().save(tableEntity);
LOG.debug("Table with id '{}' has been created.", tableEntity.getId());
return getBeanMapper().map(tableEntity, TableEto.class);
return getTableMapper().toTableEto(tableEntity);
}

@Override
Expand Down Expand Up @@ -178,5 +181,16 @@ public void setStaffmanagement(Staffmanagement staffmanagement) {

this.staffmanagement = staffmanagement;
}

public TableMapper getTableMapper() {

return tableMapper;
}

@Inject
public void setTableMapper(TableMapper tableMapper) {

this.tableMapper = tableMapper;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.oasp.gastronomy.restaurant.tablemanagement.logic.mapper;

import io.oasp.gastronomy.restaurant.tablemanagement.dataaccess.api.TableEntity;
import io.oasp.gastronomy.restaurant.tablemanagement.logic.api.to.TableEto;

import java.util.List;

import org.mapstruct.Mapper;

@Mapper(componentModel = "jsr330")
public interface TableMapper {

TableEto toTableEto(TableEntity table);

TableEntity toTableEntity(TableEto table);

List<TableEto> toTableEtos(List<TableEntity> tables);
}