From 689644801d7d761bd1aab6f005dc9743fc5a6697 Mon Sep 17 00:00:00 2001 From: Vitaliy Gonchar Date: Sun, 12 Mar 2017 09:11:48 +0200 Subject: [PATCH 1/4] Change public access to private access --- .../telegram/module/OTelegramModule.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/orienteer/telegram/module/OTelegramModule.java b/src/main/java/org/orienteer/telegram/module/OTelegramModule.java index eb368c0..01a696f 100644 --- a/src/main/java/org/orienteer/telegram/module/OTelegramModule.java +++ b/src/main/java/org/orienteer/telegram/module/OTelegramModule.java @@ -23,17 +23,17 @@ 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"; + 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"; public static final CustomAttribute TELEGRAM_SEARCH = CustomAttribute.create("orienteer.telegramSearch", OType.BOOLEAN, false, false, false); From 7154927098deb5b4abbfe1b48e67a96bf4969886 Mon Sep 17 00:00:00 2001 From: Vitaliy Gonchar Date: Mon, 13 Mar 2017 20:31:41 +0200 Subject: [PATCH 2/4] Create OTelegramCustomAttributes - class with custom attributes for Orienteer Telegram module --- .../org/orienteer/telegram/bot/Cache.java | 7 ++-- .../telegram/bot/link/ClassLink.java | 5 +-- .../component/widget/OTelegramBotWidget.java | 13 +++---- .../module/OTelegramCustomAttributes.java | 33 +++++++++++++++++ .../telegram/module/OTelegramModule.java | 35 +++++++++---------- 5 files changed, 64 insertions(+), 29 deletions(-) create mode 100644 src/main/java/org/orienteer/telegram/module/OTelegramCustomAttributes.java diff --git a/src/main/java/org/orienteer/telegram/bot/Cache.java b/src/main/java/org/orienteer/telegram/bot/Cache.java index 7c38056..1a845c5 100644 --- a/src/main/java/org/orienteer/telegram/bot/Cache.java +++ b/src/main/java/org/orienteer/telegram/bot/Cache.java @@ -2,6 +2,7 @@ import com.orientechnologies.orient.core.db.document.ODatabaseDocument; import com.orientechnologies.orient.core.metadata.schema.OClass; +import org.orienteer.telegram.module.OTelegramCustomAttributes; import org.orienteer.telegram.module.OTelegramModule; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,7 +40,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.get().getValue(oClass)) { classCache.put(oClass.getName(), oClass); } } @@ -49,10 +50,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.get().getValue(oClass); if (query == null){ query = "SELECT FROM " + oClass.getName(); - OTelegramModule.TELEGRAM_SEARCH_QUERY.setValue(oClass, query); + OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.get().setValue(oClass, query); } queryCache.put(oClass.getName(), query); } diff --git a/src/main/java/org/orienteer/telegram/bot/link/ClassLink.java b/src/main/java/org/orienteer/telegram/bot/link/ClassLink.java index 145bdfd..1ab73d6 100644 --- a/src/main/java/org/orienteer/telegram/bot/link/ClassLink.java +++ b/src/main/java/org/orienteer/telegram/bot/link/ClassLink.java @@ -9,6 +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.OTelegramCustomAttributes; import org.orienteer.telegram.module.OTelegramModule; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -64,7 +65,7 @@ protected Map execute(ODatabaseDocument oDatabaseDocument) { } else builder.append(MessageKey.WITHOUT_SUPER_CLASSES.getString(locale)); builder.append("\n"); List resultList = new ArrayList<>(); - if (OTelegramModule.TELEGRAM_CLASS_DESCRIPTION.getValue(oClass)) { + if (OTelegramCustomAttributes.TELEGRAM_CLASS_DESCRIPTION.get().getValue(oClass)) { Collection properties = oClass.properties(); for (OProperty property : properties) { resultList.add(String.format(MessageKey.HTML_STRONG_TEXT.toString(), property.getName()) @@ -78,7 +79,7 @@ protected Map execute(ODatabaseDocument oDatabaseDocument) { } ORecordIteratorClass oDocuments = oDatabaseDocument.browseClass(oClass.getName()); resultList = new ArrayList<>(); - if (OTelegramModule.TELEGRAM_DOCUMENTS_LIST.getValue(oClass)) { + if (OTelegramCustomAttributes.TELEGRAM_DOCUMENTS_LIST.get().getValue(oClass)) { builder.append("\n"); builder.append(String.format(MessageKey.HTML_STRONG_TEXT.toString(), MessageKey.CLASS_DOCUMENTS.getString(locale))); builder.append("\n"); diff --git a/src/main/java/org/orienteer/telegram/component/widget/OTelegramBotWidget.java b/src/main/java/org/orienteer/telegram/component/widget/OTelegramBotWidget.java index 53ef8cb..f027417 100644 --- a/src/main/java/org/orienteer/telegram/component/widget/OTelegramBotWidget.java +++ b/src/main/java/org/orienteer/telegram/component/widget/OTelegramBotWidget.java @@ -22,6 +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.OTelegramCustomAttributes; import org.orienteer.telegram.module.OTelegramModule; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,10 +50,10 @@ public class OTelegramBotWidget extends AbstractModeAwareWidget { public OTelegramBotWidget(String id, IModel model, IModel widgetDocumentModel) { super(id, model, widgetDocumentModel); Form form = new TransactionlessForm("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.get().getName()); + propertiesList.add(OTelegramCustomAttributes.TELEGRAM_DOCUMENTS_LIST.get().getName()); + propertiesList.add(OTelegramCustomAttributes.TELEGRAM_CLASS_DESCRIPTION.get().getName()); + propertiesList.add(OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.get().getName()); structureTable = new OrienteerStructureTable("attributes", model, propertiesList) { @Override @@ -71,13 +72,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.get().getName())) { return new Label(id, getModel()); } else { return new BooleanViewPanel(id, Model.of(getModel())); } } else if (DisplayMode.EDIT.equals(mode)){ - if (critery.equals(OTelegramModule.TELEGRAM_SEARCH_QUERY.getName())) { + if (critery.equals(OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.get().getName())) { return new TextField(id, getModel()); } else return new BooleanEditPanel(id, Model.of(getModel())); } diff --git a/src/main/java/org/orienteer/telegram/module/OTelegramCustomAttributes.java b/src/main/java/org/orienteer/telegram/module/OTelegramCustomAttributes.java new file mode 100644 index 0000000..dc70c6a --- /dev/null +++ b/src/main/java/org/orienteer/telegram/module/OTelegramCustomAttributes.java @@ -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 { + 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 get() { + CustomAttribute result = CustomAttribute.getIfExists(name); + return result != null ? result : CustomAttribute.create(name, type, defaultValue, encode, hiearchical); + } +} diff --git a/src/main/java/org/orienteer/telegram/module/OTelegramModule.java b/src/main/java/org/orienteer/telegram/module/OTelegramModule.java index 01a696f..592bcf2 100644 --- a/src/main/java/org/orienteer/telegram/module/OTelegramModule.java +++ b/src/main/java/org/orienteer/telegram/module/OTelegramModule.java @@ -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; @@ -23,23 +24,18 @@ public class OTelegramModule extends AbstractOrienteerModule { private static final Logger LOG = LoggerFactory.getLogger(OTelegramModule.class); - 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"; - - 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; @@ -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); } } @@ -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(); + } } } From 3229c8be62d3e99156b48e99d7312737c2a5d073 Mon Sep 17 00:00:00 2001 From: Vitaliy Gonchar Date: Mon, 13 Mar 2017 21:13:46 +0200 Subject: [PATCH 3/4] Rename method "get" to "getOrCreate" --- src/main/java/org/orienteer/telegram/bot/Cache.java | 7 +++---- .../org/orienteer/telegram/bot/link/ClassLink.java | 5 ++--- .../component/widget/OTelegramBotWidget.java | 13 ++++++------- .../telegram/module/OTelegramCustomAttributes.java | 2 +- 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/orienteer/telegram/bot/Cache.java b/src/main/java/org/orienteer/telegram/bot/Cache.java index 1a845c5..0f89b29 100644 --- a/src/main/java/org/orienteer/telegram/bot/Cache.java +++ b/src/main/java/org/orienteer/telegram/bot/Cache.java @@ -3,7 +3,6 @@ import com.orientechnologies.orient.core.db.document.ODatabaseDocument; import com.orientechnologies.orient.core.metadata.schema.OClass; import org.orienteer.telegram.module.OTelegramCustomAttributes; -import org.orienteer.telegram.module.OTelegramModule; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ru.ydn.wicket.wicketorientdb.utils.DBClosure; @@ -40,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 (OTelegramCustomAttributes.TELEGRAM_SEARCH.get().getValue(oClass)) { + if (OTelegramCustomAttributes.TELEGRAM_SEARCH.getOrCreate().getValue(oClass)) { classCache.put(oClass.getName(), oClass); } } @@ -50,10 +49,10 @@ private static void createClassCache(ODatabaseDocument db) { private static void createQueryCache() { queryCache = new HashMap<>(); for (OClass oClass : classCache.values()) { - String query = OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.get().getValue(oClass); + String query = OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.getOrCreate().getValue(oClass); if (query == null){ query = "SELECT FROM " + oClass.getName(); - OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.get().setValue(oClass, query); + OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.getOrCreate().setValue(oClass, query); } queryCache.put(oClass.getName(), query); } diff --git a/src/main/java/org/orienteer/telegram/bot/link/ClassLink.java b/src/main/java/org/orienteer/telegram/bot/link/ClassLink.java index 1ab73d6..6ecc31f 100644 --- a/src/main/java/org/orienteer/telegram/bot/link/ClassLink.java +++ b/src/main/java/org/orienteer/telegram/bot/link/ClassLink.java @@ -10,7 +10,6 @@ import org.orienteer.telegram.bot.OTelegramBot; import org.orienteer.telegram.bot.response.BotState; import org.orienteer.telegram.module.OTelegramCustomAttributes; -import org.orienteer.telegram.module.OTelegramModule; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ru.ydn.wicket.wicketorientdb.utils.DBClosure; @@ -65,7 +64,7 @@ protected Map execute(ODatabaseDocument oDatabaseDocument) { } else builder.append(MessageKey.WITHOUT_SUPER_CLASSES.getString(locale)); builder.append("\n"); List resultList = new ArrayList<>(); - if (OTelegramCustomAttributes.TELEGRAM_CLASS_DESCRIPTION.get().getValue(oClass)) { + if (OTelegramCustomAttributes.TELEGRAM_CLASS_DESCRIPTION.getOrCreate().getValue(oClass)) { Collection properties = oClass.properties(); for (OProperty property : properties) { resultList.add(String.format(MessageKey.HTML_STRONG_TEXT.toString(), property.getName()) @@ -79,7 +78,7 @@ protected Map execute(ODatabaseDocument oDatabaseDocument) { } ORecordIteratorClass oDocuments = oDatabaseDocument.browseClass(oClass.getName()); resultList = new ArrayList<>(); - if (OTelegramCustomAttributes.TELEGRAM_DOCUMENTS_LIST.get().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"); diff --git a/src/main/java/org/orienteer/telegram/component/widget/OTelegramBotWidget.java b/src/main/java/org/orienteer/telegram/component/widget/OTelegramBotWidget.java index f027417..4c7f11e 100644 --- a/src/main/java/org/orienteer/telegram/component/widget/OTelegramBotWidget.java +++ b/src/main/java/org/orienteer/telegram/component/widget/OTelegramBotWidget.java @@ -23,7 +23,6 @@ import org.orienteer.core.widget.AbstractModeAwareWidget; import org.orienteer.core.widget.Widget; import org.orienteer.telegram.module.OTelegramCustomAttributes; -import org.orienteer.telegram.module.OTelegramModule; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ru.ydn.wicket.wicketorientdb.components.TransactionlessForm; @@ -50,10 +49,10 @@ public class OTelegramBotWidget extends AbstractModeAwareWidget { public OTelegramBotWidget(String id, IModel model, IModel widgetDocumentModel) { super(id, model, widgetDocumentModel); Form form = new TransactionlessForm("form"); - propertiesList.add(OTelegramCustomAttributes.TELEGRAM_SEARCH.get().getName()); - propertiesList.add(OTelegramCustomAttributes.TELEGRAM_DOCUMENTS_LIST.get().getName()); - propertiesList.add(OTelegramCustomAttributes.TELEGRAM_CLASS_DESCRIPTION.get().getName()); - propertiesList.add(OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.get().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("attributes", model, propertiesList) { @Override @@ -72,13 +71,13 @@ protected Component resolveComponent(String id, DisplayMode mode, String critery mode = DisplayMode.VIEW; } if (DisplayMode.VIEW.equals(mode)) { - if (critery.equals(OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.get().getName())) { + if (critery.equals(OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.getOrCreate().getName())) { return new Label(id, getModel()); } else { return new BooleanViewPanel(id, Model.of(getModel())); } } else if (DisplayMode.EDIT.equals(mode)){ - if (critery.equals(OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.get().getName())) { + if (critery.equals(OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.getOrCreate().getName())) { return new TextField(id, getModel()); } else return new BooleanEditPanel(id, Model.of(getModel())); } diff --git a/src/main/java/org/orienteer/telegram/module/OTelegramCustomAttributes.java b/src/main/java/org/orienteer/telegram/module/OTelegramCustomAttributes.java index dc70c6a..f688a93 100644 --- a/src/main/java/org/orienteer/telegram/module/OTelegramCustomAttributes.java +++ b/src/main/java/org/orienteer/telegram/module/OTelegramCustomAttributes.java @@ -26,7 +26,7 @@ public enum OTelegramCustomAttributes { this.hiearchical = hiearchical; } - public CustomAttribute get() { + public CustomAttribute getOrCreate() { CustomAttribute result = CustomAttribute.getIfExists(name); return result != null ? result : CustomAttribute.create(name, type, defaultValue, encode, hiearchical); } From af3a3d7ccc1f2e5b70c98eda4b5fe94566ab7d45 Mon Sep 17 00:00:00 2001 From: Vitaliy Gonchar Date: Thu, 23 Mar 2017 23:15:08 +0200 Subject: [PATCH 4/4] Delete OTelegramCustomAttribute and use method getOrCreate() of class CustomAttribute --- .../org/orienteer/telegram/bot/Cache.java | 8 ++--- .../telegram/bot/link/ClassLink.java | 6 ++-- .../component/widget/OTelegramBotWidget.java | 27 +++++++-------- .../module/OTelegramCustomAttributes.java | 33 ------------------- .../telegram/module/OTelegramModule.java | 7 +++- 5 files changed, 25 insertions(+), 56 deletions(-) delete mode 100644 src/main/java/org/orienteer/telegram/module/OTelegramCustomAttributes.java diff --git a/src/main/java/org/orienteer/telegram/bot/Cache.java b/src/main/java/org/orienteer/telegram/bot/Cache.java index 0f89b29..7c38056 100644 --- a/src/main/java/org/orienteer/telegram/bot/Cache.java +++ b/src/main/java/org/orienteer/telegram/bot/Cache.java @@ -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.OTelegramCustomAttributes; +import org.orienteer.telegram.module.OTelegramModule; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ru.ydn.wicket.wicketorientdb.utils.DBClosure; @@ -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 (OTelegramCustomAttributes.TELEGRAM_SEARCH.getOrCreate().getValue(oClass)) { + if (OTelegramModule.TELEGRAM_SEARCH.getValue(oClass)) { classCache.put(oClass.getName(), oClass); } } @@ -49,10 +49,10 @@ private static void createClassCache(ODatabaseDocument db) { private static void createQueryCache() { queryCache = new HashMap<>(); for (OClass oClass : classCache.values()) { - String query = OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.getOrCreate().getValue(oClass); + String query = OTelegramModule.TELEGRAM_SEARCH_QUERY.getValue(oClass); if (query == null){ query = "SELECT FROM " + oClass.getName(); - OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.getOrCreate().setValue(oClass, query); + OTelegramModule.TELEGRAM_SEARCH_QUERY.setValue(oClass, query); } queryCache.put(oClass.getName(), query); } diff --git a/src/main/java/org/orienteer/telegram/bot/link/ClassLink.java b/src/main/java/org/orienteer/telegram/bot/link/ClassLink.java index 6ecc31f..145bdfd 100644 --- a/src/main/java/org/orienteer/telegram/bot/link/ClassLink.java +++ b/src/main/java/org/orienteer/telegram/bot/link/ClassLink.java @@ -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.OTelegramCustomAttributes; +import org.orienteer.telegram.module.OTelegramModule; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ru.ydn.wicket.wicketorientdb.utils.DBClosure; @@ -64,7 +64,7 @@ protected Map execute(ODatabaseDocument oDatabaseDocument) { } else builder.append(MessageKey.WITHOUT_SUPER_CLASSES.getString(locale)); builder.append("\n"); List resultList = new ArrayList<>(); - if (OTelegramCustomAttributes.TELEGRAM_CLASS_DESCRIPTION.getOrCreate().getValue(oClass)) { + if (OTelegramModule.TELEGRAM_CLASS_DESCRIPTION.getValue(oClass)) { Collection properties = oClass.properties(); for (OProperty property : properties) { resultList.add(String.format(MessageKey.HTML_STRONG_TEXT.toString(), property.getName()) @@ -78,7 +78,7 @@ protected Map execute(ODatabaseDocument oDatabaseDocument) { } ORecordIteratorClass oDocuments = oDatabaseDocument.browseClass(oClass.getName()); resultList = new ArrayList<>(); - if (OTelegramCustomAttributes.TELEGRAM_DOCUMENTS_LIST.getOrCreate().getValue(oClass)) { + if (OTelegramModule.TELEGRAM_DOCUMENTS_LIST.getValue(oClass)) { builder.append("\n"); builder.append(String.format(MessageKey.HTML_STRONG_TEXT.toString(), MessageKey.CLASS_DOCUMENTS.getString(locale))); builder.append("\n"); diff --git a/src/main/java/org/orienteer/telegram/component/widget/OTelegramBotWidget.java b/src/main/java/org/orienteer/telegram/component/widget/OTelegramBotWidget.java index 4c7f11e..c72d168 100644 --- a/src/main/java/org/orienteer/telegram/component/widget/OTelegramBotWidget.java +++ b/src/main/java/org/orienteer/telegram/component/widget/OTelegramBotWidget.java @@ -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.OTelegramCustomAttributes; +import org.orienteer.telegram.module.OTelegramModule; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ru.ydn.wicket.wicketorientdb.components.TransactionlessForm; @@ -33,7 +33,6 @@ import java.util.List; - /** * @author Vitaliy Gonchar */ @@ -42,22 +41,21 @@ public class OTelegramBotWidget extends AbstractModeAwareWidget { private static final long serialVersionUID = 1L; private static final Logger LOG = LoggerFactory.getLogger(OTelegramBotWidget.class); - private OrienteerStructureTable structureTable; + private OrienteerStructureTable structureTable; - private List propertiesList = new ArrayList<>(); + private List propertiesList = new ArrayList<>(); public OTelegramBotWidget(String id, IModel model, IModel widgetDocumentModel) { super(id, model, widgetDocumentModel); Form form = new TransactionlessForm("form"); - 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("attributes", model, propertiesList) { - + propertiesList.add(OTelegramModule.TELEGRAM_SEARCH); + propertiesList.add(OTelegramModule.TELEGRAM_DOCUMENTS_LIST); + propertiesList.add(OTelegramModule.TELEGRAM_CLASS_DESCRIPTION); + propertiesList.add(OTelegramModule.TELEGRAM_SEARCH_QUERY); + structureTable = new OrienteerStructureTable("attributes", model, propertiesList) { @Override - protected Component getValueComponent(String id, IModel rowModel) { - return new OClassMetaPanel(id, getModeModel(), getModel(), rowModel) { + protected Component getValueComponent(String id, IModel rowModel) { + return new OClassMetaPanel(id, getModeModel(), getModel(), Model.of(rowModel.getObject().getName())) { @Override protected Object getValue(OClass entity, String critery) { CustomAttribute customAttribute = CustomAttribute.get(critery); @@ -71,13 +69,13 @@ protected Component resolveComponent(String id, DisplayMode mode, String critery mode = DisplayMode.VIEW; } if (DisplayMode.VIEW.equals(mode)) { - if (critery.equals(OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.getOrCreate().getName())) { + if (critery.equals(OTelegramModule.TELEGRAM_SEARCH_QUERY.getName())) { return new Label(id, getModel()); } else { return new BooleanViewPanel(id, Model.of(getModel())); } } else if (DisplayMode.EDIT.equals(mode)){ - if (critery.equals(OTelegramCustomAttributes.TELEGRAM_SEARCH_QUERY.getOrCreate().getName())) { + if (critery.equals(OTelegramModule.TELEGRAM_SEARCH_QUERY.getName())) { return new TextField(id, getModel()); } else return new BooleanEditPanel(id, Model.of(getModel())); } @@ -92,7 +90,6 @@ protected void setValue(OClass entity, String critery, Object value) { }; } }; - structureTable.addCommand(new EditSchemaCommand(structureTable, getModeModel())); structureTable.addCommand(new SaveSchemaCommand(structureTable, getModeModel())); form.add(structureTable); diff --git a/src/main/java/org/orienteer/telegram/module/OTelegramCustomAttributes.java b/src/main/java/org/orienteer/telegram/module/OTelegramCustomAttributes.java deleted file mode 100644 index f688a93..0000000 --- a/src/main/java/org/orienteer/telegram/module/OTelegramCustomAttributes.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.orienteer.telegram.module; - -import com.orientechnologies.orient.core.metadata.schema.OType; -import org.orienteer.core.CustomAttribute; - -/** - * @author Vitaliy Gonchar - */ -public enum OTelegramCustomAttributes { - 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); - } -} diff --git a/src/main/java/org/orienteer/telegram/module/OTelegramModule.java b/src/main/java/org/orienteer/telegram/module/OTelegramModule.java index 592bcf2..9c60bde 100644 --- a/src/main/java/org/orienteer/telegram/module/OTelegramModule.java +++ b/src/main/java/org/orienteer/telegram/module/OTelegramModule.java @@ -37,7 +37,12 @@ public class OTelegramModule extends AbstractOrienteerModule { 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; + public static final CustomAttribute TELEGRAM_SEARCH = CustomAttribute.getOrCreate("orienteer.telegramSearch", OType.BOOLEAN, false, false, false); + public static final CustomAttribute TELEGRAM_DOCUMENTS_LIST = CustomAttribute.getOrCreate("orienteer.telegramDocumentsList", OType.BOOLEAN, false, false, false); + public static final CustomAttribute TELEGRAM_SEARCH_QUERY = CustomAttribute.getOrCreate("orienteer.telegramSearchQuery", OType.STRING, null, true, false); + public static final CustomAttribute TELEGRAM_CLASS_DESCRIPTION = CustomAttribute.getOrCreate("orienteer.telegramClassDescription", OType.BOOLEAN, false, false, false); + + private BotSession botSession; protected OTelegramModule() { super(NAME, 1);