5.3.4.1. Changing the category of a gadget

There are 2 ways to change the category of a gadget:

The following procedure instructs you how to add Hello World gadget to the My Gadgets category.

  1. Add the following content to the custom-extension.war/conf/configuration.xml file.

    
    <external-component-plugins>
        <target-component>org.exoplatform.application.registry.ApplicationRegistryService</target-component>
        <component-plugin>
            <name>new.portal.portlets.registry</name>
            <set-method>initListener</set-method>
            <type>org.exoplatform.application.registry.ApplicationCategoriesPlugins</type>
            <description>this listener init the portlets are registered in PortletRegister</description>
            <init-params>
                <object-param>
                    <name>MyGadgets</name>
                    <description>description</description>
                    <object type="org.exoplatform.application.registry.ApplicationCategory">
                        <field name="name"><string>MyGadgets</string></field>
                        <field name="displayName"><string>My Gadgets</string></field>
                        <field name="description"><string>List of personal gadgets for development.</string></field>
                        <field name="accessPermissions">
                            <collection type="java.util.ArrayList" item-type="java.lang.String">
                                <value><string>*:/developers</string></value>
                            </collection>
                        </field>
                        <field name="applications">
                            <collection type="java.util.ArrayList">
                                <value>
                                    <object type="org.exoplatform.application.registry.Application">
                                        <field name="applicationName"><string>HelloGadget</string></field>
                                        <field name="categoryName"><string>MyGadgets</string></field>
                                        <field name="displayName"><string>Hello Gadget</string></field>
                                        <field name="description"><string>The simplest gadget</string></field>
                                        <field name="type"><string>gadget</string></field>
                                        <field name="contentId"><string>HelloGadget</string></field>
                                        <field name="accessPermissions">
                                            <collection type="java.util.ArrayList" item-type="java.lang.String">
                                                <value><string>*:/developers</string></value>
                                            </collection>
                                        </field>
                                    </object>
                                </value>
                            </collection>
                        </field>
                    </object>
                </object-param>
            </init-params>
        </component-plugin>
    </external-component-plugins>
  2. Stop the server and clean up database, then restart. You will now see the Hello World gadget added to the My Gadgets category:

    Note

    You have to clean up the database because the categories defined in the Application Registry Service configuration are created only when you start eXo Platform for the first time, and when the JCR repository is empty. However, you could avoid cleaning up database and restarting the server by using the API as below.

Avoid cleaning up database and restarting the server

  1. Create the category by using the following code:

    
    PortalContainer container = PortalContainer.getInstance();
        ApplicationRegistryService appService = (ApplicationRegistryService)container.getComponentInstanceOfType(ApplicationRegistryService.class);
          try {
            if (appService.getApplication("MyGadgets/HelloGadget") == null) {
              ApplicationCategory cat = new ApplicationCategory();
              cat.setName("MyGadgets");
              cat.setDisplayName("My Gadgets");
              cat.setDescription("List of personal gadgets for development");
              cat.setAccessPermissions(Arrays.asList("*:/developers"));
              
              Application app = appService.getApplication("Gadgets/HelloGadget");
              appService.save(cat, app);
            }
          } catch (Exception e) {
            throw new RuntimeException(e);
          }
  2. Add the above code block to one portlet action, for example:

    
    @Override
      public void processAction(ActionRequest actionRquest, ActionResponse actionResponse) {
        PortalContainer container = PortalContainer.getInstance();
        ApplicationRegistryService appService = (ApplicationRegistryService)container.getComponentInstanceOfType(ApplicationRegistryService.class);
          try {
            if (appService.getApplication("MyGadgets/HelloGadget") == null) {
              ApplicationCategory cat = new ApplicationCategory();
              cat.setName("MyGadgets");
              cat.setDisplayName("My Gadgets");
              cat.setDescription("List of personal gadgets for development");
              cat.setAccessPermissions(Arrays.asList("*:/developers"));
              
              Application app = appService.getApplication("Gadgets/HelloGadget");
              appService.save(cat, app);
            }
          } catch (Exception e) {
            throw new RuntimeException(e);
          }
      }

      @RenderMode(name = "view")
      public void hello(RenderRequest request, RenderResponse response) throws IOException, PortletException {
        PrintWriter writer = response.getWriter();
        PortletURL actionURL = response.createActionURL();
        writer.append("<p>Click <a href='" + actionURL.toString() + "'>here</a> to create MyGadgets category</p>");
      }
  3. Deploy the portlet to which you have added the above code block, then do the portlet action.

Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus