001package systems.dmx.events;
002
003import systems.dmx.core.RelatedTopic;
004import systems.dmx.core.Topic;
005import systems.dmx.core.model.AssociationModel;
006import systems.dmx.core.model.RoleModel;
007import systems.dmx.core.osgi.PluginActivator;
008import systems.dmx.core.service.Inject;
009import systems.dmx.core.service.event.PreCreateAssociationListener;
010import systems.dmx.core.util.DMXUtils;
011import systems.dmx.time.TimeService;
012
013import javax.ws.rs.GET;
014import javax.ws.rs.Path;
015import javax.ws.rs.PathParam;
016import javax.ws.rs.Produces;
017import javax.ws.rs.Consumes;
018
019import java.util.List;
020import java.util.logging.Logger;
021
022
023
024@Path("/event")
025@Consumes("application/json")
026@Produces("application/json")
027public class EventsPlugin extends PluginActivator implements EventsService, PreCreateAssociationListener {
028
029    // ---------------------------------------------------------------------------------------------- Instance Variables
030
031    @Inject
032    private TimeService timeService;
033
034    private static final Logger logger = Logger.getLogger(EventsPlugin.class.getName());
035
036    // -------------------------------------------------------------------------------------------------- Public Methods
037
038
039
040    // ************************************
041    // *** EventsService Implementation ***
042    // ************************************
043
044
045
046    @GET
047    @Path("/participant/{id}")
048    @Override
049    public List<RelatedTopic> getEvents(@PathParam("id") long personId) {
050        return dmx.getTopic(personId).getRelatedTopics("dmx.events.participant", "dmx.core.default", "dmx.core.default",
051            "dmx.events.event");
052    }
053
054    @GET
055    @Path("/{id}/participants")
056    @Override
057    public List<RelatedTopic> getParticipants(@PathParam("id") long eventId) {
058        return dmx.getTopic(eventId).getRelatedTopics("dmx.events.participant", "dmx.core.default", "dmx.core.default",
059            "dmx.contacts.person");
060    }
061
062
063
064    // ********************************
065    // *** Listener Implementations ***
066    // ********************************
067
068
069
070    @Override
071    public void preCreateAssociation(AssociationModel assoc) {
072        // Event <-> Person
073        DMXUtils.associationAutoTyping(assoc, "dmx.events.event", "dmx.contacts.person",
074            "dmx.events.participant", "dmx.core.default", "dmx.core.default", dmx);
075        //
076        // Event -> Address
077        RoleModel[] roles = DMXUtils.associationAutoTyping(assoc, "dmx.events.event", "dmx.contacts.address",
078            "dmx.core.aggregation", "dmx.core.parent", "dmx.core.child", dmx);
079        if (roles != null) {
080            long eventId = roles[0].getPlayerId();
081            Topic event = dmx.getTopic(eventId);
082            event.getChildTopics().getTopic("dmx.contacts.address").getRelatingAssociation().delete();
083            timeService.setModified(event);
084        }
085    }
086}