/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@ricochet.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.ui.helpers;

import java.io.*;
import java.util.*;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.except.DataException;
import com.silverwrist.venice.except.InternalStateError;
import com.silverwrist.venice.ui.ContentExecute;
import com.silverwrist.venice.ui.RequestExec;

public class UserExportHelper implements ContentExecute
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private int m_length;
  private InputStream m_stm;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  public UserExportHelper(AdminOperations adm, Collection userdata) throws DataException, IOException
  {
    // Start by normalizing the user data.
    ArrayList tmp = new ArrayList(userdata.size());
    Iterator it;
    for (it=userdata.iterator(); it.hasNext(); )
    { // morph all the objects...
      Object o = it.next();
      if (o==null)
	continue;
      if (o instanceof AdminUserContext)
	tmp.add((AdminUserContext)o);
      else if (o instanceof UserContext)
	tmp.add(adm.getUserContext(((UserContext)o).getUID()));
      else if (o instanceof UserProfile)
	tmp.add(adm.getUserContext(((UserProfile)o).getUID()));
      else if (o instanceof UserFound)
	tmp.add(adm.getUserContext(((UserFound)o).getUID()));
      else if (o instanceof Number)
	tmp.add(adm.getUserContext(((Number)o).intValue()));
      else
	tmp.add(adm.getUserContext(o.toString()));

    } // end for

    StringWriter wr = new StringWriter();
    wr.write("<?xml version=\"1.0\"?>\n<venice-import-users>\n");
    for (it=tmp.iterator(); it.hasNext(); )
    { // get each user and export it
      AdminUserContext uc = (AdminUserContext)(it.next());
      uc.export(wr);

    } // end for

    wr.write("</venice-import-users>\n");
    try
    { // convert the output so it acts like an attachment
      byte[] ary = wr.toString().getBytes("UTF-8");
      m_length = ary.length;
      m_stm = new ByteArrayInputStream(ary);

    } // end try
    catch (UnsupportedEncodingException e)
    { // this is not supposed to happen
      throw new InternalStateError("encoding not found?!?!?!?");

    } // end catch

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface ContentExecute
   *--------------------------------------------------------------------------------
   */

  public void execute(RequestExec req) throws IOException
  {
    req.sendBinary("text/xml","exported-users.xml",m_length,m_stm);

  } // end execute

} // end class UserExportHelper
