cancel
Showing results for 
Search instead for 
Did you mean: 

Component Generator vs. Selected Items

jarrett
Champ in-the-making
Champ in-the-making
I am having a hard time getting a component generator/ converter to display the correct information for multiple selects. Following this post http://forums.alfresco.com/en/viewtopic.php?f=4&t=11687 I was able to make a working component generator and converter (I think). The component generator creates value option pair for items in a drop down list for a multiple select.

This is what works:
The details page looks great, labels are shown and not the actual values
Drop down menus on the edit page work well, and show the labels and store the value

This is what fails:
When you select an item from the drop down and it is added to the "selected items" the value and not the label is shown. Once you leave edit mode the label is shown. In edit mode on values are shown in the "Selected Items" and not the labels.

From web-client-config-custom.xml my aspect:

   <config evaluator="aspect-name" condition="yg:taxGeography">
      <property-sheet>
         <separator name="sep2" display-label="Geography" component-generator="HeaderSeparatorGenerator" />
         <show-property name="yg:Geography" converter="ListOfValuesQueryConverter" component-generator="ListOfValuesQueryGenerator" />
      </property-sheet>
   </config>

From my model:
<!– The Constraint –>
      <constraint name="yg:CodeLabel" type="org.contezza.customConstraints.ListOfValuesQueryConstraint">
         <parameter name="allowedValues">
            <list></list>
         </parameter>
         <parameter name="caseSensitive"><value>true</value></parameter>
      </constraint>

<!– The Aspect –>
      <aspect name="yg:taxGeography">
         <title></title>
         <properties>      
            <property name="yg:Geography">
               <title>Geography</title>
               <type>d:text</type>
               <mandatory>true</mandatory>
               <multiple>true</multiple>
               <constraints><constraint ref="yg:CodeLabel" /></constraints>
            </property>
         </properties>
      </aspect>

From my faces-config-custom.xml
<managed-bean>
        <description>Dropdown with lable and value</description>
        <managed-bean-name>ListOfValuesQueryGenerator</managed-bean-name>
        <managed-bean-class>org.contezza.customConstraints.ListOfValuesQueryGenerator</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<managed-bean>
        <description>Dropdown with lable and value</description>
        <managed-bean-name>ListOfValuesQueryConverter</managed-bean-name>
        <managed-bean-class>org.contezza.customConstraints.ListOfValuesQueryConverter</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

Any help and insight is greatly appreciated.
2 REPLIES 2

unknown-user
Champ on-the-rise
Champ on-the-rise
Did you ever figure this out?

Can I see the code for the fix?

joshijimit
Champ on-the-rise
Champ on-the-rise
I did it few months back.. I left it in between cause i need to implement cascading in alfresco share. but I managed it working in alfreco explorer as well. here is the code i have implemted. See it if this help….


package com.cascading.demo;

import java.util.List;

import javax.faces.component.StateHolder;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.model.SelectItem;



public class ListOfValuesQueryConverter implements Converter,StateHolder {
     
    private List<SelectItem> list;
      
       public ListOfValuesQueryConverter(List<SelectItem> list) {
          this.list=list;
       }
      
       public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
          return new Object();
       }
      
       @SuppressWarnings("unchecked")
      public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
         
          if(value instanceof String) {
                for(SelectItem item : list) {
                   if (item.getValue().equals(value)) {
                      return item.getLabel();
                   }
                }
          }
          else if(value instanceof List) {
             String result="";
             for(String val : (List<String>)value) {
                for(SelectItem item : list) {
                    if (item.getValue().equals(val)) {
                       result += item.getLabel() + " ";
                       break;
                    }
                 }
             }
             return result;
          }
         
          return new String();
       }
      
       public ListOfValuesQueryConverter() {}
       public Object saveState(FacesContext context) { return (Object)this.list; }
       public void restoreState(FacesContext context, Object state) { this.list=(List<SelectItem>)state; }
       public boolean isTransient() { return false; }
       public void setTransient(boolean newTransientValue) {}
   }


package com.cascading.demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.component.UIOutput;
import javax.faces.component.UISelectItems;
import javax.faces.component.UISelectOne;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.alfresco.service.cmr.dictionary.PropertyDefinition;
import org.alfresco.web.app.servlet.FacesHelper;
import org.alfresco.web.bean.generator.TextFieldGenerator;
import org.alfresco.web.bean.repository.Node;
import org.alfresco.web.ui.common.ComponentConstants;
import org.alfresco.web.ui.repo.component.property.PropertySheetItem;
import org.alfresco.web.ui.repo.component.property.UIPropertySheet;

public class CustomListComponentGenerator extends TextFieldGenerator {

   @Override
   public UIComponent generate(FacesContext context, String id) {

      UIOutput component = (UIOutput) context.getApplication()
            .createComponent(ComponentConstants.JAVAX_FACES_OUTPUT);
      component.setRendererType(ComponentConstants.JAVAX_FACES_TEXT);

      FacesHelper.setupComponentId(context, component, id);

      return component;
   }

   protected UIComponent createComponent(FacesContext context,
         UIPropertySheet propertySheet, PropertySheetItem item) {

      /*
       * UIComponent component = super.createComponent(context, propertySheet,
       * item); if (component instanceof UISelectOne)
       * component.getAttributes().put("onchange", "submit()");
       * System.out.println("IN createComponent"); return component;
       */
      
      
      UIComponent component = null;
      PropertyDefinition propertyDef = getPropertyDefinition(context,
            propertySheet.getNode(), item.getName());

      if (propertySheet.inEditMode()) {

         Node currentNode = (Node) propertySheet.getNode();

         component = context.getApplication().createComponent(
               UISelectOne.COMPONENT_TYPE);
         FacesHelper.setupComponentId(context, component, item.getName());

         UISelectItems itemsComponent = (UISelectItems) context
               .getApplication()
               .createComponent("javax.faces.SelectItems");

         List<SelectItem> valueList = getValueList(currentNode, propertyDef);

         itemsComponent.setValue(valueList);

         component.getChildren().add(itemsComponent);

      } else {
         component = generate(context, item.getName());
      }

      if (component instanceof UISelectOne) {
         // component.getAttributes().put("autoSubmit", Boolean.TRUE);
         component.getAttributes().put("onchange", "submit()");
      }
      return component;
   }

      private List<SelectItem> getValueList(Node currentNode,
         PropertyDefinition propertyDef) {
      List<SelectItem> valueList = new ArrayList<SelectItem>();
      System.out.println("Debugg:" + propertyDef.getName().toString());
      // Create ContactName List
      boolean root = false;
      if (propertyDef.getName().toString()
            .equals("{http://www.cascading.com/model/content/1.0}car")) {
         String baantendernumber = null;
         if (currentNode.hasProperty("ccd:car")) {

            String driverName = "org.gjt.mm.mysql.Driver";
            String serverName = "localhost";
            String mydatabase = "cascading";
            String username = "root";
            String password = "root";

            Connection connection = null;
            Statement stmt = null;
            ResultSet rs = null;

            try {

               Class.forName(driverName);
               String url = "jdbc:mysql://" + serverName + "/"
                     + mydatabase;
               connection = DriverManager.getConnection(url, username,
                     password);
               stmt = connection.createStatement();
               rs = stmt.executeQuery("select code,label from codelabel");
               valueList.add(new SelectItem("–Select–", "–Select–"));
               while (rs.next()) {
                  System.out.println("debugg:" + rs.getString("code"));

                  valueList.add(new SelectItem(rs.getString("code"), rs
                        .getString("label")));

               }
            } catch (Exception e) {
            }

            // valueList.add(new SelectItem("indian","indian"));
            // valueList.add(new SelectItem("russian","russian"));

         }

      }
      if (propertyDef.getName().toString()
            .equals("{http://www.cascading.com/model/content/1.0}car1")) {

         String countryName = (String) currentNode.getProperties().get(
               "{http://www.cascading.com/model/content/1.0}car");
         if (countryName != null && countryName.equals("india")) {
            valueList.add(new SelectItem("–Select–", "–Select–"));
            valueList.add(new SelectItem("indian1", "indian1"));
            valueList.add(new SelectItem("indian2", "indian2"));

         } else if (countryName != null && countryName.equals("russia")) {
            valueList.add(new SelectItem("–Select–", "–Select–"));
            valueList.add(new SelectItem("russian1", "russian1"));
            valueList.add(new SelectItem("russian2", "russian2"));

         } else {
            valueList.add(new SelectItem("–Select–", "–Select–"));
         }

      }
      if (propertyDef.getName().toString()
            .equals("{http://www.cascading.com/model/content/1.0}car2")) {

         String countryName = (String) currentNode.getProperties().get(
               "{http://www.cascading.com/model/content/1.0}car1");
         if (countryName != null && countryName.equals("indian1")) {
            valueList.add(new SelectItem("–Select–", "–Select–"));
            valueList.add(new SelectItem("indian11", "indian11"));
            valueList.add(new SelectItem("indian12", "indian12"));

         } else if (countryName != null && countryName.equals("russian1")) {
            valueList.add(new SelectItem("–Select–", "–Select–"));
            valueList.add(new SelectItem("russian11", "russian11"));
            valueList.add(new SelectItem("russian12", "russian12"));

         } else if (countryName != null && countryName.equals("indian2")) {
            valueList.add(new SelectItem("–Select–", "–Select–"));
            valueList.add(new SelectItem("indian21", "indian21"));
            valueList.add(new SelectItem("indian22", "indian22"));

         } else if (countryName != null && countryName.equals("russian2")) {
            valueList.add(new SelectItem("–Select–", "–Select–"));
            valueList.add(new SelectItem("russian21", "russian21"));
            valueList.add(new SelectItem("russian22", "russian22"));

         } else {
            valueList.add(new SelectItem("–Select–", "–Select–"));
         }

      }
      if (propertyDef.getName().toString()
            .equals("{http://www.cascading.com/model/content/1.0}car3")) {

         String countryName = (String) currentNode.getProperties().get(
               "{http://www.cascading.com/model/content/1.0}car2");
         if (countryName != null && countryName.equals("indian11")) {
            valueList.add(new SelectItem("–Select–", "–Select–"));
            valueList.add(new SelectItem("indian111", "indian111"));
            valueList.add(new SelectItem("indian112", "indian112"));

         } else if (countryName != null && countryName.equals("russian11")) {
            valueList.add(new SelectItem("–Select–", "–Select–"));
            valueList.add(new SelectItem("russian111", "russian111"));
            valueList.add(new SelectItem("russian112", "russian112"));

         } else if (countryName != null && countryName.equals("indian21")) {
            valueList.add(new SelectItem("–Select–", "–Select–"));
            valueList.add(new SelectItem("indian211", "indian211"));
            valueList.add(new SelectItem("indian212", "indian212"));

         } else if (countryName != null && countryName.equals("russian21")) {
            valueList.add(new SelectItem("–Select–", "–Select–"));
            valueList.add(new SelectItem("russian211", "russian211"));
            valueList.add(new SelectItem("russian212", "russian212"));

         } else {
            valueList.add(new SelectItem("–Select–", "–Select–"));
         }

      }
      return valueList;

   }

   protected void setupConverter(FacesContext context,
         UIPropertySheet propertySheet, PropertySheetItem property,
         PropertyDefinition propertyDef, UIComponent component) {

      super.setupConverter(context, propertySheet, property, propertyDef,
            component);

      if (!propertySheet.inEditMode() && propertyDef != null
            && component instanceof UIOutput) {
         Node currentNode = (Node) propertySheet.getNode();
         ((UIOutput) component).setConverter(new ListOfValuesQueryConverter(
               this.getValueList(currentNode, propertyDef)));
      }

   }
}