Example usage for org.springframework.transaction.support TransactionCallbackWithoutResult TransactionCallbackWithoutResult

List of usage examples for org.springframework.transaction.support TransactionCallbackWithoutResult TransactionCallbackWithoutResult

Introduction

In this page you can find the example usage for org.springframework.transaction.support TransactionCallbackWithoutResult TransactionCallbackWithoutResult.

Prototype

TransactionCallbackWithoutResult

Source Link

Usage

From source file:ubc.pavlab.aspiredb.server.project.ProjectManagerTest.java

@Test
public void testAddSubjectVariantsToProjectSecurity() {

    super.runAsAdmin();

    final String patientId = RandomStringUtils.randomAlphabetic(5);
    final String projectName = RandomStringUtils.randomAlphabetic(5);

    CharacteristicValueObject cvo = new CharacteristicValueObject();

    cvo.setKey("testChar");
    cvo.setValue("testcharvalue");

    Map<String, CharacteristicValueObject> charMap = new HashMap<String, CharacteristicValueObject>();
    charMap.put(cvo.getKey(), cvo);//w w  w .  j  a  v  a 2  s  . c  o  m

    CNVValueObject cnv = new CNVValueObject();

    cnv.setCharacteristics(charMap);
    cnv.setType("GAIN");

    GenomicRange gr = new GenomicRange("X", 3, 234);

    cnv.setGenomicRange(gr);

    cnv.setPatientId(patientId);

    // Another CNV from the same patient
    CNVValueObject cnv2 = new CNVValueObject();
    cnv2.setCharacteristics(charMap);
    cnv2.setType("LOSS");
    cnv2.setGenomicRange(gr);
    cnv2.setPatientId(patientId);

    ArrayList<VariantValueObject> cnvList = new ArrayList<VariantValueObject>();
    cnvList.add(cnv);
    cnvList.add(cnv2);

    try {

        projectManager.addSubjectVariantsToProject(projectName, true, cnvList);

    } catch (Exception e) {
        log.error(e.getLocalizedMessage(), e);
        fail("projectManager.addSubjectVariantsToProject threw an exception " + e);

    }

    projectManager.alterGroupWritePermissions(projectName, groupName, true);

    TransactionTemplate tt = new TransactionTemplate(transactionManager);
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {

            Project project = projectDao.findByProjectName(projectName);

            aclTestUtils.checkHasAcl(project);

            assertFalse(
                    "Project should not be viewable by user '" + aDifferentUsername + "', acl is "
                            + aclTestUtils.getAcl(project),
                    securityService.isViewableByUser(project, aDifferentUsername));

            // now to test
            List<Subject> subjects = project.getSubjects();
            assertEquals(1, subjects.size());

            Subject subject = project.getSubjects().iterator().next();

            aclTestUtils.checkHasAcl(subject);

            assertFalse(securityService.isViewableByUser(subject, aDifferentUsername));

            // equals to cnvList
            assertEquals(2, subject.getVariants().size());

            variantDao.findBySubjectPatientId(project.getId(), patientId);

            // for ( Variant v : variantCollection ) {
            // // aclTestUtils.checkHasAcl( v );
            // // assertFalse( securityService.isViewableByUser( v, aDifferentUsername ) );
            //
            // Characteristic c = v.getCharacteristics().iterator().next();
            // aclTestUtils.checkHasAcl( c );
            // assertFalse( securityService.isViewableByUser( c, aDifferentUsername ) );
            // }

        }
    });

    try {

        projectManager.deleteProject(projectName);

    } catch (Exception e) {
        log.error(e.getLocalizedMessage(), e);

    }

}

From source file:ubc.pavlab.aspiredb.server.project.ProjectManagerTest.java

@Test
public void testAlterGroupWritePermissionsForProject() {

    super.runAsAdmin();

    final String patientId = RandomStringUtils.randomAlphabetic(5);
    final String projectName = RandomStringUtils.randomAlphabetic(7);

    CharacteristicValueObject cvo = new CharacteristicValueObject();

    cvo.setKey("testChar");
    cvo.setValue("testcharvalue");

    Map<String, CharacteristicValueObject> charMap = new HashMap<String, CharacteristicValueObject>();
    charMap.put(cvo.getKey(), cvo);// ww  w .  ja  v a  2 s .  c o  m

    CNVValueObject cnv = new CNVValueObject();

    cnv.setCharacteristics(charMap);
    cnv.setType("GAIN");

    GenomicRange gr = new GenomicRange("X", 3, 234);

    cnv.setGenomicRange(gr);

    cnv.setPatientId(patientId);

    ArrayList<VariantValueObject> cnvList = new ArrayList<VariantValueObject>();
    cnvList.add(cnv);

    try {

        projectManager.addSubjectVariantsToProject(projectName, true, cnvList);

    } catch (Exception e) {

        fail("projectManager.addSubjectVariantsToProject threw an exception");

        e.printStackTrace();
    }

    // authorizedUsername is in groupName
    projectManager.alterGroupWritePermissions(projectName, groupName, true);

    // make sure authorizedUsername has read access to all the stuff in a project after security change
    TransactionTemplate tt = new TransactionTemplate(transactionManager);
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {

            Project project = projectDao.findByProjectName(projectName);

            assertTrue(securityService.isViewableByUser(project, authorizedUsername));

            Subject subject = project.getSubjects().iterator().next();

            assertTrue(securityService.isViewableByUser(subject, authorizedUsername));

            variantDao.findBySubjectPatientId(project.getId(), patientId);

            // for ( Variant v : variantCollection ) {
            //
            // assertTrue( securityService.isViewableByUser( v, authorizedUsername ) );
            //
            // assertTrue( securityService.isViewableByUser( v.getCharacteristics().iterator().next(),
            // authorizedUsername ) );
            // }

        }
    });

    // make sure aDifferentUserName doesn't have access to stuff in project
    TransactionTemplate tt2 = new TransactionTemplate(transactionManager);
    tt2.execute(new TransactionCallbackWithoutResult() {
        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {

            Project project = projectDao.findByProjectName(projectName);

            assertFalse(securityService.isViewableByUser(project, aDifferentUsername));

            Subject subject = project.getSubjects().iterator().next();

            assertFalse(securityService.isViewableByUser(subject, aDifferentUsername));

            variantDao.findBySubjectPatientId(project.getId(), patientId);

            // for ( Variant v : variantCollection ) {
            //
            // assertFalse( securityService.isViewableByUser( v, aDifferentUsername ) );
            //
            // assertFalse( securityService.isViewableByUser( v.getCharacteristics().iterator().next(),
            // aDifferentUsername ) );
            // }

        }
    });

    // test out a couple of dao methods

    super.runAsUser(authorizedUsername);

    long projectId = projectDao.findByProjectName(projectName).getId();

    variantDao.findBySubjectPatientId(projectId, patientId);

    super.runAsUser(aDifferentUsername);

    try {
        projectId = projectDao.findByProjectName(projectName).getId();
        fail("should have got Access Denied");
    } catch (AccessDeniedException e) {

    }

    Collection<Variant> vCollection2 = variantDao.findBySubjectPatientId(projectId, patientId);

    assertTrue(vCollection2.isEmpty());

    super.runAsAdmin();
    // test removing permissions

    // authorizedUsername is in groupName
    projectManager.alterGroupWritePermissions(projectName, groupName, false);

    // make sure authorizedUsername does not have read access to all the stuff in a project after security change
    TransactionTemplate tt3 = new TransactionTemplate(transactionManager);
    tt3.execute(new TransactionCallbackWithoutResult() {
        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {

            Project project = projectDao.findByProjectName(projectName);

            assertFalse(securityService.isViewableByUser(project, authorizedUsername));

            Subject subject = project.getSubjects().iterator().next();

            assertFalse(securityService.isViewableByUser(subject, authorizedUsername));

            variantDao.findBySubjectPatientId(project.getId(), patientId);

            // for ( Variant v : variantCollection ) {
            //
            // assertFalse( securityService.isViewableByUser( v, authorizedUsername ) );
            //
            // assertFalse( securityService.isViewableByUser( v.getCharacteristics().iterator().next(),
            // authorizedUsername ) );
            // }

        }
    });

    // test out a couple of dao methods to make sure authorizedUsername has no access
    super.runAsUser(authorizedUsername);

    try {
        projectId = projectDao.findByProjectName(projectName).getId();
        fail("should have got Access Denied");
    } catch (AccessDeniedException e) {

    }

    Collection<Variant> vCollection3 = variantDao.findBySubjectPatientId(projectId, patientId);

    assertTrue(vCollection3.isEmpty());

    super.runAsAdmin();

    try {

        projectManager.deleteProject(projectName);

    } catch (Exception e) {
        log.error(e.getLocalizedMessage(), e);

    }

}

From source file:xc.mst.repo.DefaultRepository.java

public void installOrUpdateIfNecessary(final String previousVersion, final String currentVersion) {
    final Repository thisthis = this;
    this.transactionTemplate.execute(new TransactionCallbackWithoutResult() {
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            try {
                LOG.debug("config.getProperty(\"version\"): " + config.getProperty("version"));
                if (previousVersion == null && currentVersion != null
                        && (currentVersion.startsWith("0.3.") || currentVersion.startsWith("1."))) {
                    getRepositoryDAO().createTables(thisthis);
                }/* w w w  .jav a 2  s  .c  om*/
            } catch (Throwable t) {
                LOG.error("", t);
                status.setRollbackOnly();
            }
        }
    });
}