/*******************************************************************************
 * Copyright (c) 2014 Codenvy, S.A.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Codenvy, S.A. - initial API and implementation
 *******************************************************************************/
package com.codenvy.flux.watcher.core.internal;

import com.codenvy.flux.watcher.core.FluxConnector;
import com.codenvy.flux.watcher.core.Message;
import com.codenvy.flux.watcher.core.RepositoryEvent;
import com.codenvy.flux.watcher.core.RepositoryEventTypes;
import com.codenvy.flux.watcher.core.RepositoryListener;
import com.codenvy.flux.watcher.core.Resource;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;
import javax.inject.Singleton;

import static com.codenvy.flux.watcher.core.Message.Fields.PROJECT;
import static com.codenvy.flux.watcher.core.Message.Fields.HASH;
import static com.codenvy.flux.watcher.core.Message.Fields.PATH;
import static com.codenvy.flux.watcher.core.Message.Fields.TIMESTAMP;
import static com.codenvy.flux.watcher.core.Message.Fields.TYPE;
import static com.codenvy.flux.watcher.core.MessageType.RESOURCE_CREATED;
import static com.codenvy.flux.watcher.core.RepositoryEventType.ENTRY_CREATED;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Listener sending a message to flux connections when a resource is created in the repository.
 *
 * @author Kevin Pollet
 */
@Singleton
@RepositoryEventTypes(ENTRY_CREATED)
public class EntryCreatedListener implements RepositoryListener {
    private final FluxConnector fluxConnector;

    /**
     * Constructs an instance of {@code EntryCreatedListener}.
     *
     * @param fluxConnector
     *         the {@link com.codenvy.flux.watcher.core.FluxConnector}.
     * @throws java.lang.NullPointerException
     *         if {@code fluxConnector} parameter is {@code null}.
     */
    @Inject
    EntryCreatedListener(FluxConnector fluxConnector) {
        this.fluxConnector = checkNotNull(fluxConnector);
    }

    @Override
    public void onEvent(RepositoryEvent event) {
        try {

            final Resource createdResource = event.resource();

            final JSONObject content = new JSONObject()
                    .put(PROJECT.value(), createdResource.projectId())
                    .put(PATH.value(), createdResource.path())
                    .put(TIMESTAMP.value(), createdResource.timestamp())
                    .put(HASH.value(), createdResource.hash())
                    .put(TYPE.value(), createdResource.type().name().toLowerCase());

            // broadcast message to all connections
            fluxConnector.broadcastMessage(new Message(RESOURCE_CREATED, content));

        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }
}

@@ -1054,23 +1054,20 @@
 .Fields.
-PROJECT
+HASH
 ;%0Aimport
@@ -1111,36 +1111,39 @@
 .Message.Fields.
-HASH
+PROJECT
 ;%0Aimport static 
@@ -1187,20 +1187,24 @@
 .Fields.
-PATH
+RESOURCE
 ;%0Aimport
@@ -1398,32 +1398,105 @@
 SOURCE_CREATED;%0A
+import static com.codenvy.flux.watcher.core.MessageType.RESOURCE_STORED;%0A
 import static co
@@ -2454,24 +2454,17 @@
 esource 
-createdR
+r
 esource 
@@ -2483,17 +2483,16 @@
 urce();%0A
-%0A
         
@@ -2577,32 +2577,25 @@
 CT.value(), 
-createdR
+r
 esource.proj
@@ -2632,12 +2632,16 @@
 put(
-PATH
+RESOURCE
 .val
@@ -2642,33 +2642,26 @@
 CE.value(), 
-c
 re
-atedRe
 source.path(
@@ -2703,33 +2703,26 @@
 MP.value(), 
-c
 re
-atedRe
 source.times
@@ -2764,32 +2764,25 @@
 SH.value(), 
-createdR
+r
 esource.hash
@@ -2828,16 +2828,9 @@
 (), 
-createdR
+r
 esou
@@ -2880,11 +2880,22 @@
     
-// 
+fluxConnector.
 broa
@@ -2903,35 +2903,56 @@
 cast
- m
+M
 essage
- to all connections
+(new Message(RESOURCE_CREATED, content));
 %0A   
@@ -3004,37 +3004,36 @@
 essage(RESOURCE_
-CREAT
+STOR
 ED, content));%0A%0A

Fix wrong field in resourceCreated message