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

Commit

Permalink
#4 Demonstrate the usage of MapStruct for bean mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
agudian committed Jun 10, 2015
1 parent d55b0a4 commit 6b05eaa
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 7 deletions.
6 changes: 6 additions & 0 deletions oasp4j-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,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 @@ -2,6 +2,7 @@

import io.oasp.gastronomy.restaurant.general.logic.base.AbstractUc;
import io.oasp.gastronomy.restaurant.tablemanagement.dataaccess.api.dao.TableDao;
import io.oasp.gastronomy.restaurant.tablemanagement.logic.mapper.TableMapper;

import javax.inject.Inject;

Expand All @@ -13,6 +14,8 @@ public abstract class AbstractTableUc extends AbstractUc {

/** @see #getTableDao() */
private TableDao tableDao;

private TableMapper tableMapper;

/**
* @return the {@link TableDao} instance.
Expand All @@ -31,4 +34,15 @@ public void setTableDao(TableDao tableDao) {
this.tableDao = tableDao;
}

public TableMapper getTableMapper() {

return tableMapper;
}

@Inject
public void setTableMapper(TableMapper tableMapper) {

this.tableMapper = tableMapper;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class UcFindTableImpl extends AbstractTableUc implements UcFindTable {
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));
}

/**
Expand All @@ -49,7 +49,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);
}

/**
Expand All @@ -62,7 +62,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);
}

/**
Expand All @@ -75,7 +75,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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,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 @@ -85,7 +85,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);
}

/**
Expand Down
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);
}

0 comments on commit 6b05eaa

Please sign in to comment.