I understand that I need to apparently import a service or query, but I don’t understand how.
Unfortunately, searching the documentation and the forum did not help.
Code:
{{groovy}}
class Test
{
String testQuery()
{
def xwql_text = "";
def query = services.query.xwql(xwql_text);
pages = query.execute();
return pages;
}
}
def t = new Test();
t.testQuery();
{{/groovy}}
The services variable simply does not exist in the scope of your method. So you need to pass it either as a parameter of #testQuery or in the Test class constructor to set a field with it.
AFAIK, each wiki content executed as a component method should have the same script bindings you have in a wiki page, which includes the services binding.
I’m sorry, but I didn’t really understand. Here is the example above (where the class is created) I passed it through the service variable and then use it. How can I do the same when creating a class in a component? After all, I’m obviously not creating it. It just registers through the component manager.
My code for the component:
{{groovy}}
import javax.inject.Named;
import javax.inject.Singleton;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.ComponentAnnotationLoader;
import org.xwiki.script.service.ScriptService;
import com.xpn.xwiki.*;
import com.xpn.xwiki.doc.*;
import com.xpn.xwiki.objects.*;
import com.xpn.xwiki.api.*;
import org.xwiki.script.internal.service.*
@Component
@Named("helloWorld")
@Singleton
public class HelloWorldGroovyScriptService implements ScriptService
{
public execute(XWikiDocument doc)
import org.xwiki.script.internal.service.*;
{
def xwql_text = "";
def query = services.query.xwql(xwql_text);
query.addFilter('hidden');
def pages = query.execute();
return pages;
}
}
// Note: we get the Component Manager for the current wiki since in our example we want to register our Script Service
// Component only in the current wiki. We could as well register it in the Root Component Manager for all wikis.
def componentManager = services.component.getComponentManager('wiki:' + services.wiki.currentWikiId)
// Parse the annotations of the class above to generate a Component Descriptor to register the class as a Component in the Component Manager.
def loader = new ComponentAnnotationLoader()
def descriptors = loader.getComponentsDescriptors(HelloWorldGroovyScriptService.class)
// Note: Annotations can define several descriptors (by implementing several roles) so we iterate over all of them and register the Component
for (descriptor in descriptors) {
componentManager.registerComponent(descriptor)
}
{{/groovy}}
How do I “pass” the service variable in this case?
it turns out that the service variable should either be passed when registering the component
OK, your HelloWorldGroovyScriptService is actually not a wiki component at all. You are using a wiki component as a hack to inject at startup a Java component implemented in Groovy.
but trying to create a query as in the documentation does not work.
Query query = this.queryManager.createQuery("");
Maybe somewhere there is a working example of a component created in a similar way where there is work with queries?
For example, some kind of extension or a working example in the documentation.
So far, all the examples from the documentation and all the tips do not lead to working code(