// Path: examples/spring-security/src/main/java/com/coverity/security/pie/example/model/Account.java
// @Entity
// public class Account {
//     
//     @Id @GeneratedValue
//     private Long id;
//     
//     @ManyToOne(optional = false)
//     private User user;
//     
//     @Column(nullable = false)
//     private BigDecimal balance;
// 
//     public User getUser() {
//         return user;
//     }
//     public void setUser(User user) {
//         this.user = user;
//     }
//     public BigDecimal getBalance() {
//         return balance;
//     }
//     public void setBalance(BigDecimal balance) {
//         this.balance = balance;
//     }
//     
//     
// }
// 
// Path: examples/spring-security/src/main/java/com/coverity/security/pie/example/service/AccountDao.java
// @Service
// public class AccountDao extends AbstractDao {
//     public Account getAccount(String username) {
//         List<?> result = getEntityManager()
//             .createQuery("SELECT A FROM Account A WHERE A.user.username = :username")
//             .setParameter("username", username)
//             .getResultList();
//         
//         if (result.size() == 0) {
//             return null;
//         }
//         return (Account)result.get(0);
//     }
//     
//     @Transactional
//     public void addAccount(Account account) {
//         getEntityManager().persist(account);
//     }
// }
// 
// Path: examples/spring-security/src/main/java/com/coverity/security/pie/example/util/DoPrivileged.java
// public class DoPrivileged {
//     public static <T> T asSystem(Callable<T> callable) throws Exception {
//         final SecurityContext priorContext = SecurityContextHolder.getContext();
//         try {
//             SecurityContextHolder.setContext(SecurityContextHolder.createEmptyContext());
//             PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken(
//                     "system", null, Arrays.asList(new SimpleGrantedAuthority(Role.SYSTEM.toString())));
//             SecurityContextHolder.getContext().setAuthentication(token);
//             
//             return callable.call();
//         } finally {
//             SecurityContextHolder.setContext(priorContext);
//         }
//         
//     }
// }
// 
// Path: examples/spring-security/src/main/java/com/coverity/security/pie/example/model/Role.java
// public enum Role {
//     GUEST, USER, ADMIN, SYSTEM;
// }
// 
// Path: examples/spring-security/src/main/java/com/coverity/security/pie/example/model/User.java
// @Entity
// public class User {
//     @Id @GeneratedValue
//     private Long id;
//     
//     private String username;
//     
//     private String passwordHash;
//     
//     private Role role;
//     
//     
//     public String getUsername() {
//         return username;
//     }
//     public void setUsername(String username) {
//         this.username = username;
//     }
//     public String getPasswordHash() {
//         return passwordHash;
//     }
//     public void setPasswordHash(String passwordHash) {
//         this.passwordHash = passwordHash;
//     }
//     public Role getRole() {
//         return role;
//     }
//     public void setRole(Role role) {
//         this.role = role;
//     }
// }
// 
// Path: examples/spring-security/src/main/java/com/coverity/security/pie/example/service/UserDao.java
// @Service
// public class UserDao extends AbstractDao {
//     
//     public List<User> getUsers() {
//         return castList(getEntityManager().createQuery("SELECT U FROM User U").getResultList(), User.class);
//     }
//     
//     @Transactional
//     public void addUser(User user) {
//         getEntityManager().persist(user);
//     }
//     
// }


import java.math.BigDecimal;
import java.util.concurrent.Callable;
import com.coverity.security.pie.example.model.Account;
import com.coverity.security.pie.example.service.AccountDao;
import com.coverity.security.pie.example.util.DoPrivileged;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.security.crypto.password.PasswordEncoder;
import com.coverity.security.pie.example.model.Role;
import com.coverity.security.pie.example.model.User;
import com.coverity.security.pie.example.service.UserDao;
package com.coverity.security.pie.example.config;




public class SampleDataLoader implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private UserDao userDao;
    
    @Autowired
    private AccountDao accountDao;
    
    @Autowired
    private PasswordEncoder passwordEncoder;
    
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
// Path: examples/spring-security/src/main/java/com/coverity/security/pie/example/model/Account.java
// @Entity
// public class Account {
//     
//     @Id @GeneratedValue
//     private Long id;
//     
//     @ManyToOne(optional = false)
//     private User user;
//     
//     @Column(nullable = false)
//     private BigDecimal balance;
// 
//     public User getUser() {
//         return user;
//     }
//     public void setUser(User user) {
//         this.user = user;
//     }
//     public BigDecimal getBalance() {
//         return balance;
//     }
//     public void setBalance(BigDecimal balance) {
//         this.balance = balance;
//     }
//     
//     
// }
// 
// Path: examples/spring-security/src/main/java/com/coverity/security/pie/example/service/AccountDao.java
// @Service
// public class AccountDao extends AbstractDao {
//     public Account getAccount(String username) {
//         List<?> result = getEntityManager()
//             .createQuery("SELECT A FROM Account A WHERE A.user.username = :username")
//             .setParameter("username", username)
//             .getResultList();
//         
//         if (result.size() == 0) {
//             return null;
//         }
//         return (Account)result.get(0);
//     }
//     
//     @Transactional
//     public void addAccount(Account account) {
//         getEntityManager().persist(account);
//     }
// }
// 
// Path: examples/spring-security/src/main/java/com/coverity/security/pie/example/util/DoPrivileged.java
// public class DoPrivileged {
//     public static <T> T asSystem(Callable<T> callable) throws Exception {
//         final SecurityContext priorContext = SecurityContextHolder.getContext();
//         try {
//             SecurityContextHolder.setContext(SecurityContextHolder.createEmptyContext());
//             PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken(
//                     "system", null, Arrays.asList(new SimpleGrantedAuthority(Role.SYSTEM.toString())));
//             SecurityContextHolder.getContext().setAuthentication(token);
//             
//             return callable.call();
//         } finally {
//             SecurityContextHolder.setContext(priorContext);
//         }
//         
//     }
// }
// 
// Path: examples/spring-security/src/main/java/com/coverity/security/pie/example/model/Role.java
// public enum Role {
//     GUEST, USER, ADMIN, SYSTEM;
// }
// 
// Path: examples/spring-security/src/main/java/com/coverity/security/pie/example/model/User.java
// @Entity
// public class User {
//     @Id @GeneratedValue
//     private Long id;
//     
//     private String username;
//     
//     private String passwordHash;
//     
//     private Role role;
//     
//     
//     public String getUsername() {
//         return username;
//     }
//     public void setUsername(String username) {
//         this.username = username;
//     }
//     public String getPasswordHash() {
//         return passwordHash;
//     }
//     public void setPasswordHash(String passwordHash) {
//         this.passwordHash = passwordHash;
//     }
//     public Role getRole() {
//         return role;
//     }
//     public void setRole(Role role) {
//         this.role = role;
//     }
// }
// 
// Path: examples/spring-security/src/main/java/com/coverity/security/pie/example/service/UserDao.java
// @Service
// public class UserDao extends AbstractDao {
//     
//     public List<User> getUsers() {
//         return castList(getEntityManager().createQuery("SELECT U FROM User U").getResultList(), User.class);
//     }
//     
//     @Transactional
//     public void addUser(User user) {
//         getEntityManager().persist(user);
//     }
//     
// }


// Path: examples/spring-security/src/main/java/com/coverity/security/pie/example/config/SampleDataLoader.java
import java.math.BigDecimal;
import java.util.concurrent.Callable;
import com.coverity.security.pie.example.model.Account;
import com.coverity.security.pie.example.service.AccountDao;
import com.coverity.security.pie.example.util.DoPrivileged;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.security.crypto.password.PasswordEncoder;
import com.coverity.security.pie.example.model.Role;
import com.coverity.security.pie.example.model.User;
import com.coverity.security.pie.example.service.UserDao;

package com.coverity.security.pie.example.config;




public class SampleDataLoader implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private UserDao userDao;
    
    @Autowired
    private AccountDao accountDao;
    
    @Autowired
    private PasswordEncoder passwordEncoder;
    
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        try {