Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix errors when Orienteer was reload #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main/java/org/orienteer/telegram/bot/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import org.orienteer.telegram.module.OTelegramModule;
import org.orienteer.telegram.module.OTelegramCustomAttributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.ydn.wicket.wicketorientdb.utils.DBClosure;
Expand Down Expand Up @@ -39,7 +39,7 @@ protected Object execute(ODatabaseDocument db) {
private static void createClassCache(ODatabaseDocument db) {
classCache = new HashMap<>();
for (OClass oClass : db.getMetadata().getSchema().getClasses()) {
if (OTelegramModule.TELEGRAM_SEARCH.getValue(oClass)) {
if (OTelegramCustomAttributes.TELEGRAM_SEARCH.getOrCreate().getValue(oClass)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we discussed that I thought that you will use CustomAttributes and add "getOrCreate()" method there instead of creating separate class OTelegramCustomAttributes. Any problems with dynamic loading in that case? If no - lets get rid of OTelegramCustomAttributes.

classCache.put(oClass.getName(), oClass);
}
}
Expand All @@ -49,10 +49,10 @@ private static void createClassCache(ODatabaseDocument db) {
private static void createQueryCache() {
queryCache = new HashMap<>();
for (OClass oClass : classCache.values()) {
String query = OTelegramModule.TELEGRAM_SEARCH_QUERY.getValue(oClass);
String query = OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.getOrCreate().getValue(oClass);
if (query == null){
query = "SELECT FROM " + oClass.getName();
OTelegramModule.TELEGRAM_SEARCH_QUERY.setValue(oClass, query);
OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.getOrCreate().setValue(oClass, query);
}
queryCache.put(oClass.getName(), query);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/orienteer/telegram/bot/link/ClassLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.orienteer.telegram.bot.MessageKey;
import org.orienteer.telegram.bot.OTelegramBot;
import org.orienteer.telegram.bot.response.BotState;
import org.orienteer.telegram.module.OTelegramModule;
import org.orienteer.telegram.module.OTelegramCustomAttributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.ydn.wicket.wicketorientdb.utils.DBClosure;
Expand Down Expand Up @@ -64,7 +64,7 @@ protected Map<Integer, String> execute(ODatabaseDocument oDatabaseDocument) {
} else builder.append(MessageKey.WITHOUT_SUPER_CLASSES.getString(locale));
builder.append("\n");
List<String> resultList = new ArrayList<>();
if (OTelegramModule.TELEGRAM_CLASS_DESCRIPTION.getValue(oClass)) {
if (OTelegramCustomAttributes.TELEGRAM_CLASS_DESCRIPTION.getOrCreate().getValue(oClass)) {
Collection<OProperty> properties = oClass.properties();
for (OProperty property : properties) {
resultList.add(String.format(MessageKey.HTML_STRONG_TEXT.toString(), property.getName())
Expand All @@ -78,7 +78,7 @@ protected Map<Integer, String> execute(ODatabaseDocument oDatabaseDocument) {
}
ORecordIteratorClass<ODocument> oDocuments = oDatabaseDocument.browseClass(oClass.getName());
resultList = new ArrayList<>();
if (OTelegramModule.TELEGRAM_DOCUMENTS_LIST.getValue(oClass)) {
if (OTelegramCustomAttributes.TELEGRAM_DOCUMENTS_LIST.getOrCreate().getValue(oClass)) {
builder.append("\n");
builder.append(String.format(MessageKey.HTML_STRONG_TEXT.toString(), MessageKey.CLASS_DOCUMENTS.getString(locale)));
builder.append("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.orienteer.core.component.structuretable.OrienteerStructureTable;
import org.orienteer.core.widget.AbstractModeAwareWidget;
import org.orienteer.core.widget.Widget;
import org.orienteer.telegram.module.OTelegramModule;
import org.orienteer.telegram.module.OTelegramCustomAttributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.ydn.wicket.wicketorientdb.components.TransactionlessForm;
Expand All @@ -49,10 +49,10 @@ public class OTelegramBotWidget extends AbstractModeAwareWidget<OClass> {
public OTelegramBotWidget(String id, IModel<OClass> model, IModel<ODocument> widgetDocumentModel) {
super(id, model, widgetDocumentModel);
Form<OClass> form = new TransactionlessForm<OClass>("form");
propertiesList.add(OTelegramModule.TELEGRAM_SEARCH.getName());
propertiesList.add(OTelegramModule.TELEGRAM_DOCUMENTS_LIST.getName());
propertiesList.add(OTelegramModule.TELEGRAM_CLASS_DESCRIPTION.getName());
propertiesList.add(OTelegramModule.TELEGRAM_SEARCH_QUERY.getName());
propertiesList.add(OTelegramCustomAttributes.TELEGRAM_SEARCH.getOrCreate().getName());
propertiesList.add(OTelegramCustomAttributes.TELEGRAM_DOCUMENTS_LIST.getOrCreate().getName());
propertiesList.add(OTelegramCustomAttributes.TELEGRAM_CLASS_DESCRIPTION.getOrCreate().getName());
propertiesList.add(OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.getOrCreate().getName());
structureTable = new OrienteerStructureTable<OClass, String>("attributes", model, propertiesList) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically you can use CustomAttribute instead of String here.


@Override
Expand All @@ -71,13 +71,13 @@ protected Component resolveComponent(String id, DisplayMode mode, String critery
mode = DisplayMode.VIEW;
}
if (DisplayMode.VIEW.equals(mode)) {
if (critery.equals(OTelegramModule.TELEGRAM_SEARCH_QUERY.getName())) {
if (critery.equals(OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.getOrCreate().getName())) {
return new Label(id, getModel());
} else {
return new BooleanViewPanel(id, Model.<Boolean>of(getModel()));
}
} else if (DisplayMode.EDIT.equals(mode)){
if (critery.equals(OTelegramModule.TELEGRAM_SEARCH_QUERY.getName())) {
if (critery.equals(OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.getOrCreate().getName())) {
return new TextField(id, getModel());
} else return new BooleanEditPanel(id, Model.<Boolean>of(getModel()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.orienteer.telegram.module;

import com.orientechnologies.orient.core.metadata.schema.OType;
import org.orienteer.core.CustomAttribute;

/**
* @author Vitaliy Gonchar
*/
public enum OTelegramCustomAttributes {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented below. It seems to be quite dummy file.

TELEGRAM_SEARCH("orienteer.telegramSearch", OType.BOOLEAN, false, false, false),
TELEGRAM_DOCUMENTS_LIST("orienteer.telegramDocumentsList", OType.BOOLEAN, false, false, false),
TELEGRAM_SEARCH_QUERY("orienteer.telegramSearchQuery", OType.STRING, null, true, false),
TELEGRAM_CLASS_DESCRIPTION("orienteer.telegramClassDescription", OType.BOOLEAN, false, false, false);

private final String name;
private final OType type;
private final Object defaultValue;
private final boolean encode;
private final boolean hiearchical;

OTelegramCustomAttributes(String name, OType type, Object defaultValue, boolean encode, boolean hiearchical) {
this.name = name;
this.type = type;
this.defaultValue = defaultValue;
this.encode = encode;
this.hiearchical = hiearchical;
}

public CustomAttribute getOrCreate() {
CustomAttribute result = CustomAttribute.getIfExists(name);
return result != null ? result : CustomAttribute.create(name, type, defaultValue, encode, hiearchical);
}
}
37 changes: 18 additions & 19 deletions src/main/java/org/orienteer/telegram/module/OTelegramModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.orienteer.core.util.OSchemaHelper;
import org.orienteer.telegram.bot.OTelegramBot;
import org.orienteer.telegram.bot.handler.LongPolligHandlerConfig;
import org.orienteer.telegram.bot.handler.OTelegramLongPollingHandler;
import org.orienteer.telegram.bot.handler.WebHookHandlerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -23,23 +24,18 @@
public class OTelegramModule extends AbstractOrienteerModule {

private static final Logger LOG = LoggerFactory.getLogger(OTelegramModule.class);
public static final String NAME = "telegram";
public static final String OCLASS_NAME = "OTelegramBotTest";
public static final String OPROPERTY_USERNAME = "username";
public static final String OPROPERTY_TOKEN = "token";
public static final String OPROPERTY_USER_SESSION = "user_session";
public static final String OPROPERTY_WEB_HOOK_ENABLE = "web_hook_enable";
public static final String OPROPERTY_WEB_HOOK_HOST = "web_hook_host";
public static final String OPROPERTY_WEB_HOOK_PORT = "web_hook_port";
public static final String OPROPERTY_WEB_HOOK_PATH_TO_CERTIFICATE_KEY = "path_to_certificate_public_key";
public static final String OPROPERTY_WEB_HOOK_PATH_TO_CERTIFICATE_STORE = "path_to_certificate_store";
public static final String OPROPERTY_WEB_HOOK_CERTIFICATE_PASSWORD = "certificate_password";


public static final CustomAttribute TELEGRAM_SEARCH = CustomAttribute.create("orienteer.telegramSearch", OType.BOOLEAN, false, false, false);
public static final CustomAttribute TELEGRAM_DOCUMENTS_LIST = CustomAttribute.create("orienteer.telegramDocumentsList", OType.BOOLEAN, false, false, false);
public static final CustomAttribute TELEGRAM_SEARCH_QUERY = CustomAttribute.create("orienteer.telegramSearchQuery", OType.STRING, null, true, false);
public static final CustomAttribute TELEGRAM_CLASS_DESCRIPTION = CustomAttribute.create("orienteer.telegramClassDescription", OType.BOOLEAN, false, false, false);

private static final String NAME = "telegram";
private static final String OCLASS_NAME = "OTelegramBotTest";
private static final String OPROPERTY_USERNAME = "username";
private static final String OPROPERTY_TOKEN = "token";
private static final String OPROPERTY_USER_SESSION = "user_session";
private static final String OPROPERTY_WEB_HOOK_ENABLE = "web_hook_enable";
private static final String OPROPERTY_WEB_HOOK_HOST = "web_hook_host";
private static final String OPROPERTY_WEB_HOOK_PORT = "web_hook_port";
private static final String OPROPERTY_WEB_HOOK_PATH_TO_CERTIFICATE_KEY = "path_to_certificate_public_key";
private static final String OPROPERTY_WEB_HOOK_PATH_TO_CERTIFICATE_STORE = "path_to_certificate_store";
private static final String OPROPERTY_WEB_HOOK_CERTIFICATE_PASSWORD = "certificate_password";

private BotSession botSession;

Expand Down Expand Up @@ -85,7 +81,8 @@ public void onInitialize(OrienteerWebApplication app, ODatabaseDocument db, ODoc
} else {
telegramBotsApi = new TelegramBotsApi();
LongPolligHandlerConfig longPolligHandlerConfig = readLongPollingBotConfig(moduleDoc);
botSession = telegramBotsApi.registerBot(OTelegramBot.getLongPollingBot(longPolligHandlerConfig));
OTelegramLongPollingHandler longPollingBot = OTelegramBot.getLongPollingBot(longPolligHandlerConfig);
botSession = telegramBotsApi.registerBot(longPollingBot);
}
}

Expand Down Expand Up @@ -130,7 +127,9 @@ private WebHookHandlerConfig readWebHookBotConfig(ODocument doc) {

@Override
public void onDestroy(OrienteerWebApplication app, ODatabaseDocument db, ODocument moduleDoc) {
if (botSession != null) botSession.close();
if (botSession != null) {
botSession.close();
}
}

}