Forum OpenACS Q&A: Learning to use XOTcl

Collapse
Posted by Richard Hamilton on

I have been reading:

http://www.matuska.org/martin/doc/xotcl-openacs-2007.pdf

Whilst eagerly looking forward to having a play with XoWiki version 2, I thought I'd start learning to use XOTcl (amazing by the way!), but have stumbled in the initial stages.

I wanted to see if I could instantiate an object from the OpenACS content repository, section 4.2 of above doc.


server1:nscp 17> ::xo::db::CrClass get_name -item_id 1054
base_01 - image.jpg
server1:nscp 18> ::xo::db::CrItem instantiate -item_id 1054

server1:nscp 22> 1054 info procs
invalid command name "1054"

Not sure why I get a blank line when I call the instantiate method, no error, just blank. I have tried to look up the instantiate method in the OpenACS API browser and the XOTcl API browser but I cannot find it. Has it been superceded or am I doing something wrong?

Regards
Richard

Collapse
2: Re: Learning to use XOTcl (response to 1)
Posted by Gustaf Neumann on
Seems like Martin Matuskas description was predating the xotcl-core db interface release form 2007
https://openacs.org/forums/message-view?message_id=1165841

See as well:
https://openacs.org/xowiki/xotcl-core-db

In your example, use

::xo::db::CrClass get_name -item_id 1054
::xo::db::CrClass get_instance_from_db -item_id 1054

The object browser shows the full class structure:
http://alice.wu-wien.ac.at:8000/xotcl/show-object?show_methods=1&show_source=0&object=%3a%3axo%3a%3adb%3a%3aCrClass

Collapse
3: Re: Learning to use XOTcl (response to 2)
Posted by Richard Hamilton on

Thank you Gustaf. That has been extremely helpful.

Do you happen to know why this happens?

server1:nscp 11> set o [::xo::db::CrClass get_instance_from_db -item_id 1054]
invalid command name "::xo::db::image"
server1:nscp 12> set o [::xo::db::CrClass get_instance_from_db -revision_id 1055]
invalid command name "::xo::db::image"

I remember reading somewhere that the db api requires that the content_type be set so I had set the content type to 'image' before trying this. I also tried setting the content type to '::xowiki::Form' but received a similar result: invalid command name "::xo::db::xowikiForm"

Clearly it has something to do with the content_type but I have not yet worked out what is missing in the database.

Regards
Richard

Collapse
4: Re: Learning to use XOTcl (response to 3)
Posted by Gustaf Neumann on
Changing the content-type is not the way to go; it would be pretty invasive to force people to change the content-type to use a certain api.

The original problem is that the object-relational interface looks for a class to handle the images, but cannot not find it (since there is no such class defined). The object-relational api contains "get_class_from_db" to generate such a class on the fly from the OpenACS meta model in the database. However, this is only implemented for plain OpenACS object types, but not for types in the content repository (such as "image"). I have looked around in my databases, and found no acs_objects of object_type "image", so photo-album seems to be more or less the only application using this type.

Adding the type is not much code, so i added it just now to xotcl-core http://fisheye.openacs.org/changelog/OpenACS/?cs=MAIN%3Agustafn%3A20101106090338

With that fetching images via "get_instance_from_db" is supposed to work.

Collapse
5: Re: Learning to use XOTcl (response to 4)
Posted by Richard Hamilton on
That sorted that out perfectly, thank you. It also gave me a guided tour of some more of the code which I found very useful.

Regards
Richard

Collapse
6: Re: Learning to use XOTcl (response to 4)
Posted by Richard Hamilton on
I have been playing with the ::xo::db API (purely to improve my understanding rather than with a definite objective at this stage) but I have a few questions.

1) I have created an ::xo::db::image object from the database and as an experiment have tried changing the parent_id to that of my xowiki instance (i.e. ::xowiki::Formpage), and the object_type to ::xowiki::File. I also added the live revision_id and creator name to the xowiki_page table, and the revision_id to the xowiki_file table, but the image still doesn't appear in my xowiki instance. Is this because objects of class ::xo::db::image do not fall within xowiki's realm?

2) I tried adding an attribute that is not part of the image content_type attribute list. I was expecting an error. To my astonishment it works, AND it goes into the persistent layer. How is this achieved? Does the ::xo::db layer automatically interface with AMS for additional attributes - even when I haven't defined slots?

server1:nscp 2> ::xo::db::CrClass get_instance_from_db -item_id 1272
::1272
server1:nscp 16> ::1272 set creator {Richard Hamilton}
Richard Hamilton
server1:nscp 17> ::1272 save
1272
server1:nscp 3> ::1272 serialize
::xo::db::image create ::1272 -noinit \
        -set creator {Richard Hamilton} \
        -set object_type image \
        -set height 100 \
        -set creation_date {2010-11-01 12:03:55.686724+00} \
        -set creation_user 667 \
        -set object_id 1273 \
        -set description thumbnail \
        -set text /12/72/1273 \
        -set package_id 966 \
        -set width 150 \
        -set nls_language {} \
        -set name {th_19 - nicepic.jpg} \
        -set mime_type image/jpeg \
        -set title {th_19 - nicepic.jpg} \
        -set publish_status ready \
        -set item_id 1272 \
        -set revision_id 1401 \
        -set last_modified {2010-11-06 19:47:21.542161+00} \
        -set publish_date {2010-11-01 12:03:55.686724+00} \
        -set modifying_user {} \
        -set parent_id 1268 \
        -set image_id 1273

3) How do I now remove that extra attribute from the persistent storage layer?

4) As an exercise, how would I go about creating a duplicate of an image object from the content repository as an ::xowiki::File so that it appears in an xowiki instance? Could this be achieved using mixins by creating a subclass of both ::xo::db::image and ::xowiki::File?

Sorry so many questions, but this is such a revelation for me that I want to try to understand as much as possible! 😊

Regards
Richard

Collapse
7: Re: Learning to use XOTcl (response to 1)
Posted by Dave Bauer on
I am not sure I understand the question.

I don't recommend trying to change the object type of an object once created. There has to be a better way.

I don't see any inherent reason why xowiki could not access any acs_object in the system, so I am not sure what you are trying to accomplish.

Most likely what you want is an xowiki::Image type that subtypes image. This should allow you to extend it with any behavior specific to xowiki that you might need.

Collapse
8: Re: Learning to use XOTcl (response to 7)
Posted by Richard Hamilton on
In order for objects to be visible to xowiki, I thought they would have to subtype a ::xowiki::CrClass so that they have the necessary characteristics to be accessible.

What I think we want to do is have a means of uploading image files to a folder within xowiki (as a bulk upload in a tar or zip file eventually) and have the CR recognise them as images and apply the specified resizing according to a parameter set either in the xowiki instance or passed in the includelet declaration.

I am not clear at this stage how that should best be done.

(I wasn't actually proposing changing the object type as any kind of solution, I was just playing around to try to understand how objects in the CR might be made accessible to xowiki. Trying the learn by trial and error really. More error than trial I guess! The oo stuff is new to me so I am not able to think oo clearly yet 😊 ).

Collapse
9: Re: Learning to use XOTcl (response to 4)
Posted by Richard Hamilton on

The object with item_id 989 is an xowiki instance

server1=# select item_id, name, content_type from cr_items where parent_id=989;
 item_id |       name        |   content_type
---------+-------------------+------------------
    1035 | en:weblog         | ::xowiki::Page
    1038 | en:weblog-portlet | ::xowiki::Object
     991 | en:index          | ::xowiki::Page
    1000 | en:gallery_demo   | ::xowiki::Page
    1027 | file:2080.jpg     | ::xowiki::File
    1382 | file:01.jpg       | ::xowiki::File
    1386 | file:02.jpg       | ::xowiki::File
    1390 | file:03.jpg       | ::xowiki::File
(8 rows)

server1=# select * from images where image_id in (1027,1382,1386,1390); image_id | width | height ----------+-------+-------- (0 rows)

So now I see that images uploaded to the content repository via xowiki are stored as instances of ::xowiki::File, whilst other images in the content repository have entries in the images table.

Presumably in order to leverage the built-in CR support for images and image resizing etc. I would need to create a subclass of ::xowiki::File that will have all the characteristics of ::xowiki::File objects, whilst also having entries in the images table and being subject to the image:: procedures shown here:

http://www.openacs.org/api-doc/proc-search?query_string=image%3A%3A&name_weight=5&param_weight=3&doc_weight=2

Is this correct, and should I also re-implement these image:: procs as methods for the ::xowiki::File::Image class?

Regards
Richard

Collapse
10: Re: Learning to use XOTcl (response to 1)
Posted by Dave Bauer on
Note images.image_id is a foreign key to cr_revisions.revision_id not cr_items.item_id.