1919import org .eclipse .emf .cdo .common .branch .CDOBranchVersion ;
2020import org .eclipse .emf .cdo .common .commit .CDOCommitInfo ;
2121import org .eclipse .emf .cdo .common .id .CDOID ;
22+ import org .eclipse .emf .cdo .common .id .CDOIDUtil ;
2223import org .eclipse .emf .cdo .common .model .CDOPackageRegistry ;
2324import org .eclipse .emf .cdo .common .revision .CDORevision ;
2425import org .eclipse .emf .cdo .common .revision .CDORevisionUtil ;
3031import org .eclipse .emf .cdo .eresource .CDOResource ;
3132import org .eclipse .emf .cdo .eresource .CDOResourceFolder ;
3233import org .eclipse .emf .cdo .eresource .CDOResourceNode ;
34+ import org .eclipse .emf .cdo .internal .common .id .CDOIDObjectLongImpl ;
3335import org .eclipse .emf .cdo .session .CDOSession ;
3436import org .eclipse .emf .cdo .spi .common .revision .InternalCDORevision ;
3537import org .eclipse .emf .cdo .transaction .CDOTransaction ;
@@ -229,6 +231,7 @@ public <T> T insert(T objectToSave) {
229231 }
230232
231233 @ Override
234+ @ SuppressWarnings ("unchecked" )
232235 public <T > T insert (T objectToSave , String pathName ) {
233236 Assert .notNull (objectToSave , "ObjectToSave must not be null!" );
234237 Assert .notNull (pathName , "pathName must not be null!" );
@@ -255,7 +258,7 @@ public <T> T save(T entity, final String repoResourcePath) {
255258 final CdoPersistentEntity <?> persistentEntity = mappingContext .getRequiredPersistentEntity (rawType );
256259
257260 T savedResult = execute (session -> {
258- EObject internalValue = null ;
261+ EObject internalValue ;
259262 final CDOID cdoid ;
260263 if (persistentEntity .isNativeCdoOrLegacyMode ()) {
261264 internalValue = (EObject ) entity ;
@@ -273,10 +276,10 @@ public <T> T save(T entity, final String repoResourcePath) {
273276 }));
274277 }
275278
276- //// TODO this causes possibly a stackoverflow exception later when removing all objects
277- // resource.getResourceSet().getPackageRegistry().put(null, Optional.ofNullable(internalValue).<IllegalStateException>orElseThrow(() -> {
278- // throw new IllegalStateException("The persistent entity model of the class was null. Maybe it was not properly annotated? Null values cannot be saved.");
279- // }));
279+ // (!) TODO (!) Cannot use this line: causes a stackoverflow exception later when removing all objects
280+ // resource.getResourceSet().getPackageRegistry().put(null, Optional.ofNullable(internalValue).<IllegalStateException>orElseThrow(() -> {
281+ // throw new IllegalStateException("The persistent entity model of the class was null. Maybe it was not properly annotated? Null values cannot be saved.");
282+ // }));
280283 CDOTransaction transaction = null ;
281284 try {
282285 //see: https://www.eclipse.org/forums/index.php/t/203394/
@@ -344,21 +347,14 @@ public <T> T save(T entity, final String repoResourcePath) {
344347 // This happens with dynamically created EMF models
345348 // If a model is just exchanged instead of modifying the model's attributes with EMF's reflective API
346349 transaction = openTransaction (session );
350+ // transaction.lockObjects(Collections.singleton(CDOUtil.getCDOObject(object)),
351+ // IRWLockManager.LockType.WRITE, session.getOptions().getWriteLockoutTimeout());
347352 CDOResource resource = transaction .getResource (repoResourcePath , true );
348353 resource .getContents ().add (objectToUpdate );
349354 transaction .commit ();
355+ // not necessary, automatic unlock after commit
356+ // CDOUtil.getCDOObject(oldDBObject).cdoWriteLock().unlock();
350357 }
351- // not necessary, automatic unlock after commit
352- // CDOUtil.getCDOObject(oldDBObject).cdoWriteLock().unlock();
353-
354- // transaction = openTransaction(session);
355- // CDOResource resource = transaction.getResource(repoResourcePath, true);
356- // CDOObject object = transaction.getObject(cdoid);
357- // transaction.lockObjects(Collections.singleton(CDOUtil.getCDOObject(object)),
358- // IRWLockManager.LockType.WRITE, session.getOptions().getWriteLockoutTimeout());
359- // resource.getContents().remove(object);
360- // resource.getContents().add(objectToUpdate);
361-
362358 } catch (NullPointerException e ) {
363359 throw new InvalidDataAccessResourceUsageException (e .toString ());
364360 } catch (CommitException e ) {
@@ -372,7 +368,7 @@ public <T> T save(T entity, final String repoResourcePath) {
372368 finally {
373369 closeTransaction (transaction );
374370 }
375- return ( T ) entity ;
371+ return entity ;
376372 });
377373
378374 AfterSaveEvent <?> eventAfter = new AfterSaveEvent <>(savedResult , null , repoResourcePath );
@@ -415,6 +411,7 @@ private CDOTransaction openTransaction(CdoClientSession session) {
415411 return trans ;
416412 }
417413
414+ @ Nullable
418415 public <T , ID > T find (final ID entityID , Class <T > javaClassType , final String resourcePath ) {
419416 return this .find (entityID , javaClassType , resourcePath , true );
420417 }
@@ -465,7 +462,7 @@ public <T, ID> T find(final ID entityID, Class<T> javaClassType, final String re
465462 closeView (cdoView );
466463 }
467464 });
468- return ( T ) execute ;
465+ return execute ;
469466 }
470467
471468 @ Override
@@ -502,7 +499,7 @@ public <T> List<T> findAll(Class<T> javaClassType, final String repoResourcePath
502499 if (explicitCDOObject || isLegacy ) {
503500 if (ClassUtils .isAssignable (ClassUtils .getUserClass (eachObject ), javaClassType ) ||
504501 javaClassType .isAssignableFrom (ClassUtils .getUserClass (eachObject ))) { //TODO make this part of a Query
505- collection .add (( T ) javaClassType .cast (eachObject ));
502+ collection .add (javaClassType .cast (eachObject ));
506503// return javaClassType.cast(eachObject);
507504 }
508505 } else {
@@ -516,7 +513,7 @@ public <T> List<T> findAll(Class<T> javaClassType, final String repoResourcePath
516513 });
517514 return collection ;
518515 } catch (InvalidURIException | IllegalArgumentException e ) {
519- e .printStackTrace ();
516+ // e.printStackTrace();
520517 return Collections .emptyList ();
521518 }
522519 });
@@ -556,7 +553,8 @@ public <T, ID> CDORevisionHolder<T> getRevisionById(@NonNull ID id, String resou
556553 ensureIDisCDOID (id );
557554
558555 CDOID cdoid = (CDOID ) id ;
559- CDORevisionHolder <T > revisionContainerResult = execute (session -> {
556+ // CDOObject object = session.getDelegate().openView(head, CDOBranchPoint.UNSPECIFIED_DATE, latestObject.cdoResource().getResourceSet()).getObject(cdoid);
557+ return execute (session -> {
560558 CDORevisionHolder <T > revisionContainer = CDORevisionHolder .create ();
561559 CDOTransaction transaction = openTransaction (session );
562560 CDOObject latestObject ;
@@ -609,7 +607,6 @@ public <T, ID> CDORevisionHolder<T> getRevisionById(@NonNull ID id, String resou
609607 }
610608 return revisionContainer ;
611609 });
612- return revisionContainerResult ;
613610 }
614611
615612 @ Override
@@ -622,7 +619,7 @@ public <T> long countAll(final Class<T> javaType, final EPackage context, final
622619 try {
623620 cdoView = session .getCdoSession ().openView ();
624621
625- CDOQuery oclQuery = null ;
622+ CDOQuery oclQuery ;
626623 if (!persistentEntity .isNativeCdoOrLegacyMode ()) {
627624 Class <?> classFor = persistentEntity .getRequiredEObjectModelProperty ().getClassFor ();
628625 Assert .notNull (classFor , "classFor property must not be null. Maybe it isn't defined for EObjectModel property." );
@@ -697,7 +694,7 @@ public <T> Collection<T> insertAll(Collection<? extends T> objectsToSave) {
697694 public <T > Collection <T > insertAll (Collection <? extends T > batchToSave , String resourcePath ) {
698695 Assert .notNull (batchToSave , "BatchToSave must not be null!" );
699696 Assert .notNull (resourcePath , "ResourcePath must not be null!" );
700- return ( Collection < T >) doInsertBatch (resourcePath , batchToSave , this .cdoConverter );
697+ return doInsertBatch (resourcePath , batchToSave , this .cdoConverter );
701698 }
702699
703700 /**
@@ -711,7 +708,6 @@ public <T> Collection<T> insertAll(Collection<? extends T> batchToSave, String r
711708 * @param <T>
712709 * @return
713710 */
714- @ SuppressWarnings ("unchecked" )
715711 protected <T > Collection <T > doInsertAll (Collection <? extends T > listToSave , CdoConverter cdoConverter ) {
716712 Map <String , List <T >> elementsByCollection = new HashMap <>();
717713 List <T > savedObjects = new ArrayList <>(listToSave .size ());
@@ -760,11 +756,10 @@ protected <T> Collection<T> doInsertBatch(String repoResourcePath, Collection<?
760756 int i = 0 ;
761757 for (T obj : batchToSave ) {
762758 if (i < savedEObjects .size ()) {
763- // T objectToSave = arr.get(i);
764- EObject bla = savedEObjects .get (i );
759+ EObject eObjectInstance = savedEObjects .get (i );
765760 Class <?> rawType = ClassUtils .getUserClass (obj );
766761 CdoPersistentEntity <?> persistentEntity = mappingContext .getRequiredPersistentEntity (rawType );
767- CDOID identifier = null ;
762+ CDOID identifier ;
768763 if (!persistentEntity .isNativeCdoOrLegacyMode ()) {
769764 GeneratingIdAccessor generatingIdAccessor ;
770765 generatingIdAccessor = new GeneratingIdAccessor (
@@ -773,17 +768,17 @@ protected <T> Collection<T> doInsertBatch(String repoResourcePath, Collection<?
773768 DefaultIdentifierGenerator .INSTANCE ,
774769 this .cdoConverter
775770 );
776- Object identifier0 = generatingIdAccessor .getIdentifier ();
777- if (Objects .nonNull (identifier0 )) {
778- if (ClassUtils .isAssignable (InternalCDORevision .class , identifier0 .getClass ())) {
779- identifier = ((InternalCDORevision ) identifier0 ).getID ();
780- }
781- // if (ClassUtils.isAssignable(CDOID.class, identifier0.getClass ()) &&
782- // Objects.nonNull(transaction.getObject(( CDOID) identifier0))) {
783- // throw new CommitException() ;
771+ // Object identifier0 = generatingIdAccessor.getIdentifier();
772+ // if (Objects.nonNull(identifier0)) {
773+ // if (ClassUtils.isAssignable(InternalCDORevision.class, identifier0.getClass())) {
774+ // identifier = ((InternalCDORevision) identifier0).getID();
775+ // } else if (ClassUtils.isAssignable(CDOIDObjectLongImpl.class, identifier0.getClass())) {
776+ // identifier = CDOIDUtil.createLong(((CDOIDObjectLongImpl) identifier0).getLongValue ());
777+ // } else if (ClassUtils.isAssignable( CDOID.class, identifier0.getClass( ))) {
778+ // identifier = (CDOID) identifier0 ;
784779// }
785- }
786- identifier = CDOUtil .getCDOObject (bla ).cdoID ();
780+ // }
781+ identifier = CDOUtil .getCDOObject (eObjectInstance ).cdoID ();
787782 generatingIdAccessor .getOrSetProvidedIdentifier (identifier );
788783 savedObjects .add (obj );
789784 } else {
@@ -914,12 +909,18 @@ protected <T> T doInsert(String repoResourcePath, T objectToSave, CdoWriter<T> w
914909 if (Objects .nonNull (identifier0 )) {
915910 if (ClassUtils .isAssignable (InternalCDORevision .class , identifier0 .getClass ())) {
916911 identifier = ((InternalCDORevision ) identifier0 ).getID ();
917- }
918- if (ClassUtils .isAssignable (CDOID .class , identifier0 .getClass ()) && Objects .nonNull (transaction .getObject ((CDOID ) identifier0 ))) {
919- throw new CommitException ();
912+ } else if (ClassUtils .isAssignable (CDOIDObjectLongImpl .class , identifier0 .getClass ())) {
913+ identifier = CDOIDUtil .createLong (((CDOIDObjectLongImpl ) identifier0 ).getLongValue ());
914+ } else if (ClassUtils .isAssignable (CDOID .class , identifier0 .getClass ()) && Objects .nonNull (transaction .getObject ((CDOID ) identifier0 ))) {
915+ identifier = (CDOID ) identifier0 ;
920916 }
921917 }
922- identifier = CDOUtil .getCDOObject (internalValue ).cdoID ();
918+ if (identifier == null ) {
919+ identifier = CDOUtil .getCDOObject (internalValue ).cdoID ();
920+ }
921+ if (identifier == null ) {
922+ throw new CommitException ();
923+ }
923924 generatingIdAccessor .getOrSetProvidedIdentifier (identifier );
924925 }
925926 //TODO: commit.getTimeStamp() add to entity
@@ -955,8 +956,6 @@ public void createResourcePath(String resourcePath) {
955956 CDOTransaction cdoTransaction = openTransaction (session );
956957 CDOResource orCreateResource = cdoTransaction .getOrCreateResource (resourcePath );
957958 cdoTransaction .commit ();
958- // CDOResource resource = cdoTransaction.getResource(resourcePath);
959- // resource.delete(Collections.emptyMap());
960959 } catch (InvalidURIException e ) {
961960 throw new EmptyResultDataAccessException ("Resource path couldn't be created." , 1 , e );
962961 } catch (CommitException e ) {
@@ -1050,7 +1049,6 @@ public CdoDeleteResult removeAll(String resourcePath) {
10501049 return execute (session -> {
10511050 try {
10521051 CDOTransaction cdoTransaction = openTransaction (session );
1053- // CDOResource resource = cdoTransaction.getResource(resourcePath);
10541052 CDOResource resource = null ;
10551053 CDOResourceFolder resourceFolder ;
10561054 boolean cleaned = false ;
@@ -1127,7 +1125,6 @@ private <T> CdoDeleteResult doRemove(final Class<T> classType, final String reso
11271125
11281126 return execute (session -> {
11291127 CDOTransaction cdoTransaction = null ;
1130- // Collection<EObject> toRemove = new LinkedList<>();
11311128 try {
11321129 cdoTransaction = openTransaction (session );
11331130 CDOResource resource = cdoTransaction .getResource (resourcePath );
@@ -1136,14 +1133,12 @@ private <T> CdoDeleteResult doRemove(final Class<T> classType, final String reso
11361133 .filter (each -> {
11371134 if (persistentEntity .isNativeCdoOrLegacyMode ()) {
11381135 if (ClassUtils .isAssignable (ClassUtils .getUserClass (each ), classType )) { //TODO make this part of a Query
1139- // toRemove.add(each);
11401136 return true ;
11411137 }
11421138 } else {
11431139 CdoPersistentProperty persistentProperty = persistentEntity .getRequiredEObjectModelProperty ();
11441140 Assert .notNull (persistentProperty .getClassFor (), "classFor property must not be null for EObjectModel property" );
11451141 if (objectMatchesCriteria (persistentProperty , each , persistentProperty .getClassFor (), nsUri , packageName )) { //TODO make this part of a Query
1146- // toRemove.add(each);
11471142 return true ;
11481143 }
11491144 }
@@ -1469,16 +1464,6 @@ public boolean objectMatchesCriteria(CdoPersistentProperty persistentProperty, E
14691464 }
14701465 //TODO: match also prefix
14711466 }
1472- // else { // is a concrete user-defined (custom) ecore class (not dynamically created)?
1473- // Class<?> type = persistentProperty.getType(); //ClassTypeInformation.from(persistentProperty.getRawType()).getType();
1474- // hasMatch = ClassUtils.isAssignable(classFor, each.getClass()) && ClassUtils.isAssignable(classFor, type);
1475- //
1476- //// Arrays.stream(classFor)
1477- //// .anyMatch(x -> ClassUtils.isAssignable(x, each.getClass()))
1478- //// && Arrays.stream(classFor)
1479- //// .anyMatch(x -> ClassUtils.isAssignable(x, type));
1480- //// hasMatch = Arrays.asList(classFor).contains(each.getClass()) && Arrays.asList(classFor).contains(ClassTypeInformation.from(persistentProperty.getRawType()).getType());
1481- // }
14821467 return hasMatch ;
14831468 }
14841469
@@ -1513,15 +1498,14 @@ private static RuntimeException potentiallyConvertRuntimeException(RuntimeExcept
15131498
15141499 @ Override
15151500 public void destroy () throws Exception {
1516- //TODO close all opened sessions and transactions or whatever
1517- // System.out.println("template destroy");
1501+ // TODO close all opened sessions and transactions ...
15181502 }
15191503
15201504 /**
15211505 * A cdo template with a cdo session bounded to it.
15221506 * Useful for concatenated operations. To be used with executeSession() method.
15231507 */
1524- static class SessionBoundCdoTemplate extends CdoTemplate {
1508+ public static class SessionBoundCdoTemplate extends CdoTemplate {
15251509
15261510 private final CdoTemplate delegate ;
15271511 private final CdoClientSession session ;
0 commit comments