Search in sources :

Example 1 with JPAQueryFactory

use of com.mysema.query.jpa.impl.JPAQueryFactory in project opencast by opencast.

the class AbstractADeleteQuery method run.

public long run(DeleteSnapshotHandler deleteSnapshotHandler) {
    // run query and map the result to records
    final long startTime = System.nanoTime();
    // resolve AST
    final DeleteQueryContribution c = contributeDelete(owner);
    // run all queries in a single transaction
    final DeletionResult deletion = am.getDb().run(new Fn<JPAQueryFactory, DeletionResult>() {

        @Override
        public DeletionResult apply(final JPAQueryFactory jpa) {
            return runQueries(jpa, c);
        }
    });
    logger.debug("Pure query ms " + (System.nanoTime() - startTime) / 1000000);
    // delete from store
    for (Tuple t : deletion.deletedSnapshots) {
        // all three t.get(..) calls won't return null since the database fields are not null.
        final String orgId = t.get(Q_SNAPSHOT.organizationId);
        final String mpId = t.get(Q_SNAPSHOT.mediaPackageId);
        final VersionImpl version = Conversions.toVersion(t.get(Q_SNAPSHOT.version));
        am.getAssetStore().delete(DeletionSelector.delete(orgId, mpId, version));
        deleteSnapshotHandler.notifyDeleteSnapshot(mpId, version);
    }
    for (String mpId : deletion.deletedEpisodes) {
        deleteSnapshotHandler.notifyDeleteEpisode(mpId);
    }
    final long searchTime = (System.nanoTime() - startTime) / 1000000;
    logger.debug("Complete query ms " + searchTime);
    return deletion.deletedItemsCount;
}
Also used : JPAQueryFactory(com.mysema.query.jpa.impl.JPAQueryFactory) Tuple(com.mysema.query.Tuple) VersionImpl(org.opencastproject.assetmanager.impl.VersionImpl)

Example 2 with JPAQueryFactory

use of com.mysema.query.jpa.impl.JPAQueryFactory in project dwoss by gg-net.

the class PersistenceIT method testPersistence.

@Test
public void testPersistence() throws Exception {
    JPAQueryFactory queryFactory = new JPAQueryFactory(() -> em);
    utx.begin();
    em.joinTransaction();
    Stock s1 = new Stock(1);
    s1.setName("1111111111111111");
    Stock s2 = new Stock(2);
    s2.setName("2222222222222222");
    em.persist(s1);
    utx.commit();
    utx.begin();
    em.joinTransaction();
    s1 = em.merge(s1);
    s2 = em.merge(s2);
    StockUnit su1 = new StockUnit("g1", 1);
    StockUnit su2 = new StockUnit("g2", 2);
    StockUnit su3 = new StockUnit("g3", 3);
    StockUnit su4 = new StockUnit("g4", 4);
    s1.addUnit(su1);
    s1.addUnit(su2);
    s1.addUnit(su3);
    s2.addUnit(su4);
    em.persist(su1);
    em.persist(su2);
    em.persist(su3);
    em.persist(su4);
    utx.commit();
    utx.begin();
    em.joinTransaction();
    List<Stock> sus = queryFactory.query().from(QStock.stock).list(QStock.stock);
    assertTrue(sus.size() == 2);
    Stock st1 = sus.get(0);
    if (st1.getId() == s2.getId()) {
        assertTrue(st1.getUnits().size() == 1);
    } else {
        assertTrue(st1.getUnits().size() == 3);
    }
    Stock st2 = sus.get(1);
    StockTransaction t1 = new StockTransaction(StockTransactionType.TRANSFER);
    // TODO: Long comment fails test.
    StringBuilder sb = new StringBuilder("Adding a");
    for (int i = 0; i < 255; i++) {
        sb.append(" very");
    }
    sb.append("long comment");
    t1.setComment(sb.toString());
    t1.setSource(st1);
    t1.setDestination(st2);
    List<StockUnit> sustemp = new ArrayList<>(st1.getUnits());
    for (Iterator<StockUnit> isu = sustemp.iterator(); isu.hasNext(); ) {
        StockUnit su = isu.next();
        t1.addPosition(new StockTransactionPosition(su));
        su.setStock(null);
    }
    Date d = new Date();
    StockTransactionStatus init = new StockTransactionStatus(StockTransactionStatusType.PREPARED, d);
    init.addParticipation(new StockTransactionParticipation(StockTransactionParticipationType.PICKER, "Hans"));
    t1.addStatus(init);
    // Why add seconds ? Ask Olli.
    StockTransactionStatus commision = new StockTransactionStatus(StockTransactionStatusType.COMMISSIONED, DateUtils.addSeconds(d, 1));
    // Has something todo with the convertion of util.Date -> sql.Date -> util.Date. The returned value shows milliseconds an is in the past. Magic.
    commision.addParticipation(new StockTransactionParticipation(StockTransactionParticipationType.PICKER, "User1", true));
    commision.addParticipation(new StockTransactionParticipation(StockTransactionParticipationType.DELIVERER, "User2", true));
    t1.addStatus(commision);
    em.persist(t1);
    utx.commit();
    utx.begin();
    em.joinTransaction();
    t1 = em.find(StockTransaction.class, t1.getId());
    st2 = em.find(Stock.class, st2.getId());
    // Why add seconds ? Ask Olli.
    StockTransactionStatus transfer = new StockTransactionStatus(StockTransactionStatusType.IN_TRANSFER, DateUtils.addSeconds(d, 2));
    transfer.addParticipation(new StockTransactionParticipation(StockTransactionParticipationType.DELIVERER, "User3", false));
    t1.addStatus(transfer);
    StockTransactionPosition removePos = t1.getPositions().get(0);
    removePos.getStockUnit().setStock(st2);
    em.remove(removePos);
    utx.commit();
    utx.begin();
    em.joinTransaction();
    t1 = em.find(StockTransaction.class, t1.getId());
    // Why add seconds ? Ask Olli.
    StockTransactionStatus receive = new StockTransactionStatus(StockTransactionStatusType.RECEIVED, DateUtils.addSeconds(d, 3));
    receive.addParticipation(new StockTransactionParticipation(StockTransactionParticipationType.RECEIVER, "User4", true));
    receive.addParticipation(new StockTransactionParticipation(StockTransactionParticipationType.DELIVERER, "User5", true));
    t1.addStatus(receive);
    utx.commit();
    utx.begin();
    em.joinTransaction();
    SearchResults<StockTransaction> tResult = queryFactory.from(QStockTransaction.stockTransaction).listResults(QStockTransaction.stockTransaction);
    assertThat(tResult.getTotal()).isEqualTo(1);
    StockTransaction t2 = tResult.getResults().get(0);
    boolean b1 = false;
    boolean b2 = false;
    boolean b3 = false;
    for (StockTransactionParticipation stp : t2.getParticipations()) {
        switch(stp.getType()) {
            case DELIVERER:
                b1 = true;
                break;
            case PICKER:
                b2 = true;
                break;
            case RECEIVER:
                b3 = true;
        }
    }
    assertTrue(b1 && b2 && b3);
    utx.commit();
    utx.begin();
    em.joinTransaction();
    t1 = em.find(StockTransaction.class, t1.getId());
    for (StockTransactionPosition position : t1.getPositions()) {
        t1.getDestination().addUnit(position.getStockUnit());
        position.setStockUnit(null);
    }
    utx.commit();
    utx.begin();
    em.joinTransaction();
    List<StockUnit> units = em.createNamedQuery("all", StockUnit.class).getResultList();
    LogicTransaction lt = new LogicTransaction();
    lt.setDossierId(2);
    for (StockUnit stockUnit : units) {
        lt.add(stockUnit);
    }
    em.persist(lt);
    utx.commit();
}
Also used : JPAQueryFactory(com.mysema.query.jpa.impl.JPAQueryFactory) Test(org.junit.Test)

Aggregations

JPAQueryFactory (com.mysema.query.jpa.impl.JPAQueryFactory)2 Tuple (com.mysema.query.Tuple)1 Test (org.junit.Test)1 VersionImpl (org.opencastproject.assetmanager.impl.VersionImpl)1