Skip to content

Commit aa8b207

Browse files
committed
Merge branch 'release/2020-05-p1'
2 parents fadda37 + 63ab490 commit aa8b207

3 files changed

Lines changed: 49 additions & 31 deletions

File tree

app/dao/impl/jpa/JpaCommitDao.java

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,26 @@
2323
import javax.persistence.criteria.Root;
2424
import java.lang.reflect.Method;
2525
import java.util.*;
26-
import java.util.function.Consumer;
2726
import java.util.function.Function;
2827
import java.util.function.Supplier;
28+
import java.util.function.UnaryOperator;
2929
import java.util.stream.Collectors;
3030
import java.util.stream.Stream;
3131

3232
@Singleton
3333
public class JpaCommitDao extends JpaDao<Commit> implements CommitDao {
3434
// TODO Explore alternative to serializing lazy entity attributes that doesn't involve resolving all proxies one level.
35-
static Consumer<Commit> PROXY_RESOLVER = commit -> commit.getChanges().stream()
36-
.filter(Objects::nonNull).map(ElementVersion::getData)
37-
.filter(mof -> mof instanceof Element).map(mof -> (Element) mof)
38-
.forEach(JpaElementDao.PROXY_RESOLVER);
35+
static UnaryOperator<Commit> PROXY_RESOLVER = commit -> {
36+
commit.getChanges().stream()
37+
.filter(Objects::nonNull)
38+
.map(ElementVersion::getData)
39+
.filter(mof -> mof instanceof Element)
40+
.map(mof -> (Element) mof)
41+
.map(JpaElementDao.PROXY_RESOLVER)
42+
.forEach(e -> {
43+
});
44+
return commit;
45+
};
3946

4047
@Inject
4148
private JPAManager jpa;
@@ -172,19 +179,27 @@ public void setIdentifier(UUID identifier) {
172179
});*/
173180

174181
return jpa.transact(em -> {
175-
commit.getChanges().stream().map(ElementVersion::getData).filter(mof -> mof instanceof MofObjectImpl).map(mof -> (MofObjectImpl) mof).map(mof -> {
176-
try {
177-
MofObjectImpl firstPassMof = mof.getClass().getConstructor().newInstance();
178-
firstPassMof.setKey(mof.getKey());
179-
return firstPassMof;
180-
} catch (ReflectiveOperationException e) {
181-
throw new RuntimeException(e);
182-
}
183-
}).forEach(em::merge);
184-
commit.setChanges(commit.getChanges().stream().map(em::merge).collect(Collectors.toSet()));
182+
commit.getChanges().stream()
183+
.map(ElementVersion::getData)
184+
.filter(mof -> mof instanceof MofObjectImpl)
185+
.map(mof -> (MofObjectImpl) mof)
186+
.map(mof -> {
187+
try {
188+
MofObjectImpl firstPassMof = mof.getClass().getConstructor().newInstance();
189+
firstPassMof.setKey(mof.getKey());
190+
return firstPassMof;
191+
} catch (ReflectiveOperationException e) {
192+
throw new RuntimeException(e);
193+
}
194+
})
195+
.forEach(em::merge);
196+
commit.setChanges(
197+
commit.getChanges().stream()
198+
.map(em::merge)
199+
.collect(Collectors.toSet())
200+
);
185201
Optional<Commit> persistedCommit = super.persist(commit, em);
186-
persistedCommit.ifPresent(PROXY_RESOLVER);
187-
return persistedCommit;
202+
return persistedCommit.map(PROXY_RESOLVER);
188203
});
189204
}
190205

@@ -253,8 +268,7 @@ public Optional<Commit> findByProjectAndId(Project project, UUID id) {
253268
} catch (NoResultException e) {
254269
return Optional.empty();
255270
}
256-
commit.ifPresent(PROXY_RESOLVER);
257-
return commit;
271+
return commit.map(PROXY_RESOLVER);
258272
});
259273
}
260274

@@ -273,8 +287,7 @@ public Optional<Commit> findHeadByProject(Project project) {
273287
} catch (NoResultException e) {
274288
return Optional.empty();
275289
}
276-
commit.ifPresent(PROXY_RESOLVER);
277-
return commit;
290+
return commit.map(PROXY_RESOLVER);
278291
});
279292
}
280293

app/dao/impl/jpa/JpaElementDao.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,21 @@
2626
import javax.persistence.criteria.*;
2727
import java.util.*;
2828
import java.util.concurrent.ConcurrentHashMap;
29-
import java.util.function.Consumer;
3029
import java.util.function.Function;
30+
import java.util.function.UnaryOperator;
3131
import java.util.stream.Collectors;
3232
import java.util.stream.Stream;
3333

3434
@Singleton
3535
public class JpaElementDao extends JpaDao<Element> implements ElementDao {
3636
// TODO Explore alternative to serializing lazy entity attributes that doesn't involve resolving all proxies one level.
37-
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);
37+
static UnaryOperator<Element> PROXY_RESOLVER = element -> {
38+
element = Hibernate.unproxy(element, Element.class);
39+
JavaBeanHelper.getBeanPropertyValues(element).values().stream()
40+
.flatMap(o -> o instanceof Collection ? ((Collection<?>) o).stream() : Stream.of(o)).filter(o -> o instanceof Element)
41+
.map(o -> (Element) o).forEach(Hibernate::unproxy);
42+
return element;
43+
};
3844

3945
@Inject
4046
private MetamodelProvider metamodelProvider;
@@ -96,7 +102,7 @@ public Set<Element> findAllByCommit(Commit commit) {
96102
.map(ElementVersion::getData)
97103
.filter(mof -> mof instanceof Element)
98104
.map(mof -> (Element) mof)
99-
.peek(PROXY_RESOLVER)
105+
.map(PROXY_RESOLVER)
100106
.collect(Collectors.toSet());
101107
});
102108
}
@@ -119,11 +125,10 @@ public Optional<Element> findByCommitAndId(Commit commit, UUID id) {
119125
);
120126
try {
121127
return Optional.of(em.createQuery(query).getSingleResult())
122-
.map(ElementVersion::getData).filter(mof -> mof instanceof Element)
123-
.map(mof -> (Element) mof).map(element -> {
124-
PROXY_RESOLVER.accept(element);
125-
return element;
126-
});
128+
.map(ElementVersion::getData)
129+
.filter(mof -> mof instanceof Element)
130+
.map(mof -> (Element) mof)
131+
.map(PROXY_RESOLVER);
127132
} catch (NoResultException e) {
128133
return Optional.empty();
129134
}
@@ -140,7 +145,7 @@ public Set<Element> findRootsByCommit(Commit commit) {
140145
.filter(mof -> mof instanceof Element)
141146
.map(mof -> (Element) mof)
142147
.filter(element -> element.getOwner() == null)
143-
.peek(PROXY_RESOLVER)
148+
.map(PROXY_RESOLVER)
144149
.collect(Collectors.toSet());
145150
});
146151
}

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name := """SysML-v2-API-Services"""
22
organization := "org.omg"
33

4-
version := "2020-05"
4+
version := "2020-05-p1"
55

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

0 commit comments

Comments
 (0)