I'm trying to do something usefull with the VersionDeleteEvent. The event is triggered but I only gat an Id. And from this event I cannot get any version information.
This is my code:
@EventListener public void VersionDeleteEvent(VersionDeleteEvent versionDeleteEvent) { Long id = versionDeleteEvent.getVersionId(); Version version = versionManager.getVersion( id ); Project project = version.getProjectObject(); }
The getVersion returns null, so I dont know what project or version is deleted. How can I retrieve this information?
Community moderators have prevented the ability to post new answers.
Define the cache
private HashMap<Long, Version> versions = new HashMap<Long, Version>();
Fill the cache, on afterPropertiesSet and version creation/deletion
private void CacheVersions() { Collection<Version> allVersions = versionManager.getAllVersions(); versions.clear(); for(Version v : allVersions) { versions.put(v.getId(), v); } }
Use the cache
if(versions.containsKey(versionDeleteEvent.getVersionId())){ Version version = versions.get(versionDeleteEvent.getVersionId()); // use the version. }
The only way is to create your own cache in the lictener class and store the attributes you need, so when the version is removed you're able to lookup the value from the cache by id. As Mizan said there is no way to get the actual object as the code in JIRA is :
// delete the version from the system versionManager.deleteVersion(version); ... eventPublisher.publish(new VersionDeleteEvent(version.getId()));
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Boris,
can you elaborate , how to create cache if the listener class is called only when the event has occured.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I suppose your listener class implements InitializingBean, DisposableBean.
In that case you can initialize the cache in the afterPropertiesSet before calling eventPublisher.register(
this
); and then clear the cache in the
destroy method after the call to eventPublisher.unregister(
this
);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I was already afraid for this answer. But why is it implemented this way. And why isn't there a pre-VersionDeleteEvent I think it can be very usefull.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can always log a feature request to JIRA :). I've got no idea why thy've implemented it this way. Actually getting an id to a non-existing object is not very useful.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
So I created a feature request: https://jira.atlassian.com/browse/JRA-33879
Lets see what happens with that.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Boris,
i will definitely your suggestion.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Hans,
as the version is deleted from DB, therefore you wont get any version information (object) except the id of it .
workaround :
you can create a separate table with version id , name , project as input when somebody creates a version in any of the project (versioncreate event can take of this).
so when somebody deletes a version , you have the version id and you can get version info from your custom created table.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.