Creating views

In the previous example, the portlet doesn't have a view, so if you install it, you will see nothing in the page of the portal. In this recipe, you will see the complete configuration and creation of the views for the portlet.

Getting ready

Add a new view for the portlet using Groovy, WebUI's preferred scripting tool. With Groovy, it is very simple to handle the WebUI components. Therefore, we need to add a new component to handle it in the view.

How to do it...

Carry out the following steps:

  1. Create a file MyPortlet.gtmpl inside the path MyApplication.war/groovy/MyApplication/webui/component:
    <%
    
      def rcontext = _ctx.getRequestContext() ;
    %>
    <div id="$uicomponent.id" class="MyPortlet">
       Started!!!
    </div>
  2. Add the Groovy template in the configuration.xml:
      <ui-component-config>
        <type>
          my.sample.MyPortlet
        </type>
        <lifecycle>
    org.exoplatform.webui.core.lifecycle.UIApplicationLifecycle
        </lifecycle>
        <template>
    app:/groovy/MyApplication/webui/component/MyPortlet.gtmpl
        </template>
      </ui-component-config>

    Or in the annotation:

    @ComponentConfig(
      type = MyPortlet.class, 
      lifecycle = UIApplicationLifecycle.class,
      template = "app:/groovy/MyApplication/webui/component/MyPortlet.gtmpl")
    public class MyPortlet extends UIPortletApplication  {
    
        ...
    }

    The template tag sets the default view for the portlet. If you install the portlet in the main page under the HomePortlet, you will see something similar to the following screen:

    How to do it...
  3. Now write a more complex component. In the portlet class, add before some Component as annotation:
    @ComponentConfigs({
          @ComponentConfig(
            lifecycle = UIApplicationLifecycle.class,  
            template = " app:/groovy/MyApplication/webui/component/MyPortlet.gtmpl "
          ),
          @ComponentConfig(
            type = UIPortalNavigation.class, 
            id = "MyUISiteMap")
    })
    public class MyPortlet extends UIPortletApplication  {
    
        ...
    }

    The same thing can be done through XML:

      <ui-component-config>
          <type>
           my.samples.MyPortlet
         </type>
          <lifecycle>
    org.exoplatform.webui.core.lifecycle.UIApplicationLifecycle
         </lifecycle>
         <template>
         app:/groovy/MyApplication/webui/component/MyPortlet.gtmpl
         </template>
      </ui-component-config>
      <ui-component-config id="MyUISiteMap">
         <type>
    org.exoplatform.portal.webui.navigation.UIPortalNavigation
         </type>
      </ui-component-config>

    In this example, you have two configurations: one is the example showed before, and for the second, we declare a new component, MyPortlet, through a WebUI component, the UIPortalNavigation, the component used for the navigation of the portal.

  4. Now we have to add the UIPortalNavigation component as a child inside the application:
    public MyPortlet() throws Exception {
    
          
    // Take the current PortletRequest
          PortletRequestContext context = (PortletRequestContext) WebuiRequestContext
                .getCurrentInstance();
          PortletRequest prequest = context.getRequest();
    
    /* Take the portlet preferences and find a preference 
       called template. If it doesn't exist add the default 
       template UISitemapTree.gtmpl. It implements a 
       navigable tree structure */
    
          PortletPreferences prefers = prequest.getPreferences();
          String template = prefers.getValue("template",
                "system:/groovy/webui/core/UISitemapTree.gtmpl");
    
    /* Add the UIPortalNavigation as a child of the 
       application and set two properties */
    
          UIPortalNavigation uiPortalNavigation = addChild(
                UIPortalNavigation.class, "MyUISiteMap", null);
          uiPortalNavigation.setTemplate(template);
          uiPortalNavigation.setUseAjax(false);
    
    /* Set in memory the informations present in the tree. In this code are memorized the first 2 levels of the tree */
    
      uiPortalNavigation.setScope(GenericScope.treeShape(2));
          
    }
  5. Rewrite the Groovy page MyPortlet.gtmpl so that we can call the new inserted component:
    <%   
          import org.exoplatform.portal.webui.navigation.UIPortalNavigation;
                   
          def uiPortalNavigation = uicomponent.getChild(UIPortalNavigation.class); 
          uiPortalNavigation.loadTreeNodes();
          uicomponent.renderChildren();
    %>

    Through the renderChildren() method, the component will be rendered for all its children. Each component will query their template and will provide their own graphic part to the page. Here is the result:

    How to do it...

    Of course, it is still a crude window. You will see how to decorate it better in the Handling different skins in a portlet recipe.

How it works...

The Groovy scripts are elaborated by a service, the Template Service. This service is loaded in the file $PORTAL_ROOT/WEB-INF/conf/common/common-configuration.xml:

<component>
  <type>
org.exoplatform.groovyscript.text.TemplateService
  </type>
</component>

It is based on Groovy, a highly efficient scripting language used for the view.

As with most components, the Template Service starts during the loading of the page that uses Groovy scripts. It manages an internal cache where it preserves the bytes and compiles the pages.

The Groovy scripts can be declared with different schemas. Each schema responds to a different resource resolver. The resource resolver searches and loads the resources. Here are the available schemas:

  • system: All scripts inside the portal root application. They are resolved by the org.exoplatform.resolver.ServletResourceResolver. It finds the resources in the ServletContext.
  • classpath: The scripts that will be loaded through classpath. They are resolved by the org.exoplatform.resolver.ClasspathResourceResolver.
  • app: All scripts inside the custom application. They are resolved by the org.exoplatform.resolver.PortletResourceResolver.
  • par: The scripts that are in a portlet application. They are resolved by the org.exoplatform.resolver.PortletResourceResolver. It finds the resources in the PortletContext.
  • war: All scripts inside a web application. They are resolved by the org.exoplatform.resolver.ServletResourceResolver.
  • jar: All scripts inside a jar application. They are resolved by the org.exoplatform.resolver.FileResourceResolver.
  • file: All scripts inside an external directory of the file system. They are resolved by the org.exoplatform.resolver.FileResourceResolver.

Note

The Template service doesn't need configuration, unless you use different Groovy compiler settings, which you can change only by directly working on the class.

Notate the uicomponent key in the Groovy script. It allows loading the current UI component application that you have declared in the configuration.xml:

<application>     
    <ui-component-root>
       my.sample.MyPortlet
    </ui-component-root>
   ...

The other keys available for the Groovy scripts can be found in:

  • orientation, dlr, isRT, isLT: These represent orientation of the text written in the Groovy template. It can be LT or RT according the nationality. For example, some nations write from right to left.
  • locale: The nationality, for example en.
  • nodeurl: The URL of the uicomponent represented as org.exoplatform.web.url.navigation.NodeURL instance.
  • _ctx: The map containing all showed variables.
  • portletContent: This can be used only if your component uses the UIPortletLifecycle. It contains the text inside the portlet. It is represented by the org.exoplatform.commons.utils.Text instance, and is very useful for AJAX or REST calls.

These variables are present in the processRender and renderTemplate methods seen in the WebUI lifecycle that you have configured in the portlet. The processRender method adds these variables in memory (except for the locale) so that they can be used in the page. The renderTemplate adds the locale to the GroovyContext and passes it to the Template Service.

Note

If you need more details on how this is done in Groovy, you can read the official documentation at the following URL:

http://groovy.codehaus.org/Documentation

There's more...

In this recipe, we introduced the concept of a service. You have seen the first service in the previous chapter with the OrganizationService. All services in GateIn can be easily identified/recognized as they all end with the name Service.

All the services are declared in the portal root application, but they can be added in other portal applications. The convention for a configuration is to have the name of the service followed by –configuration.xml; for example, common-configuration.xml.

The configuration system is based on Pico Container, a framework similar to Spring used to instantiate objects through the XML configuration. Details for Pico Container can be seen here: http://picocontainer.org.

A service can be loaded inside any UIComponent. For example, in MyPortlet, we can call the Template Service as follows:

PortletRequestContext context = (PortletRequestContext) WebuiRequestContext
            .getCurrentInstance();
       ExoContainer pcontainer = context.getApplication().getApplicationServiceContainer();
       TemplateService service = (TemplateService)pcontainer.getComponentInstanceOfType(TemplateService.class);

Or simply:

TemplateService templateService = this.getApplicationComponent(TemplateService.class)

Or by Groovy:

def templateService = uicomponent.getApplicationComponent(TemplateService.class);

GateIn provides many services so it is not possible to write about all of them. We will explain the main services in the next recipes for dialed argument.

Template Statistic Service

Another service tied to TemplateService is the TemplateStatisticService. It is used to monitor the execution time of a Groovy script. Here are the signatures of the methods:

  • getTemplateList()
  • getMaxTime(String id_template)
  • getMinTime(String id_template)
  • getExecutionCount(String id_template)
  • getAverageTime(String id_template)
  • getSlowestTemplates()
  • getMostExecutedTemplates()
  • getFastestTemplates()

See also

  • For more information on Configuring the Organization Service refer to Choosing the JAAS modules recipe in Chapter 5, Securing Portal Contents
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.232.239