
A Web Dynpro component that displays a list of available office items, and enables purchasers to select one or more of them.
This component is displayed if the user chooses to select the required items from a list in the first step of the Order Office Material process.
The following aspects of the component's structure are relevant for its implementation:
Basic Data
|
Name |
WDCODisplaySelectItems |
|
Package |
com.sap.caf.eu.gp.example.ordermat.wdco.displayselect |
|
Implemented Interfaces |
IGPWebDynproCO (com.sap.caf.eu.gp.co.webdynpro) |
|
Used Web Dynpro Components |
CContext (com.sap.caf.eu.gp.example.ordermat.wdco) Note
This is a helper component for this example that holds the example data. The component is not described in the documentation, because its implementation is not GP-specific. In a real-life scenario, the data will be retrieved from a real repository, such as a database, or a backend system. |
Component Controller
The WDCODisplaySelectItems component controller implements the functionality that the component provides.
Properties
The interface controllers of the following components are required:
Component Controller: Properties
Context
The context of the component controller contains a structure - ExampleData, with value attributes defined for each attribute of the office items:
In addition, a value attribute priceAsString is defined to represent the price of the office items converted into string.
The whole ExampleData structure, as well as each attribute (excluding priceAsString ) is mapped to the relevant value attribute in the CContext's interface controller context, so that the values are taken from CContext.
Component Controller: Context
Methods
The following methods are declared:
Component Controller: Methods
Implementation
The implementation of the component controller implies adding the relevant logic in the methods that are declared:
com.sap.caf.eu.gp.co.api.IGPTechnicalDescription
The technical description includes:
The basic data for the callable object comprises its name, description, locale and resource accessor. These are supplied as parameters when the technical description is instantiated. The name and the description can be localized.
Localization is enabled using an instance of GP WebDynproResourceAccessor.
The input parameters are defined as children of the root structure input , which exists by default. You can add both sub-structures and individual parameters. To do that, you use the com.sap.caf.eu.gp.structure.api.IGPStructureInfo interface, and its methods addStructure() and addAttribute() .
For this component, a sub-structure is added, whose value is retrieved from the CContext component. The multiplicity of the structure is 0...n , which means that the structure is a list and may be empty. It represents the attributes of an office item.
The attributes themselves are also added to the root input structure, and their values are also retrieved from the CContext component.
Output parameters are defined in the same way as input parameters. They are added to the predefined output root structure.
In this component, the output parameters are the same as the input.
The result state of a callable object indicate the outcome of the callable object execution. It is a convenient way to manage the process flow at runtime, as you can define what action is executed next according to the result state reached. You can define an arbitrary number of result states for the callable object. To do that, you use the technical description method addResultState() .
For this component, a result state RESULT_STATE_COMPLETED is defined. Optionally, a localized description can be added as well.
Method createDescription()
publiccom.sap.caf.eu.gp.co.api.IGPTechnicalDescription createDescription( java.util.Locale locale ) {//@@begin createDescription() IGPTechnicalDescription technicalDescription; //create resource accessor IWDTextAccessor textAccessor = wdComponentAPI.getTextAccessor(); GPWebDynproResourceAccessor resourceAccessor = new GPWebDynproResourceAccessor(textAccessor); technicalDescription = GPCallableObjectFactory.createTechnicalDescription( "CO_TEXT", "CO_DESC", resourceAccessor, locale); try {//INPUT: //get root input IGPStructureInfo input = technicalDescription.getInputStructureInfo(); //add item structure to root input IGPStructureInfo inputItemStructureInfo = input.addStructure(CContextInterface.STRUCTURE_ITEMS); inputItemStructureInfo.setMultiplicity(IStructureInfo.MULITIPLICITY_0_N); IGPAttributeInfo attributeInfo = null; //add attributes to item structure inputItemStructureInfo.addAttribute(CContextInterface.PARAM_ORDER_NUMBER, IGPAttributeInfo.BASE_STRING); inputItemStructureInfo.addAttribute(CContextInterface.PARAM_NAME, IGPAttributeInfo.BASE_STRING); inputItemStructureInfo.addAttribute(CContextInterface.PARAM_UNIT, IGPAttributeInfo.BASE_STRING); inputItemStructureInfo.addAttribute(CContextInterface.PARAM_CURRENCY, IGPAttributeInfo.BASE_STRING); inputItemStructureInfo.addAttribute(CContextInterface.PARAM_PRICE, IGPAttributeInfo.BASE_DOUBLE); //OUTPUT: //get root output IGPStructureInfo output = technicalDescription.getOutputStructureInfo(); //add item structure to root output IGPStructureInfo outputItemStructureInfo = output.addStructure(CContextInterface.STRUCTURE_ITEMS); outputItemStructureInfo.setMultiplicity(IGPStructureInfo.MULITIPLICITY_0_N); //add attributes to item structure outputItemStructureInfo.addAttribute(CContextInterface.PARAM_ORDER_NUMBER, IGPAttributeInfo.BASE_STRING); outputItemStructureInfo.addAttribute(CContextInterface.PARAM_NAME, IGPAttributeInfo.BASE_STRING); outputItemStructureInfo.addAttribute(CContextInterface.PARAM_UNIT, IGPAttributeInfo.BASE_STRING); outputItemStructureInfo.addAttribute(CContextInterface.PARAM_CURRENCY, IGPAttributeInfo.BASE_STRING); outputItemStructureInfo.addAttribute(CContextInterface.PARAM_PRICE, IGPAttributeInfo.BASE_DOUBLE); } catch (GPInvocationException e) {//exception handling String localizedMessage = m_TextAcc.getText("ERROR_CREATING_TECHNICAL_DESC");wdThis.wdGetWDCODisplaySelectItemsInterfaceController().wdFireEventTechnicalException(new GPTechnicalCallableObjectException(logger, localizedMessage, e)); } //result states IGPResultStateInfo success = technicalDescription.addResultState(RESULT_STATE_COMPLETED); success.setDescriptionKey("RESULTSTATE_COMPLETED_DESC"); return technicalDescription; //@@end } |
The framework calls this method when the callable object is executed as a part of a process at runtime. The method implementation includes retrieving the input data. In this component, the input structures are mapped to the output structures, so that the data can be passed on to the next action in the process. This is implemented using the private method mapInputToOutputData() .
The programmatic parameter mapping is required because this callable object is used in a loop block, and the mapping guarantees that all selected items will eventually be added to the output, and not only the last selected one.
Method execute()
publicvoid execute( com.sap.caf.eu.gp.context.api.IGPExecutionContext context ) {//@@begin execute() m_context = context; this.mapInputDataToOutputData(context); //format price for ui for(int i=0, size=wdContext.nodeExampleData().size(); i<size; i++){IPublicWDCODisplaySelectItems.IExampleDataElement exampleDataElement = wdContext.nodeExampleData().getExampleDataElementAt(i); double price = exampleDataElement.getPrice(); String priceAsString = new DecimalFormat("#0.00").format(price);exampleDataElement.setPriceAsString(priceAsString); } //@@end } |
This method is invoked when the user selects a specific item from the list, and chooses to order it. It is the method that completes the callable object execution.
The method assigns the attribute values of the selected item to the output parameters of the callable object. For that purpose, the com.sap.caf.eu.gp.structure.api.IGPStructure interface is used. While the IGPStructureInfo interface describes the structure parameters, the IGPStructure interface represents them at runtime.
To set the actual value of an attribute, you retrieve the root output structure, and then use method addStructure() to add a sub-structure, and setAttributeValue() to set the value. In this case, the sub-structures and the attribute values are retrieved from the CContext component.
Finally, the result state is set, and the processing completion is indicated to the framework by calling the processingComplete() method of the execution context.
Method addItem()
publicvoid addItem() {//@@begin addItem() try {//get current context element IPublicWDCODisplaySelectItems.IExampleDataElement exampleDataElement = wdContext.nodeExampleData().currentExampleDataElement(); //get root output IGPStructureoutput = m_context.getOutputStructure(); //set values of output parameters IGPStructure outputItemStructure = output.addStructure(CContextInterface.STRUCTURE_ITEMS); outputItemStructure.setAttributeValue(CContextInterface.PARAM_ORDER_NUMBER, exampleDataElement.getOrderNumber()); outputItemStructure.setAttributeValue(CContextInterface.PARAM_NAME, exampleDataElement.getName()); outputItemStructure.setAttributeValue(CContextInterface.PARAM_PRICE, exampleDataElement.getPrice()); outputItemStructure.setAttributeValue(CContextInterface.PARAM_CURRENCY, exampleDataElement.getCurrency()); outputItemStructure.setAttributeValue(CContextInterface.PARAM_UNIT, exampleDataElement.getUnit()); m_context.setResultState(RESULT_STATE_COMPLETED); m_context.processingComplete(); } catch (GPInvocationException e) { String localizedMessage = m_TextAcc.getText("ERROR_SETTING_PARAMETERS");wdThis.wdGetWDCODisplaySelectItemsInterfaceController().wdFireEventTechnicalException(new GPTechnicalCallableObjectException(logger, localizedMessage, e)); } catch (GPEngineException e) { String localizedMessage = m_TextAcc.getText("ERROR_SETTING_PARAMETERS");wdThis.wdGetWDCODisplaySelectItemsInterfaceController().wdFireEventTechnicalException(new GPTechnicalCallableObjectException (logger, localizedMessage, e)); } //@@end } |
Interface Controller
Properties
The WDCODisplaySelectItems component controller is required.
Interface Controller: Properties
Methods
The available methods in the interface controller are inherited from the implemented interface IGPWebDynproCO (com.sap.caf.eu.gp.co.webdynpro):
Interface Controller: Methods
Events
TechnicalException
With this exception handling mechanism, exceptions that occur in the Web Dynpro component are exposed to the GP framework for further processing.
Interface Controller: Events
Implementation
The implementation of the interface controller includes the following main aspects:
This method calls the createDescription() method of the component controller (see section Component Controller).
Method getDescription()
publiccom.sap.caf.eu.gp.co.api.IGPTechnicalDescription getDescription( java.util.Locale locale ) {//@@begin getDescription() return wdThis.wdGetWDCODisplaySelectItemsController().createDescription(locale); //@@end } |
This method invokes the execute() method of the component controller (see section Component Controller).
Method execute()
publicvoid execute( com.sap.caf.eu.gp.context.api.IGPExecutionContext executionContext ) {//@@begin execute() wdThis.wdGetWDCODisplaySelectItemsController().execute(executionContext); //@@end } |
User Interface
The user interface of the component is implemented in view VDisplaySelectItems. The view is embedded in WebDynproCO that has been created when adding the implemented interface.
Properties
The component controllers of the following components are required for the implementation of the user interface part:
View VDisplaySelectItems: Properties
Context
In the context of the view, a structure ElementData is created with the same value attributes as the structure in the component controller (see section Component Controller). In addition, the structure and the attributes are mapped to the ones available in the component controller's context (the data in the component controller's context is retrieved from component CContext).
View VDisplaySelectItems: Context
Actions
An action OrderItem is defined for the view. The action usage is discussed in the Layout section below. The implementation of the event handlers is shown in the Implementation section.
View VDisplaySelectItems: Actions
Layout
The layout of the view includes the following UI elements:
View VDisplaySelectItems: Layout (Table Properties)
In addition, a column is defined for each item attribute that should be displayed, and the relevant value attribute from the view context is referenced in the text property of the column's TextView.
View VDisplaySelectItems: Layout (Column Properties)
View VDisplaySelectItems: Layout (Button)
Implementation
In the Implementation tabstrip the event handler for the OrderItem action must be implemented. The required behavior is to add the selected item to the collection of ordered items (output structures); therefore, the component controller's addItem() method is called.
Method onActionOrderItem()
publicvoid onActionOrderItem(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent) {//@@begin onActionOrderItem(ServerEvent) wdThis.wdGetWDCODisplaySelectItemsController().addItem(); //@@end } |
For more information about the component's usage in the process, see Display List and Select .