// 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@silcom.com>,
// for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
// Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
// 
// Contributor(s): 

importPackage(java.util);
importPackage(Packages.com.silverwrist.util);
importPackage(Packages.com.silverwrist.venice.core);
importPackage(Packages.com.silverwrist.venice.except);
importPackage(Packages.com.silverwrist.venice.ui);
importPackage(Packages.com.silverwrist.venice.ui.conf);
importPackage(Packages.com.silverwrist.venice.ui.helpers);

// get the request object
rinput = bsf.lookupBean("request");

if (rinput.isFileParam("target"))
{ // the target should be a normal parameter
  logger.error("Internal Error: 'target' should be a normal param");
  vlib.output(new ErrorBox(null,"Internal Error: 'target' should be a normal param","top.js.vs"));
  vlib.done();

} // end if

// Get the operations target.
target = rinput.getParameter("target");
logger.debug("Target URL: " + target);
rinput.location = target;

// also check on file parameter status
if (!(rinput.isFileParam("thefile")))
{ // bogus file parameter
  logger.error("Internal Error: 'thefile' should be a file param");
  vlib.output(new ErrorBox(null,"Internal Error: 'thefile' should be a file param",target));
  vlib.done();

} // end if

// get the community
comm = rinput.getCommunity(true,target);

// get the current conference
currc = new CurrentConference(rinput);
conf = currc.getConference(true,target);

// get the message we're attaching to
msg = currc.getMessageParam("msg",true,target);

rc = null;
try
{ // do the attachment!
  msg.attachData(rinput.getParameterType("thefile"),rinput.getParameter("thefile"),
                 rinput.getParameterSize("thefile"),rinput.getParameterDataStream("thefile"));

  // now bounce us where we wanted to go in the first place
  rc = new Redirect(target,LinkTypes.SERVLET);

} // end try
catch (e)
{ // failure in setting attachment
  etype = vlib.exceptionType(e) + "";
  if (etype.match("DataException"))
    rc = new ErrorBox("Database Error","Database error saving attachment: " + e.message,target);
  else if (etype.match("AccessError"))
    rc = new ErrorBox("Access Error",e.message,target);
  else if (etype.match("ServletMultipartException"))
    rc = new ErrorBox("Internal Error",e.message,target);
  else
    rc = e;

} // end catch

vlib.output(rc);  // all done!
