Конфигурация для Jpa

изменения логики отдачи сообщений
This commit is contained in:
Mark Struchkov 2019-08-11 15:41:47 +03:00
parent 0f08fa6a0e
commit b4835d1c78
13 changed files with 138 additions and 64 deletions

View File

@ -71,6 +71,11 @@
<artifactId>validation-api</artifactId> <artifactId>validation-api</artifactId>
<version>${validation.api.ver}</version> <version>${validation.api.ver}</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
</dependencies> </dependencies>
<developers> <developers>

View File

@ -0,0 +1,15 @@
package org.sadtech.social.core.config;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
/**
* Конфигуратор для использования в {@link org.springframework.context.annotation.Import}.
*
* @author upagge [28/07/2019]
*/
@EnableJpaRepositories(basePackages = "org.sadtech.social.core.repository.jpa")
@EntityScan(basePackages = "org.sadtech.social.core.domain")
public class SocialJpaSpringConfig {
}

View File

@ -30,18 +30,13 @@ public class Mail extends Message {
@Description("Вложения к сообщению") @Description("Вложения к сообщению")
private List<Attachment> attachments; private List<Attachment> attachments;
@OneToMany
@Column(name = "forward_mail")
@Description("Пересланные сообщения")
private List<Mail> forwardMail;
public Mail() { public Mail() {
type = ContentType.MAIL; type = ContentType.MAIL;
} }
public Mail(Mail source) {
super(source);
this.attachments = source.getAttachments();
}
public Mail prototype() {
return new Mail(this);
}
} }

View File

@ -33,6 +33,10 @@ public abstract class Message extends BasicEntity {
@Description("Дата создания") @Description("Дата создания")
private LocalDateTime createDate; private LocalDateTime createDate;
@Column(name = "add_date")
@Description("Дата добавления в базу")
private LocalDateTime addDate;
@NotNull @NotNull
@Column(name = "person_id") @Column(name = "person_id")
@Description("Идентификатор пользователя, отправившего сообщение") @Description("Идентификатор пользователя, отправившего сообщение")

View File

@ -1,30 +0,0 @@
package org.sadtech.social.core.domain.jpa;
import com.google.gson.JsonObject;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.sadtech.social.core.domain.BasicEntity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
* TODO: Добавить описание класса.
*
* @author upagge [28/07/2019]
*/
@Data
@RequiredArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "event")
public class JsonObjectId extends BasicEntity {
@Column(name = "json")
@NonNull
private JsonObject jsonObject;
}

View File

@ -27,6 +27,8 @@ public interface ContentRepository<T extends Message> {
* @param timeTo Конец диапазона * @param timeTo Конец диапазона
* @return Список сообщений * @return Список сообщений
*/ */
List<T> betweenByTime(LocalDateTime timeFrom, LocalDateTime timeTo); List<T> betweenByCreateDateTime(LocalDateTime timeFrom, LocalDateTime timeTo);
List<T> betweenByAddDateTime(LocalDateTime from, LocalDateTime to);
} }

View File

@ -24,7 +24,13 @@ public class MailRepositoryJpaImpl implements ContentRepository<Mail> {
} }
@Override @Override
public List<Mail> betweenByTime(LocalDateTime timeFrom, LocalDateTime timeTo) { public List<Mail> betweenByCreateDateTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
return mailRepositoryJpa.findByCreateDateBetween(timeFrom, timeTo); return mailRepositoryJpa.findByCreateDateBetween(timeFrom, timeTo);
} }
@Override
public List<Mail> betweenByAddDateTime(LocalDateTime from, LocalDateTime to) {
return mailRepositoryJpa.findByAddDateBetween(from, to);
}
} }

View File

@ -22,7 +22,7 @@ public class BoardCommentRepositoryMap implements ContentRepository<BoardComment
} }
@Override @Override
public List<BoardComment> betweenByTime(LocalDateTime timeFrom, LocalDateTime timeTo) { public List<BoardComment> betweenByCreateDateTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
ArrayList<BoardComment> rezultMails = new ArrayList<>(); ArrayList<BoardComment> rezultMails = new ArrayList<>();
for (int i = saveMap.size() - 1; i >= 0; i--) { for (int i = saveMap.size() - 1; i >= 0; i--) {
if (!(saveMap.get(i).getCreateDate().isBefore(timeFrom) || saveMap.get(i).getCreateDate().isAfter(timeTo)) && saveMap.get(i).getCreateDate().equals(timeFrom)) { if (!(saveMap.get(i).getCreateDate().isBefore(timeFrom) || saveMap.get(i).getCreateDate().isAfter(timeTo)) && saveMap.get(i).getCreateDate().equals(timeFrom)) {
@ -33,4 +33,18 @@ public class BoardCommentRepositoryMap implements ContentRepository<BoardComment
} }
return rezultMails; return rezultMails;
} }
@Override
public List<BoardComment> betweenByAddDateTime(LocalDateTime from, LocalDateTime to) {
ArrayList<BoardComment> rezultMails = new ArrayList<>();
for (int i = saveMap.size() - 1; i >= 0; i--) {
if (!(saveMap.get(i).getAddDate().isBefore(from) || saveMap.get(i).getAddDate().isAfter(to)) && saveMap.get(i).getAddDate().equals(from)) {
rezultMails.add(this.saveMap.get(i));
} else if (saveMap.get(i).getCreateDate().isBefore(to)) {
break;
}
}
return rezultMails;
}
} }

View File

@ -1,5 +1,6 @@
package org.sadtech.social.core.repository.impl.local; package org.sadtech.social.core.repository.impl.local;
import lombok.extern.slf4j.Slf4j;
import org.sadtech.social.core.domain.content.Mail; import org.sadtech.social.core.domain.content.Mail;
import org.sadtech.social.core.repository.ContentRepository; import org.sadtech.social.core.repository.ContentRepository;
@ -12,29 +13,51 @@ import java.util.List;
* *
* @author upagge [27/07/2019] * @author upagge [27/07/2019]
*/ */
@Slf4j
public class MailRepositoryList implements ContentRepository<Mail> { public class MailRepositoryList implements ContentRepository<Mail> {
private final List<Mail> mails = new ArrayList<>(); private final List<Mail> mails = new ArrayList<>();
@Override @Override
public Integer add(Mail mail) { public Integer add(Mail mail) {
int id = mails.size();
mail.setId(id);
mails.add(mail); mails.add(mail);
return mails.size() - 1; return id;
} }
@Override @Override
public List<Mail> betweenByTime(LocalDateTime timeFrom, LocalDateTime timeTo) { public List<Mail> betweenByCreateDateTime(LocalDateTime from, LocalDateTime to) {
ArrayList<Mail> rezultMails = new ArrayList<>(); ArrayList<Mail> rezultMails = new ArrayList<>();
for (int i = mails.size() - 1; i >= 0; i--) { for (int i = mails.size() - 1; i >= 0; i--) {
if (!(mails.get(i).getCreateDate().isBefore(timeFrom) || mails.get(i).getCreateDate().isAfter(timeTo))) { Mail mail = mails.get(i);
rezultMails.add(this.mails.get(i)); if (isTimePeriod(from, to, mail.getAddDate())) {
} else if (mails.get(i).getCreateDate().isBefore(timeFrom)) { rezultMails.add(mail);
} else if (mail.getCreateDate().isBefore(from)) {
break; break;
} }
} }
return rezultMails; return rezultMails;
} }
@Override
public List<Mail> betweenByAddDateTime(LocalDateTime from, LocalDateTime to) {
ArrayList<Mail> rezultMails = new ArrayList<>();
for (int i = mails.size() - 1; i >= 0; i--) {
Mail mail = mails.get(i);
LocalDateTime addDate = mail.getAddDate();
if (isTimePeriod(from, to, addDate)) {
rezultMails.add(mail);
} else if (addDate.isBefore(from)) {
break;
}
}
return rezultMails;
}
private boolean isTimePeriod(LocalDateTime from, LocalDateTime to, LocalDateTime dateTime) {
return from.isBefore(dateTime) && to.isAfter(dateTime);
}
} }

View File

@ -18,4 +18,6 @@ public interface MailRepositoryJpa extends JpaRepository<Mail, Integer> {
List<Mail> findByCreateDateBetween(LocalDateTime from, LocalDateTime to); List<Mail> findByCreateDateBetween(LocalDateTime from, LocalDateTime to);
List<Mail> findByAddDateBetween(LocalDateTime from, LocalDateTime to);
} }

View File

@ -20,7 +20,7 @@ public interface MessageService<T extends Message> {
* @param timeTo Конец интервала * @param timeTo Конец интервала
* @return Список сообщений * @return Список сообщений
*/ */
List<T> getByTime(LocalDateTime timeFrom, LocalDateTime timeTo); List<T> getByAddDateTime(LocalDateTime timeFrom, LocalDateTime timeTo);
/** /**
* Получить список ПОСЛЕДНИХ сообщений для каждого пользователя за заданных временной интервал * Получить список ПОСЛЕДНИХ сообщений для каждого пользователя за заданных временной интервал
@ -28,6 +28,8 @@ public interface MessageService<T extends Message> {
* @param timeTo Конец интервала * @param timeTo Конец интервала
* @return Список сообщений * @return Список сообщений
*/ */
List<T> getLastEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo); List<T> getLastEventByCreateDateTime(LocalDateTime timeFrom, LocalDateTime timeTo);
List<T> getLastEventByAddDateTime(LocalDateTime timeFrom, LocalDateTime timeTo);
} }

View File

@ -24,14 +24,25 @@ public class BoardCommentServiceImpl implements BoardCommentService {
} }
@Override @Override
public List<BoardComment> getByTime(LocalDateTime timeFrom, LocalDateTime timeTo) { public List<BoardComment> getByAddDateTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
return null; return null;
} }
@Override @Override
public List<BoardComment> getLastEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo) { public List<BoardComment> getLastEventByCreateDateTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
log.info("Запрошены последние комментарии к обсуждению {} - {} ", timeFrom, timeTo); log.info("Запрошены последние комментарии к обсуждению {} - {} ", timeFrom, timeTo);
List<BoardComment> mails = commentRepository.betweenByTime(timeFrom, timeTo); List<BoardComment> mails = commentRepository.betweenByCreateDateTime(timeFrom, timeTo);
return getBoardComments(mails);
}
@Override
public List<BoardComment> getLastEventByAddDateTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
log.info("Запрошены последние комментарии к обсуждению {} - {} ", timeFrom, timeTo);
List<BoardComment> mails = commentRepository.betweenByAddDateTime(timeFrom, timeTo);
return getBoardComments(mails);
}
private List<BoardComment> getBoardComments(List<BoardComment> mails) {
Set<Integer> people = new HashSet<>(); Set<Integer> people = new HashSet<>();
List<BoardComment> returnMails = new ArrayList<>(); List<BoardComment> returnMails = new ArrayList<>();
for (int i = mails.size() - 1; i >= 0; i--) { for (int i = mails.size() - 1; i >= 0; i--) {
@ -42,4 +53,5 @@ public class BoardCommentServiceImpl implements BoardCommentService {
} }
return returnMails; return returnMails;
} }
} }

View File

@ -8,6 +8,7 @@ import org.sadtech.social.core.service.MailService;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -25,9 +26,34 @@ public class MailServiceImpl implements MailService {
} }
@Override @Override
public List<Mail> getLastEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo) { public List<Mail> getByAddDateTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
log.info("Запрошены последние сообщения {} - {} ", timeFrom, timeTo); log.debug("Запрошены все сообщения {} - {} ", timeFrom, timeTo);
List<Mail> mails = mailRepository.betweenByTime(timeFrom, timeTo); return mailRepository.betweenByAddDateTime(timeFrom, timeTo);
}
@Override
public List<Mail> getLastEventByCreateDateTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
log.debug("Запрошены последние сообщения {} - {} ", timeFrom, timeTo);
List<Mail> mails = mailRepository.betweenByCreateDateTime(timeFrom, timeTo);
if (mails != null && !mails.isEmpty()) {
return getReturnMails(mails);
} else {
return Collections.emptyList();
}
}
@Override
public List<Mail> getLastEventByAddDateTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
log.debug("Запрошены последние сообщения {} - {} ", timeFrom, timeTo);
List<Mail> mails = mailRepository.betweenByAddDateTime(timeFrom, timeTo);
if (mails != null && !mails.isEmpty()) {
return getReturnMails(mails);
} else {
return Collections.emptyList();
}
}
private List<Mail> getReturnMails(List<Mail> mails) {
Set<Integer> people = new HashSet<>(); Set<Integer> people = new HashSet<>();
List<Mail> returnMails = new ArrayList<>(); List<Mail> returnMails = new ArrayList<>();
for (int i = mails.size() - 1; i >= 0; i--) { for (int i = mails.size() - 1; i >= 0; i--) {
@ -36,13 +62,11 @@ public class MailServiceImpl implements MailService {
people.add(mails.get(i).getPersonId()); people.add(mails.get(i).getPersonId());
} }
} }
if (!returnMails.isEmpty()) {
return returnMails; return returnMails;
} else {
return Collections.emptyList();
} }
@Override
public List<Mail> getByTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
log.info("Запрошены все сообщения {} - {} ", timeFrom, timeTo);
return mailRepository.betweenByTime(timeFrom, timeTo);
} }
} }