Functionnal RegNo generator wich only needs to be more parametrized
This commit is contained in:
		@ -15,7 +15,7 @@ import java.util.Date;
 | 
			
		||||
 | 
			
		||||
public class MockController {
 | 
			
		||||
    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
 | 
			
		||||
 | 
			
		||||
    public final UserService userService;
 | 
			
		||||
    public final UserRepository userRepo;
 | 
			
		||||
    public final TokenRepository tokenRepo;
 | 
			
		||||
    public final TokenService tokenService;
 | 
			
		||||
@ -27,7 +27,8 @@ public class MockController {
 | 
			
		||||
    ArrayList<User> mockUsers;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public MockController(UserRepository userRepo, TokenRepository tokenRepo, TokenService tokenService, CurriculumCourseService CurriculumCourseService, CurriculumService curriculumService, CourseService courseService, InscriptionService inscriptionService){
 | 
			
		||||
    public MockController(UserService userService, UserRepository userRepo, TokenRepository tokenRepo, TokenService tokenService, CurriculumCourseService CurriculumCourseService, CurriculumService curriculumService, CourseService courseService, InscriptionService inscriptionService){
 | 
			
		||||
        this.userService = userService;
 | 
			
		||||
        this.tokenRepo = tokenRepo;
 | 
			
		||||
        this.userRepo = userRepo;
 | 
			
		||||
        this.tokenService = tokenService;
 | 
			
		||||
@ -45,21 +46,21 @@ public class MockController {
 | 
			
		||||
 | 
			
		||||
    @PostMapping("/mock")
 | 
			
		||||
    public void postMock(){
 | 
			
		||||
 | 
			
		||||
        // user part
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        User herobrine = new User("brine","hero","admin@admin.com","behind","ShadowsLand",new Date(0), null,Role.Admin,passwordEncoder.encode("admin"));
 | 
			
		||||
        User joe = new User("Mama","Joe","student@student.com","roundabout","England",new Date(0), null,Role.Student,passwordEncoder.encode("student"));
 | 
			
		||||
        User meh = new User("Polo","Marco","secretary@secretary.com","a Box","Monaco",new Date(0), null,Role.Secretary,passwordEncoder.encode("secretary"));
 | 
			
		||||
        User joke = new User("Gaillard","Corentin","teacher@teacher.com","lab","faculty",new Date(0), null,Role.Teacher,passwordEncoder.encode("teacher"));
 | 
			
		||||
        User jojo = new User("Bridoux","Justin","teacher2@teacher2.com","lab","faculty",new Date(0), null,Role.Teacher,passwordEncoder.encode("teacher"));
 | 
			
		||||
        User lena = new User("Louille","Lena","inscriptionService@InscriptionService.com","no","yes",new Date(0), null,Role.InscriptionService,passwordEncoder.encode("inscriptionService"));
 | 
			
		||||
        mockUsers = new ArrayList<>(Arrays.asList(herobrine,joe,meh,joke,lena,jojo));
 | 
			
		||||
 | 
			
		||||
        userRepo.saveAll(mockUsers);
 | 
			
		||||
        mockUsers = new ArrayList<>(Arrays.asList(joke, herobrine, joe, meh, jojo));
 | 
			
		||||
        userService.saveAll(mockUsers);
 | 
			
		||||
        userService.save(lena);
 | 
			
		||||
 | 
			
		||||
        // Course / Curriculum part
 | 
			
		||||
 | 
			
		||||
        //Course / Curriculum part
 | 
			
		||||
 | 
			
		||||
        Curriculum infoBab1 = new Curriculum(1,"info");
 | 
			
		||||
        Curriculum chemistryBab1 = new Curriculum(1,"chemistry");
 | 
			
		||||
 | 
			
		||||
@ -1,18 +1,15 @@
 | 
			
		||||
package ovh.herisson.Clyde;
 | 
			
		||||
 | 
			
		||||
import org.hibernate.HibernateException;
 | 
			
		||||
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
 | 
			
		||||
import org.hibernate.engine.spi.SessionImplementor;
 | 
			
		||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
 | 
			
		||||
import org.hibernate.id.IdentifierGenerator;
 | 
			
		||||
 | 
			
		||||
import java.io.Serializable;
 | 
			
		||||
import java.sql.Connection;
 | 
			
		||||
import java.sql.ResultSet;
 | 
			
		||||
import java.sql.SQLException;
 | 
			
		||||
import java.sql.Statement;
 | 
			
		||||
 | 
			
		||||
public class RegNoGenerator implements IdentifierGenerator {
 | 
			
		||||
    private final UserSaveCounter usv = UserSaveCounter.getInstance();
 | 
			
		||||
    @Override
 | 
			
		||||
    public Object generate(SharedSessionContractImplementor session, Object object) {
 | 
			
		||||
        try{
 | 
			
		||||
@ -20,16 +17,25 @@ public class RegNoGenerator implements IdentifierGenerator {
 | 
			
		||||
            Connection conn = jdbccon.obtainConnection();
 | 
			
		||||
 | 
			
		||||
            Statement statement = conn.createStatement();
 | 
			
		||||
            String query = "select count(reg_no)+1 from Users";
 | 
			
		||||
 | 
			
		||||
            int n = usv.getCount();
 | 
			
		||||
            String query = "select count(reg_no)+"+n+" from Users";
 | 
			
		||||
            usv.increment();
 | 
			
		||||
 | 
			
		||||
            ResultSet set = statement.executeQuery(query);
 | 
			
		||||
 | 
			
		||||
            if (set.next()){
 | 
			
		||||
                return set.getLong(1)+1000;
 | 
			
		||||
            long resp = 0;
 | 
			
		||||
            if(set.next()){
 | 
			
		||||
                resp = set.getLong(1)+1000;
 | 
			
		||||
                System.out.println(resp);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            conn.close();
 | 
			
		||||
            statement.close();
 | 
			
		||||
 | 
			
		||||
            return resp;
 | 
			
		||||
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -5,13 +5,10 @@ import org.springframework.data.repository.CrudRepository;
 | 
			
		||||
import ovh.herisson.Clyde.Tables.User;
 | 
			
		||||
 | 
			
		||||
public interface UserRepository extends CrudRepository<User, Long> {
 | 
			
		||||
 | 
			
		||||
    User findById(long id);
 | 
			
		||||
 | 
			
		||||
    User findByEmail(String email);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Query("select u from User u where u.role = ovh.herisson.Clyde.Tables.Role.Teacher")
 | 
			
		||||
    Iterable<User> findAllTeachers();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -5,13 +5,15 @@ import org.springframework.stereotype.Service;
 | 
			
		||||
import ovh.herisson.Clyde.Repositories.UserRepository;
 | 
			
		||||
import ovh.herisson.Clyde.Tables.Role;
 | 
			
		||||
import ovh.herisson.Clyde.Tables.User;
 | 
			
		||||
import ovh.herisson.Clyde.UserSaveCounter;
 | 
			
		||||
 | 
			
		||||
import java.util.*;
 | 
			
		||||
 | 
			
		||||
@Service
 | 
			
		||||
public class UserService {
 | 
			
		||||
    private final UserRepository userRepo;
 | 
			
		||||
    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
 | 
			
		||||
 | 
			
		||||
    private final UserSaveCounter usc = UserSaveCounter.getInstance();
 | 
			
		||||
    public UserService(UserRepository userRepo){
 | 
			
		||||
        this.userRepo = userRepo;
 | 
			
		||||
    }
 | 
			
		||||
@ -106,10 +108,19 @@ public class UserService {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public User save(User  user){
 | 
			
		||||
        usc.reset();
 | 
			
		||||
        user.setPassword(passwordEncoder.encode(user.getPassword()));
 | 
			
		||||
        return userRepo.save(user);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void saveAll(ArrayList<User> list){
 | 
			
		||||
        //S'assure que le compteur est bien a 0
 | 
			
		||||
        usc.reset();
 | 
			
		||||
        userRepo.saveAll(list);
 | 
			
		||||
        //Reset le compteur a zero pour les futurs ajouts
 | 
			
		||||
        usc.reset();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Iterable<User> getAll(){
 | 
			
		||||
        return userRepo.findAll();
 | 
			
		||||
    }
 | 
			
		||||
@ -131,4 +142,5 @@ public class UserService {
 | 
			
		||||
    public void delete(User user) {
 | 
			
		||||
        userRepo.delete(user);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,25 @@
 | 
			
		||||
package ovh.herisson.Clyde;
 | 
			
		||||
 | 
			
		||||
public final class UserSaveCounter {
 | 
			
		||||
    private static UserSaveCounter instance;
 | 
			
		||||
    private int count = 0;
 | 
			
		||||
 | 
			
		||||
    public void increment() {
 | 
			
		||||
        this.count+=1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getCount() {
 | 
			
		||||
        return count;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void reset(){
 | 
			
		||||
        this.count = 0;
 | 
			
		||||
    }
 | 
			
		||||
    public static UserSaveCounter getInstance(){
 | 
			
		||||
        if (instance == null){
 | 
			
		||||
            instance = new UserSaveCounter();
 | 
			
		||||
            return instance;
 | 
			
		||||
        }
 | 
			
		||||
        return instance;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -1,3 +1,2 @@
 | 
			
		||||
spring.jpa.hibernate.ddl-auto=create-drop
 | 
			
		||||
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
 | 
			
		||||
spring.sql.init.mode=always
 | 
			
		||||
		Reference in New Issue
	
	Block a user