cancel
Showing results for 
Search instead for 
Did you mean: 

Cloning Model Objects

Former Member
0 Kudos

Hello,

I want to copy (clone) a model object. The model object consists of attributes and other (deep) structures (position, sub-position etc.).

I don't want to copy each attribute of each structure. Is there a way to make a copy of the whole model object recursively, without knowing how the structure looks like, so that I can use this clone-method for all model objects?

Thanks for any help,

Thomas

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Thomas

Why do you need to clone model objects? You can create the instances of that. if you can elobrate then we can suggest some alternatives.

Regards

NagaKishore V

Former Member
0 Kudos

Hi NagaKishore,

I receive a filled model object from a R/3 backend. After that I show some of the data on the screen. The frontend user has the possibility to change this data.

Now I want to know if the user has changed the modeldata. For this I tried to make a copy of the object when I get the data from the backend, and then I can compare the old data with the new one.

The problem in this specific case is, that the data of the model object aren't shown at one view only. The frontend user can navigate through different views (in some views he can change the data, i other ones not). For this I need to compare the whole modelobject.

Regards

Thomas Morandell

Former Member
0 Kudos

Hi Thomas

1. Create a custom controller to access the data from the back end r/3

2. In the diagram view map the Model to the controller as well as the view.

3. Write a method to retrieve the data from the r/3 in the custom controller.

4. Call this method from the view implementation

5. After this use the WDCopyService method to copy the controller context to the view context.

6. Now you can play with the view context and the changes are reflected in the view context but your original data exists in the custom controller context.

Regards

NagaKishore V

Former Member
0 Kudos

Thank you for your answer,

I'll try this out. Is there no other way to copy the model object, without making a ModelContext-Node?

Now I have the old and new object. How can I compare them? Exists there also a Class which compares the objects recursively, or do I have to compare each attribute of each structure by my own?

Regards,

Thomas Morandell

Former Member
0 Kudos

Hi Thomas

You can use the changed property of the context element to know whether the node has been changed or not.

To know the exact changes you need to iterate and compare with the original node

Regards

NagaKishore

Former Member
0 Kudos

Hi NagaKishore,

is it possible to get the XML-String from a model object? I understood, that the data-transfer between the object in the webdynpro frontend and the abap backend is done with a XML-stream. If I could get that XML-Stream, I wouldn't need an extra context-node, to copy my model.

I've found the method toXML(String arg0, String arg1) which I can use with a model object, but I don't know how to use it. Any suggestion?

Regards Thomas

Former Member
0 Kudos

Thomas,

The method toXML you mentioned is a part of internal implementation rather then public API interface, so it is not documented and should be avoided.

Also you may use the same technique as for serializing context node to XML described here Replace ContextReader class with your implementation of ModelClassReader that accepts ICMIGenericModelClass as root. Then you have to drill down recursively by relation(s) and fire SAX events for corresponding relations, properties. An output could be controlled by XSLT stylesheet used, see XmlSerializer in sample.

VS

Former Member
0 Kudos

Thanks for the answer,

can you say ma, how to iterate the relation(s)? I found out, that if I know the modelClass of the relation, I get the object with the method:

getRelatedModelObjects(<modelClass_name>);

Unfortunately I'm not able to iterate the relation info's and for getting information about the relations.

Regards

Thomas

Former Member
0 Kudos

final ICMIgenericModelClass source = /*...*/ ;
final ICMIModelClassInfo infClass = source.associatedModelClassInfo();
for (final Iterator i = infClass.iterateSourceRoleInfos(); i.hasNext(); )
{
  final ICMIRelationRoleInfo infSource = (ICMIRelationRoleInfo)i.next();
  final ICMIRelationRoleInfo infTarget = infSource.getOtherRoleInfo();
			
  if ( null == infTarget || !infTarget.isNavigable() )
    continue;
  final String relationName = infTarget.getName();
  if ( infTarget.getCardinality().isMultiple() )
  {
    final Collection related = source.getRelatedModelObjects( relationName );
  }
  else
  {
    final ICMIGenericModelClass related 
      = (ICMIGenericModelClass)source.getRelatedModelObject( relationName );
  }
}

VS