Skip to content

Commit

Permalink
Merge branch 'release/2020-05-p1'
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-gomes committed Jun 8, 2020
2 parents fadda37 + 63ab490 commit aa8b207
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 31 deletions.
55 changes: 34 additions & 21 deletions app/dao/impl/jpa/JpaCommitDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,26 @@
import javax.persistence.criteria.Root;
import java.lang.reflect.Method;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Singleton
public class JpaCommitDao extends JpaDao<Commit> implements CommitDao {
// TODO Explore alternative to serializing lazy entity attributes that doesn't involve resolving all proxies one level.
static Consumer<Commit> PROXY_RESOLVER = commit -> commit.getChanges().stream()
.filter(Objects::nonNull).map(ElementVersion::getData)
.filter(mof -> mof instanceof Element).map(mof -> (Element) mof)
.forEach(JpaElementDao.PROXY_RESOLVER);
static UnaryOperator<Commit> PROXY_RESOLVER = commit -> {
commit.getChanges().stream()
.filter(Objects::nonNull)
.map(ElementVersion::getData)
.filter(mof -> mof instanceof Element)
.map(mof -> (Element) mof)
.map(JpaElementDao.PROXY_RESOLVER)
.forEach(e -> {
});
return commit;
};

@Inject
private JPAManager jpa;
Expand Down Expand Up @@ -172,19 +179,27 @@ public void setIdentifier(UUID identifier) {
});*/

return jpa.transact(em -> {
commit.getChanges().stream().map(ElementVersion::getData).filter(mof -> mof instanceof MofObjectImpl).map(mof -> (MofObjectImpl) mof).map(mof -> {
try {
MofObjectImpl firstPassMof = mof.getClass().getConstructor().newInstance();
firstPassMof.setKey(mof.getKey());
return firstPassMof;
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}).forEach(em::merge);
commit.setChanges(commit.getChanges().stream().map(em::merge).collect(Collectors.toSet()));
commit.getChanges().stream()
.map(ElementVersion::getData)
.filter(mof -> mof instanceof MofObjectImpl)
.map(mof -> (MofObjectImpl) mof)
.map(mof -> {
try {
MofObjectImpl firstPassMof = mof.getClass().getConstructor().newInstance();
firstPassMof.setKey(mof.getKey());
return firstPassMof;
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
})
.forEach(em::merge);
commit.setChanges(
commit.getChanges().stream()
.map(em::merge)
.collect(Collectors.toSet())
);
Optional<Commit> persistedCommit = super.persist(commit, em);
persistedCommit.ifPresent(PROXY_RESOLVER);
return persistedCommit;
return persistedCommit.map(PROXY_RESOLVER);
});
}

Expand Down Expand Up @@ -253,8 +268,7 @@ public Optional<Commit> findByProjectAndId(Project project, UUID id) {
} catch (NoResultException e) {
return Optional.empty();
}
commit.ifPresent(PROXY_RESOLVER);
return commit;
return commit.map(PROXY_RESOLVER);
});
}

Expand All @@ -273,8 +287,7 @@ public Optional<Commit> findHeadByProject(Project project) {
} catch (NoResultException e) {
return Optional.empty();
}
commit.ifPresent(PROXY_RESOLVER);
return commit;
return commit.map(PROXY_RESOLVER);
});
}

Expand Down
23 changes: 14 additions & 9 deletions app/dao/impl/jpa/JpaElementDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,21 @@
import javax.persistence.criteria.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Singleton
public class JpaElementDao extends JpaDao<Element> implements ElementDao {
// TODO Explore alternative to serializing lazy entity attributes that doesn't involve resolving all proxies one level.
static Consumer<Element> PROXY_RESOLVER = element -> JavaBeanHelper.getBeanPropertyValues(element).values().stream().flatMap(o -> o instanceof Collection ? ((Collection<?>) o).stream() : Stream.of(o)).filter(o -> o instanceof Element).map(o -> (Element) o).forEach(Hibernate::unproxy);
static UnaryOperator<Element> PROXY_RESOLVER = element -> {
element = Hibernate.unproxy(element, Element.class);
JavaBeanHelper.getBeanPropertyValues(element).values().stream()
.flatMap(o -> o instanceof Collection ? ((Collection<?>) o).stream() : Stream.of(o)).filter(o -> o instanceof Element)
.map(o -> (Element) o).forEach(Hibernate::unproxy);
return element;
};

@Inject
private MetamodelProvider metamodelProvider;
Expand Down Expand Up @@ -96,7 +102,7 @@ public Set<Element> findAllByCommit(Commit commit) {
.map(ElementVersion::getData)
.filter(mof -> mof instanceof Element)
.map(mof -> (Element) mof)
.peek(PROXY_RESOLVER)
.map(PROXY_RESOLVER)
.collect(Collectors.toSet());
});
}
Expand All @@ -119,11 +125,10 @@ public Optional<Element> findByCommitAndId(Commit commit, UUID id) {
);
try {
return Optional.of(em.createQuery(query).getSingleResult())
.map(ElementVersion::getData).filter(mof -> mof instanceof Element)
.map(mof -> (Element) mof).map(element -> {
PROXY_RESOLVER.accept(element);
return element;
});
.map(ElementVersion::getData)
.filter(mof -> mof instanceof Element)
.map(mof -> (Element) mof)
.map(PROXY_RESOLVER);
} catch (NoResultException e) {
return Optional.empty();
}
Expand All @@ -140,7 +145,7 @@ public Set<Element> findRootsByCommit(Commit commit) {
.filter(mof -> mof instanceof Element)
.map(mof -> (Element) mof)
.filter(element -> element.getOwner() == null)
.peek(PROXY_RESOLVER)
.map(PROXY_RESOLVER)
.collect(Collectors.toSet());
});
}
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name := """SysML-v2-API-Services"""
organization := "org.omg"

version := "2020-05"
version := "2020-05-p1"

javacOptions ++= Seq("-source", "11", "-target", "11", "-Xlint")

Expand Down

0 comments on commit aa8b207

Please sign in to comment.