Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Update resource
Update resource [message #824146] Mon, 19 March 2012 10:09 Go to next message
El Shorty is currently offline El ShortyFriend
Messages: 37
Registered: March 2012
Member
Hey everybody,

I have a little problem, or rather I don't know how to start solving it without what I think is a big work around.

I'm using EMF and working with xml files. I'm trying to get an object out the file, edit it and save it back to the file. Wich is not really a big problem but right now I load the file, get the object, edit the object, delete the original object from the file and save the new one to it. So instead of updating the object I create a new one everytime it edits.

This is no problem aslong as there are like 5-10 objects in the file, but I'm guessing that once this builds up, my performance won't be great. Is there like an update method on resource? I know there is a save and a delete method so I was wondering if there was some kind of update aswell.
Re: Update resource [message #824153 is a reply to message #824146] Mon, 19 March 2012 10:14 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33147
Registered: July 2009
Senior Member
Comments below.

On 19/03/2012 11:09 AM, El Shorty wrote:
> Hey everybody,
>
> I have a little problem, or rather I don't know how to start solving
> it without what I think is a big work around.
> I'm using EMF and working with xml files. I'm trying to get an object
> out the file, edit it and save it back to the file. Wich is not really
> a big problem but right now I load the file, get the object, edit the
> object, delete the original object from the file and save the new one
> to it.
Why not modify the object itself?
> So instead of updating the object I create a new one everytime it edits.
Why? How are you doing this?
> This is no problem aslong as there are like 5-10 objects in the file,
> but I'm guessing that once this builds up, my performance won't be great.
I'd suggest you measure performance and not make assumption.
> Is there like an update method on resource?
No. If you think about it, there's nothing in Java that's lets you
easily write just part of a file. You need to write all the bytes for
the new version.
> I know there is a save and a delete method so I was wondering if there
> was some kind of update aswell.
No.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Update resource [message #824170 is a reply to message #824153] Mon, 19 March 2012 10:47 Go to previous messageGo to next message
El Shorty is currently offline El ShortyFriend
Messages: 37
Registered: March 2012
Member
Why not modify the object itself?

That is what I think I'm doing but when I save it, I add it as a new object so I have to delete the old object, I don't know how to overwrite it. This is what I do:

This gets me the object I select in my tree from a viewer:
	Person person = (Person) item;
	GroupLabelProvider labelProvider = new GroupLabelProvider();
	final IWorkbenchWindow window =  PlatformUI.getWorkbench().getActiveWorkbenchWindow();;
	IWorkbenchPage page = window.getActivePage();
	PersonEditorInput input = new PersonEditorInput(person);
	try {
		page.openEditor(input, SecondPersonEditor.ID);
	} catch (PartInitException e) {
		e.printStackTrace();
	}
}


So this opens my editor with all the properties from this person. Say I edit them than I save it again wich I do by calling the savemethod I worte, I'm guessing that there might be an easier way.

This is what I do in the savemethod:
loadResource = resourceSet.getResource(fileURI, true);
list = loadResource.getContents();	
list.add(object);
resource = resourceSet.createResource(fileURI);
resource.getContents().addAll(list);

try {
	final Map<Object, Object> saveOptions = new HashMap<Object, Object>();
	saveOptions.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED, Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER);
	saveOptions.put(XMLResource.OPTION_KEEP_DEFAULT_CONTENT, true);
	resource.save(saveOptions);
} catch (IOException e1) {
	e1.printStackTrace();
}
Re: Update resource [message #824175 is a reply to message #824170] Mon, 19 March 2012 10:56 Go to previous messageGo to next message
El Shorty is currently offline El ShortyFriend
Messages: 37
Registered: March 2012
Member
I found the solution by using this:
bryanhunt.wordpress.com/2011/03/15/mongo-emf/

I didn't know about the eResource option.. All I had to do was instead of calling my saveMethod use eResource().save(null)

I want to thank you for the help ed, especially since that site is also made with your help.
Re: Update resource [message #824212 is a reply to message #824170] Mon, 19 March 2012 11:53 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33147
Registered: July 2009
Senior Member
Comments below.

On 19/03/2012 11:47 AM, El Shorty wrote:
> Why not modify the object itself?
>
> That is what I think I'm doing but when I save it, I add it as a new
> object so I have to delete the old object, I don't know how to
> overwrite it.
You just modify it and save the resource containing it and that will
write out your changes.
> This is what I do:
>
> This gets me the object I select in my tree from a viewer:
>
> Person person = (Person) item;
> GroupLabelProvider labelProvider = new GroupLabelProvider();
> final IWorkbenchWindow window =
> PlatformUI.getWorkbench().getActiveWorkbenchWindow();;
> IWorkbenchPage page = window.getActivePage();
> PersonEditorInput input = new PersonEditorInput(person);
> try {
> page.openEditor(input, SecondPersonEditor.ID);
> } catch (PartInitException e) {
> e.printStackTrace();
> }
> }
>
>
> So this opens my editor with all the properties from this person. Say
> I edit them than I save it again wich I do by calling the savemethod I
> worte, I'm guessing that there might be an easier way.
>
> This is what I do in the savemethod:
>
> loadResource = resourceSet.getResource(fileURI, true);
> list = loadResource.getContents();
> list.add(object);
You should just save the resource you loaded in the first place.
> resource = resourceSet.createResource(fileURI);
> resource.getContents().addAll(list);
>
> try {
> final Map<Object, Object> saveOptions = new HashMap<Object,
> Object>();
> saveOptions.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED,
> Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER);
> saveOptions.put(XMLResource.OPTION_KEEP_DEFAULT_CONTENT, true);
> resource.save(saveOptions);
> } catch (IOException e1) {
> e1.printStackTrace();
> }
Have a look at the generated editor. All it does is save the resources
in the resource set.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Update resource [message #824213 is a reply to message #824175] Mon, 19 March 2012 11:53 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33147
Registered: July 2009
Senior Member
Yes, it's as easy as that.

On 19/03/2012 11:56 AM, El Shorty wrote:
> I found the solution by using this:
> bryanhunt.wordpress.com/2011/03/15/mongo-emf/
> I didn't know about the eResource option.. All I had to do was instead
> of calling my saveMethod use eResource().save(null)
>
> I want to thank you for the help ed, especially since that site is
> also made with your help.


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Object Selection is empty
Next Topic:getParent() on children split across multiple transient tree nodes
Goto Forum:
  


Current Time: Mon May 13 20:02:18 GMT 2024

Powered by FUDForum. Page generated in 0.03432 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top