cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic List with forms properties?

marc_carrio
Champ in-the-making
Champ in-the-making
Hi guys!

How we can create list (enum) with the dynamic options, filling options in java class or with UEL expressions?

In forms (act 5.6) I used:

<select name="listCategory">

${myVariable == 'A'?
'<option value="a1">A1</option>
<option value="a2">A2</option>'
:' '}

${myVariable == 'B'?
'<option value="b1">B1</option>
<option value="b2">B2</option>'
:' '}

</select>
<input type="hidden" name="tecnicosel_type" value="String"/>'

I test in act5.7 in form properties… and UEL don't work =S maybe with Expressions/Spring?

Other option maybe:

<activiti:formProperty id="chose1" variable="" type="enum" name="chose1" required="true" readable="true" writable="true">
       <activiti:value id="#{bean.getId1()}" name="#{bean.getName1()}" />
       <activiti:value id="#{bean.getId2()}" name="#{bean.getName2()}" />
       <activiti:value id="#{bean.getId3()}" name="#{bean.getName3()}" />
</activiti:formProperty>

The problem is that in my case the number of options is different… depending of value of variable. I could put the maximum, but isn't much elegant…

Thanks a lot!  Smiley Happy  Smiley Happy
5 REPLIES 5

trademak
Star Contributor
Star Contributor
Hi,

It may be a good idea to implement your own custom form type.
In the Activiti Explorer you can find a UserFormType which is also an example of a flexible number of items.

Best regards,

marc_carrio
Champ in-the-making
Champ in-the-making
Finally I add code to the EnumFormPropertyRenderer.
I put the class and method in option values and I use java.reflect for call my Java that return a Map<String,String>.

I know that isn't the better solution  :roll: , but works and is quick.   Smiley Wink 

EnumFormPropertyRenderer code:

@SuppressWarnings("unchecked")
  @Override
  public Field getPropertyField(FormProperty formProperty) {
  
    ComboBox comboBox = new ComboBox(getPropertyLabel(formProperty));
    comboBox.setRequired(formProperty.isRequired());
    comboBox.setRequiredError(getMessage(Messages.FORM_FIELD_REQUIRED, getPropertyLabel(formProperty)));
    comboBox.setEnabled(formProperty.isWritable());

    String classe = null;
    String metode = null;
    String param = null;
    Map<String, String> llista = null;
    
    Map<String, String> values = (Map<String, String>) formProperty.getType().getInformation("values");
      
    if (values != null) {
        for (Entry<String, String> enumEntry : values.entrySet()) {
         if ("classe".equals(enumEntry.getKey())) {
          classe = enumEntry.getValue();
         }
         if ("metode".equals(enumEntry.getKey())) {
          metode = enumEntry.getValue();
         }
         if ("param".equals(enumEntry.getKey())) {
          param = enumEntry.getValue();
         }
        }
     }  
   
    if (classe == null ){
        if (values != null) {
          for (Entry<String, String> enumEntry : values.entrySet()) {
            // Add value and label (if any)
            comboBox.addItem(enumEntry.getKey());
            if (enumEntry.getValue() != null) {
              comboBox.setItemCaption(enumEntry.getKey(), enumEntry.getValue());
            }
          }
        }
        return comboBox;
    }
   
    try {
    
     Class<?> classeCombo = Class.forName(classe);
    
        Class<?> partypes[] = new Class[1];
        partypes[0] = String.class;
       
        Constructor<?> ct = classeCombo.getConstructor();
        Object retObj = ct.newInstance();
  Method metodeCombo = classeCombo.getDeclaredMethod(metode, partypes);

  llista = (Map<String, String>) metodeCombo.invoke(retObj, param);
 
} catch (ClassNotFoundException e) {
  e.printStackTrace();
} catch (SecurityException e) {
  e.printStackTrace();
} catch (NoSuchMethodException e) {
  e.printStackTrace();
} catch (IllegalArgumentException e) {
  e.printStackTrace();
} catch (IllegalAccessException e) {
  e.printStackTrace();
} catch (InvocationTargetException e) {
  e.printStackTrace();
} catch (InstantiationException e) {
  e.printStackTrace();
}
   
    
  if (llista != null) {
       for (Entry<String, String> enumEntry : llista.entrySet()) {
         // Add value and label (if any)
         comboBox.addItem(enumEntry.getKey());
         if (enumEntry.getValue() != null) {
           comboBox.setItemCaption(enumEntry.getKey(), enumEntry.getValue());
         }
       }
  }

    return comboBox;
  }

bpmn20.xml code:
<activiti:formProperty id="sel1" name="sel1" type="enum" required="true" readable="true" writable="true">
   <activiti:value id="classe" name="org.cgc.myProcess.bean.MyClass" />
   <activiti:value id="metode" name="getList" />
   <activiti:value id="param" name="${myVar}" />
</activiti:formProperty>

And MyClass code:

public class MyClass{

public Map<String,String> getList(String myvar){
 
  Map<String,String> llista = new HashMap<String, String>();

  if ("A".equals(myvar)){
   llista.put("a1", "A1");
   llista.put("a2", "A2");
  }else{
   llista.put("b1", "B1");
   llista.put("b2", "B2");
  }
 
  return llista;
}
}

Best Regards!

marc_carrio
Champ in-the-making
Champ in-the-making
I don't understand why the variable isn't evaluated… =S

<activiti:value id="param" name="${myVar}" />
Somebody knows why? and where I will touch for fix ?

Thanks!

marc_carrio
Champ in-the-making
Champ in-the-making
Ok, FormProperty values… not support Expressions…  :roll:

I solved the problem changing it in activiti-engine.  Smiley Happy

Best regards!

jwistrom
Champ in-the-making
Champ in-the-making
How did you manage to make form property values accept expressions?