Get class from space and retrieve list of attached objects..

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Get class from space and retrieve list of attached objects..

KevinC
I have a class in one of my spaces that has instances of itself attached. Eg.

MySpace
  MySpace.TopicsClass
    MySpace.TopicsClass[0]
    MySpace.TopicsClass[1]


How can I get the class from the space? I've tried:

#set ($hql = ", BaseObject as obj where obj.name = doc.fullName and obj.className = 'FranchiseTax.TopicsClass' and doc.space='FranchiseTax'")
#set($topics = $xwiki.searchDocuments($hql))

Topics comes back with one item, which I can see by displaying $topics.get(0) and it prints out MySpace.TopicsClass. So it appears to have returned a string name of the document, however when I try:

#set($topicObj = $xwiki.getDocument($topics.get(0)))

Nothing is set into $topicObj. Do I have to do something different if the thing I am trying to get is a class and not a document? Once I have the class how do I get the collection of associated objects?


Thanks!
.:. Kevin
Reply | Threaded
Open this post in threaded view
|

Re: Get class from space and retrieve list of attached objects..

Sergiu Dumitriu-2
Kevin_C wrote:

> I have a class in one of my spaces that has instances of itself attached. Eg.
>
> MySpace
>   MySpace.TopicsClass
>     MySpace.TopicsClass[0]
>     MySpace.TopicsClass[1]
>
>
> How can I get the class from the space? I've tried:
>
> #set ($hql = ", BaseObject as obj where obj.name = doc.fullName and
> obj.className = 'FranchiseTax.TopicsClass' and doc.space='FranchiseTax'")
> #set($topics = $xwiki.searchDocuments($hql))
>
> Topics comes back with one item, which I can see by displaying
> $topics.get(0) and it prints out MySpace.TopicsClass. So it appears to have
> returned a string name of the document, however when I try:
>
> #set($topicObj = $xwiki.getDocument($topics.get(0)))
>
> Nothing is set into $topicObj. Do I have to do something different if the
> thing I am trying to get is a class and not a document? Once I have the
> class how do I get the collection of associated objects?

$xwiki.searchDocuments returns a list of document names (well, it is called
"search documents"), and not objects or classes. The query does filter only
the documents that do have the objects you're interested in, you'll just have
to access them using the API, starting from the document name.

## Iterate over all documents having that type of objects
#foreach($documentName in $xwiki.searchDocuments($hql))
## Here documentName is a string, the name of the document
  #set($documentObject = $xwiki.getDocument($documentName))
  ## Now you have a Document API, http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/api/Document.html
  ## Iterate over the objects found in this document
  #foreach($object in $documentObject.getObjects('FranchiseTax.TopicsClass'))
    ## Display object properties with one of:
    $object.display('propertyname', 'view')
    $object.display('propertyname', 'edit')
    $documentObject.display('propertyname', $object
    ## ... or other display methods from the Document class.
  #end
#end

--
Sergiu Dumitriu
http://purl.org/net/sergiu/
_______________________________________________
users mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: Get class from space and retrieve list of attached objects..

Jean Couteau
In reply to this post by KevinC
Hi Kevin,

I think I have done something quite similar to what you want to do :

#set ($hql = ", BaseObject as obj where obj.name=doc.fullName and
obj.className='XWiki.DomainClass'")

#foreach ($item in $xwiki.searchDocuments($hql))
  #if($item==$doc)
    #foreach ($domain in
$xwiki.getDocument($item).getObjects("XWiki.DomainClass"))
## put there your instructions, $domain contains all the DomainClass
objects which are on my page
    #end
  #end
#end

Kevin_C a écrit :

> I have a class in one of my spaces that has instances of itself attached. Eg.
>
> MySpace
>   MySpace.TopicsClass
>     MySpace.TopicsClass[0]
>     MySpace.TopicsClass[1]
>
>
> How can I get the class from the space? I've tried:
>
> #set ($hql = ", BaseObject as obj where obj.name = doc.fullName and
> obj.className = 'FranchiseTax.TopicsClass' and doc.space='FranchiseTax'")
> #set($topics = $xwiki.searchDocuments($hql))
>
> Topics comes back with one item, which I can see by displaying
> $topics.get(0) and it prints out MySpace.TopicsClass. So it appears to have
> returned a string name of the document, however when I try:
>
> #set($topicObj = $xwiki.getDocument($topics.get(0)))
>  
#set($topicObj = $xwiki.getObject($topics) should return you one object
#set($topicObj = $xwiki.getObjects($topics) should return you all objects


> Nothing is set into $topicObj. Do I have to do something different if the
> thing I am trying to get is a class and not a document? Once I have the
> class how do I get the collection of associated objects?
>
>
> Thanks!
> .:. Kevin
>  


--
----
Jean Couteau
Code Lutin - http://www.codelutin.com
44 Bd des Pas Enchantés - 44230 St-Sébastien/Loire
Tél : 02 40 50 29 28 - Fax : 09 59 92 29 28

_______________________________________________
users mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: Get class from space and retrieve list of attached objects..

KevinC
I thought your other post looked like what I was trying but I wasn't getting the results I expected with any of the getObject* methods.. It turns out I think I was getting fooled by a toString() method. My problem was this:

#set($hql = ", BaseObject as obj where obj.name = doc.fullName and obj.className = '${doc.space}.TopicsClass'")
#set($results = $xwiki.searchDocuments($hql))

$results.get(0) ##This outputs FranchiseTax.TopicsClass

as does:

$xwiki.getDocument($results.get(0)) ## Also outputs FranchiseTax.TopicsClass but this one is actually a Document object. I was expecting an object Hash or something other than the same result as the previous line.

With the document I was able to:

$xwiki.getDocument($results.get(0)).getObjects($results.get(0)).size()



Thanks for the help!!
.:. Kevin
Reply | Threaded
Open this post in threaded view
|

Re: Get class from space and retrieve list of attached objects..

KevinC
In reply to this post by Sergiu Dumitriu-2
>$xwiki.searchDocuments returns a list of document names (well, it is called
>"search documents"), and not objects or classes. The query does filter only
>the documents that do have the objects you're interested in, you'll just have
>to access them using the API, starting from the document name.

That may be the case, but searchDocuments returns my class as a result when queried with:
#set($hql = ", BaseObject as obj where obj.name = doc.fullName and obj.className = '${doc.space}.TopicsClass'")
#set($results = $xwiki.searchDocuments($hql))

I guess the object I am looking at could just be a document holder for the class I created but it does show up in the Class list in the Class Editor which is why I referred to it as such.


Thanks again for the help!!
.:. Kevin