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

importPackage(java.lang);
importPackage(Packages.com.silverwrist.util);
importClass(Packages.com.silverwrist.dynamo.Namespaces);
importPackage(Packages.com.silverwrist.dynamo.iface);
importPackage(Packages.com.silverwrist.dynamo.security);
importPackage(Packages.com.silverwrist.dynamo.util);
importClass(Packages.com.silverwrist.venice.VeniceNamespaces);
importPackage(Packages.com.silverwrist.venice.content);

req = bsf.lookupBean("request");            // get request
req_help = bsf.lookupBean("request_help");  // get request helper
me = vlib.getUser(req);                     // get self
user = cast.queryDynamoUser(req.getObject("temp:","user"));  // get user being displayed

// are we allowed to view "private" data?
view_all = me.equals(user);
if (!view_all)
{ // look to see if we're an administrator
  srm = cast.querySecurityReferenceMonitor(req_help.getRequestObject(Namespaces.DYNAMO_OBJECT_NAMESPACE,
                                                                     "srm"));
  view_all = srm.getGlobalAcl().testPermission(me,Namespaces.DYNAMO_USER_INFO_NAMESPACE,"view.all");

} // end if

// create the view object
view = new VelocityView("User Profile: " + user.getName(),"user/profile.vm");
view.setPageQID("user/" + user.getName());

// get the privacy options for this user
priv = cast.toOptionSet(user.getObject(VeniceNamespaces.USER_PROFILE_NAMESPACE,"privacy"));

// get the user photo renderer
uphoto = vcast.queryRenderImage(req_help.getRequestObject(VeniceNamespaces.DYNAMO_OBJECT_NAMESPACE,
                                                          "venice-userphoto"));

// set the view parameters
view.setParameter("uid",cast.toIntegerObject(user.getUID()));
view.setParameter("username",user.getName());
photo_url = PropertyUtils.getPropertyNoErr(user,VeniceNamespaces.USER_PROFILE_NAMESPACE,"url.photo");
if (photo_url==null)
{ // get the "No Photo Available" default
  globals = vcast.getGlobalPropertiesStore(req);
  url = globals.getObject(VeniceNamespaces.CONTENT_LAF_NAMESPACE,"user.nophoto.url");
  urltype = globals.getObject(VeniceNamespaces.CONTENT_LAF_NAMESPACE,"user.nophoto.url.type");
  rewriter = cast.queryURLRewriter(req);
  photo_url = rewriter.rewriteURL(urltype,url);

} // end if

view.setParameter("photo",uphoto.getRenderingObject(photo_url));
view.setParameter("date_create",user.creationDate);
view.setParameter("date_lastlogin",user.lastAccessDate);
view.setParameter("date_lastupdate",user.getObject(VeniceNamespaces.USER_PROFILE_NAMESPACE,"last.update"));

buf = new StringBuffer();
s = PropertyUtils.getPropertyNoErr(user,VeniceNamespaces.USER_PROFILE_NAMESPACE,"name.prefix");
if (s!=null)
  buf.append(s).append(" ");
buf.append(user.getObject(VeniceNamespaces.USER_PROFILE_NAMESPACE,"name.given")).append(" ");
ch = PropertyUtils.getPropertyNoErr(user,VeniceNamespaces.USER_PROFILE_NAMESPACE,"name.mi");
if (ch!=null)
  buf.append(ch).append(". ");
buf.append(user.getObject(VeniceNamespaces.USER_PROFILE_NAMESPACE,"name.family"));
s = PropertyUtils.getPropertyNoErr(user,VeniceNamespaces.USER_PROFILE_NAMESPACE,"name.suffix");
if (s!=null)
  buf.append(" ").append(s);
view.setParameter("fullname",buf.toString());

if (view_all || !(priv.get(4)))
  view.setParameter("email",user.getEMailAddress());
view.setParameter("url",PropertyUtils.getPropertyNoErr(user,VeniceNamespaces.USER_PROFILE_NAMESPACE,
                                                       "url.homepage"));
view.setParameter("company",PropertyUtils.getPropertyNoErr(user,VeniceNamespaces.USER_PROFILE_NAMESPACE,
                                                           "company.name"));
if (view_all || !(priv.get(0)))
{ // add the address lines
  view.setParameter("addr1",PropertyUtils.getPropertyNoErr(user,VeniceNamespaces.USER_PROFILE_NAMESPACE,
                                                           "address.1"));
  view.setParameter("addr2",PropertyUtils.getPropertyNoErr(user,VeniceNamespaces.USER_PROFILE_NAMESPACE,
                                                           "address.2"));

} // end if

buf = new StringBuffer(user.getObject(VeniceNamespaces.USER_PROFILE_NAMESPACE,"locality").toString());
buf.append(", ").append(user.getObject(VeniceNamespaces.USER_PROFILE_NAMESPACE,"region"));
buf.append(" ").append(user.getObject(VeniceNamespaces.USER_PROFILE_NAMESPACE,"postal.code"));
view.setParameter("addr_last",buf.toString());
view.setParameter("country",cast.toCountry(user.getObject(VeniceNamespaces.USER_PROFILE_NAMESPACE,
                                                          "country")).getName());
if (view_all || !(priv.get(1)))
  view.setParameter("phone",PropertyUtils.getPropertyNoErr(user,VeniceNamespaces.USER_PROFILE_NAMESPACE,
                                                           "phone.voice"));
if (view_all || !(priv.get(3)))
  view.setParameter("fax",PropertyUtils.getPropertyNoErr(user,VeniceNamespaces.USER_PROFILE_NAMESPACE,
                                                         "phone.fax"));
if (view_all || !(priv.get(2)))
  view.setParameter("mobile",PropertyUtils.getPropertyNoErr(user,VeniceNamespaces.USER_PROFILE_NAMESPACE,
                                                            "phone.mobile"));
view.setParameter("description",PropertyUtils.getPropertyNoErr(user,VeniceNamespaces.USER_PROFILE_NAMESPACE,
                                                               "description"));
if (!(me.isAnonymous() || user.isAnonymous()))
  view.setParameter("quick_email",Boolean.TRUE);

dynamo.scriptOutput(view);  // all done!
