1
0
mirror of synced 2026-08-06 10:18:52 +00:00

Anonymous type can be replaced with lambda

This commit is contained in:
Lars Grefer
2019-08-06 23:40:48 +02:00
committed by Josh Cummings
parent 05f42a4995
commit fb39d9c255
53 changed files with 347 additions and 722 deletions
@@ -16,13 +16,10 @@
package sample.contact;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
/**
@@ -38,44 +35,32 @@ public class ContactDaoSpring extends JdbcDaoSupport implements ContactDao {
public void create(final Contact contact) {
getJdbcTemplate().update("insert into contacts values (?, ?, ?)",
new PreparedStatementSetter() {
public void setValues(PreparedStatement ps) throws SQLException {
ps.setLong(1, contact.getId());
ps.setString(2, contact.getName());
ps.setString(3, contact.getEmail());
}
ps -> {
ps.setLong(1, contact.getId());
ps.setString(2, contact.getName());
ps.setString(3, contact.getEmail());
});
}
public void delete(final Long contactId) {
getJdbcTemplate().update("delete from contacts where id = ?",
new PreparedStatementSetter() {
public void setValues(PreparedStatement ps) throws SQLException {
ps.setLong(1, contactId);
}
});
ps -> ps.setLong(1, contactId));
}
public void update(final Contact contact) {
getJdbcTemplate().update(
"update contacts set contact_name = ?, address = ? where id = ?",
new PreparedStatementSetter() {
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, contact.getName());
ps.setString(2, contact.getEmail());
ps.setLong(3, contact.getId());
}
ps -> {
ps.setString(1, contact.getName());
ps.setString(2, contact.getEmail());
ps.setLong(3, contact.getId());
});
}
public List<Contact> findAll() {
return getJdbcTemplate().query(
"select id, contact_name, email from contacts order by id",
new RowMapper<Contact>() {
public Contact mapRow(ResultSet rs, int rowNum) throws SQLException {
return mapContact(rs);
}
});
(rs, rowNum) -> mapContact(rs));
}
public List<String> findAllPrincipals() {
@@ -92,11 +77,7 @@ public class ContactDaoSpring extends JdbcDaoSupport implements ContactDao {
public Contact getById(Long id) {
List<Contact> list = getJdbcTemplate().query(
"select id, contact_name, email from contacts where id = ? order by id",
new RowMapper<Contact>() {
public Contact mapRow(ResultSet rs, int rowNum) throws SQLException {
return mapContact(rs);
}
}, id);
(rs, rowNum) -> mapContact(rs), id);
if (list.size() == 0) {
return null;
@@ -34,8 +34,6 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.Assert;
@@ -166,12 +164,10 @@ public class DataSourcePopulator implements InitializingBean {
for (int i = 1; i < createEntities; i++) {
final ObjectIdentity objectIdentity = new ObjectIdentityImpl(Contact.class,
(long) i);
tt.execute(new TransactionCallback<Object>() {
public Object doInTransaction(TransactionStatus arg0) {
mutableAclService.createAcl(objectIdentity);
tt.execute(arg0 -> {
mutableAclService.createAcl(objectIdentity);
return null;
}
return null;
});
}
@@ -273,12 +269,10 @@ public class DataSourcePopulator implements InitializingBean {
}
private void updateAclInTransaction(final MutableAcl acl) {
tt.execute(new TransactionCallback<Object>() {
public Object doInTransaction(TransactionStatus arg0) {
mutableAclService.updateAcl(acl);
tt.execute(arg0 -> {
mutableAclService.updateAcl(acl);
return null;
}
return null;
});
}
}
@@ -15,11 +15,8 @@
*/
package sample.dms;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.security.util.FieldUtils;
import org.springframework.transaction.support.TransactionSynchronizationManager;
@@ -80,23 +77,20 @@ public class DocumentDaoImpl extends JdbcDaoSupport implements DocumentDao {
/** Executes recursive SQL as needed to build a full Directory hierarchy of objects */
private Directory getDirectoryWithImmediateParentPopulated(final Long id) {
return getJdbcTemplate().queryForObject(SELECT_FROM_DIRECTORY_SINGLE,
new Object[] { id }, new RowMapper<Directory>() {
public Directory mapRow(ResultSet rs, int rowNumber)
throws SQLException {
Long parentDirectoryId = rs
.getLong("parent_directory_id");
Directory parentDirectory = Directory.ROOT_DIRECTORY;
if (parentDirectoryId != null
&& !parentDirectoryId.equals(-1L)) {
// Need to go and lookup the parent, so do that first
parentDirectory = getDirectoryWithImmediateParentPopulated(parentDirectoryId);
}
Directory directory = new Directory(rs
.getString("directory_name"), parentDirectory);
FieldUtils.setProtectedFieldValue("id", directory,
rs.getLong("id"));
return directory;
new Object[] { id }, (rs, rowNumber) -> {
Long parentDirectoryId = rs
.getLong("parent_directory_id");
Directory parentDirectory = Directory.ROOT_DIRECTORY;
if (parentDirectoryId != null
&& !parentDirectoryId.equals(-1L)) {
// Need to go and lookup the parent, so do that first
parentDirectory = getDirectoryWithImmediateParentPopulated(parentDirectoryId);
}
Directory directory = new Directory(rs
.getString("directory_name"), parentDirectory);
FieldUtils.setProtectedFieldValue("id", directory,
rs.getLong("id"));
return directory;
});
}
@@ -105,38 +99,26 @@ public class DocumentDaoImpl extends JdbcDaoSupport implements DocumentDao {
"Directory required (the ID can be null to refer to root)");
if (directory.getId() == null) {
List<Directory> directories = getJdbcTemplate().query(
SELECT_FROM_DIRECTORY_NULL, new RowMapper<Directory>() {
public Directory mapRow(ResultSet rs, int rowNumber)
throws SQLException {
return getDirectoryWithImmediateParentPopulated(rs
.getLong("id"));
}
});
SELECT_FROM_DIRECTORY_NULL, (rs, rowNumber) -> getDirectoryWithImmediateParentPopulated(rs
.getLong("id")));
return directories.toArray(new AbstractElement[] {});
}
List<AbstractElement> directories = getJdbcTemplate().query(
SELECT_FROM_DIRECTORY, new Object[] { directory.getId() },
new RowMapper<AbstractElement>() {
public Directory mapRow(ResultSet rs, int rowNumber)
throws SQLException {
return getDirectoryWithImmediateParentPopulated(rs
.getLong("id"));
}
});
(rs, rowNumber) -> getDirectoryWithImmediateParentPopulated(rs
.getLong("id")));
List<File> files = getJdbcTemplate().query(SELECT_FROM_FILE,
new Object[] { directory.getId() }, new RowMapper<File>() {
public File mapRow(ResultSet rs, int rowNumber) throws SQLException {
Long parentDirectoryId = rs
.getLong("parent_directory_id");
Directory parentDirectory = null;
if (parentDirectoryId != null) {
parentDirectory = getDirectoryWithImmediateParentPopulated(parentDirectoryId);
}
File file = new File(rs.getString("file_name"), parentDirectory);
FieldUtils.setProtectedFieldValue("id", file,
rs.getLong("id"));
return file;
new Object[] { directory.getId() }, (rs, rowNumber) -> {
Long parentDirectoryId = rs
.getLong("parent_directory_id");
Directory parentDirectory = null;
if (parentDirectoryId != null) {
parentDirectory = getDirectoryWithImmediateParentPopulated(parentDirectoryId);
}
File file = new File(rs.getString("file_name"), parentDirectory);
FieldUtils.setProtectedFieldValue("id", file,
rs.getLong("id"));
return file;
});
// Add the File elements after the Directory elements
directories.addAll(files);
@@ -15,10 +15,6 @@
*/
package sample.dms.secured;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.security.acls.domain.BasePermission;
import org.springframework.security.acls.domain.ObjectIdentityImpl;
import org.springframework.security.acls.domain.PrincipalSid;
@@ -49,11 +45,7 @@ public class SecureDocumentDaoImpl extends DocumentDaoImpl implements SecureDocu
public String[] getUsers() {
return getJdbcTemplate().query(SELECT_FROM_USERS,
new RowMapper<String>() {
public String mapRow(ResultSet rs, int rowNumber) throws SQLException {
return rs.getString("USERNAME");
}
}).toArray(new String[] {});
(rs, rowNumber) -> rs.getString("USERNAME")).toArray(new String[] {});
}
public void create(AbstractElement element) {
@@ -147,20 +147,18 @@ public class ServletApiController {
@RequestMapping("/async")
public void asynch(HttpServletRequest request, HttpServletResponse response) {
final AsyncContext async = request.startAsync();
async.start(new Runnable() {
public void run() {
Authentication authentication = SecurityContextHolder.getContext()
.getAuthentication();
try {
final HttpServletResponse asyncResponse = (HttpServletResponse) async
.getResponse();
asyncResponse.setStatus(HttpServletResponse.SC_OK);
asyncResponse.getWriter().write(String.valueOf(authentication));
async.complete();
}
catch (Exception e) {
throw new RuntimeException(e);
}
async.start(() -> {
Authentication authentication = SecurityContextHolder.getContext()
.getAuthentication();
try {
final HttpServletResponse asyncResponse = (HttpServletResponse) async
.getResponse();
asyncResponse.setStatus(HttpServletResponse.SC_OK);
asyncResponse.getWriter().write(String.valueOf(authentication));
async.complete();
}
catch (Exception e) {
throw new RuntimeException(e);
}
});
}
@@ -211,4 +209,4 @@ public class ServletApiController {
public String login(@ModelAttribute LoginForm loginForm) {
return "login";
}
}
}