Обновление ECI, репозитории, девайсы
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
package com.rosetta.im.database;
|
||||
|
||||
public class BaseEntity {
|
||||
|
||||
/**
|
||||
* Сохраняет сущность в базе данных.
|
||||
* @param entity Сущность для сохранения.
|
||||
*/
|
||||
public void save(Object entity) {
|
||||
DatabaseManager.save(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Обновляет сущность в базе данных.
|
||||
* @param entity Сущность для обновления.
|
||||
*/
|
||||
public void update(Object entity) {
|
||||
DatabaseManager.update(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Удаляет сущность из базы данных.
|
||||
* @param entity Сущность для удаления.
|
||||
*/
|
||||
public void delete(Object entity) {
|
||||
DatabaseManager.delete(entity);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.rosetta.im.database;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
import jakarta.persistence.PrePersist;
|
||||
import jakarta.persistence.PreUpdate;
|
||||
|
||||
/**
|
||||
* Базовый класс для сущностей с полями
|
||||
* времени создания и обновления
|
||||
*/
|
||||
@MappedSuperclass
|
||||
public class CreateUpdateEntity {
|
||||
|
||||
@Column(name = "createdAt", nullable = false, updatable = false)
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
@Column(name = "updatedAt", nullable = false)
|
||||
private LocalDateTime updatedAt;
|
||||
|
||||
@PrePersist
|
||||
protected void onCreate() {
|
||||
createdAt = LocalDateTime.now();
|
||||
updatedAt = LocalDateTime.now();
|
||||
}
|
||||
|
||||
@PreUpdate
|
||||
protected void onUpdate() {
|
||||
updatedAt = LocalDateTime.now();
|
||||
}
|
||||
|
||||
public LocalDateTime getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
public LocalDateTime getUpdatedAt() {
|
||||
return updatedAt;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
package com.rosetta.im.database;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class DatabaseManager {
|
||||
|
||||
public static <T> T save(T entity) {
|
||||
Session session = HibernateUtil.openSession();
|
||||
Transaction transaction = null;
|
||||
try {
|
||||
transaction = session.beginTransaction();
|
||||
session.persist(entity);
|
||||
transaction.commit();
|
||||
return entity;
|
||||
} catch (Exception e) {
|
||||
if (transaction != null) {
|
||||
transaction.rollback();
|
||||
}
|
||||
throw new RuntimeException("Error saving entity: " + e.getMessage(), e);
|
||||
} finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T update(T entity) {
|
||||
Session session = HibernateUtil.openSession();
|
||||
Transaction transaction = null;
|
||||
try {
|
||||
transaction = session.beginTransaction();
|
||||
session.merge(entity);
|
||||
transaction.commit();
|
||||
return entity;
|
||||
} catch (Exception e) {
|
||||
if (transaction != null) {
|
||||
transaction.rollback();
|
||||
}
|
||||
throw new RuntimeException("Error updating entity: " + e.getMessage(), e);
|
||||
} finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T findById(Class<T> entityClass, Long id) {
|
||||
Session session = HibernateUtil.openSession();
|
||||
try {
|
||||
return session.find(entityClass, id);
|
||||
} finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> List<T> findAll(Class<T> entityClass) {
|
||||
Session session = HibernateUtil.openSession();
|
||||
try {
|
||||
return session.createQuery("FROM " + entityClass.getSimpleName(), entityClass).list();
|
||||
} finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T findBy(Class<T> entity, HashMap<String, Object> params) {
|
||||
Session session = HibernateUtil.openSession();
|
||||
try {
|
||||
StringBuilder queryString = new StringBuilder("FROM " + entity.getSimpleName() + " WHERE ");
|
||||
int index = 0;
|
||||
for (String key : params.keySet()) {
|
||||
if (index > 0) {
|
||||
queryString.append(" AND ");
|
||||
}
|
||||
queryString.append(key).append(" = :").append(key);
|
||||
index++;
|
||||
}
|
||||
var query = session.createQuery(queryString.toString(), entity);
|
||||
for (var entry : params.entrySet()) {
|
||||
query.setParameter(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return query.uniqueResult();
|
||||
} finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> void delete(T entity) {
|
||||
Session session = HibernateUtil.openSession();
|
||||
Transaction transaction = null;
|
||||
try {
|
||||
transaction = session.beginTransaction();
|
||||
session.remove(entity);
|
||||
transaction.commit();
|
||||
} catch (Exception e) {
|
||||
if (transaction != null) {
|
||||
transaction.rollback();
|
||||
}
|
||||
throw new RuntimeException("Error deleting entity: " + e.getMessage(), e);
|
||||
} finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
158
src/main/java/com/rosetta/im/database/Repository.java
Normal file
158
src/main/java/com/rosetta/im/database/Repository.java
Normal file
@@ -0,0 +1,158 @@
|
||||
package com.rosetta.im.database;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
|
||||
/**
|
||||
* Базовый репозиторий для работы с сущностями базы данных
|
||||
*/
|
||||
public abstract class Repository<T> {
|
||||
|
||||
protected Class<T> entityClass;
|
||||
|
||||
public Repository(Class<T> entityClass) {
|
||||
this.entityClass = entityClass;
|
||||
}
|
||||
|
||||
public T save(T entity) {
|
||||
return executeInTransaction(session -> {
|
||||
session.persist(entity);
|
||||
return entity;
|
||||
});
|
||||
}
|
||||
|
||||
public T update(T entity) {
|
||||
return executeInTransaction(session -> {
|
||||
session.merge(entity);
|
||||
return entity;
|
||||
});
|
||||
}
|
||||
|
||||
public void delete(T entity) {
|
||||
executeInTransaction(session -> {
|
||||
session.remove(entity);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Поиск сущности по значению одного поля
|
||||
* @param fieldName поле
|
||||
* @param value значение
|
||||
* @return найденная сущность или null
|
||||
*/
|
||||
public T findByField(String fieldName, Object value) {
|
||||
return executeInSession(session -> {
|
||||
String queryString = "FROM " + entityClass.getSimpleName() + " WHERE " + fieldName + " = :value";
|
||||
return session.createQuery(queryString, entityClass)
|
||||
.setParameter("value", value)
|
||||
.uniqueResult();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Поиск сущности по набору полей
|
||||
* @param fields карта полей и их значений
|
||||
* @return найденная сущность или null
|
||||
*/
|
||||
public T findByField(HashMap<String, Object> fields) {
|
||||
return executeInSession(session -> {
|
||||
StringBuilder queryString = new StringBuilder("FROM " + entityClass.getSimpleName() + " WHERE ");
|
||||
int index = 0;
|
||||
for (String fieldName : fields.keySet()) {
|
||||
if (index > 0) {
|
||||
queryString.append(" AND ");
|
||||
}
|
||||
queryString.append(fieldName).append(" = :").append(fieldName);
|
||||
index++;
|
||||
}
|
||||
var query = session.createQuery(queryString.toString(), this.entityClass);
|
||||
for (var entry : fields.entrySet()) {
|
||||
query.setParameter(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return query.uniqueResult();
|
||||
});
|
||||
}
|
||||
|
||||
public void update(HashMap<String, Object> fieldsToUpdate, HashMap<String, Object> whereFields) {
|
||||
executeInTransaction(session -> {
|
||||
StringBuilder queryString = new StringBuilder("UPDATE " + entityClass.getSimpleName() + " SET ");
|
||||
int index = 0;
|
||||
for (String fieldName : fieldsToUpdate.keySet()) {
|
||||
if (index > 0) {
|
||||
queryString.append(", ");
|
||||
}
|
||||
queryString.append(fieldName).append(" = :").append(fieldName);
|
||||
index++;
|
||||
}
|
||||
queryString.append(" WHERE ");
|
||||
index = 0;
|
||||
for (String fieldName : whereFields.keySet()) {
|
||||
if (index > 0) {
|
||||
queryString.append(" AND ");
|
||||
}
|
||||
/**
|
||||
* Добавляем префикс where_ к параметрам условия WHERE,
|
||||
* чтобы избежать конфликтов имен с параметрами SET
|
||||
*/
|
||||
queryString.append(fieldName).append(" = :where_").append(fieldName);
|
||||
index++;
|
||||
}
|
||||
var query = session.createQuery(queryString.toString(), this.entityClass);
|
||||
for (var entry : fieldsToUpdate.entrySet()) {
|
||||
query.setParameter(entry.getKey(), entry.getValue());
|
||||
}
|
||||
for (var entry : whereFields.entrySet()) {
|
||||
/**
|
||||
* Устанавливаем параметры с префиксом where_ для условий WHERE,
|
||||
* чтобы избежать конфликтов имен с параметрами SET
|
||||
*/
|
||||
query.setParameter("where_" + entry.getKey(), entry.getValue());
|
||||
}
|
||||
query.executeUpdate();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
protected <R> R executeInTransaction(TransactionCallback<R> callback) {
|
||||
Session session = HibernateUtil.openSession();
|
||||
Transaction transaction = null;
|
||||
try {
|
||||
transaction = session.beginTransaction();
|
||||
R result = callback.execute(session);
|
||||
transaction.commit();
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
if (transaction != null) {
|
||||
transaction.rollback();
|
||||
}
|
||||
throw new RuntimeException("Transaction failed: " + e.getMessage(), e);
|
||||
} finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
protected <R> R executeInSession(SessionCallback<R> callback) {
|
||||
try (Session session = HibernateUtil.openSession()) {
|
||||
return callback.execute(session);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Функциональный интерфейс для обратного вызова транзакции.
|
||||
* (Коллбэки)
|
||||
*/
|
||||
@FunctionalInterface
|
||||
protected interface TransactionCallback<R> {
|
||||
R execute(Session session);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
protected interface SessionCallback<R> {
|
||||
R execute(Session session);
|
||||
}
|
||||
|
||||
}
|
||||
84
src/main/java/com/rosetta/im/database/entity/Device.java
Normal file
84
src/main/java/com/rosetta/im/database/entity/Device.java
Normal file
@@ -0,0 +1,84 @@
|
||||
package com.rosetta.im.database.entity;
|
||||
|
||||
import com.rosetta.im.database.CreateUpdateEntity;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.PrePersist;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "devices")
|
||||
public class Device extends CreateUpdateEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "publicKey", nullable = false)
|
||||
private String publicKey;
|
||||
@Column(name = "deviceId", nullable = false)
|
||||
private String deviceId;
|
||||
@Column(name = "deviceName", nullable = false)
|
||||
private String deviceName;
|
||||
@Column(name = "deviceOs", nullable = false)
|
||||
private String deviceOs;
|
||||
/**
|
||||
* Время завершения сессии устройства
|
||||
*/
|
||||
@Column(name = "leaveTime", nullable = true)
|
||||
private Long leaveTime;
|
||||
|
||||
@PrePersist
|
||||
protected void onCreate() {
|
||||
this.leaveTime = 0L;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getPublicKey() {
|
||||
return publicKey;
|
||||
}
|
||||
|
||||
public String getDeviceId() {
|
||||
return deviceId;
|
||||
}
|
||||
|
||||
public String getDeviceName() {
|
||||
return deviceName;
|
||||
}
|
||||
|
||||
public String getDeviceOs() {
|
||||
return deviceOs;
|
||||
}
|
||||
|
||||
public Long getLeaveTime() {
|
||||
return leaveTime;
|
||||
}
|
||||
|
||||
public void setLeaveTime(Long leaveTime) {
|
||||
this.leaveTime = leaveTime;
|
||||
}
|
||||
|
||||
public void setPublicKey(String publicKey) {
|
||||
this.publicKey = publicKey;
|
||||
}
|
||||
|
||||
public void setDeviceId(String deviceId) {
|
||||
this.deviceId = deviceId;
|
||||
}
|
||||
|
||||
public void setDeviceName(String deviceName) {
|
||||
this.deviceName = deviceName;
|
||||
}
|
||||
|
||||
public void setDeviceOs(String deviceOs) {
|
||||
this.deviceOs = deviceOs;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.rosetta.im.database.entity;
|
||||
|
||||
import com.rosetta.im.database.BaseEntity;
|
||||
import com.rosetta.im.database.HibernateUtil;
|
||||
import com.rosetta.im.database.CreateUpdateEntity;
|
||||
import com.rosetta.im.database.converters.StringListConverter;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
@@ -11,59 +10,40 @@ import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Index;
|
||||
import jakarta.persistence.PrePersist;
|
||||
import jakarta.persistence.PreUpdate;
|
||||
import jakarta.persistence.Table;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users", indexes = {
|
||||
@Index(name = "idx_users_publickey", columnList = "publicKey", unique = true)
|
||||
})
|
||||
public class User extends BaseEntity {
|
||||
public class User extends CreateUpdateEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "privateKey", nullable = false)
|
||||
private String privateKey;
|
||||
|
||||
@Column(name = "username")
|
||||
private String username;
|
||||
|
||||
@Column(name = "title")
|
||||
private String title;
|
||||
private String title;
|
||||
|
||||
@Column(name = "verified", nullable = false)
|
||||
private int verified;
|
||||
|
||||
@Column(name = "privateKey", nullable = false)
|
||||
private String privateKey;
|
||||
|
||||
@Column(name = "publicKey", nullable = false, unique = true)
|
||||
private String publicKey;
|
||||
|
||||
@Column(name = "createdTime", nullable = false, updatable = false)
|
||||
private Date createdTime;
|
||||
|
||||
@Column(name = "verified", nullable = false)
|
||||
private int verified;
|
||||
|
||||
@Convert(converter = StringListConverter.class)
|
||||
@Column(name = "notificationsTokens", nullable = false)
|
||||
private List<String> notificationsTokens = new ArrayList<>();
|
||||
|
||||
@PrePersist
|
||||
protected void onCreate() {
|
||||
createdTime = new Date();
|
||||
}
|
||||
|
||||
@PreUpdate
|
||||
protected void onUpdate() {
|
||||
if (createdTime == null) {
|
||||
createdTime = new Date();
|
||||
}
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
@@ -101,10 +81,6 @@ public class User extends BaseEntity {
|
||||
this.publicKey = publicKey;
|
||||
}
|
||||
|
||||
public Date getCreatedTime() {
|
||||
return createdTime;
|
||||
}
|
||||
|
||||
public int getVerified() {
|
||||
return verified;
|
||||
}
|
||||
@@ -121,98 +97,4 @@ public class User extends BaseEntity {
|
||||
this.notificationsTokens = notificationsTokens;
|
||||
}
|
||||
|
||||
public static User getByPrivateKey(String privateKey) {
|
||||
try (Session session = HibernateUtil.openSession()) {
|
||||
return session.createQuery(
|
||||
"from User where privateKey = :privateKey", User.class)
|
||||
.setParameter("privateKey", privateKey)
|
||||
.uniqueResult();
|
||||
}
|
||||
}
|
||||
|
||||
public static User getByUsername(String username) {
|
||||
try (Session session = HibernateUtil.openSession()) {
|
||||
return session.createQuery(
|
||||
"from User where username = :username", User.class)
|
||||
.setParameter("username", username)
|
||||
.uniqueResult();
|
||||
}
|
||||
}
|
||||
|
||||
public static User getByPublicKey(String publicKey) {
|
||||
try (Session session = HibernateUtil.openSession()) {
|
||||
return session.createQuery(
|
||||
"from User where publicKey = :publicKey", User.class)
|
||||
.setParameter("publicKey", publicKey)
|
||||
.uniqueResult();
|
||||
}
|
||||
}
|
||||
|
||||
public static void addNotificationToken(String privateKey, String token) {
|
||||
Session session = HibernateUtil.openSession();
|
||||
Transaction transaction = null;
|
||||
try {
|
||||
transaction = session.beginTransaction();
|
||||
User user = session.createQuery(
|
||||
"from User where privateKey = :privateKey", User.class)
|
||||
.setParameter("privateKey", privateKey)
|
||||
.uniqueResult();
|
||||
if (user != null) {
|
||||
if (!user.notificationsTokens.contains(token)) {
|
||||
user.notificationsTokens.add(token);
|
||||
session.merge(user);
|
||||
}
|
||||
}
|
||||
transaction.commit();
|
||||
} catch (Exception e) {
|
||||
if (transaction != null) {
|
||||
transaction.rollback();
|
||||
}
|
||||
throw new RuntimeException("Error adding notification token: " + e.getMessage(), e);
|
||||
} finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeNotificationToken(String privateKey, String token) {
|
||||
Session session = HibernateUtil.openSession();
|
||||
Transaction transaction = null;
|
||||
try {
|
||||
transaction = session.beginTransaction();
|
||||
User user = session.createQuery(
|
||||
"from User where privateKey = :privateKey", User.class)
|
||||
.setParameter("privateKey", privateKey)
|
||||
.uniqueResult();
|
||||
if (user != null) {
|
||||
user.notificationsTokens = new ArrayList<>(user.notificationsTokens.stream()
|
||||
.filter(t -> !t.equals(token))
|
||||
.toList());
|
||||
session.merge(user);
|
||||
}
|
||||
transaction.commit();
|
||||
} catch (Exception e) {
|
||||
if (transaction != null) {
|
||||
transaction.rollback();
|
||||
}
|
||||
throw new RuntimeException("Error removing notification token: " + e.getMessage(), e);
|
||||
} finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> getNotificationTokensByPrivateKey(String privateKey) {
|
||||
User user = getByPrivateKey(privateKey);
|
||||
if (user != null) {
|
||||
return user.notificationsTokens;
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
|
||||
public static List<String> getNotificationsTokensByPublicKey(String publicKey) {
|
||||
User user = getByPublicKey(publicKey);
|
||||
if (user != null) {
|
||||
return user.notificationsTokens;
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.rosetta.im.database.repository;
|
||||
|
||||
public class DeviceRepository {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.rosetta.im.database.repository;
|
||||
|
||||
import com.rosetta.im.database.Repository;
|
||||
import com.rosetta.im.database.entity.User;
|
||||
|
||||
public class UserRepository extends Repository<User> {
|
||||
|
||||
public UserRepository() {
|
||||
super(User.class);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user