ad_page_contract {
	This page will accect an object_id and try to resolve the
	package dealing with this object and then invoking a
	registered proc through the service contract which will
	return the package-specific page to display the object.

	This can help in listing objects without having to resolve
	the correct url.

	@see categories/doc/o.html for more details on what why and how
} {
}

if ![regexp {^([0-9]+)(-(.+))?$} [ad_conn path_info] match object_id dummy anchor] {
    ad_return_warning "Invalid object_id" [subst {
	We could not find this object. This probably means that
	the link you have clicked is broken.
	<p>We are sorry for this inconvenience.
    }]
    return
}

if ![empty_string_p $anchor] { set anchor "#$anchor" }

db_0or1row object_data {
    select o.object_type, n.object_name, n.package_id
    from acs_objects o, acs_named_objects n
    where o.object_id = :object_id
    and n.object_id = o.object_id
}

if ![info exists object_type] {
   ad_return_warning "Invalid object ID" [subst {
	    The object ID $object_id doesn't exist.
	    <p>We are sorry for this inconvenience.
   }]
   return
}

set package_urls [site_node::get_url_from_object_id -object_id $package_id]

if {[llength $package_urls] == 0} {
    ad_return_warning "Unable to resolve url" [subst {
	Unfortunately we couldn't come up with a url for [ad_quotehtml $object_name].
	<p>
	We are sorry for this inconvenience.
    }]
    return
}

# If there is more than one URL, we pick the first one.
set pkg_url [lindex $package_urls 0]
set impl "$object_type\_idhandler"

if {![acs_sc_binding_exists_p AcsObject $impl]} {
    ad_return_warning "Unable to resolve url" [subst {
	Unfortunately we couldn't come up with a url for [ad_quotehtml $object_name].
	<p>
	We are sorry for this inconvenience.
    }]
    return
}

set page_url [acs_sc::invoke -contract AcsObject -operation PageUrl -impl $impl -call_args $object_id]

ad_returnredirect "$pkg_url$page_url$anchor"