public void tearDown() throws Exception { LOG.info("in tear down"); ObjectStoreWriter osw = ObjectStoreWriterFactory.getObjectStoreWriter("osw.unittest"); if (osw.isInTransaction()) { osw.abortTransaction(); } Query q = new Query(); QueryClass qc = new QueryClass(InterMineObject.class); q.addFrom(qc); q.addToSelect(qc); SingletonResults res = osw.getObjectStore().executeSingleton(q); LOG.info("created results"); Iterator resIter = res.iterator(); osw.beginTransaction(); while (resIter.hasNext()) { InterMineObject o = (InterMineObject) resIter.next(); LOG.info("deleting: " + o.getId()); osw.delete(o); } osw.commitTransaction(); LOG.info("committed transaction"); osw.close(); LOG.info("closed objectstore"); }
private int removeOrphanTags() { // Basically: // select tag.id // from tag, (select array(select id from userprofile) as ids) as sq // where tag.userprofileid IS NULL OR tag.userprofileid <> ALL (sq.ids) // order by tag.id ObjectStoreWriter osw = getUserProfile(); Query q = new Query(); QueryClass qc = new QueryClass(Tag.class); q.setConstraint(new SimpleConstraint(new QueryField(qc, "userProfile"), ConstraintOp.IS_NULL)); Set<Object> res; try { res = osw.executeSingleton(q); } catch (Exception e) { throw new BuildException("Could not retrieve any tags", e); } try { osw.beginTransaction(); for (Object o : res) { osw.delete((Tag) o); } osw.commitTransaction(); } catch (Exception e) { throw new BuildException("Error deleting tags", e); } finally { if (osw != null) { try { if (osw.isInTransaction()) { osw.abortTransaction(); } } catch (ObjectStoreException e) { throw new BuildException("Could not even manage transactions here...", e); } } } return res.size(); }