рефактиринг рассылки.v++
This commit is contained in:
@@ -3,6 +3,7 @@ import Common.Database.Objects.DBObject;
|
||||
import Common.Database.Objects.rDBObject;
|
||||
import Common.Utils.TextLog;
|
||||
import Common.Utils.Utils_;
|
||||
import _VisualDVM.Constants;
|
||||
import _VisualDVM.Global;
|
||||
import _VisualDVM.Repository.Component.ComponentType;
|
||||
import _VisualDVM.Repository.Component.ComponentsSet;
|
||||
@@ -89,8 +90,14 @@ public class BugReport extends rDBObject {
|
||||
for (String a : data)
|
||||
if (a.length() > 0)
|
||||
res.add(a);
|
||||
//всегда добавляем себя и админов--
|
||||
if (!res.contains(Global.mainModule.getAccount().email))
|
||||
res.add(Global.mainModule.getAccount().email);
|
||||
for (String address: Constants.admins_mails) {
|
||||
if (!res.contains(address))
|
||||
res.add(address);
|
||||
}
|
||||
//---------------------------------
|
||||
return res;
|
||||
}
|
||||
public File getArchiveFile() {
|
||||
|
||||
@@ -61,7 +61,7 @@ public class Visualiser extends Component {
|
||||
//http://www.seostella.com/ru/article/2012/02/05/formatirovanie-daty-v-java.html
|
||||
@Override
|
||||
public void GetVersionInfo() {
|
||||
version = 1122;
|
||||
version = 1123;
|
||||
String pattern = "MMM dd yyyy HH:mm:ss";
|
||||
DateFormat df = new SimpleDateFormat(pattern, Locale.ENGLISH);
|
||||
date_text = df.format(getClassBuildTime());
|
||||
|
||||
@@ -4,18 +4,20 @@ import Common.Utils.Utils_;
|
||||
import java.io.File;
|
||||
import java.io.Serializable;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Vector;
|
||||
public class EmailMessage implements Serializable {
|
||||
public String subject = ""; //тема письма
|
||||
public String text = ""; //текст письма
|
||||
public Vector<String> targets = new Vector<>(); //адресаты
|
||||
public LinkedHashMap<String, byte[]> files = new LinkedHashMap<>(); //вложения.
|
||||
public EmailMessage() {
|
||||
}
|
||||
public EmailMessage(String subject_in, String text_in, Vector<String> targets_in) {
|
||||
public EmailMessage(String subject_in, String text_in) {
|
||||
subject = subject_in;
|
||||
text = text_in;
|
||||
targets.addAll(targets_in);
|
||||
}
|
||||
public EmailMessage(EmailMessage message_in) {
|
||||
subject = message_in.subject;
|
||||
text = message_in.text;
|
||||
files.putAll(message_in.files);
|
||||
}
|
||||
public void addAttachement(File f) throws Exception {
|
||||
files.put(f.getName(), Utils_.fileToBytes(f));
|
||||
|
||||
@@ -83,10 +83,7 @@ public abstract class RepositoryServer<D extends Database> {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
protected void checkTargets(EmailMessage message_in) {
|
||||
}
|
||||
public void Email(EmailMessage message_in, File... directAttachements) throws Exception {
|
||||
checkTargets(message_in);
|
||||
public void Email(EmailMessage message_in, String address_in) throws Exception {
|
||||
Thread thread = new Thread(() -> {
|
||||
try {
|
||||
Properties props = new Properties();
|
||||
@@ -115,45 +112,34 @@ public abstract class RepositoryServer<D extends Database> {
|
||||
Constants.MailPassword);
|
||||
}
|
||||
});
|
||||
for (String target : message_in.targets) {
|
||||
boolean done = false;
|
||||
int attempts = 5;
|
||||
while (!done && (attempts > 0)) {
|
||||
try {
|
||||
MimeMessage message = new MimeMessage(session);
|
||||
message.setFrom(new InternetAddress(Constants.MailAddress));
|
||||
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(target));
|
||||
message.setSubject(message_in.subject);
|
||||
Multipart multipart = new MimeMultipart();
|
||||
MimeBodyPart textBodyPart = new MimeBodyPart();
|
||||
textBodyPart.setText(message_in.text);
|
||||
multipart.addBodyPart(textBodyPart);
|
||||
for (String aName : innerFiles.keySet()) {
|
||||
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
|
||||
DataSource source = new FileDataSource(innerFiles.get(aName));
|
||||
attachmentBodyPart.setDataHandler(new DataHandler(source));
|
||||
attachmentBodyPart.setFileName(aName);
|
||||
multipart.addBodyPart(attachmentBodyPart);
|
||||
}
|
||||
for (File f : directAttachements) {
|
||||
if (f.exists()) {
|
||||
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
|
||||
DataSource source = new FileDataSource(f);
|
||||
attachmentBodyPart.setDataHandler(new DataHandler(source));
|
||||
attachmentBodyPart.setFileName(f.getName());
|
||||
multipart.addBodyPart(attachmentBodyPart);
|
||||
}
|
||||
}
|
||||
message.setContent(multipart);
|
||||
Transport.send(message);
|
||||
done = true;
|
||||
} catch (Exception ex) {
|
||||
System.out.println("Исключение во время отправки сообщения абоненту " + Utils_.Brackets(target));
|
||||
ex.printStackTrace();
|
||||
Utils_.sleep(1000);
|
||||
} finally {
|
||||
attempts--;
|
||||
boolean done = false;
|
||||
int attempts = 5;
|
||||
while (!done && (attempts > 0)) {
|
||||
try {
|
||||
MimeMessage message = new MimeMessage(session);
|
||||
message.setFrom(new InternetAddress(Constants.MailAddress));
|
||||
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(address_in));
|
||||
message.setSubject(message_in.subject);
|
||||
Multipart multipart = new MimeMultipart();
|
||||
MimeBodyPart textBodyPart = new MimeBodyPart();
|
||||
textBodyPart.setText(message_in.text);
|
||||
multipart.addBodyPart(textBodyPart);
|
||||
for (String aName : innerFiles.keySet()) {
|
||||
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
|
||||
DataSource source = new FileDataSource(innerFiles.get(aName));
|
||||
attachmentBodyPart.setDataHandler(new DataHandler(source));
|
||||
attachmentBodyPart.setFileName(aName);
|
||||
multipart.addBodyPart(attachmentBodyPart);
|
||||
}
|
||||
message.setContent(multipart);
|
||||
Transport.send(message);
|
||||
done = true;
|
||||
} catch (Exception ex) {
|
||||
System.out.println("Исключение во время отправки сообщения абоненту " + Utils_.Brackets(address_in));
|
||||
ex.printStackTrace();
|
||||
Utils_.sleep(1000);
|
||||
} finally {
|
||||
attempts--;
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
@@ -224,7 +210,7 @@ public abstract class RepositoryServer<D extends Database> {
|
||||
break;
|
||||
case Email:
|
||||
Print("Отправка сообщения электронной почты");
|
||||
Email((EmailMessage) request.object);
|
||||
Email((EmailMessage) request.object, request.arg);
|
||||
response = new ServerExchangeUnit_2021(ServerCode.OK);
|
||||
break;
|
||||
//</editor-fold>
|
||||
|
||||
@@ -2,6 +2,7 @@ package _VisualDVM.Repository.Server;
|
||||
import Common.Database.Objects.DBObject;
|
||||
import Common.Database.RepositoryRefuseException;
|
||||
import Common.Utils.Utils_;
|
||||
import _VisualDVM.Constants;
|
||||
import _VisualDVM.Global;
|
||||
import _VisualDVM.GlobalData.Account.Account;
|
||||
import _VisualDVM.GlobalData.Machine.Machine;
|
||||
@@ -85,10 +86,12 @@ public class ComponentsServer extends RepositoryServer<BugReportsDatabase> {
|
||||
if (rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
|
||||
EmailMessage message = new EmailMessage(
|
||||
"db backup",
|
||||
"копия баз данных журнала ошибок",
|
||||
new Vector<>()
|
||||
"копия баз данных журнала ошибок"
|
||||
);
|
||||
Email(message, db.getFile());
|
||||
message.addAttachement(db.getFile());
|
||||
for (String address : Constants.admins_mails) {
|
||||
Email(message, address);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -107,21 +110,6 @@ public class ComponentsServer extends RepositoryServer<BugReportsDatabase> {
|
||||
return Global.properties.ComponentsServerPort;
|
||||
}
|
||||
@Override
|
||||
protected void checkTargets(EmailMessage message_in) {
|
||||
System.out.println("unckeched targets = "+String.join(" ", message_in.targets));
|
||||
Vector<String> checkedTargets = new Vector<>();
|
||||
for (String email : message_in.targets) {
|
||||
if (db.subscribers.containsKey(email)) {
|
||||
Subscriber subscriber = db.subscribers.get(email);
|
||||
if (subscriber.mailOn > 0) {
|
||||
checkedTargets.add(email);
|
||||
}
|
||||
} else checkedTargets.add(email); //если почта не зарегана значит это мейл с регистрацией.
|
||||
}
|
||||
message_in.targets = checkedTargets;
|
||||
System.out.println("checked targets = "+String.join(" ", message_in.targets));
|
||||
}
|
||||
@Override
|
||||
public void afterDeleteAction(DBObject object) throws Exception {
|
||||
if (object instanceof BugReport) {
|
||||
BugReport bugReport = (BugReport) object;
|
||||
@@ -134,10 +122,11 @@ public class ComponentsServer extends RepositoryServer<BugReportsDatabase> {
|
||||
if (Global.properties.EmailAdminsOnStart) {
|
||||
EmailMessage message = new EmailMessage(
|
||||
"Сервер Sapfor запущен",
|
||||
new Date().toString(),
|
||||
new Vector<>()
|
||||
new Date().toString()
|
||||
);
|
||||
Email(message);
|
||||
for (String address:Constants.admins_mails) {
|
||||
Email(message, address);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
|
||||
@@ -88,5 +88,6 @@ public enum ServerCode {
|
||||
GetSapforForCompilation,
|
||||
GetMaxSapforVersion,
|
||||
PerformAutoSapforTesting,
|
||||
Email_new
|
||||
;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user