package com.celements.navigation.event.converter;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xwiki.component.annotation.Component;
import org.xwiki.observation.event.Event;
import org.xwiki.observation.remote.LocalEventData;
import org.xwiki.observation.remote.RemoteEventData;
import org.xwiki.observation.remote.converter.AbstractEventConverter;

@Component("TreeNodeEventConverter")
public class TreeNodeEventConverter extends AbstractEventConverter {

  private static Log LOGGER = LogFactory.getFactory().getInstance(
      TreeNodeEventConverter.class);

  /**
   * {@inheritDoc}
   * 
   * @see org.xwiki.observation.remote.converter.AbstractEventConverter#getPriority()
   */
  public int getPriority() {
    return 1000;
  }

  /**
   * {@inheritDoc}
   * 
   * @see org.xwiki.observation.remote.converter.RemoteEventConverter#fromRemote(org.xwiki.observation.remote.RemoteEventData,
   *      org.xwiki.observation.remote.LocalEventData)
   */
  public boolean fromRemote(RemoteEventData remoteEvent, LocalEventData localEvent) {
    LOGGER.debug("fromRemote remoteEvent [" + remoteEvent + "], localEvent [" + localEvent
        + "].");
    if (remoteEvent.getEvent() instanceof Event) {
      localEvent.setEvent((Event) remoteEvent.getEvent());
      if (remoteEvent.getSource() != null) {
        localEvent.setSource(remoteEvent.getSource());
      }
      if (remoteEvent.getData() != null) {
        localEvent.setData(remoteEvent.getData());
      }
      return true;
    } else {
      LOGGER.debug("fromRemote not instance of Event [" + remoteEvent.getEvent() + "].");
    }
    return false;
  }

  /**
   * {@inheritDoc}
   * 
   * @see org.xwiki.observation.remote.converter.LocalEventConverter#toRemote(org.xwiki.observation.remote.LocalEventData,
   *      org.xwiki.observation.remote.RemoteEventData)
   */
  public boolean toRemote(LocalEventData localEvent, RemoteEventData remoteEvent) {
    LOGGER.debug("toRemote remoteEvent [" + remoteEvent + "], localEvent [" + localEvent
        + "].");
    if (isSerializable(localEvent)) {
      remoteEvent.setEvent((Serializable) localEvent.getEvent());
      remoteEvent.setSource((Serializable) localEvent.getSource());
      remoteEvent.setData((Serializable) localEvent.getData());
      return true;
    } else {
      LOGGER.debug("toRemote not serializale [" + isSerializable(localEvent) + "].");
    }

    return false;
  }

  /**
   * Indicate if a local event is fully serializable.
   * 
   * @param localEvent
   *          the local event
   * @return true is the local event is fully serializable, false otherwise.
   */
  private boolean isSerializable(LocalEventData localEvent) {
    return localEvent.getEvent() instanceof Serializable
        && isSerializable(localEvent.getData()) && isSerializable(localEvent.getSource());
  }

  /**
   * Indicate if an object is serializable.
   * 
   * @param obj
   *          the object to test
   * @return true is the object is serializable, false otherwise.
   */
  private boolean isSerializable(Object obj) {
    return obj instanceof Serializable || obj == null;
  }

}

@@ -2416,16 +2416,17 @@
 liza
+b
 le %5B%22 + 
 isSe
@@ -2421,31 +2421,80 @@
 le %5B%22 + 
-isSerializable(
+localEvent.getClass().getName()%0A          + %22%5D, localEvent %5B%22 + 
 localEve
@@ -2495,17 +2495,16 @@
 calEvent
-)
  + %22%5D.%22)
@@ -2803,23 +2803,272 @@
 ) %7B%0A    
-return 
+boolean isSerializable = localEvent.getEvent() instanceof Serializable%0A        && isSerializable(localEvent.getData()) && isSerializable(localEvent.getSource());%0A    LOGGER.trace(%22isSerializable: %5B%22 + isSerializable + %22%5D, getEvent instanceof %5B%22%0A        + (
 localEve
@@ -3096,32 +3096,64 @@
 eof Serializable
+) + %22%5D isSerializable getData %5B%22
 %0A        && isSe
@@ -3137,34 +3137,33 @@
 Data %5B%22%0A        
-&&
++
  isSerializable(
@@ -3176,34 +3176,74 @@
 vent.getData()) 
-&&
++ %22%5D isSerializable getSource %5B%22%0A        +
  isSerializable(
@@ -3261,24 +3261,59 @@
 getSource())
+ + %22%5D.%22);%0A    return isSerializable
 ;%0A  %7D%0A%0A  /**

improve logging for TreeNodeEventConverter.