2929import org .hibernate .proxy .HibernateProxy ;
3030import org .omg .sysml .data .ProjectUsage ;
3131import org .omg .sysml .internal .CommitDataVersionIndex ;
32+ import org .omg .sysml .internal .WorkingDataVersion ;
3233import org .omg .sysml .internal .impl .CommitDataVersionIndexImpl ;
3334import org .omg .sysml .internal .impl .CommitDataVersionIndexImpl_ ;
35+ import org .omg .sysml .internal .impl .WorkingDataVersionImpl ;
3436import org .omg .sysml .lifecycle .Commit ;
3537import org .omg .sysml .lifecycle .Data ;
3638import org .omg .sysml .lifecycle .DataIdentity ;
@@ -95,6 +97,7 @@ public List<Data> findByCommitAndQuery(Commit commit, Query query) {
9597 Query q = query .getId () == null || em .contains (query ) ? query : em .find (QueryImpl .class , query .getId ());
9698 return getCommitIndex (c , em ).getWorkingDataVersion ().stream ()
9799 .filter (scope (q ))
100+ .map (WorkingDataVersion ::getDataVersion )
98101 .map (DataVersion ::getPayload )
99102 .filter (constrain (q .getWhere ()))
100103 .map (data -> JpaDataDao .resolve (data , Data .class ))
@@ -115,17 +118,21 @@ protected CommitDataVersionIndex getCommitIndex(Commit commit, EntityManager em)
115118 CommitDataVersionIndex index = new CommitDataVersionIndexImpl ();
116119 index .setCommit (commit );
117120 index .setWorkingDataVersion (streamWorkingDataVersions (commit , em ).collect (Collectors .toSet ()));
121+
118122 EntityTransaction transaction = em .getTransaction ();
119123 transaction .begin ();
124+ index .getWorkingDataVersion ().forEach (em ::persist );
120125 em .persist (index );
121126 transaction .commit ();
127+
122128 return index ;
123129 }
124130
125- protected Stream <DataVersion > streamWorkingDataVersions (Commit commit , EntityManager em ) {
131+ protected Stream <WorkingDataVersion > streamWorkingDataVersions (Commit commit , EntityManager em ) {
126132 Set <UUID > visitedIds = ConcurrentHashMap .newKeySet ();
127133 Set <ProjectUsage > projectUsages = ConcurrentHashMap .newKeySet ();
128- Map <Commit , Set <DataVersion >> results = queryCommitTree (commit ,
134+
135+ Map <Commit , Set <WorkingDataVersion >> _ownedDataVersions = queryCommitTree (commit ,
129136 c -> c .getChange ().stream ()
130137 .filter (record -> record .getIdentity () != null && record .getIdentity ().getId () != null )
131138 .filter (record -> !visitedIds .contains (record .getIdentity ().getId ()))
@@ -136,20 +143,36 @@ protected Stream<DataVersion> streamWorkingDataVersions(Commit commit, EntityMan
136143 projectUsages .add (((ProjectUsage ) record .getPayload ()));
137144 }
138145 })
146+ .map (version -> {
147+ WorkingDataVersionImpl workingDataVersion = new WorkingDataVersionImpl ();
148+ workingDataVersion .setDataVersion (version );
149+ return workingDataVersion ;
150+ })
139151 .collect (Collectors .toSet ())
140152 );
141- Stream <DataVersion > ownedDataVersions = results .values ().stream ()
153+ Stream <WorkingDataVersion > ownedDataVersions = _ownedDataVersions .values ().stream ()
142154 .flatMap (Set ::stream );
143- Stream <DataVersion > usedDataVersions = projectUsages .stream ()
144- .map (ProjectUsage ::getUsedProjectCommit )
145- .filter (Objects ::nonNull )
146- .map (fnCommit -> getCommitIndex (fnCommit , em ))
147- .map (CommitDataVersionIndex ::getWorkingDataVersion )
148- .flatMap (Set ::stream )
149- .filter (record -> !visitedIds .contains (record .getIdentity ().getId ()))
150- .peek (record -> visitedIds .add (record .getIdentity ().getId ()))
151- .collect (Collectors .toSet ())
152- .stream ();
155+
156+ Set <WorkingDataVersion > _usedDataVersions = ConcurrentHashMap .newKeySet ();
157+ for (ProjectUsage projectUsage : projectUsages ) {
158+ Commit usedCommit = projectUsage .getUsedProjectCommit ();
159+ if (usedCommit == null ) {
160+ continue ;
161+ }
162+ getCommitIndex (usedCommit , em ).getWorkingDataVersion ().stream ()
163+ .map (WorkingDataVersion ::getDataVersion )
164+ .filter (version -> !visitedIds .contains (version .getIdentity ().getId ()))
165+ .peek (version -> visitedIds .add (version .getIdentity ().getId ()))
166+ .map (version -> {
167+ WorkingDataVersionImpl workingDataVersion = new WorkingDataVersionImpl ();
168+ workingDataVersion .setSource (projectUsage );
169+ workingDataVersion .setDataVersion (version );
170+ return workingDataVersion ;
171+ })
172+ .forEach (_usedDataVersions ::add );
173+ }
174+ Stream <WorkingDataVersion > usedDataVersions = _usedDataVersions .stream ();
175+
153176 return Streams .concat (ownedDataVersions , usedDataVersions );
154177 }
155178
@@ -241,12 +264,12 @@ else if (constraint instanceof CompositeConstraint) {
241264 }
242265 }
243266
244- protected Predicate <DataVersion > scope (Query query ) {
267+ protected Predicate <WorkingDataVersion > scope (Query query ) {
245268 if (query .getScope () == null || query .getScope ().isEmpty ()) {
246269 return ev -> true ;
247270 }
248- return ev -> ev .getIdentity () != null && query .getScope ().stream ()
271+ return working -> working . getDataVersion () .getIdentity () != null && query .getScope ().stream ()
249272 .map (DataIdentity ::getId )
250- .anyMatch (id -> Objects .equals (id , ev .getIdentity ().getId ()));
273+ .anyMatch (id -> Objects .equals (id , working . getDataVersion () .getIdentity ().getId ()));
251274 }
252275}
0 commit comments