Example usage for org.springframework.jdbc.core JdbcTemplate queryForMap

List of usage examples for org.springframework.jdbc.core JdbcTemplate queryForMap

Introduction

In this page you can find the example usage for org.springframework.jdbc.core JdbcTemplate queryForMap.

Prototype

@Override
    public Map<String, Object> queryForMap(String sql, @Nullable Object... args) throws DataAccessException 

Source Link

Usage

From source file:com.company.project.service.dao.PersistentTokenDao.java

public Map<String, Object> get(String series) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    Map<String, Object> map = null;
    map = jdbcTemplate.queryForMap("SELECT * from persistent_logins where series = ?", series);
    return map;//from   ww  w  . j av a  2s.  c o m
}

From source file:com.company.project.service.dao.UserDao.java

public Map<String, Object> get(String username) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    Map<String, Object> map = null;
    map = jdbcTemplate.queryForMap("SELECT * from users where username = ?", username);
    return map;/*from  w  w  w .j a v  a 2s .c om*/
}

From source file:ca.nrc.cadc.cred.server.CertificateDAO.java

public X509CertificateChain get(String hashKey) {
    Profiler profiler = new Profiler(this.getClass());
    X509CertificateChain x509CertificateChain = null;

    String query = "select canon_dn, exp_date, cert_chain, private_key, csr from " + config.getTable()
            + " where hash_dn = ? ";

    try {/*w  ww.  jav a  2  s. c  om*/
        JdbcTemplate jdbc = new JdbcTemplate(config.getDataSource());
        Map<String, Object> map = jdbc.queryForMap(query, new String[] { hashKey });
        String canonDn = (String) map.get("canon_dn");
        Date expDate = (Date) map.get("exp_date");
        String certChainStr = (String) map.get("cert_chain");
        byte[] bytesPrivateKey = (byte[]) map.get("private_key");

        // Sybase trims the trailing 0's of a varbinary. To compensate we add 0's to the 
        // privateKey byte array. Extra bytes in the private key array are ignored
        // when the key is built so the added 0's are only used when needed.
        // ad 20/07/2011
        bytesPrivateKey = Arrays.copyOf(bytesPrivateKey, bytesPrivateKey.length + 10);

        String csrStr = (String) map.get("csr");

        PrivateKey privateKey = SSLUtil.readPrivateKey(bytesPrivateKey);
        X500Principal principal = new X500Principal(canonDn);

        if (certChainStr != null) {
            byte[] bytesCertChain = certChainStr.getBytes();
            X509Certificate[] certs = SSLUtil.readCertificateChain(bytesCertChain);

            x509CertificateChain = new X509CertificateChain(Arrays.asList(certs));
        } else {
            x509CertificateChain = new X509CertificateChain(principal, privateKey, csrStr);
        }
        x509CertificateChain.setCsrString(csrStr);
        x509CertificateChain.setExpiryDate(expDate);
        x509CertificateChain.setHashKey(hashKey);
        x509CertificateChain.setKey(privateKey);
        x509CertificateChain.setPrincipal(principal);
    } catch (EmptyResultDataAccessException e) {
        // Record not exists.
        return null;
    } catch (InvalidKeySpecException ex) {
        throw new RuntimeException("BUG: failed to read private key", ex);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException("BUG: failed to read private key", ex);
    } catch (CertificateException ex) {
        throw new RuntimeException("BUG: failed to read certficate chain", ex);
    } catch (IOException ex) {
        throw new RuntimeException("BUG: failed to read certificate chain", ex);
    }
    profiler.checkpoint("get");
    return x509CertificateChain;
}

From source file:org.apache.syncope.core.rest.VirAttrTestITCase.java

@Test
public void issueSYNCOPE453() {
    final String resourceName = "issueSYNCOPE453-Res-" + getUUIDString();
    final String roleName = "issueSYNCOPE453-Role-" + getUUIDString();

    // -------------------------------------------
    // Create a resource ad-hoc
    // -------------------------------------------
    final ResourceTO resourceTO = new ResourceTO();

    resourceTO.setName(resourceName);/*from   ww  w  .  j  av a 2  s.  co m*/
    resourceTO.setConnectorId(107L);

    MappingTO mapping = new MappingTO();

    MappingItemTO item = new MappingItemTO();
    item.setIntAttrName("aLong");
    item.setIntMappingType(IntMappingType.UserSchema);
    item.setPurpose(MappingPurpose.PROPAGATION);
    item.setAccountid(true);
    mapping.setAccountIdItem(item);

    item = new MappingItemTO();
    item.setExtAttrName("USERNAME");
    item.setIntAttrName("username");
    item.setIntMappingType(IntMappingType.Username);
    item.setPurpose(MappingPurpose.PROPAGATION);
    mapping.getItems().add(item);

    item = new MappingItemTO();
    item.setExtAttrName("EMAIL");
    item.setIntAttrName("rvirtualdata");
    item.setIntMappingType(IntMappingType.RoleVirtualSchema);
    item.setPurpose(MappingPurpose.PROPAGATION);
    mapping.getItems().add(item);

    resourceTO.setUmapping(mapping);
    assertNotNull(getObject(resourceService.create(resourceTO).getLocation(), ResourceService.class,
            ResourceTO.class));
    // -------------------------------------------

    // -------------------------------------------
    // Create a role ad-hoc
    // -------------------------------------------
    RoleTO roleTO = new RoleTO();
    roleTO.setName(roleName);
    roleTO.setParent(8L);
    roleTO.getRVirAttrTemplates().add("rvirtualdata");
    roleTO.getVirAttrs().add(attributeTO("rvirtualdata", "ml@role.it"));
    roleTO.getResources().add(RESOURCE_NAME_LDAP);
    roleTO = createRole(roleTO);
    assertEquals(1, roleTO.getVirAttrs().size());
    assertEquals("ml@role.it", roleTO.getVirAttrs().get(0).getValues().get(0));
    // -------------------------------------------

    // -------------------------------------------
    // Create new user
    // -------------------------------------------
    UserTO userTO = UserTestITCase.getUniqueSampleTO("syncope453@syncope.apache.org");
    userTO.getAttrs().add(attributeTO("aLong", "123"));
    userTO.getResources().clear();
    userTO.getResources().add(resourceName);
    userTO.getVirAttrs().clear();
    userTO.getDerAttrs().clear();
    userTO.getMemberships().clear();

    final MembershipTO membership = new MembershipTO();
    membership.setRoleId(roleTO.getId());
    membership.getVirAttrs().add(attributeTO("mvirtualdata", "mvirtualvalue"));
    userTO.getMemberships().add(membership);

    userTO = createUser(userTO);
    assertEquals(2, userTO.getPropagationStatusTOs().size());
    assertTrue(userTO.getPropagationStatusTOs().get(0).getStatus().isSuccessful());
    assertTrue(userTO.getPropagationStatusTOs().get(1).getStatus().isSuccessful());

    JdbcTemplate jdbcTemplate = new JdbcTemplate(testDataSource);

    final Map<String, Object> actuals = jdbcTemplate.queryForMap(
            "SELECT id, surname, email FROM testsync WHERE id=?",
            new Object[] { Integer.parseInt(userTO.getAttrMap().get("aLong").getValues().get(0)) });

    assertEquals(userTO.getAttrMap().get("aLong").getValues().get(0), actuals.get("id").toString());
    assertEquals("ml@role.it", actuals.get("email"));
    // -------------------------------------------

    // -------------------------------------------
    // Delete resource and role ad-hoc
    // -------------------------------------------
    resourceService.delete(resourceName);
    roleService.delete(roleTO.getId());
    // -------------------------------------------
}

From source file:org.apache.syncope.fit.core.reference.VirAttrITCase.java

@Test
public void issueSYNCOPE453() {
    final String resourceName = "issueSYNCOPE453-Res-" + getUUIDString();
    final String groupName = "issueSYNCOPE453-Group-" + getUUIDString();

    // -------------------------------------------
    // Create a resource ad-hoc
    // -------------------------------------------
    ResourceTO resourceTO = new ResourceTO();

    resourceTO.setKey(resourceName);//  www.  ja v  a 2 s .  com
    resourceTO.setConnector(107L);

    ProvisionTO provisionTO = new ProvisionTO();
    provisionTO.setAnyType(AnyTypeKind.USER.name());
    provisionTO.setObjectClass(ObjectClass.ACCOUNT_NAME);
    resourceTO.getProvisions().add(provisionTO);

    MappingTO mapping = new MappingTO();
    provisionTO.setMapping(mapping);

    MappingItemTO item = new MappingItemTO();
    item.setIntAttrName("aLong");
    item.setIntMappingType(IntMappingType.UserPlainSchema);
    item.setExtAttrName("ID");
    item.setPurpose(MappingPurpose.PROPAGATION);
    item.setConnObjectKey(true);
    mapping.setConnObjectKeyItem(item);

    item = new MappingItemTO();
    item.setExtAttrName("USERNAME");
    item.setIntAttrName("username");
    item.setIntMappingType(IntMappingType.Username);
    item.setPurpose(MappingPurpose.PROPAGATION);
    mapping.getItems().add(item);

    item = new MappingItemTO();
    item.setExtAttrName("EMAIL");
    item.setIntAttrName("rvirtualdata");
    item.setIntMappingType(IntMappingType.GroupVirtualSchema);
    item.setPurpose(MappingPurpose.PROPAGATION);
    mapping.getItems().add(item);

    assertNotNull(getObject(resourceService.create(resourceTO).getLocation(), ResourceService.class,
            ResourceTO.class));
    // -------------------------------------------

    // -------------------------------------------
    // Create a VirAttrITCase ad-hoc
    // -------------------------------------------
    GroupTO groupTO = new GroupTO();
    groupTO.setName(groupName);
    groupTO.setRealm("/");
    groupTO.getVirAttrs().add(attrTO("rvirtualdata", "ml@group.it"));
    groupTO.getResources().add(RESOURCE_NAME_LDAP);
    groupTO = createGroup(groupTO);
    assertEquals(1, groupTO.getVirAttrs().size());
    assertEquals("ml@group.it", groupTO.getVirAttrs().iterator().next().getValues().get(0));
    // -------------------------------------------

    // -------------------------------------------
    // Create new user
    // -------------------------------------------
    UserTO userTO = UserITCase.getUniqueSampleTO("syncope453@syncope.apache.org");
    userTO.getPlainAttrs().add(attrTO("aLong", "123"));
    userTO.getResources().clear();
    userTO.getResources().add(resourceName);
    userTO.getVirAttrs().clear();
    userTO.getDerAttrs().clear();
    userTO.getMemberships().clear();

    MembershipTO membership = new MembershipTO();
    membership.setRightKey(groupTO.getKey());
    userTO.getMemberships().add(membership);

    userTO = createUser(userTO);
    assertEquals(2, userTO.getPropagationStatusTOs().size());
    assertTrue(userTO.getPropagationStatusTOs().get(0).getStatus().isSuccessful());
    assertTrue(userTO.getPropagationStatusTOs().get(1).getStatus().isSuccessful());

    JdbcTemplate jdbcTemplate = new JdbcTemplate(testDataSource);

    final Map<String, Object> actuals = jdbcTemplate.queryForMap(
            "SELECT id, surname, email FROM testsync WHERE id=?",
            new Object[] { Integer.parseInt(userTO.getPlainAttrMap().get("aLong").getValues().get(0)) });

    assertEquals(userTO.getPlainAttrMap().get("aLong").getValues().get(0), actuals.get("id").toString());
    assertEquals("ml@group.it", actuals.get("email"));
    // -------------------------------------------

    // -------------------------------------------
    // Delete resource and group ad-hoc
    // -------------------------------------------
    resourceService.delete(resourceName);
    groupService.delete(groupTO.getKey());
    // -------------------------------------------
}

From source file:org.apache.syncope.fit.core.VirAttrITCase.java

@Test
public void issueSYNCOPE453() {
    String resourceName = "issueSYNCOPE453Res" + getUUIDString();
    String groupKey = null;/*from   w  ww  .  ja  v  a  2  s  .co m*/
    String groupName = "issueSYNCOPE453Group" + getUUIDString();

    try {
        // -------------------------------------------
        // Create a VirAttrITCase ad-hoc
        // -------------------------------------------
        VirSchemaTO rvirtualdata;
        try {
            rvirtualdata = schemaService.read(SchemaType.VIRTUAL, "rvirtualdata");
        } catch (SyncopeClientException e) {
            LOG.warn("rvirtualdata not found, re-creating", e);

            rvirtualdata = new VirSchemaTO();
            rvirtualdata.setKey("rvirtualdata");
            rvirtualdata.setExtAttrName("businessCategory");
            rvirtualdata.setResource(RESOURCE_NAME_LDAP);
            rvirtualdata.setAnyType(AnyTypeKind.GROUP.name());

            rvirtualdata = createSchema(SchemaType.VIRTUAL, rvirtualdata);
        }
        assertNotNull(rvirtualdata);

        if (!"minimal group".equals(rvirtualdata.getAnyTypeClass())) {
            LOG.warn("rvirtualdata not in minimal group, restoring");

            AnyTypeClassTO minimalGroup = anyTypeClassService.read("minimal group");
            minimalGroup.getVirSchemas().add(rvirtualdata.getKey());
            anyTypeClassService.update(minimalGroup);

            rvirtualdata = schemaService.read(SchemaType.VIRTUAL, rvirtualdata.getKey());
            assertEquals("minimal group", rvirtualdata.getAnyTypeClass());
        }

        // -------------------------------------------
        // Create a resource ad-hoc
        // -------------------------------------------
        ResourceTO resourceTO = new ResourceTO();

        resourceTO.setKey(resourceName);
        resourceTO.setConnector("be24b061-019d-4e3e-baf0-0a6d0a45cb9c");

        ProvisionTO provisionTO = new ProvisionTO();
        provisionTO.setAnyType(AnyTypeKind.USER.name());
        provisionTO.setObjectClass(ObjectClass.ACCOUNT_NAME);
        resourceTO.getProvisions().add(provisionTO);

        MappingTO mapping = new MappingTO();
        provisionTO.setMapping(mapping);

        ItemTO item = new ItemTO();
        item.setIntAttrName("fullname");
        item.setExtAttrName("ID");
        item.setPurpose(MappingPurpose.PROPAGATION);
        item.setConnObjectKey(true);
        mapping.setConnObjectKeyItem(item);

        item = new ItemTO();
        item.setIntAttrName("username");
        item.setExtAttrName("USERNAME");
        item.setPurpose(MappingPurpose.PROPAGATION);
        mapping.getItems().add(item);

        item = new ItemTO();
        item.setIntAttrName("groups[" + groupName + "].rvirtualdata");
        item.setExtAttrName("EMAIL");
        item.setPurpose(MappingPurpose.PROPAGATION);
        mapping.getItems().add(item);

        assertNotNull(getObject(resourceService.create(resourceTO).getLocation(), ResourceService.class,
                ResourceTO.class));
        // -------------------------------------------

        GroupTO groupTO = new GroupTO();
        groupTO.setName(groupName);
        groupTO.setRealm("/");
        groupTO.getVirAttrs().add(attrTO(rvirtualdata.getKey(), "ml@group.it"));
        groupTO.getResources().add(RESOURCE_NAME_LDAP);
        groupTO = createGroup(groupTO).getEntity();
        groupKey = groupTO.getKey();
        assertEquals(1, groupTO.getVirAttrs().size());
        assertEquals("ml@group.it", groupTO.getVirAttrs().iterator().next().getValues().get(0));
        // -------------------------------------------

        // -------------------------------------------
        // Create new user
        // -------------------------------------------
        UserTO userTO = UserITCase.getUniqueSampleTO("syn453@syncope.apache.org");
        userTO.getPlainAttrs().add(attrTO("fullname", "123"));
        userTO.getResources().clear();
        userTO.getResources().add(resourceName);
        userTO.getVirAttrs().clear();
        userTO.getMemberships().clear();

        userTO.getMemberships().add(new MembershipTO.Builder().group(groupTO.getKey()).build());

        ProvisioningResult<UserTO> result = createUser(userTO);
        assertEquals(2, result.getPropagationStatuses().size());
        assertEquals(PropagationTaskExecStatus.SUCCESS, result.getPropagationStatuses().get(0).getStatus());
        assertEquals(PropagationTaskExecStatus.SUCCESS, result.getPropagationStatuses().get(1).getStatus());
        userTO = result.getEntity();

        JdbcTemplate jdbcTemplate = new JdbcTemplate(testDataSource);

        Map<String, Object> actuals = jdbcTemplate.queryForMap(
                "SELECT id, surname, email FROM testpull WHERE id=?",
                new Object[] { userTO.getPlainAttr("fullname").get().getValues().get(0) });

        assertEquals(userTO.getPlainAttr("fullname").get().getValues().get(0), actuals.get("id").toString());
        assertEquals("ml@group.it", actuals.get("email"));
        // -------------------------------------------
    } catch (Exception e) {
        LOG.error("Unexpected error", e);
    } finally {
        // -------------------------------------------
        // Delete resource and group ad-hoc
        // -------------------------------------------
        resourceService.delete(resourceName);
        if (groupKey != null) {
            groupService.delete(groupKey);
        }
        // -------------------------------------------
    }
}

From source file:org.springframework.jdbc.core.JdbcTemplateQueryTests.java

public void testQueryForMapWithArgsAndSingleRowAndColumn() throws Exception {
    String sql = "SELECT AGE FROM CUSTMR WHERE ID < ?";

    mockResultSetMetaData.getColumnCount();
    ctrlResultSetMetaData.setReturnValue(1);
    mockResultSetMetaData.getColumnLabel(1);
    ctrlResultSetMetaData.setReturnValue("age", 1);

    mockResultSet.getMetaData();/*  www .j av  a  2s .c om*/
    ctrlResultSet.setReturnValue(mockResultSetMetaData);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getObject(1);
    ctrlResultSet.setReturnValue(new Integer(11));
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockPreparedStatement.setObject(1, new Integer(3));
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeQuery();
    ctrlPreparedStatement.setReturnValue(mockResultSet);
    if (debugEnabled) {
        mockPreparedStatement.getWarnings();
        ctrlPreparedStatement.setReturnValue(null);
    }
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(sql);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource);

    Map map = template.queryForMap(sql, new Object[] { new Integer(3) });
    assertEquals("Row is Integer", 11, ((Integer) map.get("age")).intValue());
}