Skip to content

Commit 99e1899

Browse files
committed
Adding documentation
1 parent 8ccc428 commit 99e1899

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+457
-113
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ If you also need support for mapping JPA managed entities and be able to update
2929
* `Mappings` object that will provides mapping for given source and target class - mapping is information how to update existing object (managed entity) with data from source object,
3030
* `IdentifierCollector` should collect managed entity ID from source object
3131

32-
The easiest way to setup all of this is to extend `AbstractCompositeContextProvider`, implement `IdentifierCollector` and implement a set of `MappingProvider` for each type of entity. To provide implementations of `MappingProvider` you should create update methods in your MapStruct mappers.
32+
The easiest way to setup all of this is to extend `AbstractJpaContextProvider`, implement `IdentifierCollector` and implement a set of `MappingProvider` for each type of entity. To provide implementations of `MappingProvider` you should create update methods in your MapStruct mappers. It utilize `CompositeContext` which can incorporate any number of contexts as a composite.
3333

3434
All of this can be managed by some DI container like Spring or Guice.
3535

src/main/java/pl/wavesoftware/lang/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @author <a href="[email protected]">Krzysztof Suszyński</a>
2+
* @author <a href="mailto:[email protected]">Krzysztof Suszyński</a>
33
* @since 2018-05-03
44
*/
55
@ParametersAreNonnullByDefault

src/main/java/pl/wavesoftware/utils/mapstruct/jpa/AbstractCompositeContextMapping.java

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,71 @@
33
import pl.wavesoftware.lang.TriConsumer;
44

55
/**
6-
* @author <a href="[email protected]">Krzysztof Suszyński</a>
6+
* An abstract class that can be extended to provide logic in how to apply data from
7+
* {@link I} input object to an target {@link O} output object. It uses {@link CompositeContext}
8+
* as a MapStruct context.
9+
*
10+
* <p>
11+
* It's designed to be used easily with
12+
* <a href="http://mapstruct.org/documentation/stable/reference/html/#updating-bean-instances">
13+
* MapStruct update methods</a>.
14+
* <pre>
15+
* &#064;Service
16+
* &#064;RequiredArgsConstructor
17+
* final class PetCompositeContextMapping extends AbstractCompositeContextMapping&lt;Pet,PetData&gt; {
18+
* private final PetMapper petMapper;
19+
* &#064;Override
20+
* public void accept(Pet pet, PetData data, CompositeContext context) {
21+
* petMapper.updateFromPet(pet, data, context);
22+
* }
23+
* }
24+
* </pre>
25+
*
26+
* There is also a convenience method {@link #mappingFor(Class, Class, TriConsumer)} which can be
27+
* used to create mapping easily with {@link AbstractJpaContextProvider}:
28+
*
29+
* <br>
30+
* <pre>
31+
* &#064;RequiredArgsConstructor
32+
* final class OwnerMappingProvider implements MappingProvider&lt;Owner, OwnerJPA, CompositeContext&gt; {
33+
* private final OwnerMapper ownerMapper;
34+
*
35+
* &#064;Override
36+
* public Mapping&lt;Owner, OwnerJPA, CompositeContext&gt; provide() {
37+
* return AbstractCompositeContextMapping.mappingFor(
38+
* Owner.class, OwnerJPA.class,
39+
* ownerMapper::updateFromOwner
40+
* );
41+
* }
42+
* }
43+
* </pre>
44+
*
45+
* @param <I> a type of input object to map from
46+
* @param <O> a type of output object to map to
47+
* @author <a href="mailto:[email protected]">Krzysztof Suszyński</a>
748
* @since 2018-05-02
849
*/
9-
public abstract class AbstractCompositeContextMapping<I, O> extends AbstractMapping<I, O, CompositeContext> {
50+
public abstract class AbstractCompositeContextMapping<I, O>
51+
extends AbstractMapping<I, O, CompositeContext> {
52+
1053
protected AbstractCompositeContextMapping(Class<I> sourceClass,
1154
Class<O> targetClass) {
1255
super(sourceClass, targetClass, CompositeContext.class);
1356
}
1457

15-
public static <I, O> AbstractCompositeContextMapping<I, O> mapperFor(
58+
/**
59+
* A convenience method which can be used to create mapping easily with
60+
* {@link AbstractJpaContextProvider}.
61+
*
62+
* @param inputClass a class of an input object
63+
* @param outputClass a class of an output object
64+
* @param consumer a consumer of 3 values: input object, output object
65+
* and {@link CompositeContext} object
66+
* @param <I> a type of input object to map from
67+
* @param <O> a type of output object to map to
68+
* @return a mapping for given values
69+
*/
70+
public static <I, O> Mapping<I, O, CompositeContext> mappingFor(
1671
Class<I> inputClass,
1772
Class<O> outputClass,
1873
TriConsumer<I, O, CompositeContext> consumer) {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package pl.wavesoftware.utils.mapstruct.jpa;
2+
3+
import javax.persistence.EntityManager;
4+
import java.util.function.Supplier;
5+
6+
/**
7+
* A base abstract class that can be used as a provider for configured and fully working
8+
* JPA aware mapping context with update capability. It's designed to be implemented and
9+
* configured in your DI container to used in your mapper facade to produce new context each
10+
* time you are doing a mapping.
11+
*
12+
* @author <a href="mailto:[email protected]">Krzysztof Suszyński</a>
13+
* @since 2018-05-03
14+
*/
15+
public abstract class AbstractJpaContextProvider
16+
implements MapStructContextProvider<CompositeContext> {
17+
18+
private final JpaMappingContextProviderImpl provider =
19+
new JpaMappingContextProviderImpl();
20+
21+
/**
22+
* A method to that returns a supplier of JPA's {@link EntityManager} bounded to
23+
* current transaction context.
24+
*
25+
* @return a supplier of current <code>EntityManager</code>
26+
*/
27+
protected abstract Supplier<EntityManager> getEntityManager();
28+
29+
/**
30+
* Returns a collection of mapping providers to be used in mapping. Each mapping provider
31+
* is for other mapping (for ex.: Pet to PetData etc.).
32+
*
33+
* @return a collection of mapping providers
34+
*/
35+
protected abstract Iterable<MappingProvider<?, ?, ?>> getMappingProviders();
36+
37+
/**
38+
* Returns a identifier collector that can fetch an ID to be used to fetch a managed entity
39+
* from {@link EntityManager}. It will try to fetch the ID from source mapping object, so
40+
* it must contain some kind of way to provide it.
41+
*
42+
* @return a identifier collector
43+
*/
44+
protected abstract IdentifierCollector getIdentifierCollector();
45+
46+
@Override
47+
public CompositeContext createNewContext() {
48+
return provider.provide(
49+
getEntityManager(), getMappingProviders(), getIdentifierCollector()
50+
);
51+
}
52+
}

src/main/java/pl/wavesoftware/utils/mapstruct/jpa/AbstractMapping.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
import lombok.RequiredArgsConstructor;
55

66
/**
7+
* An abstract mapping that holds all classes representations.
8+
*
9+
* @param <I> a type of input object for map from
10+
* @param <O> a type of output (target) object for map to
11+
* @param <C> a type of context to be used in the mapping
12+
*
713
* @author <a href="mailto:[email protected]">Krzysztof Suszynski</a>
814
* @since 25.04.18
915
*/

src/main/java/pl/wavesoftware/utils/mapstruct/jpa/CompositeContext.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import java.util.Optional;
1313

1414
/**
15-
* A composite mapping context that can utilize multiple mapping contexts.
15+
* A composite mapping context that can utilize multiple mapping contexts to provide
16+
* the joined processing. User can add any number of {@link MappingContext} to use in
17+
* this composite class.
1618
*
1719
* @author <a href="mailto:[email protected]">Krzysztof Suszynski</a>
1820
* @since 02.05.18
@@ -75,11 +77,10 @@ private static final class CompositeContextBuilderImpl implements CompositeConte
7577
private final List<MappingContext> mappingContexts = new ArrayList<>();
7678

7779
@Override
78-
public CompositeContextBuilderImpl addContext(MappingContext... mappingContexts) {
80+
public void addContext(MappingContext... mappingContexts) {
7981
this.mappingContexts.addAll(
8082
Arrays.asList(mappingContexts)
8183
);
82-
return this;
8384
}
8485

8586
@Override
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
package pl.wavesoftware.utils.mapstruct.jpa;
22

33
/**
4+
* This is a builder for composite context.
5+
*
6+
* @see CompositeContext
47
* @author <a href="mailto:[email protected]">Krzysztof Suszynski</a>
58
* @since 04.05.18
69
*/
710
public interface CompositeContextBuilder {
8-
CompositeContextBuilder addContext(MappingContext... mappingContexts);
11+
/**
12+
* Adds context to the builder.
13+
*
14+
* @param mappingContexts A mapping contexts to be added to builder and then created
15+
* the composite context from it.
16+
*/
17+
void addContext(MappingContext... mappingContexts);
18+
19+
/**
20+
* Builds a composite context from provided set of other mapping contexts.
21+
*
22+
* @return a builded instance of composite context
23+
*/
924
CompositeContext build();
1025
}

src/main/java/pl/wavesoftware/utils/mapstruct/jpa/CyclicGraphContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* cycles.
1919
*
2020
* @author Andreas Gudian
21-
* @author <a href="[email protected]">Krzysztof Suszyński</a>
21+
* @author <a href="mailto:[email protected]">Krzysztof Suszyński</a>
2222
* @since 2018-04-12
2323
*/
2424
public final class CyclicGraphContext implements MapStructContext {

src/main/java/pl/wavesoftware/utils/mapstruct/jpa/IdentifierCollector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* }
2222
* </pre>
2323
*
24-
* @author <a href="[email protected]">Krzysztof Suszyński</a>
24+
* @author <a href="mailto:[email protected]">Krzysztof Suszyński</a>
2525
* @since 2018-05-02
2626
*/
2727
public interface IdentifierCollector {

src/main/java/pl/wavesoftware/utils/mapstruct/jpa/JpaMappingContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package pl.wavesoftware.utils.mapstruct.jpa;
22

33
/**
4+
* This is provided {@link JpaMappingContext} and it have a factory
5+
* {@link JpaMappingContextFactory}. This interface represents a JPA aware mapping context.
6+
*
47
* @author <a href="mailto:[email protected]">Krzysztof Suszynski</a>
58
* @since 02.05.18
69
*/

0 commit comments

Comments
 (0)