Difference between revisions of "Java API:Enumerated Fields"

From AgileApps Support Wiki
imported>Aeric
imported>Aeric
 
(3 intermediate revisions by the same user not shown)
Line 23: Line 23:


//Retrieving the FieldMetaData
//Retrieving the FieldMetaData
FieldMetaDataBean fmdb = getFieldMetaData("someField", "picklist");
FieldMetaDataBean fmdb = getFieldMetaData("myObject", "picklistField");
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder();


//Get the database column date
//Get the field name
String tableColumn = fmdb.getTableColumn();
String fieldName = fmdb.getTableColumn();


//Get the field type
//Get the field type
Line 36: Line 36:
String objectId = fmdb.getObjectId();
String objectId = fmdb.getObjectId();


//Title of the Field
//Get the field label
String title = fmdb.getTitle();
String fieldLabel = fmdb.getTitle();
sb.append("Field Name -> " + tableColumn + "\n") ;
 
// Echo the attributes
sb.append("Field Name -> " + fieldName + "\n") ;
sb.append("Field Type -> " + fieldType + "\n");
sb.append("Field Type -> " + fieldType + "\n");
sb.append("Object Id -> " + objectId + "\n");
sb.append("Object Id -> " + objectId + "\n");
sb.append("Title -> " + title + "\n");
sb.append("Field Label -> " + fieldLabel + "\n");


//Enuemration Details
//Enumeration Details
EnumerationDetailsBean  enumerationDetails = fmdb.getEnumerationDetails();
EnumerationDetailsBean  enumerationDetails = fmdb.getEnumerationDetails();


Line 96: Line 98:
}
}


Functions.debug(sb.toString());
Logger.info(sb.toString(), "Enum");
</syntaxhighlight>
</syntaxhighlight>


Line 112: Line 114:


//Setting the objectId
//Setting the objectId
fmdb.setObjectId("myField");
fmdb.setObjectId("myObject");


//Setting the database column name
//Setting the field name
fmdb.setTableColumn("picklist");
fmdb.setTableColumn("priority_picklist");


//Setting the field type
//Setting the field type
fmdb.setType("PICK_LIST");
fmdb.setType("PICK_LIST");


//Setting the field title
//Setting the field label
fmdb.setTitle("Picklist");
fmdb.setTitle("Priorities");


//Setting enumeration details
//Setting enumeration details
EnumerationDetailsBean enumDetails = new EnumerationDetailsBean();
EnumerationDetailsBean enumDetails = new EnumerationDetailsBean();
String[] items = {"priority", "urgent", "rush", "second-day delivery"}; 
List<String> itemList = Arrays.asList(items);
enumDetails.setEnumerationItems(itemList);


//Sorting the enuerated values  
//Sorting the enumerated values  
enumDetails.setSortFlag(true);
enumDetails.setSortFlag(true);


fmdb.setEnumerationDetails(enumDetails);
fmdb.setEnumerationDetails(enumDetails);


//On successful addition of the field, the database column name is returned
//On successful addition of the field, the field name name is returned
String tableColumn = Functions.addFieldMetaData(fmdb);
String fieldName = Functions.addFieldMetaData(fmdb);


Functions.debug("Added field " + tableColumn);
Logger.info("Added field " + fieldName, "Enum");
</syntaxhighlight>
</syntaxhighlight>


Line 149: Line 154:


//Instantiate FieldMetaDataBean
//Instantiate FieldMetaDataBean
FieldMetaDataBean fmdb = getFieldMetaData("someField", "picklist");
FieldMetaDataBean fmdb = getFieldMetaData("myObject", "picklistField");


//Setting enumeration details
//Setting enumeration details
EnumerationDetailsBean enumDetails = new EnumerationDetailsBean();
EnumerationDetailsBean enumDetails = new EnumerationDetailsBean();
String[] items = {"priority", "rush", "second-day delivery"}; 
List<String> itemList = Arrays.asList(items);
enumDetails.setEnumerationItems(itemList);


//Sorting the enuerated values  
//Sorting the enuerated values  
Line 188: Line 196:
fmdb.setEnumerationDetails(enumDetails);
fmdb.setEnumerationDetails(enumDetails);


//On successful addition of the field, the database column name is returned
//On successful addition of the field, the field name is returned
String tableColumn = Functions.updateFieldMetaData(fmdb);
String fieldName = Functions.updateFieldMetaData(fmdb);
</syntaxhighlight>
</syntaxhighlight>



Latest revision as of 01:35, 10 September 2013

About Enumerated Fields

There are multiple types of enumerated fields. The type is specified using FieldMetaDataBean.setType(String type). These are the possible values. (They are passed within quotes, as strings):

  • PICK_LIST
  • GLOBAL_PICK_LIST
  • DEPENDENT_PICK_LIST
  • MULTI_PICK_LIST
  • RADIO_BUTTON
  • MULTI_CHECK_BOX

The standard field methods are used to work with enumerated fields:

  • Functions.addFieldMetaData(FieldMetaDataBean fmdb);
  • Functions.updateFieldMetaData(FieldMetaDataBean fmdb);
  • Functions.deleteFieldMetaData(FieldMetaDataBean fmdb);
Considerations
  • Add/update/delete is not supported for global picklists. All other enumeration types are supported.

Retrieving Field Metadata

<syntaxhighlight lang="java" enclose="div">

import com.platform.beans.EnumerationDetailsBean.*;

//Retrieving the FieldMetaData FieldMetaDataBean fmdb = getFieldMetaData("myObject", "picklistField"); StringBuilder sb = new StringBuilder();

//Get the field name String fieldName = fmdb.getTableColumn();

//Get the field type // (PICK_LIST, DEPENDENT_PICK_LIST, RADIO_BUTTON, MULTI_CHECK_BOX, GLOBAL_PICKLIST) String fieldType = fmdb.getType();

//String object Id String objectId = fmdb.getObjectId();

//Get the field label String fieldLabel = fmdb.getTitle();

// Echo the attributes sb.append("Field Name -> " + fieldName + "\n") ; sb.append("Field Type -> " + fieldType + "\n"); sb.append("Object Id -> " + objectId + "\n"); sb.append("Field Label -> " + fieldLabel + "\n");

//Enumeration Details EnumerationDetailsBean enumerationDetails = fmdb.getEnumerationDetails();

//Internal Id of the Enumerated Type field String name = enumerationDetails.getName(); sb.append("Internal Picklist Id -> " + name + "\n");

//Sort flag Boolean sortFlag = enumerationDetails.getSortFlag(); sb.append("Sort flag -> " + sortFlag + "\n");

//Show first value as default flag Boolean showFirstValAsDefaultFlag = enumerationDetails.getShowFirstValueAsDefault();

//Get the list of Enumeration Items List<EnumerationItemBean> enumerationItems = enumerationDetails.getEnumerationItems();

//Extracting the Item details for(EnumerationItemBean item : enumerationItems) {

   //Enumeration Item Id
   String id = item.getId();	
   sb.append("Enumeration Item Id -> " + id + "\n");
   //Enumeration Item label
   String enumeratedItemLabel = item.getPicklistLabel();
   sb.append("Enumeration Item Label -> " + enumeratedItemLabel + "\n");
   //Enumeration Item Value
   String enumeratedItemValue = item.getPicklistValue();	
   sb.append("Enumeration Item Value -> " + enumeratedItemValue + "\n");
   //Text color of the Enumeration Item Label
   String textColor = item.getTextColor();
   sb.append("Enuemration Label text color -> " + textColor + "\n");
   //Text background color of the Enumeration Item label
   String textBackgroundColor = item.getTextBgColor();	
   sb.append("Enumeration Label text background color -> " + textBackgroundColor + "\n");
   //Show only image flag
   Boolean showOnlyImageFlag = item.getShowOnlyImage();	
   sb.append("Show only image flag -> " + showOnlyImageFlag + "\n");
   //Indicates if the item is an option group laabel
   Boolean flagIsOptGroup = item.getFlagIsOptgroup();	
   sb.append("Is options group flag -> " + flagIsOptGroup + "\n");
   //Order of the Item
   Integer itemOrder = item.getItemOrder();	
   sb.append("Item order -> " + itemOrder + "\n");

}

Logger.info(sb.toString(), "Enum"); </syntaxhighlight>

Adding a Picklist Field

To create an enumerated field:

  1. Add the new field to the object using Functions.addFieldMetaData().
  2. Add the list of items, using the Functions.updateFieldMetaData().
<syntaxhighlight lang="java" enclose="div">

//Instantiate FieldMetaDataBean FieldMetaDataBean fmdb = new FieldMetaDataBean();

//Setting the objectId fmdb.setObjectId("myObject");

//Setting the field name fmdb.setTableColumn("priority_picklist");

//Setting the field type fmdb.setType("PICK_LIST");

//Setting the field label fmdb.setTitle("Priorities");

//Setting enumeration details EnumerationDetailsBean enumDetails = new EnumerationDetailsBean(); String[] items = {"priority", "urgent", "rush", "second-day delivery"}; List<String> itemList = Arrays.asList(items); enumDetails.setEnumerationItems(itemList);

//Sorting the enumerated values enumDetails.setSortFlag(true);

fmdb.setEnumerationDetails(enumDetails);

//On successful addition of the field, the field name name is returned String fieldName = Functions.addFieldMetaData(fmdb);

Logger.info("Added field " + fieldName, "Enum"); </syntaxhighlight>

Updating a Picklist

To update a specific item in an enumeration, it's ID must be specified.

To update the entire field:

  • The new list of items replaces the old one. All existing enumeration items must be included in the list, or they are not preserved.
  • For dependent picklists, all existing items plus the ParentToChildMapping beans must be provided.
<syntaxhighlight lang="java" enclose="div">

import com.platform.beans.EnumerationDetailsBean.*;

//Instantiate FieldMetaDataBean FieldMetaDataBean fmdb = getFieldMetaData("myObject", "picklistField");

//Setting enumeration details EnumerationDetailsBean enumDetails = new EnumerationDetailsBean(); String[] items = {"priority", "rush", "second-day delivery"}; List<String> itemList = Arrays.asList(items); enumDetails.setEnumerationItems(itemList);

//Sorting the enuerated values enumDetails.setSortFlag(true);

//Creating a list of Enumeration items List<EnumerationItemBean> listEnumItems = new ArrayList<EnumerationItemBean>();

//Setting up an enumeration item EnumerationItemBean enumItemBean = new EnumerationItemBean();

//Specify the ID of the enumeration item to be update, //where ENUMERATION_ITEM_ID is retrieved using getFieldMetaData() //enumItemBean.setId(ENUMERATION_ITEM_ID)

//setting the label of the Enumerated item enumItemBean.setPicklistLabel("Label 1");

//setting the value of the Enumerated item enumItemBean.setPicklistValue("value1");

//Setting the text color of the Enumerated item label enumItemBean.setTextColor("#000000");

//Setting the Back ground color of the Enumerated item label enumItemBean.setTextBgColor("#FFFFFF");

//Adding enumeration item to the list. In a similar way we can add more items listEnumItems.add(enumItemBean);

//Setting the list of Enumeration Items in the EnumerationDetailsBean enumDetails.setEnumerationItems(listEnumItems);

//Setting in the Enumeration details in the FieldMetaDataBean fmdb.setEnumerationDetails(enumDetails);

//On successful addition of the field, the field name is returned String fieldName = Functions.updateFieldMetaData(fmdb); </syntaxhighlight>

Define Mapping for a Dependent Picklist

A Dependent Picklist is one where the list of choices depends on a selection made in a parent field (another field of type enumerated). This sample code maps each choice in a parent picklist to the list of values in the dependent picklist.

<syntaxhighlight lang="java" enclose="div">

import com.platform.beans.EnumerationDetailsBean.*;

//Retrieve the metadata for the dependent picklist FieldMetaDataBean dependentPicklistMeta = getFieldMetaData("myObject", "dependentField");

String parentFieldName = dependentPicklistMeta.getParentPicklistTableColumn(); FieldMetaDataBean parentEnumeratedFieldMeta = getFieldMetaData("myObject", parentFieldName); EnumerationDetailsBean parentEnumerationDetails =

   parentEnumeratedFieldMeta.getEnumerationDetails();

List<EnumerationItemBean> parentEnumerationItemList =

   parentEnumerationDetails.getEnumerationItems();

Map<String,String> parentItemValToItemIdMap = new HashMap<String,String>();

for(EnumerationItemBean item : parentEnumerationItemList) {

   String itemValue = item.getPicklistValue();
   String itemLabel = item.getPicklistLabel();
   String itemId = item.getId();
   parentItemValToItemIdMap.put(itemValue,itemId);

}

EnumerationDetailsBean dependentEnumerationDetails =

   dependentPicklistMeta.getEnumerationDetails();

List<EnumerationItemBean> dependentEnumerationItemList =

   dependentEnumerationDetails.getEnumerationItems();

Map<String,String> dependentItemValToItemIdMap = new HashMap<String,String>();

for(EnumerationItemBean item : dependentEnumerationItemList) {

   String itemValue = item.getPicklistValue();
   String itemLabel = item.getPicklistLabel();
   String itemId = item.getId();
   dependentItemValToItemIdMap.put(itemValue,itemId);

}

List<ParentToChildMapping> pTc = new ArrayList<ParentToChildMapping>(); String dependentItemId = dependentItemValToItemIdMap.get("dep1"); String parentItemId = parentItemValToItemIdMap.get("value1"); ParentToChildMapping tmpPTCMap = new ParentToChildMapping(); tmpPTCMap.setDependentPicklistItemId(dependentItemId); tmpPTCMap.setParentPicklistItemId(parentItemId); pTc.add(tmpPTCMap); dependentEnumerationDetails.setParentToChildMapping(pTc);

String response = Functions.updateFieldMetaData(dependentPicklistMeta); </syntaxhighlight>