Added EventManager
This commit is contained in:
parent
eee0e50061
commit
a39f8cc8d6
|
@ -1,5 +1,8 @@
|
|||
# Changelog for org.gcube.application.cms-plugin-framework
|
||||
|
||||
## [v1.0.5-SNAPSHOT] - 2023-12-21
|
||||
- Added Event Manager
|
||||
|
||||
## [v1.0.4] - 2023-09-06
|
||||
- Using parent range version [#25572]
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>cms-plugin-framework</artifactId>
|
||||
<version>1.0.4</version>
|
||||
<version>1.0.5-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.gcube.application.cms</groupId>
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
package org.gcube.application.cms.plugins.events;
|
||||
|
||||
public interface EventListener<T extends ItemObservable> {
|
||||
|
||||
public void updated(T observerd);
|
||||
|
||||
}
|
|
@ -0,0 +1,130 @@
|
|||
package org.gcube.application.cms.plugins.events;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The Class EventManager.
|
||||
*
|
||||
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
|
||||
*
|
||||
* Dec 11, 2023
|
||||
*/
|
||||
public class EventManager implements EventManagerInterface<ItemObservable> {
|
||||
|
||||
private HashMap<Event, List<EventListener<ItemObservable>>> listeners = new HashMap<Event, List<EventListener<ItemObservable>>>();
|
||||
|
||||
/**
|
||||
* The Class Event.
|
||||
*
|
||||
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
|
||||
*
|
||||
* Nov 29, 2023
|
||||
*/
|
||||
public static enum Event {
|
||||
|
||||
PROJECT_CREATED("PROJECT_CREATED"),
|
||||
PROJECT_UPDATED("PROJECT_UPDATED"),
|
||||
PROJECT_DELETED("PROJECT_DELETED"),
|
||||
LIFECYCLE_STEP_PERFORMED("LIFECYCLE_STEP_PERFORMED");
|
||||
|
||||
String name;
|
||||
|
||||
/**
|
||||
* Instantiates a new event.
|
||||
*
|
||||
* @param name the name
|
||||
*/
|
||||
Event(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name.
|
||||
*
|
||||
* @return the name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// private instance, so that it can be
|
||||
// accessed by only by getInstance() method
|
||||
private static EventManager instance;
|
||||
|
||||
/**
|
||||
* Instantiates a new event manager.
|
||||
*/
|
||||
private EventManager() {
|
||||
// private constructor
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the single instance of EventManager.
|
||||
*
|
||||
* @return single instance of EventManager
|
||||
*/
|
||||
public static EventManager getInstance() {
|
||||
if (instance == null) {
|
||||
// synchronized block to remove overhead
|
||||
synchronized (EventManager.class) {
|
||||
if (instance == null) {
|
||||
// if instance is null, initialize
|
||||
instance = new EventManager();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe.
|
||||
*
|
||||
* @param eventType the event type
|
||||
* @param listener the listener
|
||||
*/
|
||||
public void subscribe(Event eventType, EventListener<ItemObservable> listener) {
|
||||
List<EventListener<ItemObservable>> list = listeners.get(eventType);
|
||||
if (list == null)
|
||||
list = new ArrayList<EventListener<ItemObservable>>();
|
||||
|
||||
list.add(listener);
|
||||
listeners.put(eventType, list);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsubscribe.
|
||||
*
|
||||
* @param eventType the event type
|
||||
* @param listener the listener
|
||||
*/
|
||||
public void unsubscribe(Event eventType, EventListener<ItemObservable> listener) {
|
||||
List<EventListener<ItemObservable>> list = listeners.get(eventType);
|
||||
if (list == null)
|
||||
return;
|
||||
|
||||
list.remove(listener);
|
||||
listeners.put(eventType, list);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify.
|
||||
*
|
||||
* @param eventType the event type
|
||||
* @param item the item
|
||||
*/
|
||||
public void notify(Event eventType, ItemObservable item) {
|
||||
for (EventManager.Event event : listeners.keySet()) {
|
||||
if (event.equals(eventType)) {
|
||||
for (EventListener<ItemObservable> eventListner : listeners.get(eventType)) {
|
||||
eventListner.updated(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package org.gcube.application.cms.plugins.events;
|
||||
|
||||
import org.gcube.application.cms.plugins.events.EventManager.Event;
|
||||
|
||||
public interface EventManagerInterface<T extends ItemObservable> {
|
||||
|
||||
public void subscribe(Event eventType, EventListener<T> listener);
|
||||
|
||||
public void unsubscribe(Event eventType, EventListener<T> listener);
|
||||
|
||||
public void notify(Event eventType, T item);
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package org.gcube.application.cms.plugins.events;
|
||||
|
||||
public interface ItemObservable {
|
||||
|
||||
public String getId();
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package org.gcube.application.cms.plugins.events;
|
||||
|
||||
import org.gcube.application.geoportal.common.model.document.Project;
|
||||
import org.gcube.application.geoportal.common.model.document.accounting.Context;
|
||||
import org.gcube.application.geoportal.common.model.document.accounting.User;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Slf4j
|
||||
public class ItemObserved<T extends Project> implements ItemObservable {
|
||||
User caller;
|
||||
Context context;
|
||||
T project;
|
||||
EventManager.Event event;
|
||||
|
||||
public String getId() {
|
||||
log.debug("Called getId");
|
||||
if (project == null)
|
||||
return null;
|
||||
return project.getId();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue