// 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) 2001-2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
// 
// Contributor(s): 

importPackage(Packages.com.silverwrist.venice.core);
importPackage(Packages.com.silverwrist.venice.except);
importPackage(Packages.com.silverwrist.venice.ui);
importPackage(Packages.com.silverwrist.venice.ui.helpers);
importPackage(Packages.com.silverwrist.venice.ui.view);

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

rc = null;
try
{ // retrieve the user profile and save it off
  prof = rinput.user.getProfile(username);
  rinput.setRequestAttribute("profile",prof);

  // determine the full name of the user and save it off
  buf = new java.lang.StringBuffer(prof.givenName);
  if (!(vlib.emptyString(prof.namePrefix)))
  { // insert the prefix at the beginning of the string
    if (buf.length()>0)
      buf.insert(0," ");
    buf.insert(0,prof.namePrefix);

  } // end if

  tmp = String.fromCharCode(prof.middleInitial);
  if (tmp!=" ") // append the middle initial
    buf.append(" ").append(tmp).append(".");

  if (!(vlib.emptyString(prof.familyName)))
    buf.append(" ").append(prof.familyName);
  if (!(vlib.emptyString(prof.nameSuffix)))
    buf.append(" ").append(prof.nameSuffix);

  sfullname = buf.toString().trim();
  if (vlib.emptyString(sfullname))
    sfullname = username;
  rinput.setRequestAttribute("fullname",sfullname);

  // determine the last line of the address and save it off
  if (vlib.emptyString(prof.locality))
  { // allocate stringbuffer based on region
    if (vlib.emptyString(prof.region))
      buf = new java.lang.StringBuffer();
    else
      buf = new java.lang.StringBuffer(prof.region);

  } // end if
  else if (vlib.emptyString(prof.region))
    buf = new java.lang.StringBuffer(prof.locality);
  else
  { // tack together both locality and region
    buf = new java.lang.StringBuffer(prof.locality);
    buf.append(", ").append(prof.region);

  } // end else

  if (!(vlib.emptyString(prof.postalCode)))
  { // append the postal code
    buf.append(" ").append(prof.postalCode);

  } // end if

  rinput.setRequestAttribute("address.lastline",buf.toString().trim());

  scountry = prof.fullCountry;
  if (prof.country.equals("XX"))
    scountry = "";
  rinput.setRequestAttribute("country",scountry);

  // Save off the user photo tag.
  html = vlib.queryHTMLRendering(rinput);
  rinput.setRequestAttribute("user.photo",html.getUserPhotoTag(prof.photoURL));

  // Create a view object and send it out.
  rc = new JSPView("User Profile - " + username,"user_profile.jsp");

} // end try
catch (e)
{ // handle an exception thrown by the profile code
  etype = vlib.exceptionType(e) + "";
  if (etype.match("DataException"))
    rc = new ErrorBox("Database Error","Database error finding user: " + e.message,"top.js.vs");
  else
    rc = e;

} // end catch

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