001    package de.deepamehta.core.impl;
002    
003    import de.deepamehta.core.Association;
004    import de.deepamehta.core.AssociationType;
005    import de.deepamehta.core.Topic;
006    import de.deepamehta.core.TopicType;
007    import de.deepamehta.core.model.AssociationModel;
008    import de.deepamehta.core.model.TopicModel;
009    import de.deepamehta.core.service.ClientState;
010    import de.deepamehta.core.service.DeepaMehtaEvent;
011    import de.deepamehta.core.service.Directives;
012    import de.deepamehta.core.service.EventListener;
013    import de.deepamehta.core.service.event.*;
014    
015    // ### TODO: hide Jersey internals. Move to JAX-RS 2.0.
016    import com.sun.jersey.spi.container.ContainerRequest;
017    import com.sun.jersey.spi.container.ContainerResponse;
018    
019    import javax.servlet.http.HttpServletRequest;
020    
021    
022    
023    /**
024     * Events fired by the DeepaMehta core service.
025     * Plugins can listen to these events by implementing the respective listener interfaces.
026     *
027     * @see de.deepamehta.core.service.event
028     */
029    class CoreEvent {
030    
031        static DeepaMehtaEvent PRE_CREATE_TOPIC = new DeepaMehtaEvent(PreCreateTopicListener.class) {
032            @Override
033            public void deliver(EventListener listener, Object... params) {
034                ((PreCreateTopicListener) listener).preCreateTopic(
035                    (TopicModel) params[0], (ClientState) params[1]
036                );
037            }
038        };
039    
040        static DeepaMehtaEvent PRE_CREATE_ASSOCIATION = new DeepaMehtaEvent(PreCreateAssociationListener.class) {
041            @Override
042            public void deliver(EventListener listener, Object... params) {
043                ((PreCreateAssociationListener) listener).preCreateAssociation(
044                    (AssociationModel) params[0], (ClientState) params[1]
045                );
046            }
047        };
048    
049        // ---
050    
051        static DeepaMehtaEvent POST_CREATE_TOPIC = new DeepaMehtaEvent(PostCreateTopicListener.class) {
052            @Override
053            public void deliver(EventListener listener, Object... params) {
054                ((PostCreateTopicListener) listener).postCreateTopic(
055                    (Topic) params[0], (ClientState) params[1], (Directives) params[2]
056                );
057            }
058        };
059    
060        static DeepaMehtaEvent POST_CREATE_ASSOCIATION = new DeepaMehtaEvent(PostCreateAssociationListener.class) {
061            @Override
062            public void deliver(EventListener listener, Object... params) {
063                ((PostCreateAssociationListener) listener).postCreateAssociation(
064                    (Association) params[0], (ClientState) params[1], (Directives) params[2]
065                );
066            }
067        };
068    
069        // ---
070    
071        static DeepaMehtaEvent PRE_UPDATE_TOPIC = new DeepaMehtaEvent(PreUpdateTopicListener.class) {
072            @Override
073            public void deliver(EventListener listener, Object... params) {
074                ((PreUpdateTopicListener) listener).preUpdateTopic(
075                    (Topic) params[0], (TopicModel) params[1], (Directives) params[2]
076                );
077            }
078        };
079    
080        static DeepaMehtaEvent PRE_UPDATE_ASSOCIATION = new DeepaMehtaEvent(PreUpdateAssociationListener.class) {
081            @Override
082            public void deliver(EventListener listener, Object... params) {
083                ((PreUpdateAssociationListener) listener).preUpdateAssociation(
084                    (Association) params[0], (AssociationModel) params[1], (Directives) params[2]
085                );
086            }
087        };
088    
089        // ---
090    
091        static DeepaMehtaEvent POST_UPDATE_TOPIC = new DeepaMehtaEvent(PostUpdateTopicListener.class) {
092            @Override
093            public void deliver(EventListener listener, Object... params) {
094                ((PostUpdateTopicListener) listener).postUpdateTopic(
095                    (Topic) params[0], (TopicModel) params[1], (TopicModel) params[2], (ClientState) params[3],
096                    (Directives) params[4]
097                );
098            }
099        };
100    
101        static DeepaMehtaEvent POST_UPDATE_ASSOCIATION = new DeepaMehtaEvent(PostUpdateAssociationListener.class) {
102            @Override
103            public void deliver(EventListener listener, Object... params) {
104                ((PostUpdateAssociationListener) listener).postUpdateAssociation(
105                    (Association) params[0], (AssociationModel) params[1], (ClientState) params[2], (Directives) params[3]
106                );
107            }
108        };
109    
110        // ---
111    
112        static DeepaMehtaEvent POST_UPDATE_TOPIC_REQUEST = new DeepaMehtaEvent(PostUpdateTopicRequestListener.class) {
113            @Override
114            public void deliver(EventListener listener, Object... params) {
115                ((PostUpdateTopicRequestListener) listener).postUpdateTopicRequest(
116                    (Topic) params[0]
117                );
118            }
119        };
120    
121        // Note: a corresponding POST_UPDATE_ASSOCIATION_REQUEST event is not necessary.
122        // It would be equivalent to POST_UPDATE_ASSOCIATION.
123        // Per request exactly one association is updated. Its childs are topics (never associations).
124    
125        // ---
126    
127        static DeepaMehtaEvent PRE_DELETE_TOPIC = new DeepaMehtaEvent(PreDeleteTopicListener.class) {
128            @Override
129            public void deliver(EventListener listener, Object... params) {
130                ((PreDeleteTopicListener) listener).preDeleteTopic(
131                    (Topic) params[0], (Directives) params[1]
132                );
133            }
134        };
135    
136        static DeepaMehtaEvent PRE_DELETE_ASSOCIATION = new DeepaMehtaEvent(PreDeleteAssociationListener.class) {
137            @Override
138            public void deliver(EventListener listener, Object... params) {
139                ((PreDeleteAssociationListener) listener).preDeleteAssociation(
140                    (Association) params[0], (Directives) params[1]
141                );
142            }
143        };
144    
145        // ---
146    
147        static DeepaMehtaEvent POST_DELETE_TOPIC = new DeepaMehtaEvent(PostDeleteTopicListener.class) {
148            @Override
149            public void deliver(EventListener listener, Object... params) {
150                ((PostDeleteTopicListener) listener).postDeleteTopic(
151                    (Topic) params[0], (Directives) params[1]
152                );
153            }
154        };
155    
156        static DeepaMehtaEvent POST_DELETE_ASSOCIATION = new DeepaMehtaEvent(PostDeleteAssociationListener.class) {
157            @Override
158            public void deliver(EventListener listener, Object... params) {
159                ((PostDeleteAssociationListener) listener).postDeleteAssociation(
160                    (Association) params[0], (Directives) params[1]
161                );
162            }
163        };
164    
165        // ---
166    
167        static DeepaMehtaEvent SERVICE_REQUEST_FILTER = new DeepaMehtaEvent(ServiceRequestFilterListener.class) {
168            @Override
169            public void deliver(EventListener listener, Object... params) {
170                ((ServiceRequestFilterListener) listener).serviceRequestFilter(
171                    (ContainerRequest) params[0]
172                );
173            }
174        };
175    
176        static DeepaMehtaEvent SERVICE_RESPONSE_FILTER = new DeepaMehtaEvent(ServiceResponseFilterListener.class) {
177            @Override
178            public void deliver(EventListener listener, Object... params) {
179                ((ServiceResponseFilterListener) listener).serviceResponseFilter(
180                    (ContainerResponse) params[0]
181                );
182            }
183        };
184    
185        static DeepaMehtaEvent RESOURCE_REQUEST_FILTER = new DeepaMehtaEvent(ResourceRequestFilterListener.class) {
186            @Override
187            public void deliver(EventListener listener, Object... params) {
188                ((ResourceRequestFilterListener) listener).resourceRequestFilter(
189                    (HttpServletRequest) params[0]
190                );
191            }
192        };
193    
194        // ---
195    
196        static DeepaMehtaEvent PRE_SEND_TOPIC = new DeepaMehtaEvent(PreSendTopicListener.class) {
197            @Override
198            public void deliver(EventListener listener, Object... params) {
199                ((PreSendTopicListener) listener).preSendTopic(
200                    (Topic) params[0], (ClientState) params[1]
201                );
202            }
203        };
204    
205        static DeepaMehtaEvent PRE_SEND_ASSOCIATION = new DeepaMehtaEvent(PreSendAssociationListener.class) {
206            @Override
207            public void deliver(EventListener listener, Object... params) {
208                ((PreSendAssociationListener) listener).preSendAssociation(
209                    (Association) params[0], (ClientState) params[1]
210                );
211            }
212        };
213    
214        static DeepaMehtaEvent PRE_SEND_TOPIC_TYPE = new DeepaMehtaEvent(PreSendTopicTypeListener.class) {
215            @Override
216            public void deliver(EventListener listener, Object... params) {
217                ((PreSendTopicTypeListener) listener).preSendTopicType(
218                    (TopicType) params[0], (ClientState) params[1]
219                );
220            }
221        };
222    
223        static DeepaMehtaEvent PRE_SEND_ASSOCIATION_TYPE = new DeepaMehtaEvent(PreSendAssociationTypeListener.class) {
224            @Override
225            public void deliver(EventListener listener, Object... params) {
226                ((PreSendAssociationTypeListener) listener).preSendAssociationType(
227                    (AssociationType) params[0], (ClientState) params[1]
228                );
229            }
230        };
231    
232        // ---
233    
234        static DeepaMehtaEvent ALL_PLUGINS_ACTIVE = new DeepaMehtaEvent(AllPluginsActiveListener.class) {
235            @Override
236            public void deliver(EventListener listener, Object... params) {
237                ((AllPluginsActiveListener) listener).allPluginsActive();
238            }
239        };
240    
241        // ---
242    
243        // This event has a double nature:
244        //   a) it is fired regularily (see EmbeddedService.createTopicType()).
245        //   b) it is fired locally (see PluginImpl.introduceTopicTypesToPlugin()).
246        static DeepaMehtaEvent INTRODUCE_TOPIC_TYPE = new DeepaMehtaEvent(IntroduceTopicTypeListener.class) {
247            @Override
248            public void deliver(EventListener listener, Object... params) {
249                ((IntroduceTopicTypeListener) listener).introduceTopicType(
250                    (TopicType) params[0], (ClientState) params[1]
251                );
252            }
253        };
254    
255        // This event has a double nature:
256        //   a) it is fired regularily (see EmbeddedService.createAssociationType()).
257        //   b) it is fired locally (see PluginImpl.introduceAssociationTypesToPlugin()).
258        static DeepaMehtaEvent INTRODUCE_ASSOCIATION_TYPE = new DeepaMehtaEvent(IntroduceAssociationTypeListener.class) {
259            @Override
260            public void deliver(EventListener listener, Object... params) {
261                ((IntroduceAssociationTypeListener) listener).introduceAssociationType(
262                    (AssociationType) params[0], (ClientState) params[1]
263                );
264            }
265        };
266    }