Skip to content

Commit

Permalink
further link improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Jun 26, 2024
1 parent cb196ba commit 97c5123
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/io/github/mmm/entity/id/GenericId.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ default GenericId<E, I, R> create(String valueString) {
}

@Override
default Id<E> withoutRevision() {
default GenericId<E, I, ?> withoutRevision() {

return withRevision(null);
}
Expand Down
26 changes: 24 additions & 2 deletions core/src/main/java/io/github/mmm/entity/id/PkMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ public PkMapperId(PkMapperRevision next) {
super(null, "Id", next);
}

/**
* The constructor.
*
* @param idTemplate the {@link GenericId} as template.
* @param next the {@link PkMapperRevision}.
*/
public PkMapperId(GenericId idTemplate, PkMapperRevision next) {

super(idTemplate, "Id", next);
}

@Override
public Class<? extends Object> getTargetType() {

Expand All @@ -125,6 +136,14 @@ public String mapName(String name, String separator) {
return name;
}

@Override
public Id toSource(Object target) {

assert (next() == null); // no revision field
assert (!this.idTemplate.hasRevisionField());
return this.idTemplate.withIdAndRevision(target, null);
}

@Override
public Object toTarget(Id id) {

Expand All @@ -147,8 +166,11 @@ public static PkMapper of(Id<?> id) {

Objects.requireNonNull(id);
GenericId genericId = (GenericId) id;
PkMapperRevision revMapper = new PkMapperRevision(genericId.withIdAndRevision(null, null));
return new PkMapperId(revMapper);
PkMapperRevision revMapper = null;
if (genericId.hasRevisionField()) {
revMapper = new PkMapperRevision(genericId.withIdAndRevision(null, null));
}
return new PkMapperId(genericId, revMapper);
}

}
7 changes: 6 additions & 1 deletion core/src/main/java/io/github/mmm/entity/link/EntityLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package io.github.mmm.entity.link;

import io.github.mmm.entity.Entity;
import io.github.mmm.entity.id.GenericId;
import io.github.mmm.entity.id.Id;

/**
Expand Down Expand Up @@ -36,7 +37,11 @@ public boolean isResolved() {
@Override
public Id<E> getId() {

return Id.from(this.entity);
Id<E> id = Id.from(this.entity);
if (id instanceof GenericId g) {
id = g.withoutRevision();
}
return id;
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/io/github/mmm/entity/link/IdLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ public class IdLink<E> extends AbstractLink<E> {
* @param resolver the {@link Function} to {@link #isResolved() resolve} the {@link #getTarget() link target} (the
* entity).
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({ "rawtypes" })
protected IdLink(Id<E> id, Function<Id<E>, E> resolver) {

super();
Objects.requireNonNull(id, "id");
if (id.get() == null) {
throw new IllegalArgumentException("Cannot create link for empty ID - primary key must be present!");
}
this.id = (GenericId) id;
this.id = ((GenericId) id).withoutRevision();
this.resolver = resolver;
}

Expand Down

0 comments on commit 97c5123

Please sign in to comment.