[JAVA-14174] Renamed paterns to paterns-module (#12718)
* [JAVA-14174] Renamed paterns to paterns-module * [JAVA-14174] naming fixes Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
+53
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.daopattern.application;
|
||||
|
||||
import com.baeldung.daopattern.config.JpaEntityManagerFactory;
|
||||
import com.baeldung.daopattern.daos.Dao;
|
||||
import com.baeldung.daopattern.daos.JpaUserDao;
|
||||
import com.baeldung.daopattern.entities.User;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
public class UserApplication {
|
||||
|
||||
private static JpaUserDao jpaUserDao;
|
||||
|
||||
public static void main(String[] args) {
|
||||
User user1 = getUser(1);
|
||||
System.out.println(user1);
|
||||
updateUser(user1, new String[]{"John", "john@domain.com"});
|
||||
saveUser(new User("Monica", "monica@domain.com"));
|
||||
deleteUser(getUser(2));
|
||||
getAllUsers().forEach(user -> System.out.println(user.getName()));
|
||||
}
|
||||
|
||||
private static class JpaUserDaoHolder {
|
||||
private static final JpaUserDao jpaUserDao = new JpaUserDao(new JpaEntityManagerFactory().getEntityManager());
|
||||
}
|
||||
|
||||
public static Dao getJpaUserDao() {
|
||||
return JpaUserDaoHolder.jpaUserDao;
|
||||
}
|
||||
|
||||
public static User getUser(long id) {
|
||||
Optional<User> user = getJpaUserDao().get(id);
|
||||
return user.orElseGet(()-> {return new User("non-existing user", "no-email");});
|
||||
}
|
||||
|
||||
public static List<User> getAllUsers() {
|
||||
return getJpaUserDao().getAll();
|
||||
}
|
||||
|
||||
public static void updateUser(User user, String[] params){
|
||||
getJpaUserDao().update(user, params);
|
||||
}
|
||||
|
||||
public static void saveUser(User user) {
|
||||
getJpaUserDao().save(user);
|
||||
}
|
||||
|
||||
public static void deleteUser(User user) {
|
||||
getJpaUserDao().delete(user);
|
||||
}
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
package com.baeldung.daopattern.config;
|
||||
|
||||
import com.baeldung.daopattern.entities.User;
|
||||
import com.mysql.cj.jdbc.MysqlDataSource;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.sql.DataSource;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.spi.PersistenceUnitInfo;
|
||||
import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl;
|
||||
import org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor;
|
||||
|
||||
public class JpaEntityManagerFactory {
|
||||
|
||||
private final String DB_URL = "jdbc:mysql://databaseurl";
|
||||
private final String DB_USER_NAME = "username";
|
||||
private final String DB_PASSWORD = "password";
|
||||
|
||||
public EntityManager getEntityManager() {
|
||||
return getEntityManagerFactory().createEntityManager();
|
||||
}
|
||||
|
||||
protected EntityManagerFactory getEntityManagerFactory() {
|
||||
PersistenceUnitInfo persistenceUnitInfo = getPersistenceUnitInfo(getClass().getSimpleName());
|
||||
Map<String, Object> configuration = new HashMap<>();
|
||||
return new EntityManagerFactoryBuilderImpl(new PersistenceUnitInfoDescriptor(persistenceUnitInfo), configuration)
|
||||
.build();
|
||||
}
|
||||
|
||||
protected PersistenceUnitInfoImpl getPersistenceUnitInfo(String name) {
|
||||
return new PersistenceUnitInfoImpl(name, getEntityClassNames(), getProperties());
|
||||
}
|
||||
|
||||
protected List<String> getEntityClassNames() {
|
||||
return Arrays.asList(getEntities())
|
||||
.stream()
|
||||
.map(Class::getName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
protected Properties getProperties() {
|
||||
Properties properties = new Properties();
|
||||
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
|
||||
properties.put("hibernate.id.new_generator_mappings", false);
|
||||
properties.put("hibernate.connection.datasource", getMysqlDataSource());
|
||||
return properties;
|
||||
}
|
||||
|
||||
protected Class[] getEntities() {
|
||||
return new Class[]{User.class};
|
||||
}
|
||||
|
||||
protected DataSource getMysqlDataSource() {
|
||||
MysqlDataSource mysqlDataSource = new MysqlDataSource();
|
||||
mysqlDataSource.setURL(DB_URL);
|
||||
mysqlDataSource.setUser(DB_USER_NAME);
|
||||
mysqlDataSource.setPassword(DB_PASSWORD);
|
||||
return mysqlDataSource;
|
||||
}
|
||||
}
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
package com.baeldung.daopattern.config;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import javax.sql.DataSource;
|
||||
import javax.persistence.SharedCacheMode;
|
||||
import javax.persistence.ValidationMode;
|
||||
import javax.persistence.spi.ClassTransformer;
|
||||
import javax.persistence.spi.PersistenceUnitInfo;
|
||||
import javax.persistence.spi.PersistenceUnitTransactionType;
|
||||
import org.hibernate.jpa.HibernatePersistenceProvider;
|
||||
|
||||
public class PersistenceUnitInfoImpl implements PersistenceUnitInfo {
|
||||
|
||||
public static final String JPA_VERSION = "2.1";
|
||||
private final String persistenceUnitName;
|
||||
private PersistenceUnitTransactionType transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;
|
||||
private final List<String> managedClassNames;
|
||||
private final List<String> mappingFileNames = new ArrayList<>();
|
||||
private final Properties properties;
|
||||
private DataSource jtaDataSource;
|
||||
private DataSource nonJtaDataSource;
|
||||
|
||||
public PersistenceUnitInfoImpl(String persistenceUnitName, List<String> managedClassNames, Properties properties) {
|
||||
this.persistenceUnitName = persistenceUnitName;
|
||||
this.managedClassNames = managedClassNames;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPersistenceUnitName() {
|
||||
return persistenceUnitName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPersistenceProviderClassName() {
|
||||
return HibernatePersistenceProvider.class.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PersistenceUnitTransactionType getTransactionType() {
|
||||
return transactionType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSource getJtaDataSource() {
|
||||
return jtaDataSource;
|
||||
}
|
||||
|
||||
public PersistenceUnitInfoImpl setJtaDataSource(DataSource jtaDataSource) {
|
||||
this.jtaDataSource = jtaDataSource;
|
||||
this.nonJtaDataSource = null;
|
||||
transactionType = PersistenceUnitTransactionType.JTA;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSource getNonJtaDataSource() {
|
||||
return nonJtaDataSource;
|
||||
}
|
||||
|
||||
public PersistenceUnitInfoImpl setNonJtaDataSource(DataSource nonJtaDataSource) {
|
||||
this.nonJtaDataSource = nonJtaDataSource;
|
||||
this.jtaDataSource = null;
|
||||
transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getMappingFileNames() {
|
||||
return mappingFileNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<URL> getJarFileUrls() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public URL getPersistenceUnitRootUrl() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getManagedClassNames() {
|
||||
return managedClassNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean excludeUnlistedClasses() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SharedCacheMode getSharedCacheMode() {
|
||||
return SharedCacheMode.UNSPECIFIED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValidationMode getValidationMode() {
|
||||
return ValidationMode.AUTO;
|
||||
}
|
||||
|
||||
public Properties getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPersistenceXMLSchemaVersion() {
|
||||
return JPA_VERSION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassLoader getClassLoader() {
|
||||
return Thread.currentThread().getContextClassLoader();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addTransformer(ClassTransformer transformer) {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassLoader getNewTempClassLoader() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.daopattern.daos;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface Dao<T> {
|
||||
|
||||
Optional<T> get(long id);
|
||||
|
||||
List<T> getAll();
|
||||
|
||||
void save(T t);
|
||||
|
||||
void update(T t, String[] params);
|
||||
|
||||
void delete(T t);
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.daopattern.daos;
|
||||
|
||||
import com.baeldung.daopattern.entities.User;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityTransaction;
|
||||
import javax.persistence.Query;
|
||||
|
||||
public class JpaUserDao implements Dao<User> {
|
||||
|
||||
private final EntityManager entityManager;
|
||||
|
||||
public JpaUserDao(EntityManager entityManager) {
|
||||
this.entityManager = entityManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<User> get(long id) {
|
||||
return Optional.ofNullable(entityManager.find(User.class, id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> getAll() {
|
||||
Query query = entityManager.createQuery("SELECT e FROM User e");
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(User user) {
|
||||
executeInsideTransaction(entityManager -> entityManager.persist(user));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(User user, String[] params) {
|
||||
user.setName(Objects.requireNonNull(params[0], "Name cannot be null"));
|
||||
user.setEmail(Objects.requireNonNull(params[1], "Email cannot be null"));
|
||||
executeInsideTransaction(entityManager -> entityManager.merge(user));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(User user) {
|
||||
executeInsideTransaction(entityManager -> entityManager.remove(user));
|
||||
}
|
||||
|
||||
private void executeInsideTransaction(Consumer<EntityManager> action) {
|
||||
final EntityTransaction tx = entityManager.getTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
action.accept(entityManager);
|
||||
tx.commit();
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.daopattern.daos;
|
||||
|
||||
import com.baeldung.daopattern.entities.User;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
public class UserDao implements Dao<User> {
|
||||
|
||||
private List<User> users = new ArrayList<>();
|
||||
|
||||
public UserDao() {
|
||||
users.add(new User("John", "john@domain.com"));
|
||||
users.add(new User("Susan", "susan@domain.com"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<User> get(long id) {
|
||||
return Optional.ofNullable(users.get((int) id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> getAll() {
|
||||
return users;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(User user) {
|
||||
users.add(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(User user, String[] params) {
|
||||
user.setName(Objects.requireNonNull(params[0], "Name cannot be null"));
|
||||
user.setEmail(Objects.requireNonNull(params[1], "Email cannot be null"));
|
||||
users.add(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(User user) {
|
||||
users.remove(user);
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.daopattern.entities;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
private String name;
|
||||
private String email;
|
||||
|
||||
public User(){}
|
||||
|
||||
public User(String name, String email) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.dtopattern;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Main.class, args);
|
||||
}
|
||||
}
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.dtopattern.api;
|
||||
|
||||
import com.baeldung.dtopattern.domain.Role;
|
||||
import com.baeldung.dtopattern.domain.User;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
@Component
|
||||
class Mapper {
|
||||
public UserDTO toDto(User user) {
|
||||
String name = user.getName();
|
||||
List<String> roles = user
|
||||
.getRoles()
|
||||
.stream()
|
||||
.map(Role::getName)
|
||||
.collect(toList());
|
||||
|
||||
return new UserDTO(name, roles);
|
||||
}
|
||||
|
||||
public User toUser(UserCreationDTO userDTO) {
|
||||
return new User(userDTO.getName(), userDTO.getPassword(), new ArrayList<>());
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.dtopattern.api;
|
||||
|
||||
import com.baeldung.dtopattern.domain.RoleService;
|
||||
import com.baeldung.dtopattern.domain.User;
|
||||
import com.baeldung.dtopattern.domain.UserService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/users")
|
||||
class UserController {
|
||||
|
||||
private UserService userService;
|
||||
private RoleService roleService;
|
||||
private Mapper mapper;
|
||||
|
||||
public UserController(UserService userService, RoleService roleService, Mapper mapper) {
|
||||
this.userService = userService;
|
||||
this.roleService = roleService;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@ResponseBody
|
||||
public List<UserDTO> getUsers() {
|
||||
return userService.getAll()
|
||||
.stream()
|
||||
.map(mapper::toDto)
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
|
||||
@PostMapping
|
||||
@ResponseBody
|
||||
public UserIdDTO create(@RequestBody UserCreationDTO userDTO) {
|
||||
User user = mapper.toUser(userDTO);
|
||||
|
||||
userDTO.getRoles()
|
||||
.stream()
|
||||
.map(role -> roleService.getOrCreate(role))
|
||||
.forEach(user::addRole);
|
||||
|
||||
userService.save(user);
|
||||
|
||||
return new UserIdDTO(user.getId());
|
||||
}
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.dtopattern.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class UserCreationDTO {
|
||||
|
||||
private String name;
|
||||
private String password;
|
||||
private List<String> roles;
|
||||
|
||||
UserCreationDTO() {}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public List<String> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
void setRoles(List<String> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.dtopattern.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class UserDTO {
|
||||
private String name;
|
||||
private List<String> roles;
|
||||
|
||||
public UserDTO(String name, List<String> roles) {
|
||||
this.name = name;
|
||||
this.roles = roles;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public List<String> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.dtopattern.api;
|
||||
|
||||
public class UserIdDTO {
|
||||
|
||||
private String id;
|
||||
|
||||
public UserIdDTO(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.dtopattern.domain;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
class InMemoryRepository implements UserRepository, RoleRepository {
|
||||
|
||||
private Map<String, User> users = new LinkedHashMap<>();
|
||||
private Map<String, Role> roles = new LinkedHashMap<>();
|
||||
|
||||
@Override
|
||||
public List<User> getAll() {
|
||||
return new ArrayList<>(users.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(User user) {
|
||||
user.setId(UUID.randomUUID().toString());
|
||||
users.put(user.getId(), user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Role role) {
|
||||
role.setId(UUID.randomUUID().toString());
|
||||
roles.put(role.getId(), role);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Role getRoleById(String id) {
|
||||
return roles.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Role getRoleByName(String name) {
|
||||
return roles.values()
|
||||
.stream()
|
||||
.filter(role -> role.getName().equalsIgnoreCase(name))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAll() {
|
||||
users.clear();
|
||||
roles.clear();
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.dtopattern.domain;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Role {
|
||||
private String id;
|
||||
private String name;
|
||||
|
||||
public Role(String name) {
|
||||
this.name = Objects.requireNonNull(name);
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
void setId(String id) {
|
||||
this.id = Objects.requireNonNull(id);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = Objects.requireNonNull(name);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.dtopattern.domain;
|
||||
|
||||
public interface RoleRepository {
|
||||
Role getRoleById(String id);
|
||||
Role getRoleByName(String name);
|
||||
void save(Role role);
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.dtopattern.domain;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@Service
|
||||
public class RoleService {
|
||||
|
||||
private RoleRepository repository;
|
||||
|
||||
public RoleService(RoleRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public Role getOrCreate(String name) {
|
||||
Role role = repository.getRoleByName(name);
|
||||
|
||||
if (role == null) {
|
||||
role = new Role(name);
|
||||
repository.save(role);
|
||||
}
|
||||
|
||||
return role;
|
||||
}
|
||||
|
||||
public void save(Role role) {
|
||||
Objects.requireNonNull(role);
|
||||
repository.save(role);
|
||||
}
|
||||
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package com.baeldung.dtopattern.domain;
|
||||
|
||||
import javax.crypto.*;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class User {
|
||||
|
||||
private static SecretKeySpec KEY = initKey();
|
||||
|
||||
static SecretKeySpec initKey(){
|
||||
try {
|
||||
SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey();
|
||||
return new SecretKeySpec(secretKey.getEncoded(), "AES");
|
||||
} catch (NoSuchAlgorithmException ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String id;
|
||||
private String name;
|
||||
private String password;
|
||||
private List<Role> roles;
|
||||
|
||||
public User(String name, String password, List<Role> roles) {
|
||||
this.name = Objects.requireNonNull(name);
|
||||
this.password = this.encrypt(password);
|
||||
this.roles = Objects.requireNonNull(roles);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public void addRole(Role role) {
|
||||
roles.add(role);
|
||||
}
|
||||
|
||||
public List<Role> getRoles() {
|
||||
return Collections.unmodifiableList(roles);
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
String encrypt(String password) {
|
||||
Objects.requireNonNull(password);
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance("AES");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, KEY);
|
||||
final byte[] encryptedBytes = cipher.doFinal(password.getBytes(StandardCharsets.UTF_8));
|
||||
return new String(encryptedBytes, StandardCharsets.UTF_8);
|
||||
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
|
||||
// do nothing
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.dtopattern.domain;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface UserRepository {
|
||||
List<User> getAll();
|
||||
void save(User user);
|
||||
void deleteAll();
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.dtopattern.domain;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
private UserRepository repository;
|
||||
|
||||
public UserService(UserRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public List<User> getAll() {
|
||||
return repository.getAll();
|
||||
}
|
||||
|
||||
public void save(User user) {
|
||||
Objects.requireNonNull(user);
|
||||
repository.save(user);
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.mvc_mvp.mvc;
|
||||
|
||||
public class MvcMainClass {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Product model = retrieveProductFromDatabase();
|
||||
ProductView view = new ProductView();
|
||||
model.setView(view);
|
||||
model.showProduct();
|
||||
|
||||
ProductController controller = new ProductController(model);
|
||||
controller.setName("SmartPhone");
|
||||
model.showProduct();
|
||||
}
|
||||
|
||||
private static Product retrieveProductFromDatabase() {
|
||||
Product product = new Product();
|
||||
product.setName("Mobile");
|
||||
product.setDescription("New Brand");
|
||||
product.setPrice(1000.0);
|
||||
return product;
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.mvc_mvp.mvc;
|
||||
|
||||
public class Product {
|
||||
private String name;
|
||||
private String description;
|
||||
private Double price;
|
||||
private ProductView view;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public ProductView getView() {
|
||||
return view;
|
||||
}
|
||||
|
||||
public void setView(ProductView view) {
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
public void showProduct() {
|
||||
view.printProductDetails(name, description, price);
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.mvc_mvp.mvc;
|
||||
|
||||
public class ProductController {
|
||||
private final Product product;
|
||||
|
||||
public ProductController(Product product) {
|
||||
this.product = product;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return product.getName();
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
product.setName(name);
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return product.getDescription();
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
product.setDescription(description);
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return product.getPrice();
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
product.setPrice(price);
|
||||
}
|
||||
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.mvc_mvp.mvc;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ProductView {
|
||||
private static Logger log = LoggerFactory.getLogger(ProductView.class);
|
||||
|
||||
public void printProductDetails(String name, String description, Double price) {
|
||||
log.info("Product details:");
|
||||
log.info("product Name: " + name);
|
||||
log.info("product Description: " + description);
|
||||
log.info("product price: " + price);
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.mvc_mvp.mvp;
|
||||
|
||||
public class MvpMainClass {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Product model = retrieveProductFromDatabase();
|
||||
ProductView view = new ProductView();
|
||||
ProductPresenter presenter = new ProductPresenter(model, view);
|
||||
presenter.showProduct();
|
||||
presenter.setName("SmartPhone");
|
||||
presenter.showProduct();
|
||||
}
|
||||
|
||||
private static Product retrieveProductFromDatabase() {
|
||||
Product product = new Product();
|
||||
product.setName("Mobile");
|
||||
product.setDescription("New Brand");
|
||||
product.setPrice(1000.0);
|
||||
return product;
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.mvc_mvp.mvp;
|
||||
|
||||
public class Product {
|
||||
private String name;
|
||||
private String description;
|
||||
private Double price;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.mvc_mvp.mvp;
|
||||
|
||||
public class ProductPresenter {
|
||||
private final Product product;
|
||||
private final ProductView view;
|
||||
|
||||
public ProductPresenter(Product product, ProductView view) {
|
||||
this.product = product;
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return product.getName();
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
product.setName(name);
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return product.getDescription();
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
product.setDescription(description);
|
||||
}
|
||||
|
||||
public Double getProductPrice() {
|
||||
return product.getPrice();
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
product.setPrice(price);
|
||||
}
|
||||
|
||||
public void showProduct() {
|
||||
view.printProductDetails(product.getName(), product.getDescription(), product.getPrice());
|
||||
}
|
||||
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.mvc_mvp.mvp;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ProductView {
|
||||
private static Logger log = LoggerFactory.getLogger(ProductView.class);
|
||||
|
||||
public void printProductDetails(String name, String description, Double price) {
|
||||
log.info("Product details:");
|
||||
log.info("product Name: " + name);
|
||||
log.info("product Description: " + description);
|
||||
log.info("product price: " + price);
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.repositoryvsdaopattern;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Tweet {
|
||||
|
||||
private String email;
|
||||
|
||||
private String tweetText;
|
||||
|
||||
private Date dateCreated;
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getTweetText() {
|
||||
return tweetText;
|
||||
}
|
||||
|
||||
public void setTweetText(String tweetText) {
|
||||
this.tweetText = tweetText;
|
||||
}
|
||||
|
||||
public Date getDateCreated() {
|
||||
return dateCreated;
|
||||
}
|
||||
|
||||
public void setDateCreated(Date dateCreated) {
|
||||
this.dateCreated = dateCreated;
|
||||
}
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.repositoryvsdaopattern;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface TweetDao {
|
||||
|
||||
List<Tweet> fetchTweets(String email);
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.repositoryvsdaopattern;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TweetDaoImpl implements TweetDao {
|
||||
|
||||
@Override
|
||||
public List<Tweet> fetchTweets(String email) {
|
||||
List<Tweet> tweets = new ArrayList<Tweet>();
|
||||
|
||||
//call Twitter API and prepare Tweet object
|
||||
|
||||
return tweets;
|
||||
}
|
||||
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.repositoryvsdaopattern;
|
||||
|
||||
public class User {
|
||||
|
||||
private Long id;
|
||||
private String userName;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String email;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.repositoryvsdaopattern;
|
||||
|
||||
public interface UserDao {
|
||||
|
||||
void create(User user);
|
||||
|
||||
User read(Long id);
|
||||
|
||||
void update(User user);
|
||||
|
||||
void delete(String userName);
|
||||
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.repositoryvsdaopattern;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
public class UserDaoImpl implements UserDao {
|
||||
|
||||
private final EntityManager entityManager;
|
||||
|
||||
public UserDaoImpl(EntityManager entityManager) {
|
||||
this.entityManager = entityManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create(User user) {
|
||||
entityManager.persist(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User read(Long id) {
|
||||
return entityManager.find(User.class, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(User user) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String userName) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.repositoryvsdaopattern;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface UserRepository {
|
||||
|
||||
User get(Long id);
|
||||
|
||||
void add(User user);
|
||||
|
||||
void update(User user);
|
||||
|
||||
void remove(User user);
|
||||
|
||||
User findByUserName(String userName);
|
||||
|
||||
User findByEmail(String email);
|
||||
|
||||
List<Tweet> fetchTweets(User user);
|
||||
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.repositoryvsdaopattern;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class UserRepositoryImpl implements UserRepository {
|
||||
|
||||
private UserDaoImpl userDaoImpl;
|
||||
private TweetDaoImpl tweetDaoImpl;
|
||||
|
||||
@Override
|
||||
public User get(Long id) {
|
||||
UserSocialMedia user = (UserSocialMedia) userDaoImpl.read(id);
|
||||
|
||||
List<Tweet> tweets = tweetDaoImpl.fetchTweets(user.getEmail());
|
||||
user.setTweets(tweets);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(User user) {
|
||||
userDaoImpl.create(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(User user) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(User user) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Tweet> fetchTweets(User user) {
|
||||
return tweetDaoImpl.fetchTweets(user.getEmail());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public User findByUserName(String userName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User findByEmail(String email) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.repositoryvsdaopattern;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class UserSocialMedia extends User {
|
||||
|
||||
private List<Tweet> tweets;
|
||||
|
||||
public List<Tweet> getTweets() {
|
||||
return tweets;
|
||||
}
|
||||
|
||||
public void setTweets(List<Tweet> tweets) {
|
||||
this.tweets = tweets;
|
||||
}
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.service.locator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 4/20/2018.
|
||||
*/
|
||||
public class Cache {
|
||||
private List<MessagingService> services;
|
||||
|
||||
public Cache(){
|
||||
services = new ArrayList<MessagingService>();
|
||||
}
|
||||
|
||||
public MessagingService getService(String serviceName){
|
||||
|
||||
for (MessagingService service : services) {
|
||||
if(service.getServiceName().equalsIgnoreCase(serviceName)){
|
||||
System.out.println("Returning cached " + serviceName + " object");
|
||||
return service;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addService(MessagingService newService){
|
||||
boolean exists = false;
|
||||
|
||||
for (MessagingService service : services) {
|
||||
if(service.getServiceName().equalsIgnoreCase(newService.getServiceName())){
|
||||
exists = true;
|
||||
}
|
||||
}
|
||||
if(!exists){
|
||||
services.add(newService);
|
||||
}
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.service.locator;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 4/20/2018.
|
||||
*/
|
||||
public class EmailService implements MessagingService {
|
||||
|
||||
public String getMessageBody() {
|
||||
return "email message";
|
||||
}
|
||||
|
||||
public String getServiceName() {
|
||||
return "EmailService";
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.service.locator;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 4/20/2018.
|
||||
*/
|
||||
public class InitialContext {
|
||||
|
||||
public Object lookup(String serviceName) {
|
||||
|
||||
if (serviceName.equalsIgnoreCase("EmailService")) {
|
||||
return new EmailService();
|
||||
} else if (serviceName.equalsIgnoreCase("SMSService")) {
|
||||
return new SMSService();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.service.locator;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 4/20/2018.
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
MessagingService service = ServiceLocator.getService("EmailService");
|
||||
String email = service.getMessageBody();
|
||||
System.out.println(email);
|
||||
|
||||
service = ServiceLocator.getService("SMSService");
|
||||
String sms = service.getMessageBody();
|
||||
System.out.println(sms);
|
||||
|
||||
service = ServiceLocator.getService("EmailService");
|
||||
String newEmail = service.getMessageBody();
|
||||
System.out.println(newEmail);
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.service.locator;
|
||||
|
||||
public interface MessagingService {
|
||||
|
||||
String getMessageBody();
|
||||
|
||||
String getServiceName();
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.service.locator;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 4/20/2018.
|
||||
*/
|
||||
public class SMSService implements MessagingService {
|
||||
|
||||
public String getMessageBody() {
|
||||
return "sms message";
|
||||
}
|
||||
|
||||
public String getServiceName() {
|
||||
return "SMSService";
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.service.locator;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 4/20/2018.
|
||||
*/
|
||||
public class ServiceLocator {
|
||||
|
||||
private static Cache cache;
|
||||
|
||||
static {
|
||||
cache = new Cache();
|
||||
}
|
||||
|
||||
public static MessagingService getService(String serviceName){
|
||||
|
||||
MessagingService service = cache.getService(serviceName);
|
||||
|
||||
if(service != null){
|
||||
return service;
|
||||
}
|
||||
|
||||
InitialContext context = new InitialContext();
|
||||
MessagingService service1 = (MessagingService)context.lookup(serviceName);
|
||||
cache.addService(service1);
|
||||
return service1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user