Новая архитектура

This commit is contained in:
Mark Struchkov 2019-01-11 03:21:58 +03:00
parent b79c3c7cfe
commit 1cf0b4dce6
32 changed files with 168 additions and 481 deletions

23
pom.xml
View File

@ -21,19 +21,12 @@
</build>
<properties>
<persistence.ver>1.0.2</persistence.ver>
<lombok.ver>1.18.4</lombok.ver>
<log4j>1.2.17</log4j>
<spring.data>2.1.3.RELEASE</spring.data>
<junit.ver>4.12</junit.ver>
</properties>
<dependencies>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>${persistence.ver}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
@ -47,16 +40,14 @@
<version>${log4j}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring.data}</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.ver}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,17 @@
package org.sadtech.autoresponder;
import lombok.AllArgsConstructor;
import org.sadtech.autoresponder.entity.Unit;
import org.sadtech.autoresponder.service.UnitService;
@AllArgsConstructor
public class Autoresponder {
private UnitService unitService;
public Unit nextUnit(Integer idUnit, String message) {
Unit unit = unitService.getUnitById(idUnit);
return unitService.nextUnit(unit, message);
}
}

View File

@ -1,7 +0,0 @@
package org.sadtech.autoresponder;
public interface MessageSender {
void send(Integer idNetSoc, String text);
}

View File

@ -1,23 +0,0 @@
package org.sadtech.autoresponder.database.entity;
import lombok.Data;
import javax.persistence.*;
@Data
@Entity
public class Mail {
@Id
@GeneratedValue
private Long idMess;
private String text;
private Long date;
private String sourceMessage;
@ManyToOne
@JoinColumn(name = "idPerson", nullable = false)
private Person person;
}

View File

@ -1,16 +0,0 @@
package org.sadtech.autoresponder.database.entity;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
@Data
public class NextUnit {
@Id
private Long id;
private Long idNext;
}

View File

@ -1,36 +0,0 @@
package org.sadtech.autoresponder.database.entity;
import lombok.Data;
import javax.persistence.*;
import java.util.HashMap;
import java.util.Map;
@Entity
@Data
public class Person {
@Id
@GeneratedValue
private Long id;
@Column(length = 20)
private String name;
@Column(length = 20)
private String lastName;
@Column(length = 20)
private String city;
private String token;
private String email;
@ElementCollection
@CollectionTable(name = "PersonSocialNetworks")
@MapKeyColumn(name = "KeysSocialNetworks", length = 20)
private Map<String, Integer> socialNetworks = new HashMap<>();
}

View File

@ -1,19 +0,0 @@
package org.sadtech.autoresponder.database.entity;
import lombok.Data;
import javax.persistence.*;
@Entity
@Data
public class SaveUnit {
@Id
private Long id;
private Long idStage;
@OneToOne
@JoinColumn(name = "idPerson", nullable = false)
private Person user;
}

View File

@ -1,17 +0,0 @@
package org.sadtech.autoresponder.database.entity;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
@Data
public class Unit {
@Id
private Long id;
private String text;
private String wordKeys;
}

View File

@ -1,15 +0,0 @@
package org.sadtech.autoresponder.database.repository;
import org.sadtech.autoresponder.database.entity.Mail;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.Collection;
public interface MailRepository extends JpaRepository<Mail, Long> {
@Query("SELECT u FROM Mail u WHERE u.date > :date")
Collection<Mail> getMessagesByRange(@Param("date") Long date);
}

View File

@ -1,9 +0,0 @@
package org.sadtech.autoresponder.database.repository;
import org.sadtech.autoresponder.database.entity.NextUnit;
import org.springframework.data.jpa.repository.JpaRepository;
public interface NextUnitRepository extends JpaRepository<NextUnit, Long> {
}

View File

@ -1,13 +0,0 @@
package org.sadtech.autoresponder.database.repository;
import org.sadtech.autoresponder.database.entity.Person;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface PersonRepository extends JpaRepository<Person, Long> {
@Query("SELECT u FROM Person u INNER JOIN u.socialNetworks p WHERE KEY(p) = :type2 AND VALUE(p) = :socialId")
Person getUserBySocialNetworksId(@Param("type2") String type, @Param("socialId") Integer socialNetworksId);
}

View File

@ -1,7 +0,0 @@
package org.sadtech.autoresponder.database.repository;
import org.sadtech.autoresponder.database.entity.SaveUnit;
import org.springframework.data.jpa.repository.JpaRepository;
public interface SaveUnitRepositoriy extends JpaRepository<SaveUnit, Long> {
}

View File

@ -1,7 +0,0 @@
package org.sadtech.autoresponder.database.repository;
import org.sadtech.autoresponder.database.entity.Unit;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UnitRepositoriy extends JpaRepository<Unit, Long> {
}

View File

@ -1,12 +0,0 @@
package org.sadtech.autoresponder.database.service;
import org.sadtech.autoresponder.database.entity.Mail;
import java.util.List;
public interface MailService {
void addMessage(Mail message);
List<Mail> getMessageRange(Long date);
}

View File

@ -1,4 +0,0 @@
package org.sadtech.autoresponder.database.service;
public interface NextUnitService {
}

View File

@ -1,18 +0,0 @@
package org.sadtech.autoresponder.database.service;
import org.sadtech.autoresponder.database.entity.Person;
import java.util.Map;
public interface PersonService {
void addUser(Person user);
void removeUser(Long id);
Person getUserByID(Long id);
Map<String, Integer> getSocialNetwork(Long id);
Person getUserBySocialNetworksId(String type, Integer socialNetworksId);
}

View File

@ -1,4 +0,0 @@
package org.sadtech.autoresponder.database.service;
public interface SaveUnitService {
}

View File

@ -1,11 +0,0 @@
package org.sadtech.autoresponder.database.service;
import org.sadtech.autoresponder.database.entity.Unit;
public interface UnitService {
void addUnit(Unit unit);
void removeUnit(Long id);
}

View File

@ -1,25 +0,0 @@
package org.sadtech.autoresponder.database.service.impl;
import org.sadtech.autoresponder.database.entity.Mail;
import org.sadtech.autoresponder.database.repository.MailRepository;
import org.sadtech.autoresponder.database.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MailServiceImpl implements MailService {
@Autowired
private MailRepository repository;
public void addMessage(Mail message) {
repository.saveAndFlush(message);
}
@Override
public List<Mail> getMessageRange(Long date) {
return (List<Mail>) repository.getMessagesByRange(date);
}
}

View File

@ -1,14 +0,0 @@
package org.sadtech.autoresponder.database.service.impl;
import org.sadtech.autoresponder.database.repository.NextUnitRepository;
import org.sadtech.autoresponder.database.service.NextUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class NextUnitServiceImpl implements NextUnitService {
@Autowired
private NextUnitRepository repository;
}

View File

@ -1,41 +0,0 @@
package org.sadtech.autoresponder.database.service.impl;
import org.sadtech.autoresponder.database.repository.PersonRepository;
import org.sadtech.autoresponder.database.entity.Person;
import org.sadtech.autoresponder.database.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonRepository repositoriy;
@Override
public void addUser(Person user) {
repositoriy.saveAndFlush(user);
}
@Override
public void removeUser(Long id) {
repositoriy.deleteById(id);
}
@Override
public Person getUserByID(Long id) {
return repositoriy.getOne(id);
}
@Override
public Map<String, Integer> getSocialNetwork(Long id) {
return repositoriy.getOne(id).getSocialNetworks();
}
@Override
public Person getUserBySocialNetworksId(String type, Integer socialNetworksId) {
return repositoriy.getUserBySocialNetworksId(type, socialNetworksId);
}
}

View File

@ -1,14 +0,0 @@
package org.sadtech.autoresponder.database.service.impl;
import org.sadtech.autoresponder.database.repository.SaveUnitRepositoriy;
import org.sadtech.autoresponder.database.service.SaveUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SaveUnitServiceImpl implements SaveUnitService {
@Autowired
private SaveUnitRepositoriy repositoriy;
}

View File

@ -1,22 +0,0 @@
package org.sadtech.autoresponder.database.service.impl;
import org.sadtech.autoresponder.database.repository.UnitRepositoriy;
import org.sadtech.autoresponder.database.entity.Unit;
import org.sadtech.autoresponder.database.service.UnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UnitServiceImpl implements UnitService {
@Autowired
private UnitRepositoriy repositoriy;
public void addUnit(Unit unit) {
repositoriy.saveAndFlush(unit);
}
public void removeUnit(Long id) {
}
}

View File

@ -0,0 +1,17 @@
package org.sadtech.autoresponder.entity;
import lombok.Data;
import java.util.List;
import java.util.Set;
@Data
public class Unit {
private Integer idUnit;
private List<Unit> nextUnits;
private String answer;
private Integer priority;
private Set<String> keyWords;
}

View File

@ -1,54 +0,0 @@
package org.sadtech.autoresponder.processing;
import lombok.extern.log4j.Log4j;
import org.sadtech.autoresponder.database.entity.Mail;
import org.sadtech.autoresponder.MessageSender;
import org.sadtech.autoresponder.database.entity.Person;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Log4j
@Component
public class MessageHandler {
private MessageLogicService messageLogicService;
private PersonLogicService userLogicService;
private Map<String, MessageSender> senderHashMap = new HashMap<>();
private long data;
public MessageHandler(MessageLogicService messageLogicService, PersonLogicService userLogicService) {
this.messageLogicService = messageLogicService;
this.userLogicService = userLogicService;
data = new Date().getTime() / 1000;
}
public void addMessageSendler(String type, MessageSender messageSender) {
senderHashMap.put(type, messageSender);
}
@Async
@Transactional
public void processing() {
log.info("process");
while (true) {
List<Mail> messages = messageLogicService.getMessageRange(data);
if (messages.size()>0) {
for (Mail message : messages) {
log.info(message);
Person person = message.getPerson();
Integer idNetSoc = person.getSocialNetworks().get(message.getSourceMessage());
senderHashMap.get(message.getSourceMessage()).send(idNetSoc, "Тестовое сообщение");
data = message.getDate();
}
}
}
}
}

View File

@ -1,25 +0,0 @@
package org.sadtech.autoresponder.processing;
import org.sadtech.autoresponder.database.entity.Mail;
import org.sadtech.autoresponder.database.service.MailService;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class MessageLogicService {
private MailService messageService;
public MessageLogicService(MailService messageService) {
this.messageService = messageService;
}
public void addMessage(Mail message) {
messageService.addMessage(message);
}
public List<Mail> getMessageRange(long data) {
return messageService.getMessageRange(data);
}
}

View File

@ -1,52 +0,0 @@
package org.sadtech.autoresponder.processing;
import lombok.extern.log4j.Log4j;
import org.sadtech.autoresponder.database.entity.Person;
import org.sadtech.autoresponder.database.service.PersonService;
import org.springframework.stereotype.Component;
@Log4j
@Component
public class PersonLogicService {
private PersonService personService;
public PersonLogicService(PersonService userService) {
this.personService = userService;
}
public void addUser(Person user) {
personService.addUser(user);
log.info("Пользователь добавлен в бд");
}
public boolean checkPersonBySocialNetworksId(String socType, Integer socId) {
Person checkPerson = personService.getUserBySocialNetworksId(socType, socId);
if (checkPerson != null) {
log.info("Пользователь уже есть в бд");
return true;
}
log.info("Пользователя нет в бд");
return false;
}
public Person getPersonBySocialNetworksId(Person person) {
for (String key : person.getSocialNetworks().keySet()) {
Person getPerson = personService.getUserBySocialNetworksId(key, person.getSocialNetworks().get(key));
if (getPerson != null) {
log.info("Пользователь уже есть в бд");
return getPerson;
}
}
return null;
}
public Person getUserById(Long id) {
return personService.getUserByID(id);
}
public Person getUserBySocialId(String type, Integer id) {
return personService.getUserBySocialNetworksId(type, id);
}
}

View File

@ -0,0 +1,16 @@
package org.sadtech.autoresponder.repository;
import org.sadtech.autoresponder.entity.Unit;
import java.util.Collection;
public interface UnitRepository {
Unit getUnitById(Integer idUnit);
void addUnit(Unit unit);
void addUnits(Collection<Unit> units);
void removeUnit(Integer idUnit);
}

View File

@ -0,0 +1,33 @@
package org.sadtech.autoresponder.repository.impl;
import org.sadtech.autoresponder.entity.Unit;
import org.sadtech.autoresponder.repository.UnitRepository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class UnitRepositoryImpl implements UnitRepository {
private Map<Integer, Unit> units = new HashMap<>();
@Override
public Unit getUnitById(Integer idUnit) {
return units.get(idUnit);
}
@Override
public void addUnit(Unit unit) {
units.put(unit.getIdUnit(), unit);
}
@Override
public void addUnits(Collection<Unit> units) {
units.addAll(units);
}
@Override
public void removeUnit(Integer idUnit) {
units.remove(idUnit);
}
}

View File

@ -0,0 +1,10 @@
package org.sadtech.autoresponder.service;
import lombok.NonNull;
import org.sadtech.autoresponder.entity.Unit;
public interface UnitService {
Unit nextUnit(Unit unit, String message);
Unit getUnitById(Integer idUnit);
}

View File

@ -0,0 +1,42 @@
package org.sadtech.autoresponder.service.impl;
import com.sun.istack.internal.NotNull;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
import org.sadtech.autoresponder.entity.Unit;
import org.sadtech.autoresponder.repository.UnitRepository;
import org.sadtech.autoresponder.submodule.parser.Parser;
import org.sadtech.autoresponder.service.UnitService;
import java.util.ArrayList;
@AllArgsConstructor
public class UnitServiceImpl implements UnitService {
private UnitRepository unitRepository;
public Unit nextUnit(@NotNull Unit unit, @NotNull String message) {
ArrayList<Unit> nextUnits = (ArrayList<Unit>) unit.getNextUnits();
Unit unitReturn = new Unit();
if (nextUnits.size() > 0) {
Parser parser = new Parser();
parser.setText(message);
parser.parse();
for (Unit nextUnit : nextUnits) {
if (nextUnit.getKeyWords().retainAll(parser.getWords()) && (nextUnit.getPriority()>unitReturn.getPriority())) {
unitReturn = nextUnit;
}
}
return unitReturn;
} else {
return null;
}
}
@Override
public Unit getUnitById(@NotNull Integer idUnit) {
return unitRepository.getUnitById(idUnit);
}
}

View File

@ -0,0 +1,26 @@
package org.sadtech.autoresponder.submodule.parser;
import lombok.Getter;
import lombok.Setter;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Parser {
@Getter
private Set<String> words;
@Setter
private String text;
public void parse() {
Pattern p = Pattern.compile("[а-яА-Я0-9]+");
Matcher m = p.matcher(text);
while (m.find()) {
words.add(m.group());
}
}
}