How to instantiate an Item from its identifier?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to instantiate an Item from its identifier?

Sam Hokin-3
Hi, devs. This has to be something folks do all the time, but I'm stumped on the class/method for doing it. I've got a Collection of
Items stored in an Item, and I'd like to instantiate them. In other words, with an Item bigItem already existing with a Collection
called smallItems:

ReferenceList smallItemRefList = bigItem.getCollection("smallItems");
if (smallItemRefList!=null) {
        List<String> smallItemRefIdList = smallItemRefList.getRefIds();
        for (String smallItemRefId : smallItemRefIdList) {
          // how can I instantiate this SmallItem given its smallItemRefId????
         }
}

So far I've been creating lots of maps to store Items separately, but it sure would be nice if I could use the InterMine API to
instantiate Items that already exist in a Collection!

Thanks!
Sam

_______________________________________________
dev mailing list
[hidden email]
http://mail.intermine.org/cgi-bin/mailman/listinfo/dev
Reply | Threaded
Open this post in threaded view
|

Re: How to instantiate an Item from its identifier?

Justin Clark-Casey
Hi Sam.  I might have the wrong end of the stick here.  If I do, a bit more context for this code would be great.  However, when you add an Item to a collection
(e.g. via Item.addToCollection(String name, Item item) then it is only the reference ID that is stored, not the Item object.

If the Item has already been written to the objectstore then it should be available via ObjectStore.getObjectById(Integer id).  Otherwise, I believe you have to
keep your own map of the Items as you've been doing.

--
Justin Clark-Casey, Synbiomine/InterMine Developer
http://synbiomine.org
http://twitter.com/justincc

On 23/02/16 22:13, Sam Hokin wrote:

> Hi, devs. This has to be something folks do all the time, but I'm stumped on the class/method for doing it. I've got a Collection of Items stored in an Item,
> and I'd like to instantiate them. In other words, with an Item bigItem already existing with a Collection called smallItems:
>
> ReferenceList smallItemRefList = bigItem.getCollection("smallItems");
> if (smallItemRefList!=null) {
>      List<String> smallItemRefIdList = smallItemRefList.getRefIds();
>      for (String smallItemRefId : smallItemRefIdList) {
>              // how can I instantiate this SmallItem given its smallItemRefId????
>          }
> }
>
> So far I've been creating lots of maps to store Items separately, but it sure would be nice if I could use the InterMine API to instantiate Items that already
> exist in a Collection!
>
> Thanks!
> Sam
>
> _______________________________________________
> dev mailing list
> [hidden email]
> http://mail.intermine.org/cgi-bin/mailman/listinfo/dev

_______________________________________________
dev mailing list
[hidden email]
http://mail.intermine.org/cgi-bin/mailman/listinfo/dev
Reply | Threaded
Open this post in threaded view
|

Re: How to instantiate an Item from its identifier?

Sam Hokin-3
On 02/25/2016 09:17 AM, Justin Clark-Casey wrote:
> Hi Sam.  I might have the wrong end of the stick here.  If I do, a bit more context for this code would be great.  However, when you
> add an Item to a collection (e.g. via Item.addToCollection(String name, Item item) then it is only the reference ID that is stored,
> not the Item object.
>
> If the Item has already been written to the objectstore then it should be available via ObjectStore.getObjectById(Integer id).
> Otherwise, I believe you have to keep your own map of the Items as you've been doing.

You got it, Justin, thanks! The Item in the collection had, in fact, already been written to the object store, but that's an
important fact to keep in mind. ObjectStore.getObjectById(Integer id) is exactly what I'm looking for!

Cheers!
Sam

_______________________________________________
dev mailing list
[hidden email]
http://mail.intermine.org/cgi-bin/mailman/listinfo/dev