-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
188 lines (159 loc) · 6.93 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import classes.Document.Book;
import classes.Document.Copy;
import classes.Document.DocAddress;
import classes.User.Librarian;
import classes.User.Patron;
import classes.User.Status;
import com.mongodb.DB;
import database.*;
import org.bson.types.ObjectId;
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.exceptions.TelegramApiException;
import org.telegram.telegrambots.logging.BotLogger;
import org.telegram.telegrambots.logging.BotsFileHandler;
import org.telegram.telegraph.ExecutorOptions;
import org.telegram.telegraph.TelegraphContext;
import org.telegram.telegraph.TelegraphContextInitializer;
import services.Constants;
import services.LocationDecoder;
import services.SendMail;
import services.TelegraphAccount;
import updatehandler.CustomTimerTask;
import updatehandler.MainBot;
import updatehandler.TimerExecutor;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
public class Main {
private static final String LOGTAG = "Class Main: ";
public static void main(String[] args) {
//Configure logging system
start();
//test1();
//test2();
//test3();
//test4();
//Run tests
try {
// Tester.TC1();
// Tester.TC2();
// Tester.TC3();
// Tester.TC4();
// Tester.TC5();
// Tester.TC6(true);
// Tester.TC7();
// Tester.TC8();
// Tester.TC9();
// Tester.TC10();
} catch (AssertionError error) {
//drop database
DB db = DatabaseManager.getDB("Library");
db.dropDatabase();
throw error;
}
}
public static void start() {
BotLogger.setLevel(Level.ALL);
BotLogger.registerLogger(new ConsoleHandler());
try {
BotLogger.registerLogger(new BotsFileHandler());
} catch (IOException e) {
String logInfo = "BotLogger IOException";
BotLogger.severe(LOGTAG + logInfo, e);
}
// Initialize Telegraph context
TelegraphContextInitializer.init();
TelegraphContext.registerInstance(ExecutorOptions.class, new ExecutorOptions());
// Initialize Telegram Api Context
ApiContextInitializer.init();
//Connection Establishment
System.out.println("Establishing connections!");
DatabaseManager.getInstance();
System.out.println("Database connected successfully!");
TelegraphAccount.getInstance();
System.out.println("Telegraph connected successfully!");
LocationDecoder.getInstance();
System.out.println("GeoDecoder API connected successfully!");
// Instantiate Telegram Bots API
TelegramBotsApi botsApi = new TelegramBotsApi();
// Register our bot
TelegramLongPollingBot bot = MainBot.getInstance();
try {
botsApi.registerBot(bot);
System.out.println("Bot registered successfully!");
} catch (TelegramApiException e) {
String logInfo = "BotsAPI TelegramApiException";
BotLogger.severe(LOGTAG + logInfo, e);
}
// //Notifications Thread creation
CustomTimerTask task = new CustomTimerTask("Notifications", Integer.MAX_VALUE) {
@Override
public void execute() {
MainBot.getInstance().sendNotifications();
}
};
TimerExecutor.getInstance().startExecutionEveryDayAt(task, 14,40,0);
System.out.println("Notifications Thread is created successfully!");
}
/**
* Run test: create and add books to database.
*/
public static void test1() {
ArrayList<String> authors = new ArrayList<>(Arrays.asList("William L. Briggs", "Lyle Cochran", "Bernard Gillett"));
ArrayList<String> keywords = new ArrayList<>(Arrays.asList("calculus", "William", "Briggs", "Bernard"));
Book book = new Book("Calculus", "1st Edition", authors, "https://www.pearsonhighered.com/assets/bigcovers/0/3/2/1/0321570561.jpg",
1699.90, keywords, true);
ArrayList<ObjectId> copyIds = new ArrayList<>();
DocAddress address = new DocAddress("room", "level", "docCase");
Copy copy = new Copy(book.getId(), address);
CopyDB.insertCopy(copy);
copyIds.add(copy.getId());
book.setCopyIds(copyIds);
BookDB.insertBook(book);
ArrayList<String> authors1 = new ArrayList<>(Arrays.asList("Maggard Thomson", "Jonh Jones", "Maksudov Rishat"));
ArrayList<String> keywords1 = new ArrayList<>(Arrays.asList("Math", "Discrete", "Maggard"));
Book book1 = new Book("Discrete Math", "1st edition", authors1, "https://ars.els-cdn.com/content/image/X0012365X.jpg",
3200.0, keywords1, false);
copyIds = new ArrayList<>();
address = new DocAddress("room", "level", "docCase");
copy = new Copy(book.getId(), address);
CopyDB.insertCopy(copy);
copyIds.add(copy.getId());
book1.setCopyIds(copyIds);
BookDB.insertBook(book1);
ArrayList<String> authors2 = new ArrayList<>(Collections.singletonList("Arthur Conan Doyle"));
ArrayList<String> keywords2 = new ArrayList<>(Arrays.asList("Holmes", "Conan", "Doyle", "Sherlock"));
Book book2 = new Book("Sherlock Holmes", "1st edition", authors2, "https://s00.yaplakal.com/pics/pics_original/2/0/8/10510802.jpg",
13200.0, keywords2, true);
copyIds = new ArrayList<>();
address = new DocAddress("room", "level", "docCase");
copy = new Copy(book.getId(), address);
CopyDB.insertCopy(copy);
copyIds.add(copy.getId());
book2.setCopyIds(copyIds);
BookDB.insertBook(book2);
}
/**
* Run test: create and add patron to database.
*/
public static void test2() {
//get id by asking person to send message to bot
Librarian patron = new Librarian((long) 149477679, "Rishat", "Maksudov", "r.maksudov@innopolis.ru", "+77777777777", "Innopolis University");
LibrarianDB.insertLibrarian(patron);
}
public static void test3() {
SendMail.sendMail("t.holmatov@innopolis.ru","Testing SendMail class", "Hello World!\nTemur");
}
public static void test4() {
Patron patron = new Patron((long) 3656229, "Temur", "Kholmatov", Status.Student, "t.holmatov@innopolis.ru", "+9999999999", "Innopolis University");
//Patron patron = new Patron((long) 149477679, "Rishat", "Kholmatov", Status.Student, "t.holmatov@innopolis.ru", "+9999999999", "Innopolis University");
PatronDB.insertPatron(patron);
ArrayList<Book> books = BookDB.getBooksList();
patron.checkOutDocument(books.get(0), Constants.BOOK_COLLECTION);
}
}