-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restore hierarchical id manager functionality
- Loading branch information
Jan Wittler
committed
Feb 5, 2023
1 parent
9222e4f
commit 045a894
Showing
17 changed files
with
479 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
bundles/tools.vitruv.change.atomic/src/tools/vitruv/change/atomic/EChangeUuidManager.xtend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package tools.vitruv.change.atomic | ||
|
||
import org.eclipse.emf.ecore.EObject | ||
import tools.vitruv.change.atomic.eobject.CreateEObject | ||
import tools.vitruv.change.atomic.eobject.EObjectAddedEChange | ||
import tools.vitruv.change.atomic.eobject.EObjectExistenceEChange | ||
import tools.vitruv.change.atomic.eobject.EObjectSubtractedEChange | ||
import tools.vitruv.change.atomic.feature.FeatureEChange | ||
import tools.vitruv.change.atomic.uuid.UuidResolver | ||
|
||
import static com.google.common.base.Preconditions.checkArgument | ||
import static com.google.common.base.Preconditions.checkState | ||
|
||
/** | ||
* Provides logic for initializing the UUIDs within changes. | ||
*/ | ||
class EChangeUuidManager { | ||
val UuidResolver uuidResolver | ||
|
||
/** | ||
* Initializes the manager with a {@link UuidResolver}. | ||
* | ||
* @param uuidResolver - | ||
* the {@link UuidResolver} to use for UUID management | ||
*/ | ||
new(UuidResolver uuidResolver) { | ||
checkArgument(uuidResolver !== null, "uuid resolver must not be null") | ||
this.uuidResolver = uuidResolver | ||
} | ||
|
||
def static void setOrGenerateIds(Iterable<EChange> eChanges, UuidResolver uuidResolver) { | ||
setOrGenerateIds(eChanges, uuidResolver, true) | ||
} | ||
|
||
def static void setOrGenerateIds(Iterable<EChange> eChanges, UuidResolver uuidResolver, boolean endTransaction) { | ||
val manager = new EChangeUuidManager(uuidResolver) | ||
eChanges.forEach [ eChange | | ||
manager.setOrGenerateIds(eChange) | ||
] | ||
if (endTransaction) { | ||
uuidResolver.endTransaction | ||
} | ||
} | ||
|
||
def void setOrGenerateIds(EChange eChange) { | ||
switch eChange { | ||
CreateEObject<?>: | ||
setOrGenerateCreatedEObjectUuid(eChange) | ||
EObjectExistenceEChange<?>: | ||
setAffectedEObjectUuid(eChange) | ||
FeatureEChange<?,?>: | ||
setAffectedEObjectUuid(eChange) | ||
} | ||
switch eChange { | ||
EObjectSubtractedEChange<?>: | ||
setOldValueUuid(eChange) | ||
} | ||
switch eChange { | ||
EObjectAddedEChange<?>: | ||
setOrGenerateNewValueUuid(eChange) | ||
} | ||
} | ||
|
||
private def String getUuid(EObject object) { | ||
val id = uuidResolver.getUuid(object) | ||
checkState(id !== null, "uuid must not be null") | ||
return id | ||
} | ||
|
||
private def String getOrGenerateUuid(EObject object) { | ||
try { | ||
getUuid(object) | ||
} | ||
catch (IllegalStateException e) { | ||
uuidResolver.registerEObject(object) | ||
} | ||
} | ||
|
||
private def void setOrGenerateNewValueUuid(EObjectAddedEChange<?> addedEChange) { | ||
if (addedEChange.newValue === null) { | ||
return; | ||
} | ||
addedEChange.newValueID = addedEChange.newValue.getOrGenerateUuid | ||
} | ||
|
||
private def void setOldValueUuid(EObjectSubtractedEChange<?> subtractedEChange) { | ||
if (subtractedEChange.oldValue === null) { | ||
return; | ||
} | ||
subtractedEChange.oldValueID = subtractedEChange.oldValue.uuid | ||
} | ||
|
||
private def void setAffectedEObjectUuid(EObjectExistenceEChange<?> existenceChange) { | ||
val affectedEObject = existenceChange.affectedEObject | ||
checkArgument(affectedEObject !== null, "existence change must have an affected EObject: %s", existenceChange) | ||
existenceChange.affectedEObjectID = affectedEObject.uuid | ||
} | ||
|
||
private def void setOrGenerateCreatedEObjectUuid(CreateEObject<?> existenceChange) { | ||
val affectedEObject = existenceChange.affectedEObject | ||
checkArgument(affectedEObject !== null, "existence change must have an affected EObject: %s", existenceChange) | ||
existenceChange.affectedEObjectID = affectedEObject.getOrGenerateUuid | ||
} | ||
|
||
private def void setAffectedEObjectUuid(FeatureEChange<?, ?> featureChange) { | ||
val affectedEObject = featureChange.affectedEObject | ||
checkArgument(affectedEObject !== null, "feature change must have an affected EObject: %s", featureChange) | ||
featureChange.affectedEObjectID = affectedEObject.uuid | ||
} | ||
|
||
} |
Oops, something went wrong.