Home » Modeling » EMF » [CDO] CDOID.toString() => String to CDO ID ?(How can I retrieve a CDO ID or object based on the CDO ID as a string ?)
|
Re: [CDO] CDOID.toString() => String to CDO ID ? [message #517718 is a reply to message #517678] |
Mon, 01 March 2010 20:06 |
|
This is a multi-part message in MIME format.
--------------000803030504040507000105
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Hi Sabri,
Here is a 2.0 example:
| CDOIDObjectFactory cdoidObjectFactory = cdoView().getSession();
CDOID cdoID = CDOIDUtil.read(uriFragment, cdoidObjectFactory);|
Note, that in 3.0 there are no ID factories anymore:
| CDOID cdoID = CDOIDUtil.read(uriFragment);|
Cheers
/Eike
----
http://thegordian.blogspot.com
http://twitter.com/eikestepper
Am 01.03.2010 17:40, schrieb Sabri Skhiri:
> Hi,
>
> We are implementing a prototype of exportation of subset of a model
> stored in CDO within an EMF resource. This EMF resource is then
> modified and we use EMF compare for comparing the exported and the
> stored models.
> When we export the CDO Objects, we set the XMI ID with the CDO ID in
> order to keep the right correspondence between the exported and the
> stored model. Then, our ID provider just extract the cdoid.toString().
>
> Now, from the XMI resource we have to retrieve the corresponding cdo
> object from the repository by giving the Cdo ID string. Is there any
> facility method for this purpose ?
>
> Note: I have found on the wiki CDOID id =
> CDOUtil.extractResourceID(URI.create("cdo:17463524"));
> but this method is not visible on CDOUtils on CDO 2.0.
> The other solution is to execute a query, but as we would like to use
> this mechanism in a merge, if we have to execute a query on each
> element of my treeiterator, it is heavy.
> I use Eclipse 3.5 and CDO 2.0.
--------------000803030504040507000105
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Hi Sabri,<br>
<br>
Here is a 2.0 example:<br>
<br>
<title></title>
<style type="text/css">
<!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- ======================================================== -->
<!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard markus@jave.de = -->
<!-- = Further information: http://www.java2html.de = -->
<div class="java" align="left">
<table bgcolor="#ffffff" border="0" cellpadding="3" cellspacing="0">
<tbody>
<tr>
<!-- start source code --> <td align="left" nowrap="nowrap"
valign="top"> <code><font color="#ffffff"> </font><font
color="#000000">CDOIDObjectFactory cdoidObjectFactory = cdoView </font><font
color="#000000">()</font><font color="#000000">.getSession</font><font
color="#000000">()</font><font color="#000000">;</font><br>
<font color="#ffffff"> </font><font color="#000000">CDOID cdoID = CDOIDUtil.read</font><font
color="#000000">(</font><font color="#000000">uriFragment, cdoidObjectFactory</font><font
color="#000000">)</font><font color="#000000">;</font></code> </td>
<!-- end source code --> </tr>
</tbody>
</table>
</div>
<!-- = END of automatically generated HTML code = -->
<!-- ======================================================== --><br>
Note, that in 3.0 there are no ID factories anymore:<br>
<br>
<title></title>
<style type="text/css">
<!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- ======================================================== -->
<!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard markus@jave.de = -->
<!-- = Further information: http://www.java2html.de = -->
<div class="java" align="left">
<table bgcolor="#ffffff" border="0" cellpadding="3" cellspacing="0">
<tbody>
<tr>
<!-- start source code --> <td align="left" nowrap="nowrap"
valign="top"> <code><font color="#ffffff"> </font><font
color="#000000">CDOID cdoID = CDOIDUtil.read</font><font
color="#000000">(</font><font color="#000000">uriFragment</font><font
color="#000000">)</font><font color="#000000">;</font></code> </td>
<!-- end source code --> </tr>
</tbody>
</table>
</div>
<!-- = END of automatically generated HTML code = -->
<!-- ======================================================== --><br>
Cheers<br>
/Eike<br>
<br>
----<br>
<a class="moz-txt-link-freetext" href="http://thegordian.blogspot.com">http://thegordian.blogspot.com</a><br>
<a class="moz-txt-link-freetext" href="http://twitter.com/eikestepper">http://twitter.com/eikestepper</a><br>
<br>
<br>
Am 01.03.2010 17:40, schrieb Sabri Skhiri:
<blockquote cite="mid:hmgqm8$7ac$1@build.eclipse.org" type="cite">Hi,
<br>
<br>
We are implementing a prototype of exportation of subset of a model
stored in CDO within an EMF resource. This EMF resource is then
modified and we use EMF compare for comparing the exported and the
stored models.
<br>
When we export the CDO Objects, we set the XMI ID with the CDO ID in
order to keep the right correspondence between the exported and the
stored model. Then, our ID provider just extract the cdoid.toString().
<br>
<br>
Now, from the XMI resource we have to retrieve the corresponding cdo
object from the repository by giving the Cdo ID string. Is there any
facility method for this purpose ?
<br>
<br>
Note: I have found on the wiki CDOID id =
CDOUtil.extractResourceID(URI.create("cdo:17463524"));
<br>
but this method is not visible on CDOUtils on CDO 2.0.
<br>
The other solution is to execute a query, but as we would like to use
this mechanism in a merge, if we have to execute a query on each
element of my treeiterator, it is heavy. <br>
I use Eclipse 3.5 and CDO 2.0. </blockquote>
</body>
</html>
--------------000803030504040507000105--
Cheers
/Eike
----
http://www.esc-net.de
http://thegordian.blogspot.com
http://twitter.com/eikestepper
|
|
| | |
Re: [CDO] CDOID.toString() => String to CDO ID ? [message #520235 is a reply to message #520148] |
Thu, 11 March 2010 16:17 |
|
Hi Sabri,
Sorry I must finish for today. I'll look at it tomorrow...
Cheers
/Eike
----
http://thegordian.blogspot.com
http://twitter.com/eikestepper
Am 11.03.2010 11:51, schrieb Sabri Skhiri:
> Hi Eike,
>
> I tried your solution but I have got problems with URI fragment, it
> seems there is a bug.
> I got the URI fragment directly from a CDO object, and the code you
> advised did not work. Let's see in code:
>
> // open the transaction
> CDOTransaction transaction = session.openTransaction();
> // get the resource
> CDOResource resource =
> transaction.getResource("/edine/resto2.inventory");
> //get the first object and cast it on business object
> Restaurant restaurant = (Restaurant) resource.getContents().get(0);
>
> A code inspect at this stage gives:
> Got restaurant with CDO ID = OID14, URI Fragment= 14
> Now we can use the code you answered in this post:
> cdoID = CDOIDUtil.read(restaurant.cdoID().toURIFragment(),
> cdoidObjectFactory);
> Throws an exception:
> java.lang.NumberFormatException: For input string: ""
> at java.lang.NumberFormatException.forInputString(NumberFormatE
> xception.java:48)
> at java.lang.Long.parseLong(Long.java:431)
> at java.lang.Long.valueOf(Long.java:525)
> at org.eclipse.emf.cdo.spi.common.id.AbstractCDOIDLong.read(Abs
> tractCDOIDLong.java:59)
> at org.eclipse.emf.cdo.common.id.CDOIDUtil.read(CDOIDUtil.java: 181)
>
> More over, if use the code:
> CDOIDObjectFactory cdoidObjectFactory =(CDOIDObjectFactory)
> resource.cdoView().getSession();
> EObject resto = resource.getEObject(restaurant.cdoID().toURIFragment());
> System.out.println("Got the resto " + resto);
> In this case the resto variable is null.
>
> Do you know what's wrong ?
>
> Thank you,
>
>
>
Cheers
/Eike
----
http://www.esc-net.de
http://thegordian.blogspot.com
http://twitter.com/eikestepper
|
|
|
Re: [CDO] CDOID.toString() => String to CDO ID ? [message #520428 is a reply to message #520148] |
Fri, 12 March 2010 10:45 |
|
This is a multi-part message in MIME format.
--------------030901030702030201000304
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Ok, my fault. It must be like:
| *public **void *testURIFragment() *throws *Exception
{
CDOSession session = openModel1Session();
CDOTransaction transaction = session.openTransaction();
CDOResource resource = transaction.createResource("/test1");
Supplier supplier = getModel1Factory().createSupplier();
supplier.setName("Stepper");
resource.getContents().add(supplier);
transaction.commit();
StringBuilder builder = *new *StringBuilder();
CDOIDUtil.write(builder, CDOUtil.getCDOObject(supplier).cdoID());
String uriFragment = builder.toString();
System.out.println(uriFragment);
CDOID id = CDOIDUtil.read(uriFragment, (CDOIDObjectFactory)session);
System.out.println(id);
}|
Cheers
/Eike
----
http://thegordian.blogspot.com
http://twitter.com/eikestepper
Am 11.03.2010 11:51, schrieb Sabri Skhiri:
> Hi Eike,
>
> I tried your solution but I have got problems with URI fragment, it
> seems there is a bug.
> I got the URI fragment directly from a CDO object, and the code you
> advised did not work. Let's see in code:
>
> // open the transaction
> CDOTransaction transaction = session.openTransaction();
> // get the resource
> CDOResource resource =
> transaction.getResource("/edine/resto2.inventory");
> //get the first object and cast it on business object
> Restaurant restaurant = (Restaurant) resource.getContents().get(0);
>
> A code inspect at this stage gives:
> Got restaurant with CDO ID = OID14, URI Fragment= 14
> Now we can use the code you answered in this post:
> cdoID = CDOIDUtil.read(restaurant.cdoID().toURIFragment(),
> cdoidObjectFactory);
> Throws an exception:
> java.lang.NumberFormatException: For input string: ""
> at java.lang.NumberFormatException.forInputString(NumberFormatE
> xception.java:48)
> at java.lang.Long.parseLong(Long.java:431)
> at java.lang.Long.valueOf(Long.java:525)
> at org.eclipse.emf.cdo.spi.common.id.AbstractCDOIDLong.read(Abs
> tractCDOIDLong.java:59)
> at org.eclipse.emf.cdo.common.id.CDOIDUtil.read(CDOIDUtil.java: 181)
>
> More over, if use the code:
> CDOIDObjectFactory cdoidObjectFactory =(CDOIDObjectFactory)
> resource.cdoView().getSession();
> EObject resto = resource.getEObject(restaurant.cdoID().toURIFragment());
> System.out.println("Got the resto " + resto);
> In this case the resto variable is null.
>
> Do you know what's wrong ?
>
> Thank you,
>
>
>
--------------030901030702030201000304
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Ok, my fault. It must be like:<br>
<br>
<title></title>
<style type="text/css">
<!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- ======================================================== -->
<!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard markus@jave.de = -->
<!-- = Further information: http://www.java2html.de = -->
<div class="java" align="left">
<table bgcolor="#ffffff" border="0" cellpadding="3" cellspacing="0">
<tbody>
<tr>
<!-- start source code --> <td align="left" nowrap="nowrap"
valign="top"> <code><font color="#ffffff"> </font><font
color="#7f0055"><b>public </b></font><font color="#7f0055"><b>void </b></font><font
color="#000000">testURIFragment</font><font color="#000000">() </font><font
color="#7f0055"><b>throws </b></font><font color="#000000">Exception</font><br>
<font color="#ffffff"> </font><font color="#000000">{</font><br>
<font color="#ffffff"> </font><font color="#000000">CDOSession session = openModel1Session</font ><font
color="#000000">()</font><font color="#000000">;</font><br>
<font color="#ffffff"> </font><font color="#000000">CDOTransaction transaction = session.openTransaction </font><font
color="#000000">()</font><font color="#000000">;</font><br>
<font color="#ffffff"> </font><font color="#000000">CDOResource resource = transaction.createResource </font><font
color="#000000">(</font><font color="#2a00ff">"/test1"</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff"> </font><font color="#000000">Supplier supplier = getModel1Factory</font ><font
color="#000000">()</font><font color="#000000">.createSupplier</font><font
color="#000000">()</font><font color="#000000">;</font><br>
<font color="#ffffff"> </font><font color="#000000">supplier.setName</font><font
color="#000000">(</font><font color="#2a00ff">"Stepper"</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff"> </font><font color="#000000">resource.getContents</font><font
color="#000000">()</font><font color="#000000">.add</font><font
color="#000000">(</font><font color="#000000">supplier</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff"> </font><font color="#000000">transaction.commit</font><font
color="#000000">()</font><font color="#000000">;</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff"> </font><font color="#000000">StringBuilder builder = </font><font
color="#7f0055"><b>new </b></font><font color="#000000">StringBuilder</font><font
color="#000000">()</font><font color="#000000">;</font><br>
<font color="#ffffff"> </font><font color="#000000">CDOIDUtil.write</font><font
color="#000000">(</font><font color="#000000">builder, CDOUtil.getCDOObject</font><font
color="#000000">(</font><font color="#000000">supplier</font><font
color="#000000">)</font><font color="#000000">.cdoID</font><font
color="#000000">())</font><font color="#000000">;</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff"> </font><font color="#000000">String uriFragment = builder.toString</font ><font
color="#000000">()</font><font color="#000000">;</font><br>
<font color="#ffffff"> </font><font color="#000000">System.out.println</font><font
color="#000000">(</font><font color="#000000">uriFragment</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff"> </font><font color="#000000">CDOID id = CDOIDUtil.read</font><font
color="#000000">(</font><font color="#000000">uriFragment, </font><font
color="#000000">(</font><font color="#000000">CDOIDObjectFactory</font><font
color="#000000">)</font><font color="#000000">session</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff"> </font><font color="#000000">System.out.println</font><font
color="#000000">(</font><font color="#000000">id</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff"> </font><font color="#000000">}</font></code>
</td>
<!-- end source code --> </tr>
</tbody>
</table>
</div>
<!-- = END of automatically generated HTML code = -->
<!-- ======================================================== --><br>
Cheers<br>
/Eike<br>
<br>
----<br>
<a class="moz-txt-link-freetext" href="http://thegordian.blogspot.com">http://thegordian.blogspot.com</a><br>
<a class="moz-txt-link-freetext" href="http://twitter.com/eikestepper">http://twitter.com/eikestepper</a><br>
<br>
<br>
<br>
Am 11.03.2010 11:51, schrieb Sabri Skhiri:
<blockquote cite="mid:hnai0a$sog$1@build.eclipse.org" type="cite">Hi
Eike,
<br>
<br>
I tried your solution but I have got problems with URI fragment, it
seems there is a bug.
<br>
I got the URI fragment directly from a CDO object, and the code you
advised did not work. Let's see in code:
<br>
<br>
// open the transaction
<br>
CDOTransaction transaction = session.openTransaction();
<br>
// get the resource
<br>
CDOResource resource =
transaction.getResource("/edine/resto2.inventory");
<br>
//get the first object and cast it on business object
<br>
Restaurant restaurant = (Restaurant) resource.getContents().get(0);
<br>
<br>
A code inspect at this stage gives:
<br>
Got restaurant with CDO ID = OID14, URI Fragment= 14
<br>
Now we can use the code you answered in this post:
<br>
cdoID = CDOIDUtil.read(restaurant.cdoID().toURIFragment(),
cdoidObjectFactory);
<br>
Throws an exception:
<br>
java.lang.NumberFormatException: For input string: ""
<br>
at java.lang.NumberFormatException.forInputString(NumberFormatE
xception.java:48)
<br>
at java.lang.Long.parseLong(Long.java:431)
<br>
at java.lang.Long.valueOf(Long.java:525)
<br>
at org.eclipse.emf.cdo.spi.common.id.AbstractCDOIDLong.read(Abs
tractCDOIDLong.java:59)
<br>
at org.eclipse.emf.cdo.common.id.CDOIDUtil.read(CDOIDUtil.java:
181)
<br>
<br>
More over, if use the code:
<br>
CDOIDObjectFactory cdoidObjectFactory =(CDOIDObjectFactory)
resource.cdoView().getSession();
<br>
EObject resto =
resource.getEObject(restaurant.cdoID().toURIFragment());
<br>
System.out.println("Got the resto " + resto);
<br>
In this case the resto variable is null.
<br>
<br>
Do you know what's wrong ?
<br>
<br>
Thank you,
<br>
<br>
<br>
<br>
</blockquote>
</body>
</html>
--------------030901030702030201000304--
Cheers
/Eike
----
http://www.esc-net.de
http://thegordian.blogspot.com
http://twitter.com/eikestepper
|
|
| |
Re: [CDO] CDOID.toString() => String to CDO ID ? [message #520772 is a reply to message #520758] |
Mon, 15 March 2010 09:54 |
|
Am 15.03.2010 09:40, schrieb Sabri Skhiri:
> The code works, but let me come back on my first question, how can I
> can retrieve the CDO ID from a serialized-string ? (for instance "OID14)
> In your snippet you use:
> StringBuilder builder = *new *StringBuilder();
> CDOIDUtil.write(builder, CDOUtil.getCDOObject(supplier).cdoID());
> String uriFragment = builder.toString();
> The CDOIDUtil uses the CDO ID object to get:
> 1. the ordinal
> 2. the uri fragment
> As a result if I serialized the CDO ID as a string, I cannot rebuild
> the URI fragment and therefore, I cannot get the corresponding CDO
> Object.
> Does it means that the only way to get the corresponding CDO Object is
> to serialized directly the URI Fragment ?
I'm not sure I understand your point. Didn't the test code snippet
demonstrate that you *can* CDOUtil.read() the serialized form?!
BTW. CDOID extends Serializeable, hence you could also use Java
serialization. but the result would probably not be human readable.
Cheers
/Eike
----
http://thegordian.blogspot.com
http://twitter.com/eikestepper
Cheers
/Eike
----
http://www.esc-net.de
http://thegordian.blogspot.com
http://twitter.com/eikestepper
|
|
| | |
Goto Forum:
Current Time: Thu Apr 25 13:59:32 GMT 2024
Powered by FUDForum. Page generated in 0.03163 seconds
|