Forum OpenACS Development: callback API

Collapse
Posted by Iuri Sampaio on
I have written ns_log's before, after and inside a callback proc but it seems the error is somewhere else. Right after the end of the proc but before the next ns_log. Such as in:

ns_log Notice "**** before callback "

callback contact::special_attributes::ad_form_save -party_id $organization_party_id -form "party_ae"

ns_log Notice "**** after callback "

Inside the api source code i wrote:
{...
ns_log Notice "Running callback contact::special_attributes::ad_form_save -impl contacts "

...

ns_log Notice "End callback contact::special_attributes::ad_form_save -impl contacts "
}

what does it run after a callback proc ?

Collapse
2: Re: callback API (response to 1)
Posted by Jim Lynch on
Could you post the complete stack trace
Collapse
3: Re: callback API (response to 1)
Posted by Jim Lynch on
https://openacs.org/api-doc/proc-view?proc=callback&source_p=1&version_id=

Take a look at the above link. It should show the code for "callback", and I noticed a coupla places where it throws an error.

Refer to the similar link for the openacs instance in question in case (1) the code was changed in either place, or (2) the versions differ.

Collapse
4: Re: callback API (response to 3)
Posted by Iuri Sampaio on
Jim, yes i see them as well. The error was none of those.
The complete stack trace of the error is right bellow

I have wrote ns_logs in the entire API to track down what is causing it. However i have not found yet.

Furthermore in the page that calls the callback i have:

ns_log Notice "**** Before callback "

callback contact::special_attributes::ad_form_save -party_id $organization_party_id -form "party_ae"

ns_log Notice "**** after callback "

[09/Feb/2010:21:11:18][2297.3008273328][-default:0-] Notice: Inside else lsearch!
[09/Feb/2010:21:11:18][2297.3008273328][-default:0-] Notice: AFTER QRY
[09/Feb/2010:21:11:18][2297.3008273328][-default:0-] Notice: END API
[09/Feb/2010:21:11:18][2297.3008273328][-default:0-] Error: Ns_PgExec: result status: 7 message: ERROR: insert or update on table "user_preferences" violates foreign key constraint "user_preferences_user_id_fk"
DETAIL: Key (user_id)=(2491) is not present in table "users".

[09/Feb/2010:21:11:18][2297.3008273328][-default:0-] Warning: /contacts/add/organization has no doc(title) set.
[09/Feb/2010:21:11:18][2297.3008273328][-default:0-] Error: POST http://192.168.1.15/contacts/add/organization?
referred by "http://192.168.1.15/contacts/add/organization?object%5fid%5ftwo=&group%5fids=2198+%2d2&object%5ftype=organization&role%5ftwo=";
Database operation "dml" failed
(exception ERROR, "ERROR: insert or update on table "user_preferences" violates foreign key constraint "user_preferences_user_id_fk"
DETAIL: Key (user_id)=(2491) is not present in table "users".
")

while executing
"ns_pg_bind dml nsdb0 {
insert into user_preferences (user_id, locale)
select :party_id as user_id, :locale as locale
from dual..."
("uplevel" body line 1)
invoked from within
"uplevel $ulevel [list ns_pg_bind $type $db $sql]"
invoked from within
"db_exec dml $db $full_statement_name $sql"
("uplevel" body line 2)
invoked from within
"uplevel 1 $code_block "
invoked from within
"db_with_handle -dbn $dbn db {
db_exec dml $db $full_statement_name $sql
}"
(procedure "db_dml" line 100)
invoked from within
"db_dml insert_preference {
insert into user_preferences (user_id, locale)
select :party_id as user_id, :locale as locale
from ..."
(procedure "::callback::contact::special_attributes::ad_form_save::impl:..." line 18)
invoked from within
"::callback::contact::special_attributes::ad_form_save::impl::contacts_locale -party_id 2491 -form party_ae"
("uplevel" body line 1)
invoked from within
"::uplevel 1 $procname $args"
invoked from within
"callback contact::special_attributes::ad_form_save -party_id $organization_party_id -form "party_ae""
("foreach" body line 74)
invoked from within
"foreach object_type $object_types {
if { $object_type eq "person" } {

if { ![exists_and_not_null url] } {
set url ""
}

# Initiali..."
("uplevel" body line 13)
invoked from within
"uplevel #$level $new_data"
(procedure "ad_form" line 626)
invoked from within
"ad_form -extend -name party_ae -export {return_url} -on_request {

if { $object_type eq "person" } {
set required_attributes [list first_names ..."
("uplevel" body line 174)
invoked from within
"uplevel {
ad_page_contract {

@author Matthew Geddert mailto:openacs@geddert.com
@creation-date 2004-07-28
@cvs-id $Id: contact-add.tcl,v 1.57 20..."
(procedure "code::tcl::/usr/local/aolserver/servers/openacs/packages/con..." line 2)
invoked from within
"code::tcl::$__adp_stub"
("uplevel" body line 12)
invoked from within
"uplevel {

if { [file exists $__adp_stub.tcl] } {

# ensure that data source preparation procedure exists and is up-to-date
adp_init t..."
(procedure "adp_prepare" line 2)
invoked from within
"adp_prepare"
invoked from within
"template::adp_parse [file root [ad_conn file]] {}"
(procedure "adp_parse_ad_conn_file" line 6)
invoked from within
"$handler"
("uplevel" body line 2)
invoked from within
"uplevel $code"
invoked from within
"ad_try {
$handler
} ad_script_abort val {
# do nothing
}"
invoked from within
"rp_serve_concrete_file [ad_conn file]"
(procedure "rp_serve_abstract_file" line 60)
invoked from within
"rp_serve_abstract_file $path"
(procedure "rp_internal_redirect" line 26)
invoked from within
"rp_internal_redirect contact-add"
("add/organization" arm line 5)
invoked from within
"switch $file_url {
"add/person" {
if { ![ns_queryexists object_type] } {
rp_form_put object_type "person"
..."
invoked from within
"if { $party_is_contact_p } {
contact::require_visiblity -party_id $party_id

if { [llength $file_list] == "1" } {
# we..."
(file "/usr/local/aolserver/servers/openacs/packages/contacts/www/index.vuh" line 24)
invoked from within
"source [ad_conn file]"
(procedure "rp_handle_tcl_request" line 3)
invoked from within
"$handler"
("uplevel" body line 2)
invoked from within
"uplevel $code"
invoked from within
"ad_try {
$handler
} ad_script_abort val {
# do nothing
}"
invoked from within
"rp_serve_concrete_file [ad_conn file]"
(procedure "rp_serve_abstract_file" line 60)
invoked from within
"rp_serve_abstract_file -noredirect -nodirectory -extension_pattern ".vuh" "$root$prefix""
("uplevel" body line 3)
invoked from within
"uplevel $code"
invoked from within
"ad_try {
ad_conn -set path_info [string range [ad_conn extra_url] [expr {[string length $prefix] - 1}] end]
rp_serve_..."

Collapse
5: Re: callback API (response to 1)
Posted by Dave Bauer on
Check your stack trace for the ultimate cause of the error:

"db_dml insert_preference {
insert into user_preferences (user_id, locale)
select :party_id as user_id, :locale as locale
from ..."
(procedure "::callback::contact::special_attributes::ad_form_save::impl:..." line 18)
invoked from within

Find the code where "insert_preferences" query is called.

Collapse
6: Re: callback API (response to 5)
Posted by Iuri Sampaio on
I found part of code that is causing the error.
It has even a commentary about it.

# Create the entry in user_preferences.
# This only works if we drop the constraint on users.
db_dml insert_preference {
insert into user_preferences (user_id, locale)
select :party_id as user_id, :locale as locale
from dual
where not exists (select 1 from user_preferences where user_id = :party_id)
}

The error happens because it searches for an user_id while the object_type of the respective object_id in this context is an organization instead. So it looks for an inexistent user.

I have to add an organization also has a member of the system in order to fix the error. Also this is what i want to achieve: "having orgs as members"

I put the API to create a new user:
#Create user with user_id equals to organization_party_id
db_transaction {
array set creation_info [auth::create_user \
-user_id $organization_party_id \
-first_names $name \
-last_name $name \
-email $email \
]
}

as expected, that caused a duplicated acs_object_id . Since organizations were also inserted on table acs_objects before by the API organization::new as object_type 'organization'

it is clear to force organization as user in the system causes too many collateral problems.

What would be a solution then?

Database operation "0or1row" failed
(exception ERROR, "ERROR: duplicate key violates unique constraint "acs_objects_object_id_pk"
CONTEXT: SQL statement "insert into acs_objects (object_id, object_type, title, package_id, context_id, creation_date, creation_user, creation_ip, security_inherit_p) values ( $1 , $2 , $3 , $4 , $5 , $6 , $7 , $8 , $9 )"
PL/pgSQL function "acs_object__new" line 39 at SQL statement
PL/pgSQL function "party__new" line 12 at assignment
PL/pgSQL function "person__new" line 14 at assignment
PL/pgSQL function "acs_user__new" line 28 at assignment
PL/pgSQL function "acs__add_user" line 21 at assignment
")

while executing
"ns_pg_bind 0or1row nsdb0 {

select acs__add_user(
:user_id,
'user',
now(),
..."
("uplevel" body line 1)
invoked from within
"uplevel $ulevel [list ns_pg_bind $type $db $sql]"
invoked from within
"db_exec 0or1row $db $full_statement_name $sql"
("uplevel" body line 8)
invoked from within
"uplevel 1 $code_block "
invoked from within
"db_with_handle -dbn $dbn db {
# plsql calls that are simple selects bypass the plpgsql
# mechanism for creating anonymous fun..."
(procedure "db_exec_plsql" line 57)
invoked from within
"db_exec_plsql user_insert {}"
("uplevel" body line 3)
invoked from within
"uplevel 1 $transaction_code "
(procedure "db_transaction" line 1)
invoked from within
"db_transaction {

Collapse
7: Re: callback API (response to 6)
Posted by Iuri Sampaio on
I can just write this code and it will work fine!

if { [string equal "organization" $object_type] } {
set org_id [db_nextval acs_object_id_seq]
set last_name "$cnpj"

#Create user with user_id equals to org_id
db_transaction {
array set creation_info [auth::create_user \
-user_id $org_id \
-first_names $name \
-last_name $cnpj \
-email $email \
]
}
}

That solves the issue for now 😊

Thanks Dave and Jim.