/*
 * JBoss, Home of Professional Open Source
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package org.jboss.deployment.spi;

import java.io.Serializable;
import java.util.ArrayList;
import javax.enterprise.deploy.spi.TargetModuleID;

/**
 * A Serializable representation of the TargetModuleID
 * 
 * @author Scott.Stark@jboss.org
 * @version $Revision: 1.1 $
 */
public class SerializableTargetModuleID
   implements Serializable
{
   private final long serialVersionUID = 1L;

   private SerializableTargetModuleID parentModuleID;
   private ArrayList childModuleIDs = new ArrayList();
   private String moduleID;
   private int moduleType;
   private boolean isRunning;

   public SerializableTargetModuleID(TargetModuleIDImpl impl)
   {
      this(null, impl);
   }
   public SerializableTargetModuleID(SerializableTargetModuleID parent,
      TargetModuleIDImpl impl)
   {
      parentModuleID = parent;
      moduleID = impl.getModuleID();
      moduleType = impl.getModeType().getValue();

      TargetModuleID[] children = impl.getChildTargetModuleID();
      int length = children != null ? children.length : 0;
      for(int n = 0; n < length; n ++)
      {
         TargetModuleIDImpl child = (TargetModuleIDImpl) children[n];
         childModuleIDs.add(new SerializableTargetModuleID(this, child));
      }
   }
   public SerializableTargetModuleID(SerializableTargetModuleID parent,
      String moduleID, int moduleType, boolean isRunning)
   {
      parentModuleID = parent;
      this.moduleID = moduleID;
      this.moduleType = moduleType;
      this.isRunning = isRunning;
   }

   public SerializableTargetModuleID getParentModuleID()
   {
      return parentModuleID;
   }
   public void addChildTargetModuleID(SerializableTargetModuleID child)
   {
      childModuleIDs.add(child);
   }
   public void clearChildModuleIDs()
   {
      childModuleIDs.clear();
   }
   public SerializableTargetModuleID[] getChildModuleIDs()
   {
      SerializableTargetModuleID[] ids = new SerializableTargetModuleID[childModuleIDs.size()];
      childModuleIDs.toArray(ids);
      return ids;
   }

   public String getModuleID()
   {
      return moduleID;
   }

   public int getModuleType()
   {
      return moduleType;
   }

   public boolean isRunning()
   {
      return isRunning;
   }
   public void setRunning(boolean flag)
   {
      this.isRunning = flag;
   }

   public String toString()
   {
      return "SerializableTargetModuleID{" +
         "parentModuleID=@" + System.identityHashCode(parentModuleID) +
         ", childModuleIDs=" + childModuleIDs +
         ", moduleID='" + moduleID + "'" +
         ", moduleType=" + moduleType +
         ", isRunning=" + isRunning +
         "}";
   }

}
