From version 9.1
To version < 10.1
edited by slauriere
on 2020/01/28 16:47
Change comment: Migrated property [author] from class [XWiki.XWikiComments]

Summary

Details

Page properties
Parent
... ... @@ -1,0 +1,1 @@
1 +Extension.WebHome
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.XWikiGuest
1 +xwiki:XWiki.slauriere
Default language
... ... @@ -1,0 +1,1 @@
1 +en
Tags
... ... @@ -1,0 +1,1 @@
1 +groovy
Content
EXOExtensionCode.ExtensionClass[0]
ExtensionCode.ExtensionClass[0]
Description
... ... @@ -1,0 +1,48 @@
1 +{{warning}}
2 +Please refer instead to the [[Groovy Script Service Tutorial>>extensions:Extension.Script Module.GroovyScriptServiceTutorial]]. This snippet is deprecated since the introduction of [[wiki components>>extensions:Extension.WikiComponent Module]]. Also, bear in mind this snippet has a major downside: it requires the page containing it to be viewed at least once for the component to be correctly registered, hence the need of a scheduler, which is overkill.
3 +{{/warning}}
4 +
5 +XWiki components can be written in [[Java>>platform:DevGuide.WritingComponents]], but also using Groovy.
6 +
7 +{{code language=java}}
8 +{{groovy}}
9 +import org.xwiki.component.descriptor.*;
10 +import org.xwiki.script.service.*;
11 +import org.xwiki.context.*;
12 +import com.xpn.xwiki.web.*;
13 +
14 +class TestComponent implements ScriptService {
15 + org.xwiki.context.Execution e;
16 + List<String> getDocsByAuthor(String authorFullName) {
17 + def context = e.getContext().getProperty("xwikicontext");
18 + def hql = " where doc.author=(?)";
19 + def results = context.getWiki().getStore().searchDocuments(hql,0, 0, [authorFullName], context);
20 + print results;
21 + if (results.size() > 0) {
22 + return results;
23 + }
24 + return [];
25 + }
26 +}
27 +DefaultComponentDescriptor descriptor = new DefaultComponentDescriptor(implementation: TestComponent.class, role: ScriptService.class, roleHint: 'testComponent');
28 +DefaultComponentDependency dep = new DefaultComponentDependency(name: 'e', role: Execution.class, roleHint: 'default');
29 +descriptor.addComponentDependency(dep);
30 +def cm = services.component.getComponentManager(null)
31 +cm.unregisterComponent(ScriptService.class, 'testComponent');
32 +cm.registerComponent(descriptor);
33 +{{/groovy}}
34 +
35 +{{/code}}
36 +
37 +The component must be registered once by executing the code above: either manually view the page containing the code or put the code in a scheduled job with very frequent trigger to auto-register after the server restarts
38 +
39 +==Accessing the component from Velocity==
40 +
41 +{{code}}
42 +{{velocity}}
43 +$services.testComponent.getDocsByAuthor("XWiki.SimpleUser");
44 +{{/velocity}}
45 +{{/code}}
46 +
47 +==Other component implementations==
48 +* [[platform:DevGuide.GroovyNotificationTutorial]]
Type
... ... @@ -1,0 +1,1 @@
1 +snippet
Only Custom Installation instructions
... ... @@ -1,0 +1,1 @@
1 +No
Summary
... ... @@ -1,0 +1,1 @@
1 +Creates a component which retrieves all the documents with a certain author
License Name
... ... @@ -1,0 +1,1 @@
1 +GNU Lesser General Public License 2.1
Name
... ... @@ -1,0 +1,1 @@
1 +Create a component using Groovy
contributors
... ... @@ -1,0 +1,1 @@
1 +Oana Tabaranu
Authors
... ... @@ -1,0 +1,1 @@
1 +xwiki:XWiki.OanaTabaranu
XWiki.XWikiComments[0]
Date
... ... @@ -1,0 +1,1 @@
1 +2011-07-19 09:53:03.0
Author
... ... @@ -1,0 +1,1 @@
1 +xwiki:XWiki.VincentMassol
Comment
... ... @@ -1,0 +1,1 @@
1 +Note that in the future we'll have the ability to run scripts at wiki initialization (see http://jira.xwiki.org/jira/browse/XWIKI-4936) and a Groovy script registering a component would be able to be set up as an initialization script so that it's executed once when the wiki is started.

null

cogCreates a component which retrieves all the documents with a certain author
Developed by

Oana Florea

Rating
Rate!
0 Votes
LicenseGNU Lesser General Public License 2.1

Description

Please refer instead to the Groovy Script Service Tutorial. This snippet is deprecated since the introduction of wiki components. Also, bear in mind this snippet has a major downside: it requires the page containing it to be viewed at least once for the component to be correctly registered, hence the need of a scheduler, which is overkill.

XWiki components can be written in Java, but also using Groovy.

{{groovy}}
import org.xwiki.component.descriptor.*;
import org.xwiki.script.service.*;
import org.xwiki.context.*;
import com.xpn.xwiki.web.*;

class TestComponent implements ScriptService {
  org.xwiki.context.Execution e;
  List<String> getDocsByAuthor(String authorFullName) {
    def context = e.getContext().getProperty("xwikicontext");
    def hql = " where doc.author=(?)";
    def results = context.getWiki().getStore().searchDocuments(hql,0, 0, [authorFullName], context);
    print results;
   if (results.size() > 0) {
     return results;
    }
   return [];
 }
}
DefaultComponentDescriptor descriptor = new DefaultComponentDescriptor(implementation: TestComponent.class, role: ScriptService.class, roleHint: 'testComponent');
DefaultComponentDependency dep = new DefaultComponentDependency(name: 'e', role: Execution.class, roleHint: 'default');
descriptor.addComponentDependency(dep);
def cm = services.component.getComponentManager(null)
cm.unregisterComponent(ScriptService.class, 'testComponent');
cm.registerComponent(descriptor);
{{/groovy}}

The component must be registered once by executing the code above: either manually view the page containing the code or put the code in a scheduled job with very frequent trigger to auto-register after the server restarts

Accessing the component from Velocity

{{velocity}}
$services.testComponent.getDocsByAuthor("XWiki.SimpleUser");
{{/velocity}}

Other component implementations

Prerequisites & Installation Instructions

Copy the code snippet to a page and save it.

Get Connected