Skip to content

Commit

Permalink
feat: Gamification Connector Clean events after update config page- M…
Browse files Browse the repository at this point in the history
…EED-2887 - Meeds-io/MIPs#94 (#1339)

This will ensure that events will be deleted after being deleted from
the Events configuration page.
  • Loading branch information
AzmiTouil authored and exo-swf committed Nov 22, 2023
1 parent 70e3ee3 commit eb902ad
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,12 @@ public interface EventService {
* @return {@link EventDTO}
*/
EventDTO getEvent(long eventId);

/**
* Deletes an existing event
*
* @param eventId Event technical identifier to delete
* @return deleted {@link EventDTO}
*/
EventDTO deleteEventById(long eventId) throws ObjectNotFoundException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,26 @@
package io.meeds.gamification.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import io.meeds.gamification.model.EventDTO;
import io.meeds.gamification.model.filter.EventFilter;
import io.meeds.gamification.plugin.EventConfigPlugin;
import org.apache.commons.lang3.StringUtils;
import org.exoplatform.commons.exception.ObjectNotFoundException;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.picocontainer.Startable;
import org.exoplatform.commons.ObjectAlreadyExistsException;
import io.meeds.gamification.service.EventRegistry;
import io.meeds.gamification.service.EventService;

public class EventRegistryImpl implements Startable, EventRegistry {

private static final Log LOG = ExoLogger.getLogger(EventRegistryImpl.class);

private final Map<String, EventConfigPlugin> eventConfigPluginMap = new HashMap<>();

private final EventService eventService;
Expand Down Expand Up @@ -78,6 +84,26 @@ public void start() {
}
}
}
List<EventDTO> eventDAOList = eventService.getEvents(new EventFilter(), 0, -1);
eventDAOList.forEach(eventDTO -> {
EventConfigPlugin eventConfigPlugin = eventConfigPluginMap.values()
.stream()
.filter(eventConfig -> eventConfig.getEvent()
.getType()
.equals(eventDTO.getType())
&& eventConfig.getEvent()
.getTitle()
.equals(eventDTO.getTitle()))
.findFirst()
.orElse(null);
if (eventConfigPlugin == null) {
try {
eventService.deleteEventById(eventDTO.getId());
} catch (ObjectNotFoundException e) {
LOG.warn("Error while clean gamification event {}", eventDTO.getId(), e);
}
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,15 @@ public EventDTO updateEvent(EventDTO eventDTO) throws ObjectNotFoundException {
public EventDTO getEvent(long eventId) {
return eventStorage.getEventById(eventId);
}

@Override
public EventDTO deleteEventById(long eventId) throws ObjectNotFoundException {
EventDTO eventDTO;
try {
eventDTO = eventStorage.deleteEventById(eventId);
} catch (ObjectNotFoundException e) {
throw new ObjectNotFoundException("Event with id " + eventId + " is not found");
}
return eventDTO;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.meeds.gamification.model.EventDTO;
import io.meeds.gamification.model.filter.EventFilter;
import io.meeds.gamification.storage.mapper.EventMapper;
import org.exoplatform.commons.exception.ObjectNotFoundException;

public class EventStorage {

Expand Down Expand Up @@ -93,22 +94,27 @@ public EventDTO saveEvent(EventDTO eventDTO) {
}

/**
* Updates Event
* Retrieves gamification event by event id
*
* @param eventDTO {@link EventDTO} to update
* @param eventId Event Identifier
* @return {@link EventDTO}
*/
public void updateEvent(EventDTO eventDTO) {
EventEntity eventEntity = EventMapper.toEntity(eventDTO);
eventDAO.update(eventEntity);
public EventDTO getEventById(long eventId) {
return EventMapper.fromEntity(eventDAO.find(eventId));
}

/**
* Retrieves gamification event by event id
* Deletes event by id
*
* @param eventId Event Identifier
* @return {@link EventDTO}
*/
public EventDTO getEventById(long eventId) {
return EventMapper.fromEntity(eventDAO.find(eventId));
public EventDTO deleteEventById(long eventId) throws ObjectNotFoundException {
EventEntity eventEntity = eventDAO.find(eventId);
if (eventEntity == null) {
throw new ObjectNotFoundException("Event with id " + eventId + " does not exist");
}
eventDAO.delete(eventEntity);
return EventMapper.fromEntity(eventEntity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,32 @@ public void testCreateEvent() throws Exception {
assertEquals(1, eventService.countEvents(eventFilter));
}

public void testDeleteEvent() throws Exception {
EventFilter eventFilter = new EventFilter();
assertEquals(Collections.emptyList(), eventService.getEvents(eventFilter, OFFSET, LIMIT));
EventEntity eventEntity = new EventEntity();
eventEntity.setType("connectorName");
eventEntity.setTitle("event1");
eventEntity.setTrigger("trigger1");
EventDTO eventDTO = eventService.createEvent(EventMapper.fromEntity(eventEntity));
assertNotNull(eventService.getEvents(eventFilter, OFFSET, LIMIT));
eventService.deleteEventById(eventDTO.getId());
assertEquals(Collections.emptyList(), eventService.getEvents(eventFilter, OFFSET, LIMIT));
}

public void testUpdateEvent() throws Exception {
EventEntity eventEntity = new EventEntity();
eventEntity.setType("connectorName");
eventEntity.setTitle("event1");
eventEntity.setTrigger("trigger1");
EventDTO eventDTO = eventService.createEvent(EventMapper.fromEntity(eventEntity));
assertNotNull(eventService.getEventByTitleAndTrigger("event1", "trigger1"));
assertEquals("trigger1", eventService.getEvent(eventDTO.getId()).getTrigger());
eventDTO.setTrigger("trigger2");
eventService.updateEvent(eventDTO);
assertEquals("trigger2", eventService.getEvent(eventDTO.getId()).getTrigger());
}

public void testGetEvents() {
EventFilter eventFilter = new EventFilter();

Expand Down

0 comments on commit eb902ad

Please sign in to comment.