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

[Feature] Dynamic state parent group #48

Merged
merged 8 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*
* @see <a href="https://www.touch-portal.com/sdk/index.php?section=categories">TP Documentation: Categories</a>
*/
@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Category {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.christophecvb.touchportal.annotations.Category;

import javax.lang.model.element.Element;
import java.lang.reflect.Field;

/**
* Touch Portal Plugin Category Helper
Expand All @@ -48,6 +49,17 @@ public static String getCategoryId(Element pluginElement, Element categoryElemen
return CategoryHelper._getCategoryId(PluginHelper.getPluginId(pluginElement), category.id().isEmpty() ? categoryElement.getSimpleName().toString() : category.id());
}

/**
* Get Category Short ID
*
* @param categoryField {@link Field}
* @param category {@link Category}
* @return String categoryId
*/
public static String getCategoryShortId(Field categoryField, Category category) {
return category.id().isEmpty() ? categoryField.getName() : category.id();
}

/**
* Get the generated Category Name
*
Expand All @@ -59,6 +71,17 @@ public static String getCategoryName(Element categoryElement, Category category)
return category.name().isEmpty() ? categoryElement.getSimpleName().toString() : category.name();
}

/**
* Get the generated Category Name
*
* @param categoryField {@link Field}
* @param category {@link Category}
* @return String categoryName
*/
public static String getCategoryName(Field categoryField, Category category) {
return category.name().isEmpty() ? categoryField.getName() : category.name();
}

/**
* Get the generated Category ID
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class PluginHelper {
/**
* Touch Portal Plugin System version
*/
public static final int TOUCH_PORTAL_PLUGIN_VERSION = 5;
public static final int TOUCH_PORTAL_PLUGIN_VERSION = 6;
/**
* Argument passed to the jar to start the plugin
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ public class SentMessageHelper {
public static final String OPTIONS = "options";
public static final String CONNECTOR_ID = "connectorId";
public static final String SHORT_ID = "shortId";
public static final String PARENT_GROUP = "parentGroup";

}
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,21 @@ public boolean sendStateUpdate(String stateId, Object value, boolean allowEmptyV
* @return boolean stateUpdateMessageSent
*/
public boolean sendCreateState(String categoryId, String stateId, String description, Object value) {
return this.sendCreateState(categoryId, stateId, description, value, false, false);
return this.sendCreateState(categoryId, stateId, null, description, value, false, false);
}

/**
* Send a Create a State Message to the Touch Portal Plugin System not allowing empty value
*
* @param categoryId String
* @param stateId String
* @param parentGroup String
* @param description String
* @param value Object
* @return boolean stateUpdateMessageSent
*/
public boolean sendCreateState(String categoryId, String stateId, String parentGroup, String description, Object value) {
return this.sendCreateState(categoryId, stateId, parentGroup, description, value, false, false);
}

/**
Expand All @@ -727,6 +741,22 @@ public boolean sendCreateState(String categoryId, String stateId, String descrip
* @return boolean stateCreateSent
*/
public boolean sendCreateState(String categoryId, String stateId, String description, Object value, boolean allowEmptyValue, boolean forceUpdate) {
return this.sendCreateState(categoryId, stateId, null, description, value, allowEmptyValue, forceUpdate);
}

/**
* Send a Create a State Message to the Touch Portal Plugin System
*
* @param categoryId String
* @param stateId String
* @param parentGroup String
* @param description String
* @param value Object
* @param allowEmptyValue boolean
* @param forceUpdate boolean
* @return boolean stateCreateSent
*/
public boolean sendCreateState(String categoryId, String stateId, String parentGroup, String description, Object value, boolean allowEmptyValue, boolean forceUpdate) {
boolean sent = false;
String valueStr = value != null ? String.valueOf(value) : null;
if (categoryId != null && !categoryId.isEmpty() && stateId != null && !stateId.isEmpty() && description != null && !description.isEmpty() && valueStr != null && (allowEmptyValue || !valueStr.isEmpty())) {
Expand All @@ -737,6 +767,24 @@ public boolean sendCreateState(String categoryId, String stateId, String descrip
createStateMessage.addProperty(SentMessageHelper.ID, stateId);
createStateMessage.addProperty(SentMessageHelper.DESCRIPTION, description);
createStateMessage.addProperty(SentMessageHelper.DEFAULT_VALUE, valueStr);
if (parentGroup == null || parentGroup.isEmpty()) {
classLoop:
for (Class<?> subClass : this.pluginClass.getDeclaredClasses()) {
for (Field subClassField : subClass.getDeclaredFields()) {
if (subClassField.isAnnotationPresent(Category.class)) {
Category category = subClassField.getAnnotation(Category.class);

if(categoryId.equals(CategoryHelper.getCategoryShortId(subClassField, category))) {
createStateMessage.addProperty(SentMessageHelper.PARENT_GROUP, CategoryHelper.getCategoryName(subClassField, category));
Pjiesco marked this conversation as resolved.
Show resolved Hide resolved
break classLoop;
}
}
}
}
} else {
createStateMessage.addProperty(SentMessageHelper.PARENT_GROUP, parentGroup);
}

sent = this.send(createStateMessage);
if (sent) {
this.currentStates.put(stateId, valueStr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,37 +321,43 @@ public void testDynamicStates() {
assertFalse(this.touchPortalPluginTest.sendCreateState(null, null, null, ""));
assertFalse(this.touchPortalPluginTest.sendCreateState("", "", "", ""));

assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", null, null, null));
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "", null, null));
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", null, "", null));
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", null, null, ""));
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "", "", ""));
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", null, null, null));
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "", null, null));
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", null, "", null));
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", null, null, ""));
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "", "", ""));

assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", null, null));
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", "", null));
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", null, ""));
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", "", ""));
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", null, null));
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "", null));
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", null, ""));
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "", ""));

assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", "Dynamically Created State", null));
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", "Dynamically Created State", ""));
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", null));
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", ""));

assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "StateId", "Dynamically Created State", null, true, false));
assertTrue(this.touchPortalPluginTest.sendCreateState("CategoryId", "StateId", "Dynamically Created State", "", true, false));
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "StateId", "Dynamically Created State", "", true, false));
assertTrue(this.touchPortalPluginTest.sendCreateState("CategoryId", "StateId", "Dynamically Created State", "", true, true));
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", null, true, false));
assertTrue(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", "", true, false));
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", "", true, false));
assertTrue(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", "", true, true));

assertTrue(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", "Default Value 01"));
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", "Default Value 01"));
assertTrue(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", "Default Value 02"));

assertTrue(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Parent", "Dynamically Created State", "Default Value 01"));
assertTrue(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "", "Dynamically Created State", "Default Value 02"));
assertTrue(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", null, "Dynamically Created State", "Default Value 03"));
assertTrue(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Parent", "Dynamically Created State", "Default Value 04", true, false));

assertTrue(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", "Dynamically Created State", "Default Value 01"));
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", "Dynamically Created State", "Default Value 01"));
assertTrue(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", "Dynamically Created State", "Default Value 02"));

assertFalse(this.touchPortalPluginTest.sendRemoveState(null, null));
assertFalse(this.touchPortalPluginTest.sendRemoveState("", null));
assertFalse(this.touchPortalPluginTest.sendRemoveState(null, ""));
assertFalse(this.touchPortalPluginTest.sendRemoveState("", ""));
assertFalse(this.touchPortalPluginTest.sendRemoveState("CategoryId", null));
assertFalse(this.touchPortalPluginTest.sendRemoveState("CategoryId", ""));
assertFalse(this.touchPortalPluginTest.sendRemoveState("BaseCategory", null));
assertFalse(this.touchPortalPluginTest.sendRemoveState("BaseCategory", ""));

assertTrue(this.touchPortalPluginTest.sendRemoveState("CategoryId", "SateId"));
assertTrue(this.touchPortalPluginTest.sendRemoveState("BaseCategory", "StateId"));
}

@Test
Expand All @@ -360,8 +366,8 @@ public void testSendFail() {
assertFalse(this.touchPortalPluginTest.sendStateUpdate(TouchPortalPluginTestConstants.BaseCategory.States.CustomState.ID, "New Value"));
assertFalse(this.touchPortalPluginTest.sendChoiceUpdate("listId", null, true));
assertFalse(this.touchPortalPluginTest.sendSpecificChoiceUpdate("listId", "instanceId", null, true));
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", "Dynamically Created State", "Default Value 01"));
assertFalse(this.touchPortalPluginTest.sendRemoveState("CategoryId", "SateId"));
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", "Default Value 01"));
assertFalse(this.touchPortalPluginTest.sendRemoveState("BaseCategory", "StateId"));
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ allprojects {
}

ext.versionMajor = 8
ext.versionMinor = 1
ext.versionPatch = 2
ext.versionMinor = 2
ext.versionPatch = 0

ext.isRelease = System.getenv('IS_RELEASE') == 'YES'

Expand Down