cancel
Showing results for 
Search instead for 
Did you mean: 

Attribute value only valid for first execution cycle

Former Member
0 Kudos

Hello,

In my app I have 2 views, the Main View and Tableview. Depending on which button is clicked in the MainView, the attribute TableName is set. TableName attribute is mapped to the component controller, and thru the component controller to TableView. In TableView, I have a delete button, clicking which executes a method in View Controller, which calls another method in Component controller. The first delete cycle works successfully and tablename is successfully fetched from the component controller. But when I click delete button again, the table name attribute returns null resulting execution failing in the view controller. During 2nd delete cycle, when the TableView controller tries to get the TableName from the component controller, it returns a null.

Where should I store the TableName so that it is available through out my application. In my app, the TableName is valid only for the first delete cycle.

I am new to this, so any help would be appreciated.

Thanks,

Ajay

Accepted Solutions (1)

Accepted Solutions (1)

p_2_5_6_9_6_0
Active Participant
0 Kudos

Hi,

Ideally, you should be setting the name of the table inside the action attached to the button (The one that you click on the MainView).

1.)If you are losing the Table Name after the first cycle that usually indicates that you are calling the invalidate() method somewhere and that is invalidating the context.

2.) If you arent already doing so - please set the table name in the view context of the MainView (whixh is mapped to the Component Controller). Table View should be mapped to the same component controller to have the value passed.

3.) Debug your application through 1 cycle - so that you can ensure where the context attribute is being invalidated.

Hope that helps.

Thanks.

p256960

Former Member
0 Kudos

Hi,

I debugged my app, and found that I am not invalidating the context nodes holding the tableName. I am invalidating other context nodes which holds some other data. But the node holding tableName is untouched.

I am setting the tableName inside the action of the button in the MainView using the following code:

public void onActionAddMapping(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )

{

//@@begin onActionAddMapping(ServerEvent)

String tableName = "TMP_COMPANY_CODES";

ITableElement newTable = wdContext.nodeTable().createTableElement();

newTable.setTableName(tableName);

wdContext.nodeTable().addElement(newTable);

wdThis.wdGetPIMCompController().SubmitData();

wdThis.wdFirePlugToTableView();

//@@end

}

Also, this tableName is mapped to the component controller. The TableView is also mapped to the same controller context node. When I do the first delete cycle, the TableView has the value of the tableName from the component controller (which in turn got it from Main View). When I delete first time, the action in the TableView calls a method in the component controller. Then wdDoModifyView of TableView is called, where I do a view.resetView(); and dynamically create the table and contents. Does this cause any problem for the tableName stored in the context? Then TableView is displayed after removing the record deleted.

But when I select another record and click on Delete again in the TableView, it gives me a null exception. And when I debug I see that the value of tableName in TableView is empty for the following:

String tableName = wdContext.nodeTableTview().currentTableTviewElement().getTableName();

This value had the table name in the 1st delete cycle.

Is there some way to set the lifetime of the context or some other way around this?

Thanks,

Ajay

Former Member
0 Kudos

Hi,

Use the code given below instead of view.reset()

String tableNameBackup = (String) wdContext.currentContextElement().getAttributeValue("tableName");

wdContext.getContext().reset();

wdThis.wdComponentController().wdGetContext().getContext().reset();

wdContext.currentContextElement().setAttributeValue("tableName", tableNameBackup );

Regards

Padma N

Former Member
0 Kudos

Hi Ajay,

Please let me know if my understanding of the flow is correct:

You are clicking on a button 'delete' in MainView. This calls an onActionAddMapping() method. In this first you are creating context node element then calling a controller method Submit() & then firing plug to table view. In the table view you are doing a view.resetView() in the wdDomodifyView(). After this when and how are you coming back to the main view to start the second cycle?

Regards,

Anagha

Former Member
0 Kudos

Hi Anagha,

The delete button is in TableView. The MainView has a button called "Add Mapping" which has an action onActionAddMapping() where I store the table name. To delete, I dont come to the Main View. I am doing the 2nd delete also in the TableView.

Thanks,

Ajay

Former Member
0 Kudos

Could you share the code in your onActionDelete() method? Also, what is being done before firing the plug to table view i.e, in the in SubmitData() method of the controller?

Edited by: Anagha Jawalekar on Dec 15, 2008 11:30 AM

Former Member
0 Kudos

Hi,

As per my understanding you are setting the table name dynamically in main view.

For the first delete in tableview your able to set the table name for second time value is giving null.

Create one context attribute in comp controller and also in 2 views.

Map the attributes to compoonent controller.

While setting the value to newTable.setTableName(tableName);

Set the same to comp controller new attribute in Mainview

wdThis.wdgetCompCotroller.wdGetContext().currentContextElement().settablename(newTable.getTableName());

In tableView.(On click delete)

String tableName = wdThis.wdgetCompCotroller.wdGetContext().currentContextElement().gettablename();

newTable.setTableName(tableName);

Regards,

Goriga

Former Member
0 Kudos

Hi,

I am slightly confused. Could you please take a look at the code below and tell me what I am doing wrong?

Here is the code:

onActionAddMapping() method called in MainView, where tablename is set:

public void onActionAddMapping(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
  {
    //@@begin onActionAddMapping(ServerEvent)
    
	String tableName = "TMP_COMPANY_CODES";
	
	ITabElement ntab = wdThis.wdGetPIMCompController().wdGetContext().nodeTab().createTabElement();
	ntab.setTName(tableName);
	wdThis.wdGetPIMCompController().wdGetContext().nodeTab().addElement(ntab);
	
	wdThis.wdGetPIMCompController().SubmitData();
	wdThis.wdFirePlugToTableView();
   
    //@@end
  }

onActionDeleteMapping() method in TableView

public void onActionDeleteMapping(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
  {
    //@@begin onActionDeleteMapping(ServerEvent)
	String selectedCode = null;
	String selectedId = null;
	long id = 0;
	long code = 0;
	int lead = wdContext.nodeViewResultSet().getLeadSelection();

	String tableName = wdThis.wdGetPIMCompController().wdGetContext().nodeTab().currentTabElement().getTName();

	if (lead >= 0) {
		IKeysElement newKeys = wdContext.nodeKeys().createKeysElement();
		if (tableName == "TMP_COMPANY_CODES")
		{
			selectedCode = wdContext.nodeViewResultSet().currentViewResultSetElement().getAttributeValue("CODE").toString();
			code = Long.parseLong(selectedCode);
		} 
		else
		{
			selectedId = wdContext.nodeViewResultSet().currentViewResultSetElement().getAttributeValue("ID").toString();
			id = Long.parseLong(selectedId);
		}

		newKeys.setCode(code);
		newKeys.setId(id);
		wdContext.nodeKeys().addElement(newKeys);
		

		wdThis.wdGetPIMCompController().DeleteData();
		wdContext.nodeViewResultSet().removeElement(wdContext.nodeViewResultSet().currentViewResultSetElement());
		
	}

    //@@end
  }

SubmitData() method in comp controller:

public void SubmitData( )
  {
    //@@begin SubmitData()
	IWDMessageManager messageMgr = wdComponentAPI.getMessageManager();
	String tableName = (String) wdContext.nodeTab().getCurrentElement().getAttributeValue("TName");
	try {
		
		//	Get the database connection
		InitialContext dbInitContext = new InitialContext();
		Properties sysProperties = System.getProperties();
		String sysname = sysProperties.getProperty("SAPSYSTEMNAME");
		String dbName = "jdbc/" + "SAP" + sysname + "DB";
		DataSource dataSource = (DataSource) dbInitContext.lookup(dbName);
		Connection conn = dataSource.getConnection();

		String SelectStmt = null;

		if (tableName == "TMP_COMPANY_CODES")
		{
			String dest = wdContext.nodeCC().currentCCElement().getDestination();
			String active = wdContext.nodeCC().currentCCElement().getActive();
			long code = wdContext.nodeCC().currentCCElement().getCode();
			SelectStmt = "INSERT INTO TMP_COMPANY_CODES(CODE,DESTINATION,ACTIVE) values("+code+",'"+dest+"','"+active+"')";
		} 
		else if (tableName == "TMP_BRANCHES")
		{
			String dest = wdContext.nodeBranches().currentBranchesElement().getDestination();
			String site = wdContext.nodeBranches().currentBranchesElement().getSite();
			String desc = wdContext.nodeBranches().currentBranchesElement().getDescription();
			long id = wdContext.nodeBranches().currentBranchesElement().getId();
			SelectStmt = "INSERT INTO TMP_BRANCHES(ID,SITE,DESCRIPTION,DESTINATION) values("+id+",'"+site+"','"+desc+"','"+dest+"')";
		}
		else
		{
			String dest = wdContext.nodeCC().currentCCElement().getDestination();
			String active = wdContext.nodeCC().currentCCElement().getActive();
			long code = wdContext.nodeCC().currentCCElement().getCode();
			SelectStmt = "INSERT INTO TMP_TEST(CODE,DESTINATION,ACTIVE) values("+code+",'"+dest+"','"+active+"')";
		}
		
		
			
		//	Prepare the SQL statements
		PreparedStatement stmt = conn.prepareStatement(SelectStmt);
	
		//	Execute the Query
		stmt.executeUpdate();
		stmt.close();
		conn.close();

	} 
	catch (NamingException e) {
		messageMgr.reportException(e.getMessage(),false);
	} catch (SQLException e) {
		messageMgr.reportException(e.getMessage(),false);
	}
	catch(Exception e)	{
		messageMgr.reportException(e.getMessage(),false);
	}

	wdThis.RetrieveData(tableName);	
    //@@end
  }

onPlugFromMainView() method in TableView called when Add Mapping Button is clicked in MainView:

public void onPlugFromMainView(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
  {
    //@@begin onPlugFromMainView(ServerEvent)
    
	String tableName = wdThis.wdGetPIMCompController().wdGetContext().nodeTab().currentTabElement().getTName();

	//	First remove the dynamic attributes of ViewResultSet context node
	boolean hasAttr = false;
	for (Iterator iter = wdContext.nodeViewResultSet().getNodeInfo().iterateAttributes(); iter.hasNext();) 
	{
		hasAttr = true;
		break;
	}
	
	if(hasAttr == true)
	{
		wdContext.nodeViewResultSet().getContext().reset();
	}
	
	//	Create dynamic attribute in ViewResultSet context node
	String[] attributes = new String[wdContext.nodeResultSetMetaData().size()];
	
	for (int columnIndex = 0; columnIndex < wdContext.nodeResultSetMetaData().size(); columnIndex++) {
		String columnName = wdContext.nodeResultSetMetaData().currentResultSetMetaDataElement().getColumnName();
		attributes[columnIndex] = columnName; 
		wdContext.nodeViewResultSet().getNodeInfo().addAttribute(columnName,wdContext.nodeResultSetMetaData().getNodeInfo().getAttribute("ColumnName").getDataType());
		wdContext.nodeResultSetMetaData().moveNext();
	}
	
	//	Retrieve the database contents from the controller context node of ResultSet and assign those values to ViewResultSet
	for (int recordIndex = 0; recordIndex < wdThis.wdGetPIMCompController().wdGetContext().nodeResultSet().size(); recordIndex++) {
		IPublicPIMComp.IResultSetElement controllerElement = wdThis.wdGetPIMCompController().wdGetContext().nodeResultSet().getResultSetElementAt(recordIndex);
		IPrivateTableView.IViewResultSetElement viewElement = wdContext.nodeViewResultSet().createViewResultSetElement();
		
		for (int j = 0; j < attributes.length; j++) {
			String attrValue = controllerElement.getAttributeValue(attributes[j]).toString();
			viewElement.setAttributeValue(attributes[j],attrValue);
		}
		wdContext.nodeViewResultSet().addElement(viewElement);
	}
    //@@end
  }

wdDoModifyView() method in TableView:

public static void wdDoModifyView(IPrivateTableView wdThis, IPrivateTableView.IContextNode wdContext, com.sap.tc.webdynpro.progmodel.api.IWDView view, boolean firstTime)
  {
    //@@begin wdDoModifyView
    
	//	First, Delete all the dynamically created view elements
	view.resetView();
    
	//	Get the Root Transparent Container
	IWDTransparentContainer container = (IWDTransparentContainer) view.getElement("RootUIElementContainer");

	//	Get the Table
	IWDTable table = (IWDTable) view.getElement("Table");
	
	for (int columnIndex = 0; columnIndex < wdContext.nodeResultSetMetaData().size(); columnIndex++) {
		//	Create Table Cell Editor
		IWDTextView editor = (IWDTextView) view.createElement(IWDTextView.class, "TableColumnTextViewEditor"+columnIndex);
		editor.bindText(wdContext.nodeViewResultSet().getNodeInfo().getAttribute(wdContext.nodeResultSetMetaData().getResultSetMetaDataElementAt(columnIndex).getColumnName()));
	 	
		//	Create Table Cell Header
		 String columnHeader1 = "TableColumnCaption"+columnIndex;
		 IWDCaption columnCaption1 = (IWDCaption) view.createElement(IWDCaption.class, columnHeader1);
		 columnCaption1.setText(wdContext.nodeResultSetMetaData().getResultSetMetaDataElementAt(columnIndex).getColumnName());
		 
		//		Create Table Column
		IWDTableColumn column = (IWDTableColumn) view.createElement(IWDTableColumn.class, "TableColumn" + columnIndex);
		column.setTableCellEditor(editor);
		column.setHeader(columnCaption1);
		table.addGroupedColumn(column);
	 }
    //@@end
  }

Thanks,

Ajay

Former Member
0 Kudos

Hi,

I am still unable to get thru this issue. Any help would be appreciated!

Thanks,

Ajay

Answers (1)

Answers (1)

Former Member
0 Kudos

If you have doubts with refreshing dynamically created UI or context have a look at this thread:

Regards,

Anagha