Changing the category of a gadget

This recipe will guide you through the process of modifying the default category of an existing gadget.

Getting ready

Prepare to use the web console, XML configuration, and a few lines of Java code.

How to do it...

Follow these to change the category of a gadget:

  1. If you created the gadget from the web console, you can choose the category, instead of using the default category with name Gadgets. Simply select the created gadget from the Application Registry. You will see a red link inside the description:
    How to do it...
  2. Click on the red link shown above to change the category. You will see the category selection form:
    How to do it...
  3. To change the category in the configuration, you need to write to the portal/WEB-INF/conf/portal/application-registry-configuration.xml and manually add the new category and a definition of the application for the created gadget. Here is an example that adds the Skype Talk gadget definition in the Application Registry Service:
    <object-param>
       <name>GadgetsMine</name>
       <description>GadgetsMine</description>
       <object type="org.exoplatform.application.registry.ApplicationCategory">
         <field name="name">
           <string>GadgetsMine</string>
         </field>
         <field name="displayName">
           <string>GadgetsMine</string>
         </field>
         <field name="description">
           <string>GadgetsMine</string>
         </field>
         <field name="accessPermissions">
           <collection type="java.util.ArrayList" item-type="java.lang.String">
              <value>
                <string>*:/platform/users</string>
              </value>
           </collection>
         </field>
         <field name="applications">
           <collection type="java.util.ArrayList">
              <value>
                <object type="org.exoplatform.application.registry.Application">
                  <field name="categoryName">
                    <string>GadgetsMine</string>
                  </field>
                  <field name="applicationName">
                    <string>SkypeTalk</string>
                  </field>
                  <field name="displayName">
                    <string>Skype Talk</string>
                  </field>
                  <field name="description">
                    <string>Skype Talk Gadget, easily manage and track your daily to-do list.</string>
                  </field>
                  <field name="type">
                    <string>gadget</string>
                  </field>
                  <field name="contentId">
                    <string>SkypeTalk</string>
                  </field>
                  <field name="accessPermissions">
                     <collection type="java.util.ArrayList" item-type="java.lang.String">
                       <value>
                         <string>*:/platform/users</string>
                       </value>
                     </collection>
                  </field>
                </object>
              </value>
            </collection>
          </field>
        </object>
    </object-param>
  4. This code will create a new definition of the Skype Talk gadget that points to the new category GadgetsMine. You therefore now have two definitions for the Skype Talk gadget. Remove the old declaration as shown in the earlier recipe, Removing gadgets.

The categories defined in the Application Registry Service configuration are created only when you start GateIn for the first time, and when the JCR repository is empty. Pay attention to this feature, because if the repository is just created, you can only modify the categories by using the API. Here is an example of code that modifies a category with an API:

import org.exoplatform.application.registry.Application;
import org.exoplatform.application.registry.ApplicationCategory;
import org.exoplatform.application.registry.ApplicationRegistryService;

...

PortalRequestContext context = (PortalRequestContext) WebuiRequestContext.getCurrentInstance();
ExoContainer pcontainer = context.getApplication()
.getApplicationServiceContainer();

ApplicationRegistryService applicationRegistryService = (ApplicationRegistryService) pcontainer
.getComponentInstanceOfType(ApplicationRegistryService.class);

try {
   if (applicationRegistryService
      .getApplication("GadgetsMine/SkypeTalk") == null) {
      ApplicationCategory applicationCategory = new ApplicationCategory();
      applicationCategory.setName("GadgetsMine");
      applicationCategory.setDisplayName("GadgetsMine");
      Application application = applicationRegistryService
      .getApplication("Gadgets/SkypeTalk");
     applicationRegistryService
    .save(applicationCategory, application);
   }
} catch (Exception e) {
   e.printStackTrace();
}

This code can be put in a new WEBUI component of the web application or in a simple servlet filter. For WEBUI, see Chapter 7, Developing Using Components API for more details.

How it works...

When you click on the Import Applications button of the Application Registry, the default category for the gadgets is applied, so all gadgets that are still not installed, usually those inside an application and deployed, will take that category. It's the code in the org.exoplatform.application.registry.impl.ApplicationRegistryServiceImpl class that is executed:

   public void importExoGadgets() throws Exception
   {
      ContentRegistry registry = getContentRegistry();

      //
      ExoContainer container = ExoContainerContext.getCurrentContainer();
      GadgetRegistryService gadgetService = (GadgetRegistryService)container.getComponentInstanceOfType(GadgetRegistryService.class);
      List<Gadget> eXoGadgets = gadgetService.getAllGadgets();

      //
      if (eXoGadgets != null)
      {
         ArrayList<String> permissions = new ArrayList<String>();
         permissions.add(UserACL.EVERYONE);
         String categoryName = "Gadgets";
         
         //
         CategoryDefinition category = registry.getCategory(categoryName);
         if (category == null)
         {
            category = registry.createCategory(categoryName);
..................Content has been hidden....................

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