python-3.xgroovyvelocityxwiki

XWiki AppWithinMinutes add custom velocity script on display


I am new to XWiki and not a Java Developer. I would like to develop a simple AppWithinMinutes app (Let us call it ServerCatalog) for a list of servers with their properties like OS, Environment, etc.. That is all doable. I can develop that from AppWithinMinites UI.
I also have developed a macro which lists all pages by tags.

Now for example, when Someone creates an entry in my ServerCatalog say SERVERD56 Where D stands for Development environment.
when we display that Entry, I also want to execute my macro that I developed so that it can list all pages that matches the tag SERVERX56.
Note that I replaced D with X So before executing macro, I want to manipulate that page title and remove Environment prefix (in this case D for Development Environment) with X.

I hope it is clear of what I want to accomplish. Please let me know if it is unclear.

If someone can assist me on doing it, I will truly appreciate that.

EDITS:
As suggested by Eduard Moraru, I tried following things:

I have created a custom macro called "PageListByTag". Since python has more control over string operations, I added following at the end of ServerCatalogSheet

.
.
.
.
{{/html}}
{{/velocity}}


{{python}}
title = document.getTitle()
newTitle = title[:6] + "X" + title[6+1:]
print(newTitle)
{{/python}}

There are several problems with it..

  1. my html tag is already ended. I can't inject any new content in it anymore
  2. I can't add python code inside velocity tags as nested scripting is not allowed by XWiki
  3. I still don't know how to call my macro inside python code if I want to use python

If I can replace 6th character in a string using velocity, then everything works. I can use macro inside velocity script since it is within that html boundaries.


Solution

  • As far as I understand, you want to customize the "sheet" of the application which is used when displaying an application entry.

    If you have a look a the documentation's section on customization, you get a lot of starting points on where you should be looking:

    https://extensions.xwiki.org/xwiki/bin/view/Extension/App%20Within%20Minutes%20Application#HCustomization

    This line would be interesting (but also the above links on how XWiki applications work):

    The sheet, which is used to display and edit application entries (e.g. HolidaySheet)

    In your case, it the document to customize (edit in wiki syntax mode) should be ServerCatalog.Code.ServerCatalogSheet

    Inside that document, you have code automatically generated by AWM to which you can add a call to your macro.

    ## AWM generated code...
    ...
    
    ## Customization:
    {{displayAssociatedPages /}}
    

    Then, inside this macro (called displayAssociatedPages, in this example), you could do something like:

    {{velocity}}
    #set ($title = $doc.title)
    ## OR #set ($title = $doc.name), depending if your pages have titles
    
    #if ($title.startsWith('SERVERD'))
      #set ($tag = $title.replaceFirst('SERVERD', 'SERVERX'))
      ##
      ## We can reuse and customize this snippet: https://snippets.xwiki.org/xwiki/bin/view/Extension/Display%20pages%20with%20a%20specific%20tag/
      ##
      #set ($references = $xwiki.tag.getDocumentsWithTag($tag))
      #foreach($reference in $references)
        #set ($document = $xwiki.getDocument($reference))
        #set ($label = $document.getTitle())
        [[$label>>$reference]]
      #end 
    #end
    {{/velocity}}
    

    API documentation and more info at https://www.xwiki.org/xwiki/bin/view/Documentation/DevGuide/API/

    Also, you should understand that once you customize an AWM application, you should be careful when editing it with AWM itself. For example, if you add a new field, AFAIK, you risk having the customizations done to your sheet, as in the above example, overwritten, so you should recover them from document history and re-apply them.