Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Agent CRUD with new EAV model

Christian Bauerf04243d9ab32
diff --git a/container/build.gradle b/container/build.gradle
index 99315d2e7..6c27c9533 100644
--- a/container/build.gradle
+++ b/container/build.gradle
@@ -10,6 +10,10 @@ dependencies {
compile "io.reactivex:rxjava:$rxjavaVersion"
compile "com.google.guava:guava:$guavaVersion"
+ compile "com.fasterxml.jackson.core:jackson-annotations:$jacksonVersion"
+ compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
+ compile "com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:$jacksonVersion"
+
compile "io.undertow:undertow-servlet:$undertowVersion"
compile "io.undertow:undertow-websockets-jsr:$undertowVersion"
diff --git a/container/src/main/java/org/openremote/container/Container.java b/container/src/main/java/org/openremote/container/Container.java
index ff0720f63..154c5e6da 100644
--- a/container/src/main/java/org/openremote/container/Container.java
+++ b/container/src/main/java/org/openremote/container/Container.java
@@ -26,7 +26,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
-import org.openremote.container.util.ElementalJsonModule;
+import org.openremote.container.json.ElementalJsonModule;
import java.io.IOException;
import java.io.InputStream;
@@ -154,6 +154,8 @@ public class Container {
LOG.fine("Starting service: " + service);
service.start(Container.this);
}
+ } catch (RuntimeException ex) {
+ throw ex;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
diff --git a/container/src/main/java/org/openremote/container/util/ElementalJsonModule.java b/container/src/main/java/org/openremote/container/json/ElementalJsonModule.java
similarity index 93%
rename from container/src/main/java/org/openremote/container/util/ElementalJsonModule.java
rename to container/src/main/java/org/openremote/container/json/ElementalJsonModule.java
index 9ac8682c0..3399e24f1 100644
--- a/container/src/main/java/org/openremote/container/util/ElementalJsonModule.java
+++ b/container/src/main/java/org/openremote/container/json/ElementalJsonModule.java
@@ -17,21 +17,24 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-package org.openremote.container.util;
+package org.openremote.container.json;
import com.fasterxml.jackson.core.*;
-import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonDeserializer;
+import com.fasterxml.jackson.databind.JsonSerializer;
+import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import elemental.json.*;
-import javax.ws.rs.NotSupportedException;
import java.io.IOException;
public class ElementalJsonModule extends SimpleModule {
+
private static class ElementalJsonDeserializer<T extends JsonValue> extends JsonDeserializer<T> {
@Override
- public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
- return (T)genDeserialise(p, ctxt);
+ public T deserialize(JsonParser p, DeserializationContext context) throws IOException, JsonProcessingException {
+ return (T)genDeserialise(p, context);
}
}
//
diff --git a/container/src/main/java/org/openremote/container/json/JsonStringSqlTypeDescriptor.java b/container/src/main/java/org/openremote/container/json/JsonStringSqlTypeDescriptor.java
index 9acea1d6f..162072c38 100644
--- a/container/src/main/java/org/openremote/container/json/JsonStringSqlTypeDescriptor.java
+++ b/container/src/main/java/org/openremote/container/json/JsonStringSqlTypeDescriptor.java
@@ -48,17 +48,17 @@ public class JsonStringSqlTypeDescriptor implements SqlTypeDescriptor {
return new BasicExtractor<X>(javaTypeDescriptor, this) {
@Override
protected X doExtract(ResultSet rs, String name, WrapperOptions options) throws SQLException {
- return javaTypeDescriptor.wrap(rs.getObject(name), options);
+ return javaTypeDescriptor.wrap(rs.getString(name), options);
}
@Override
protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
- return javaTypeDescriptor.wrap(statement.getObject(index), options);
+ return javaTypeDescriptor.wrap(statement.getString(index), options);
}
@Override
protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException {
- return javaTypeDescriptor.wrap(statement.getObject(name), options);
+ return javaTypeDescriptor.wrap(statement.getString(name), options);
}
};
}
diff --git a/container/src/main/java/org/openremote/container/util/JsonUtil.java b/container/src/main/java/org/openremote/container/json/JsonUtil.java
similarity index 90%
rename from container/src/main/java/org/openremote/container/util/JsonUtil.java
rename to container/src/main/java/org/openremote/container/json/JsonUtil.java
index b4c8c9dc4..ed0ce884c 100644
--- a/container/src/main/java/org/openremote/container/util/JsonUtil.java
+++ b/container/src/main/java/org/openremote/container/json/JsonUtil.java
@@ -1,4 +1,4 @@
-package org.openremote.container.util;
+package org.openremote.container.json;
import com.fasterxml.jackson.databind.ObjectMapper;
diff --git a/container/src/main/java/org/openremote/container/persistence/PersistenceService.java b/container/src/main/java/org/openremote/container/persistence/PersistenceService.java
index df7e38c40..97216c58c 100644
--- a/container/src/main/java/org/openremote/container/persistence/PersistenceService.java
+++ b/container/src/main/java/org/openremote/container/persistence/PersistenceService.java
@@ -22,10 +22,15 @@ package org.openremote.container.persistence;
import org.openremote.container.Container;
import org.openremote.container.ContainerService;
+import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.util.HashMap;
import java.util.Map;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.logging.Level;
import java.util.logging.Logger;
public class PersistenceService implements ContainerService {
@@ -112,6 +117,37 @@ public class PersistenceService implements ContainerService {
generateSchema("drop");
}
+ public void doTransaction(Consumer<EntityManager> entityManagerConsumer) {
+ doTransaction(entityManager -> {
+ entityManagerConsumer.accept(entityManager);
+ return null;
+ });
+ }
+
+ public <R> R doTransaction(Function<EntityManager, R> entityManagerFunction) {
+ EntityManager em = getEntityManagerFactory().createEntityManager();
+ EntityTransaction tx = em.getTransaction();
+ try {
+ tx.begin();
+ R result = entityManagerFunction.apply(em);
+ tx.commit();
+ return result;
+ } catch (RuntimeException ex) {
+ // TODO Check that this is good tx code, no idea how this stupid transaction API works. Should we use JTA?
+ if (tx != null && tx.isActive() && tx.getRollbackOnly()) {
+ try {
+ LOG.log(Level.FINE, "Rolling back failed transaction, cause follows", ex);
+ tx.rollback();
+ } catch (RuntimeException rbEx) {
+ LOG.log(Level.SEVERE, "Rollback of transaction failed!", rbEx);
+ }
+ }
+ throw ex;
+ } finally {
+ em.close();
+ }
+ }
+
protected void generateSchema(String action) {
// Take exiting EMF properties, override the schema generation setting on a copy
Map<String, Object> createSchemaProperties = new HashMap<>(persistenceUnitProperties);
diff --git a/gradle.properties b/gradle.properties
index 784e855d7..828dbaf5e 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -19,7 +19,7 @@ gwtVersion = 2.8.0-beta1
ginVersion = 2.1.2
cglibVersion = 3.2.0
gwtJacksonVersion = 0.13.0
-jacksonVersion = 2.6.3
+jacksonVersion = 2.7.3
groovyVersion = 2.4.1
sqliteVersion = 3.8.11.2
hibernateVersion = 5.0.7.Final
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/AdminModule.java b/manager/client/src/main/java/org/openremote/manager/client/admin/AdminModule.java
index c7f07c566..bb5ded846 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/admin/AdminModule.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/AdminModule.java
@@ -31,6 +31,7 @@ import org.openremote.manager.client.admin.overview.AdminOverviewActivity;
import org.openremote.manager.client.admin.overview.AdminOverviewImpl;
import org.openremote.manager.client.admin.tenant.*;
import org.openremote.manager.client.admin.users.*;
+import org.openremote.manager.shared.agent.AgentResource;
import org.openremote.manager.shared.connector.ConnectorResource;
import org.openremote.manager.shared.security.TenantResource;
import org.openremote.manager.shared.security.UserResource;
@@ -92,4 +93,15 @@ public class AdminModule extends AbstractGinModule {
public static native ConnectorResource getNativeConnectorResource() /*-{
return $wnd.ConnectorResource;
}-*/;
+
+ @Provides
+ @Singleton
+ public AgentResource getAgentResource() {
+ return getNativeAgentResource();
+ }
+
+ public static native AgentResource getNativeAgentResource() /*-{
+ return $wnd.AgentResource;
+ }-*/;
+
}
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentActivity.java b/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentActivity.java
index fa5e28979..fc11d99df 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentActivity.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentActivity.java
@@ -32,9 +32,9 @@ import org.openremote.manager.client.service.RequestService;
import org.openremote.manager.client.service.SecurityService;
import org.openremote.manager.shared.Runnable;
import org.openremote.manager.shared.agent.Agent;
-import org.openremote.manager.shared.ngsi.EntityResource;
-import org.openremote.manager.shared.connector.ConnectorResource;
+import org.openremote.manager.shared.agent.AgentResource;
import org.openremote.manager.shared.connector.Connector;
+import org.openremote.manager.shared.connector.ConnectorResource;
import org.openremote.manager.shared.event.ui.ShowInfoEvent;
import javax.inject.Inject;
@@ -54,10 +54,10 @@ public class AdminAgentActivity
final protected EventBus eventBus;
final protected SecurityService securityService;
final protected RequestService requestService;
- final protected EntityResource assetsResource;
final protected ConnectorResource connectorResource;
- final protected AgentMapper agentMapper = new AgentMapper();
- final protected ConnectorArrayMapper connectorArrayMapper = new ConnectorArrayMapper();
+ final protected AgentResource agentResource;
+ final protected AgentMapper agentMapper;
+ final protected ConnectorArrayMapper connectorArrayMapper;
protected String id;
protected Connector[] connectors;
@@ -77,16 +77,20 @@ public class AdminAgentActivity
EventBus eventBus,
SecurityService securityService,
RequestService requestService,
- EntityResource assetsResource,
- ConnectorResource connectorResource) {
+ ConnectorResource connectorResource,
+ AgentResource agentResource,
+ AgentMapper agentMapper,
+ ConnectorArrayMapper connectorArrayMapper) {
super(adminView, adminNavigationPresenter, view);
this.managerMessages = managerMessages;
this.placeController = placeController;
this.eventBus = eventBus;
this.securityService = securityService;
this.requestService = requestService;
- this.assetsResource = assetsResource;
this.connectorResource = connectorResource;
+ this.agentResource = agentResource;
+ this.agentMapper = agentMapper;
+ this.connectorArrayMapper = connectorArrayMapper;
notFoundConnector.setId(notFoundConnectorId);
}
@@ -153,7 +157,7 @@ public class AdminAgentActivity
requestService.execute(
agentMapper,
requestParams -> {
- assetsResource.postEntity(requestParams, agent);
+ agentResource.create(requestParams, agent);
},
204,
() -> {
@@ -177,7 +181,7 @@ public class AdminAgentActivity
requestService.execute(
agentMapper,
requestParams -> {
- assetsResource.putEntityAttributes(requestParams, id, agent);
+ agentResource.update(requestParams, id, agent);
},
204,
() -> {
@@ -196,7 +200,7 @@ public class AdminAgentActivity
clearViewFieldErrors();
requestService.execute(
requestParams -> {
- assetsResource.deleteEntity(requestParams, this.id);
+ agentResource.delete(requestParams, this.id);
},
204,
() -> {
@@ -237,7 +241,7 @@ public class AdminAgentActivity
protected void loadAgent(Runnable onSuccess) {
requestService.execute(
agentMapper,
- requestParams -> assetsResource.getEntity(requestParams, id, null),
+ requestParams -> agentResource.get(requestParams, id),
200,
agent -> {
this.agent = agent;
@@ -275,7 +279,7 @@ public class AdminAgentActivity
}
if (assignedConnector != null) {
assignedConnector.readSettings(agent);
- } else {
+ } else if (!Agent.NO_CONNECTOR_ASSIGNED_TYPE.equals(agent.getConnectorType())){
notFoundConnector.setName(
agent.getConnectorType() + " (" + managerMessages.connectorNotInstalled() + ")"
);
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentImpl.java b/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentImpl.java
index 5a758e162..629722e5a 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentImpl.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentImpl.java
@@ -28,9 +28,10 @@ import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.*;
import org.openremote.manager.client.i18n.ManagerMessages;
-import org.openremote.manager.client.widget.EntityFormView;
+import org.openremote.manager.client.widget.AttributesFormView;
import org.openremote.manager.client.widget.FormGroup;
import org.openremote.manager.client.widget.PushButton;
+import org.openremote.manager.shared.attribute.Attributes;
import org.openremote.manager.shared.connector.Connector;
import javax.inject.Inject;
@@ -38,7 +39,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Logger;
-public class AdminAgentImpl extends EntityFormView implements AdminAgent {
+public class AdminAgentImpl extends AttributesFormView implements AdminAgent {
private static final Logger LOG = Logger.getLogger(AdminAgentImpl.class.getName());
@@ -143,7 +144,7 @@ public class AdminAgentImpl extends EntityFormView implements AdminAgent {
if (connector != null) {
FormGroup[] attributeFormGroups = createAttributeFormGroups(
style.connectorAttributeTextBox(),
- connector.getAttributes()
+ new Attributes(connector.getSettings())
);
for (FormGroup attributeFormGroup : attributeFormGroups) {
attributesContainer.add(attributeFormGroup);
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentsActivity.java b/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentsActivity.java
index 0b356e28c..0d58fd967 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentsActivity.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentsActivity.java
@@ -29,11 +29,11 @@ import org.openremote.manager.client.event.bus.EventRegistration;
import org.openremote.manager.client.i18n.ManagerMessages;
import org.openremote.manager.client.service.RequestService;
import org.openremote.manager.shared.agent.Agent;
-import org.openremote.manager.shared.ngsi.EntityResource;
-import org.openremote.manager.shared.ngsi.params.EntityListParams;
+import org.openremote.manager.shared.agent.AgentResource;
import javax.inject.Inject;
import java.util.Collection;
+import java.util.logging.Logger;
import static org.openremote.manager.client.http.RequestExceptionHandler.handleRequestException;
@@ -41,11 +41,13 @@ public class AdminAgentsActivity
extends AbstractAdminActivity<AdminAgentsPlace, AdminAgents>
implements AdminAgents.Presenter {
+ private static final Logger LOG = Logger.getLogger(AdminAgentsActivity.class.getName());
+
final protected ManagerMessages managerMessages;
final protected PlaceController placeController;
final protected RequestService requestService;
- final protected EntityResource assetsResource;
- final protected AgentArrayMapper agentArrayMapper = new AgentArrayMapper();
+ final protected AgentResource agentResource;
+ final protected AgentArrayMapper agentArrayMapper;
@Inject
public AdminAgentsActivity(AdminView adminView,
@@ -54,12 +56,14 @@ public class AdminAgentsActivity
ManagerMessages managerMessages,
PlaceController placeController,
RequestService requestService,
- EntityResource assetsResource) {
+ AgentResource agentResource,
+ AgentArrayMapper agentArrayMapper) {
super(adminView, adminNavigationPresenter, view);
this.managerMessages = managerMessages;
this.placeController = placeController;
this.requestService = requestService;
- this.assetsResource = assetsResource;
+ this.agentResource = agentResource;
+ this.agentArrayMapper = agentArrayMapper;
}
@Override
@@ -74,13 +78,12 @@ public class AdminAgentsActivity
requestService.execute(
agentArrayMapper,
- requestParams -> {
- assetsResource.getEntities(requestParams, new EntityListParams().type(Agent.TYPE));
- },
+ agentResource::getAll,
200,
adminContent::setAgents,
ex -> handleRequestException(ex, eventBus, managerMessages)
);
+
}
@Override
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentsImpl.ui.xml b/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentsImpl.ui.xml
index 55b985006..635a7bfb8 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentsImpl.ui.xml
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentsImpl.ui.xml
@@ -8,7 +8,7 @@
<ui:style field="agentsTableStyle" type="org.openremote.manager.client.admin.agent.AdminAgentsTable.Style">
.nameColumn {
- width: 15em;
+ width: 20em;
}
.descriptionColumn {
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentsTable.java b/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentsTable.java
index 16036f253..4face67e1 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentsTable.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AdminAgentsTable.java
@@ -63,7 +63,10 @@ public class AdminAgentsTable extends FormTable<Agent> {
final protected TextColumn<Agent> connectorColumn = new TextColumn<Agent>() {
@Override
public String getValue(Agent agent) {
- return agent.getConnectorType() != null ? agent.getConnectorType() : "-";
+ if (agent.getConnectorType() == null
+ || Agent.NO_CONNECTOR_ASSIGNED_TYPE.equals(agent.getConnectorType()))
+ return "-";
+ return agent.getConnectorType();
}
};
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AgentArrayMapper.java b/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AgentArrayMapper.java
index 6fc0179a9..abd6d22a5 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AgentArrayMapper.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AgentArrayMapper.java
@@ -19,28 +19,17 @@
*/
package org.openremote.manager.client.admin.agent;
-import elemental.json.JsonArray;
-import elemental.json.impl.JsonUtil;
+import com.github.nmorel.gwtjackson.client.ObjectMapper;
+import com.github.nmorel.gwtjackson.client.annotation.JsonMixIns;
+import org.openremote.manager.client.interop.jackson.DefaultJsonMixin;
import org.openremote.manager.shared.agent.Agent;
import org.openremote.manager.shared.http.EntityReader;
+import org.openremote.manager.shared.http.EntityWriter;
-import java.util.ArrayList;
-import java.util.List;
-
-public class AgentArrayMapper implements EntityReader<Agent[]> {
-
- @Override
- public Agent[] read(String value) {
- JsonArray jsonArray = JsonUtil.parse(value);
- List<Agent> list = new ArrayList<>();
- for (int i = 0; i < jsonArray.length(); i++) {
- Agent agent = new Agent(jsonArray.getObject(i));
- // TODO: URGENT FIX NEEDED! ENTITY TYPE QUERIES ARE NOT SUPPORTED SO HERE WE GET _ALL_ ENTITIES!
- if (agent.getType().equals(Agent.TYPE)) {
- list.add(agent);
- }
- }
- return list.toArray(new Agent[list.size()]);
- }
-
+@JsonMixIns({@JsonMixIns.JsonMixIn(target = Agent.class, mixIn = DefaultJsonMixin.class)})
+public interface AgentArrayMapper
+ extends ObjectMapper<Agent[]>,
+ EntityReader<Agent[]>,
+ EntityWriter<Agent[]> {
}
+
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AgentMapper.java b/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AgentMapper.java
index 4881f764b..f658d1955 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AgentMapper.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/agent/AgentMapper.java
@@ -19,20 +19,16 @@
*/
package org.openremote.manager.client.admin.agent;
-import elemental.json.impl.JsonUtil;
+import com.github.nmorel.gwtjackson.client.ObjectMapper;
+import com.github.nmorel.gwtjackson.client.annotation.JsonMixIns;
+import org.openremote.manager.client.interop.jackson.DefaultJsonMixin;
import org.openremote.manager.shared.agent.Agent;
import org.openremote.manager.shared.http.EntityReader;
import org.openremote.manager.shared.http.EntityWriter;
-public class AgentMapper implements EntityReader<Agent>, EntityWriter<Agent> {
-
- @Override
- public Agent read(String value) {
- return new Agent(JsonUtil.parse(value));
- }
-
- @Override
- public String write(Agent value) {
- return value.getJsonObject().toJson();
- }
+@JsonMixIns({@JsonMixIns.JsonMixIn(target = Agent.class, mixIn = DefaultJsonMixin.class)})
+public interface AgentMapper
+ extends ObjectMapper<Agent>,
+ EntityReader<Agent>,
+ EntityWriter<Agent> {
}
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/agent/ConnectorArrayMapper.java b/manager/client/src/main/java/org/openremote/manager/client/admin/agent/ConnectorArrayMapper.java
index ff8cd08d8..e8d26494b 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/admin/agent/ConnectorArrayMapper.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/agent/ConnectorArrayMapper.java
@@ -19,24 +19,16 @@
*/
package org.openremote.manager.client.admin.agent;
-import elemental.json.JsonArray;
-import elemental.json.impl.JsonUtil;
+import com.github.nmorel.gwtjackson.client.ObjectMapper;
+import com.github.nmorel.gwtjackson.client.annotation.JsonMixIns;
+import org.openremote.manager.client.interop.jackson.DefaultJsonMixin;
import org.openremote.manager.shared.connector.Connector;
import org.openremote.manager.shared.http.EntityReader;
+import org.openremote.manager.shared.http.EntityWriter;
-import java.util.ArrayList;
-import java.util.List;
-
-public class ConnectorArrayMapper implements EntityReader<Connector[]> {
-
- @Override
- public Connector[] read(String value) {
- JsonArray jsonArray = JsonUtil.parse(value);
- List<Connector> list = new ArrayList<>();
- for (int i = 0; i < jsonArray.length(); i++) {
- list.add(new Connector(jsonArray.getObject(i)));
- }
- return list.toArray(new Connector[list.size()]);
- }
-
+@JsonMixIns({@JsonMixIns.JsonMixIn(target = Connector.class, mixIn = DefaultJsonMixin.class)})
+public interface ConnectorArrayMapper
+ extends ObjectMapper<Connector[]>,
+ EntityReader<Connector[]>,
+ EntityWriter<Connector[]> {
}
diff --git a/manager/client/src/main/java/org/openremote/manager/client/assets/AssetsModule.java b/manager/client/src/main/java/org/openremote/manager/client/assets/AssetsModule.java
index e1fdcc3e3..1a41c65d0 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/assets/AssetsModule.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/assets/AssetsModule.java
@@ -20,7 +20,6 @@
package org.openremote.manager.client.assets;
import com.google.gwt.inject.client.AbstractGinModule;
-import com.google.inject.Provides;
import com.google.inject.Singleton;
import org.openremote.manager.client.assets.asset.AssetActivity;
import org.openremote.manager.client.assets.asset.AssetView;
@@ -28,7 +27,6 @@ import org.openremote.manager.client.assets.asset.AssetViewImpl;
import org.openremote.manager.client.assets.browser.AssetBrowser;
import org.openremote.manager.client.assets.browser.AssetBrowserImpl;
import org.openremote.manager.client.assets.browser.AssetBrowserPresenter;
-import org.openremote.manager.shared.ngsi.EntityResource;
public class AssetsModule extends AbstractGinModule {
@@ -44,13 +42,4 @@ public class AssetsModule extends AbstractGinModule {
bind(AssetActivity.class);
}
- @Provides
- @Singleton
- public EntityResource getAssetsResource() {
- return getNativeAssetsResource();
- }
-
- public static native EntityResource getNativeAssetsResource() /*-{
- return $wnd.AssetsResource;
- }-*/;
}
diff --git a/manager/client/src/main/java/org/openremote/manager/client/interop/jackson/ElementalJsonDeserializer.java b/manager/client/src/main/java/org/openremote/manager/client/interop/jackson/ElementalJsonDeserializer.java
new file mode 100644
index 000000000..53e5c4e1a
--- /dev/null
+++ b/manager/client/src/main/java/org/openremote/manager/client/interop/jackson/ElementalJsonDeserializer.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2016, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.manager.client.interop.jackson;
+
+import com.github.nmorel.gwtjackson.client.JsonDeserializationContext;
+import com.github.nmorel.gwtjackson.client.JsonDeserializer;
+import com.github.nmorel.gwtjackson.client.JsonDeserializerParameters;
+import com.github.nmorel.gwtjackson.client.stream.JsonReader;
+import elemental.json.JsonValue;
+
+/**
+ * This can only (safely) deserialize JSON Objects and Arrays, not primitives.
+ */
+public class ElementalJsonDeserializer extends JsonDeserializer<JsonValue> {
+
+ @Override
+ protected JsonValue doDeserialize(JsonReader reader, JsonDeserializationContext ctx, JsonDeserializerParameters params) {
+ if (reader.hasNext()) {
+ return (JsonValue) reader.nextJavaScriptObject(true);
+ }
+ return null;
+ }
+}
diff --git a/manager/client/src/main/java/org/openremote/manager/client/interop/jackson/ElementalJsonSerializer.java b/manager/client/src/main/java/org/openremote/manager/client/interop/jackson/ElementalJsonSerializer.java
new file mode 100644
index 000000000..79a8be416
--- /dev/null
+++ b/manager/client/src/main/java/org/openremote/manager/client/interop/jackson/ElementalJsonSerializer.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2016, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.manager.client.interop.jackson;
+
+import com.github.nmorel.gwtjackson.client.JsonSerializationContext;
+import com.github.nmorel.gwtjackson.client.JsonSerializer;
+import com.github.nmorel.gwtjackson.client.JsonSerializerParameters;
+import com.github.nmorel.gwtjackson.client.stream.JsonWriter;
+import elemental.json.JsonValue;
+
+import javax.annotation.Nonnull;
+
+public class ElementalJsonSerializer extends JsonSerializer<JsonValue> {
+
+ @Override
+ protected void doSerialize(JsonWriter writer, @Nonnull JsonValue value, JsonSerializationContext ctx, JsonSerializerParameters params) {
+ writer.rawValue(value.toJson());
+ }
+}
diff --git a/manager/client/src/main/java/org/openremote/manager/client/interop/jackson/SerializationConfiguration.java b/manager/client/src/main/java/org/openremote/manager/client/interop/jackson/SerializationConfiguration.java
index 4bcb195cd..b6261ccb5 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/interop/jackson/SerializationConfiguration.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/interop/jackson/SerializationConfiguration.java
@@ -19,17 +19,26 @@
*/
package org.openremote.manager.client.interop.jackson;
-import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.github.nmorel.gwtjackson.client.AbstractConfiguration;
+import elemental.json.JsonArray;
+import elemental.json.JsonObject;
+/**
+ * TODO: On my OS X I have to delete GWT temp files in /var/folders/vn/* every time I change this class and then restart SDM
+ */
public class SerializationConfiguration extends AbstractConfiguration {
@Override
protected void configure() {
+ /* TODO https://github.com/nmorel/gwt-jackson/issues/90
fieldVisibility(JsonAutoDetect.Visibility.ANY);
getterVisibility(JsonAutoDetect.Visibility.NONE);
setterVisibility(JsonAutoDetect.Visibility.NONE);
isGetterVisibility(JsonAutoDetect.Visibility.NONE);
creatorVisibility(JsonAutoDetect.Visibility.NONE);
+ */
+
+ type(JsonObject.class).serializer(ElementalJsonSerializer.class).deserializer(ElementalJsonDeserializer.class);
+ type(JsonArray.class).serializer(ElementalJsonSerializer.class).deserializer(ElementalJsonDeserializer.class);
}
}
diff --git a/manager/client/src/main/java/org/openremote/manager/client/widget/EntityFormView.java b/manager/client/src/main/java/org/openremote/manager/client/widget/AttributesFormView.java
similarity index 90%
rename from manager/client/src/main/java/org/openremote/manager/client/widget/EntityFormView.java
rename to manager/client/src/main/java/org/openremote/manager/client/widget/AttributesFormView.java
index 2dfb811bb..70e956e76 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/widget/EntityFormView.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/widget/AttributesFormView.java
@@ -22,21 +22,19 @@ package org.openremote.manager.client.widget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import elemental.json.Json;
-import org.openremote.manager.shared.connector.Connector;
-import org.openremote.manager.shared.ngsi.Attribute;
-import org.openremote.manager.shared.ngsi.AttributeType;
-import org.openremote.manager.shared.ngsi.MetadataElement;
+import org.openremote.manager.shared.attribute.Attribute;
+import org.openremote.manager.shared.attribute.AttributeType;
+import org.openremote.manager.shared.attribute.Attributes;
+import org.openremote.manager.shared.attribute.MetadataElement;
import java.util.ArrayList;
import java.util.List;
-public class EntityFormView extends FormView {
+public class AttributesFormView extends FormView {
- public FormGroup[] createAttributeFormGroups(String formFieldStyleName, Attribute[] attributes) {
+ public FormGroup[] createAttributeFormGroups(String formFieldStyleName, Attributes attributes) {
List<FormGroup> list = new ArrayList<>();
- for (Attribute attribute : attributes) {
- if (Connector.IMMUTABLE_ATTRIBUTES.contains(attribute.getName()))
- continue;
+ for (Attribute attribute : attributes.get()) {
FormGroup formGroup = createAttributeFormGroup(attribute);
diff --git a/manager/client/src/main/resources/org/openremote/manager/Manager.gwt.xml b/manager/client/src/main/resources/org/openremote/manager/Manager.gwt.xml
index 19a09d24f..c33a1093b 100644
--- a/manager/client/src/main/resources/org/openremote/manager/Manager.gwt.xml
+++ b/manager/client/src/main/resources/org/openremote/manager/Manager.gwt.xml
@@ -32,11 +32,9 @@
<extend-property name="locale" values="en"/>
<!-- ##################################################################### -->
-<!--
<extend-configuration-property name="gwtjackson.configuration.extension"
value="org.openremote.manager.client.interop.jackson.SerializationConfiguration"/>
--->
<replace-with class="org.openremote.manager.client.interop.jackson.JsonDeserializationContextBuilder">
<when-type-assignable class="com.github.nmorel.gwtjackson.client.JsonDeserializationContext.Builder" />
diff --git a/manager/server/src/main/java/org/openremote/manager/server/SampleDataService.java b/manager/server/src/main/java/org/openremote/manager/server/SampleDataService.java
index 38b377e46..f4623c7a1 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/SampleDataService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/SampleDataService.java
@@ -20,7 +20,6 @@
package org.openremote.manager.server;
import elemental.json.Json;
-import elemental.json.JsonObject;
import org.apache.log4j.Logger;
import org.keycloak.admin.client.resource.*;
import org.keycloak.common.enums.SslRequired;
@@ -32,17 +31,19 @@ import org.openremote.container.security.AuthForm;
import org.openremote.manager.server.agent.AgentService;
import org.openremote.manager.server.agent.ConnectorService;
import org.openremote.manager.server.security.ManagerIdentityService;
-import org.openremote.manager.shared.agent.PersistentAgent;
+import org.openremote.manager.shared.agent.Agent;
+import org.openremote.manager.shared.attribute.Attribute;
+import org.openremote.manager.shared.attribute.Attributes;
import rx.Observable;
-import javax.persistence.EntityManager;
-import javax.persistence.EntityTransaction;
import javax.ws.rs.core.UriBuilder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.openremote.manager.shared.Constants.*;
+import static org.openremote.manager.shared.attribute.AttributeType.INTEGER;
+import static org.openremote.manager.shared.attribute.AttributeType.STRING;
import static rx.Observable.fromCallable;
public class SampleDataService implements ContainerService {
@@ -55,6 +56,7 @@ public class SampleDataService implements ContainerService {
public static final String ADMIN_CLI_CLIENT_ID = "admin-cli";
public static final String ADMIN_PASSWORD = "admin";
+ protected Container container;
protected PersistenceService persistenceService;
protected ManagerIdentityService identityService;
protected ConnectorService connectorService;
@@ -62,6 +64,7 @@ public class SampleDataService implements ContainerService {
@Override
public void init(Container container) throws Exception {
+ this.container = container;
persistenceService = container.getService(PersistenceService.class);
identityService = container.getService(ManagerIdentityService.class);
connectorService = container.getService(ConnectorService.class);
@@ -90,15 +93,7 @@ public class SampleDataService implements ContainerService {
configureMasterRealm(accessToken);
registerClientApplications(accessToken);
addRolesAndTestUsers(accessToken);
-
- EntityManager em = persistenceService.getEntityManagerFactory().createEntityManager();
- EntityTransaction tx = em.getTransaction();
- tx.begin();
-
- storeSampleAgent(em);
-
- tx.commit();
- em.close();
+ storeSampleAgent();
}
@Override
@@ -243,29 +238,33 @@ public class SampleDataService implements ContainerService {
}
- protected void storeSampleAgent(EntityManager em) {
- PersistentAgent controller2Agent = new PersistentAgent();
+ protected void storeSampleAgent() {
+ Agent controller2Agent = new Agent();
controller2Agent.setName("OpenRemote Controller2 Test Agent");
controller2Agent.setDescription("A test agent for OpenRemote Controller version 2.x");
controller2Agent.setEnabled(true);
controller2Agent.setConnectorType("urn:openremote:connector:controller2");
- JsonObject connectorSettings = Json.createObject();
- connectorSettings.put("host", Json.create("192.168.0.0"));
- connectorSettings.put("port", Json.create(8080));
- controller2Agent.setConnectorSettings(connectorSettings);
+ Attributes connectorSettings = new Attributes();
+ connectorSettings.add(new Attribute("host", STRING, Json.create("192.168.0.0")));
+ connectorSettings.add(new Attribute("port", INTEGER, Json.create(8080)));
+ controller2Agent.setConnectorSettings(connectorSettings.getJsonObject());
- em.persist(controller2Agent);
+ persistenceService.doTransaction(em -> {
+ em.persist(controller2Agent);
+ });
- // TODO Remove this test
+ // TODO Remove tests
+/*
List hosts = em.createNativeQuery(
"SELECT a.CONNECTOR_SETTINGS -> \"$.host\" " +
"FROM AGENT a " +
- "WHERE JSON_EXTRACT(a.CONNECTOR_SETTINGS, \"$.port\") = 8080")
+ "WHERE JSON_EXTRACT(a.CONNECTOR_SETTINGS, \"$.port.value\") = 8080")
.getResultList();
for (Object host : hosts) {
LOG.info("### GOT: " + host);
}
+*/
}
}
diff --git a/manager/server/src/main/java/org/openremote/manager/server/agent/AgentResourceImpl.java b/manager/server/src/main/java/org/openremote/manager/server/agent/AgentResourceImpl.java
new file mode 100644
index 000000000..3b129f7c7
--- /dev/null
+++ b/manager/server/src/main/java/org/openremote/manager/server/agent/AgentResourceImpl.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2016, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.manager.server.agent;
+
+import org.openremote.container.web.WebResource;
+import org.openremote.manager.shared.agent.AgentResource;
+import org.openremote.manager.shared.agent.Agent;
+import org.openremote.manager.shared.http.RequestParams;
+
+import javax.ws.rs.BeanParam;
+
+public class AgentResourceImpl extends WebResource implements AgentResource {
+
+ protected final AgentService agentService;
+
+ public AgentResourceImpl(AgentService agentService) {
+ this.agentService = agentService;
+ }
+
+ @Override
+ public Agent[] getAll(@BeanParam RequestParams requestParams) {
+ return agentService.getAll();
+ }
+
+ @Override
+ public Agent get(@BeanParam RequestParams requestParams, String agentId) {
+ return agentService.get(agentId);
+ }
+
+ @Override
+ public void create(@BeanParam RequestParams requestParams, Agent agent) {
+ agentService.create(agent);
+ }
+
+ @Override
+ public void update(@BeanParam RequestParams requestParams, String agentId, Agent agent) {
+ agentService.update(agent);
+
+ }
+
+ @Override
+ public void delete(@BeanParam RequestParams requestParams, String agentId) {
+ agentService.delete(agentId);
+ }
+}
diff --git a/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java b/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java
index ad10b39e7..20e3f73dd 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java
@@ -21,16 +21,26 @@ package org.openremote.manager.server.agent;
import org.openremote.container.Container;
import org.openremote.container.ContainerService;
+import org.openremote.container.persistence.PersistenceService;
+import org.openremote.container.web.WebService;
+import org.openremote.manager.shared.agent.Agent;
+
+import java.util.List;
public class AgentService implements ContainerService {
+ protected PersistenceService persistenceService;
+
@Override
public void init(Container container) throws Exception {
-
+ persistenceService = container.getService(PersistenceService.class);
}
@Override
public void configure(Container container) throws Exception {
+ container.getService(WebService.class).getApiSingletons().add(
+ new AgentResourceImpl(this)
+ );
}
@Override
@@ -43,4 +53,41 @@ public class AgentService implements ContainerService {
}
+ public Agent[] getAll() {
+ return persistenceService.doTransaction(em -> {
+ List<Agent> result =
+ em.createQuery(
+ "select pa from Agent pa order by pa.createdOn desc"
+ , Agent.class
+ ).getResultList();
+ return result.toArray(new Agent[result.size()]);
+ });
+ }
+
+ public Agent get(String agentId) {
+ return persistenceService.doTransaction(em -> {
+ return em.find(Agent.class, agentId);
+ });
+ }
+
+ public void create(Agent agent) {
+ persistenceService.doTransaction(em -> {
+ em.persist(agent);
+ });
+ }
+
+ public void update(Agent agent) {
+ persistenceService.doTransaction(em -> {
+ em.merge(agent);
+ });
+ }
+
+ public void delete(String agentId) {
+ persistenceService.doTransaction(em -> {
+ Agent agent = em.find(Agent.class, agentId);
+ if (agent != null) {
+ em.remove(agent);
+ }
+ });
+ }
}
\ No newline at end of file
diff --git a/manager/server/src/main/java/org/openremote/manager/server/agent/ConnectorService.java b/manager/server/src/main/java/org/openremote/manager/server/agent/ConnectorService.java
index 7f783ccae..4fbed7c0e 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/agent/ConnectorService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/agent/ConnectorService.java
@@ -32,12 +32,10 @@ import org.apache.camel.util.CamelContextHelper;
import org.openremote.container.Container;
import org.openremote.container.ContainerService;
import org.openremote.container.message.MessageBrokerService;
+import org.openremote.container.util.IdentifierUtil;
import org.openremote.container.web.WebService;
+import org.openremote.manager.shared.attribute.*;
import org.openremote.manager.shared.connector.Connector;
-import org.openremote.manager.shared.ngsi.Attribute;
-import org.openremote.manager.shared.ngsi.AttributeType;
-import org.openremote.manager.shared.ngsi.Metadata;
-import org.openremote.manager.shared.ngsi.MetadataElement;
import javax.validation.constraints.NotNull;
import java.lang.reflect.Field;
@@ -148,9 +146,8 @@ public class ConnectorService implements ContainerService {
UriEndpointComponent component) {
LOG.fine("Creating connector: " + connectorComponentName);
- Connector connector = new Connector(
- componentProperties.get(Connector.PROPERTY_TYPE).toString()
- );
+ Connector connector = new Connector(IdentifierUtil.generateGlobalUniqueId());
+ connector.setType(componentProperties.get(Connector.PROPERTY_TYPE).toString());
UriEndpoint uriEndpoint = component.getEndpointClass().getAnnotation(UriEndpoint.class);
if (uriEndpoint == null) {
@@ -179,94 +176,103 @@ public class ConnectorService implements ContainerService {
connector.setSupportsInventory(Boolean.valueOf(supportsInventory.toString()));
}
- addConnectorAttributes(
- connector,
+ Attributes settings = buildConnectorSettings(
connectorComponentName,
component.createComponentConfiguration(),
component.getEndpointClass()
);
+ connector.setSettings(settings.getJsonObject());
+
return connector;
}
- protected void addConnectorAttributes(Connector connector,
- String connectorComponentName,
- ComponentConfiguration componentConfiguration,
- Class<? extends Endpoint> endpointClass) {
+ protected Attributes buildConnectorSettings(String connectorComponentName,
+ ComponentConfiguration componentConfiguration,
+ Class<? extends Endpoint> endpointClass) {
+
+ Attributes settings = new Attributes();
for (Map.Entry<String, ParameterConfiguration> configEntry : componentConfiguration.getParameterConfigurationMap().entrySet()) {
try {
Field field = endpointClass.getDeclaredField(configEntry.getKey());
if (field.isAnnotationPresent(UriParam.class)) {
- UriParam uriParam = field.getAnnotation(UriParam.class);
-
- String attributeName = uriParam.name().length() != 0 ? uriParam.name() : field.getName();
-
- Attribute attribute = new Attribute(attributeName, Json.createObject());
- attribute.setMetadata(new Metadata(Json.createObject()));
-
- if (String.class.isAssignableFrom(field.getType())) {
- attribute.setType(AttributeType.STRING);
- } else if (Integer.class.isAssignableFrom(field.getType())) {
- attribute.setType(AttributeType.INTEGER);
- } else if (Double.class.isAssignableFrom(field.getType())) {
- attribute.setType(AttributeType.FLOAT);
- } else if (Boolean.class.isAssignableFrom(field.getType())) {
- attribute.setType(AttributeType.BOOLEAN);
- } else {
- throw new RuntimeException(
- "Unsupported endpoint attribute type of connector'" + connectorComponentName + "': " + field.getType()
- );
- }
+ Attribute setting = buildConnectorSetting(field, connectorComponentName);
+ settings.add(setting);
+ LOG.fine("Adding connector setting: " + setting);
+ }
+ } catch (NoSuchFieldException ex) {
+ // Ignoring setting if there is no annotated field on endpoint class
+ // TODO: Inheritance of endpoint classes? Do we care?
+ }
+ }
- if (uriParam.label().length() > 0) {
- attribute.getMetadata().addElement(
- new MetadataElement("label", Json.createObject())
- .setType(AttributeType.STRING.getName())
- .setValue(Json.create(uriParam.label()))
- );
- }
+ return settings;
+ }
- if (uriParam.description().length() > 0) {
- attribute.getMetadata().addElement(
- new MetadataElement("description", Json.createObject())
- .setType(AttributeType.STRING.getName())
- .setValue(Json.create(uriParam.description()))
- );
- }
+ protected Attribute buildConnectorSetting(Field field, String connectorComponentName) {
+ UriParam uriParam = field.getAnnotation(UriParam.class);
- if (uriParam.defaultValue().length() > 0) {
- attribute.getMetadata().addElement(
- new MetadataElement("defaultValue", Json.createObject())
- .setType(AttributeType.STRING.getName())
- .setValue(Json.create(uriParam.defaultValue()))
- );
- }
+ String attributeName = uriParam.name().length() != 0 ? uriParam.name() : field.getName();
- if (uriParam.defaultValueNote().length() > 0) {
- attribute.getMetadata().addElement(
- new MetadataElement("defaultValueNote", Json.createObject())
- .setType(AttributeType.STRING.getName())
- .setValue(Json.create(uriParam.defaultValueNote()))
- );
- }
+ Attribute attribute = new Attribute(attributeName, Json.createObject());
+ attribute.setMetadata(new Metadata(Json.createObject()));
- if (field.isAnnotationPresent(NotNull.class)) {
- attribute.getMetadata().addElement(
- new MetadataElement("required", Json.createObject())
- .setType(AttributeType.BOOLEAN.getName())
- .setValue(Json.create(true))
- );
- }
+ if (String.class.isAssignableFrom(field.getType())) {
+ attribute.setType(AttributeType.STRING);
+ } else if (Integer.class.isAssignableFrom(field.getType())) {
+ attribute.setType(AttributeType.INTEGER);
+ } else if (Double.class.isAssignableFrom(field.getType())) {
+ attribute.setType(AttributeType.FLOAT);
+ } else if (Boolean.class.isAssignableFrom(field.getType())) {
+ attribute.setType(AttributeType.BOOLEAN);
+ } else {
+ throw new RuntimeException(
+ "Unsupported endpoint attribute type of connector'" + connectorComponentName + "': " + field.getType()
+ );
+ }
- LOG.fine("Adding connector attribute '" + attributeName + "': " + attribute);
- connector.addAttribute(attribute);
- }
- } catch (NoSuchFieldException ex) {
- // Ignoring config parameter if there is no annotated field on endpoint class
- // TODO: Inheritance of endpoint classes? Do we care?
- }
+ if (uriParam.label().length() > 0) {
+ attribute.getMetadata().addElement(
+ new MetadataElement("label", Json.createObject())
+ .setType(AttributeType.STRING.getName())
+ .setValue(Json.create(uriParam.label()))
+ );
+ }
+
+ if (uriParam.description().length() > 0) {
+ attribute.getMetadata().addElement(
+ new MetadataElement("description", Json.createObject())
+ .setType(AttributeType.STRING.getName())
+ .setValue(Json.create(uriParam.description()))
+ );
+ }
+
+ if (uriParam.defaultValue().length() > 0) {
+ attribute.getMetadata().addElement(
+ new MetadataElement("defaultValue", Json.createObject())
+ .setType(AttributeType.STRING.getName())
+ .setValue(Json.create(uriParam.defaultValue()))
+ );
+ }
+
+ if (uriParam.defaultValueNote().length() > 0) {
+ attribute.getMetadata().addElement(
+ new MetadataElement("defaultValueNote", Json.createObject())
+ .setType(AttributeType.STRING.getName())
+ .setValue(Json.create(uriParam.defaultValueNote()))
+ );
}
+
+ if (field.isAnnotationPresent(NotNull.class)) {
+ attribute.getMetadata().addElement(
+ new MetadataElement("required", Json.createObject())
+ .setType(AttributeType.BOOLEAN.getName())
+ .setValue(Json.create(true))
+ );
+ }
+
+ return attribute;
}
}
diff --git a/manager/server/src/main/java/org/openremote/manager/server/security/TenantResourceImpl.java b/manager/server/src/main/java/org/openremote/manager/server/security/TenantResourceImpl.java
index 7797821a7..5eeb737ec 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/security/TenantResourceImpl.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/security/TenantResourceImpl.java
@@ -21,10 +21,8 @@ package org.openremote.manager.server.security;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.ClientsResource;
-import org.keycloak.admin.client.resource.RolesResource;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
-import org.keycloak.representations.idm.RoleRepresentation;
import org.openremote.container.web.WebResource;
import org.openremote.manager.server.i18n.I18NService;
import org.openremote.manager.shared.http.RequestParams;
@@ -33,19 +31,15 @@ import org.openremote.manager.shared.security.TenantResource;
import org.openremote.manager.shared.validation.ConstraintViolation;
import org.openremote.manager.shared.validation.ConstraintViolationReport;
-import javax.ws.rs.BeanParam;
import javax.ws.rs.ClientErrorException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
-import javax.ws.rs.core.UriBuilder;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
-import static org.openremote.container.util.JsonUtil.convert;
-import static org.openremote.manager.shared.Constants.MANAGER_CLIENT_ID;
+import static org.openremote.container.json.JsonUtil.convert;
import static org.openremote.manager.shared.Constants.MASTER_REALM;
import static org.openremote.manager.shared.validation.ConstraintViolationReport.VIOLATION_EXCEPTION_HEADER;
diff --git a/manager/server/src/main/java/org/openremote/manager/server/security/UserResourceImpl.java b/manager/server/src/main/java/org/openremote/manager/server/security/UserResourceImpl.java
index 0009f46b9..78179f685 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/security/UserResourceImpl.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/security/UserResourceImpl.java
@@ -40,13 +40,12 @@ import javax.ws.rs.ClientErrorException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Logger;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
-import static org.openremote.container.util.JsonUtil.convert;
+import static org.openremote.container.json.JsonUtil.convert;
import static org.openremote.manager.shared.Constants.*;
import static org.openremote.manager.shared.validation.ConstraintViolationReport.VIOLATION_EXCEPTION_HEADER;
diff --git a/manager/server/src/main/resources/META-INF/persistence.xml b/manager/server/src/main/resources/META-INF/persistence.xml
index 428b895cf..354605c7a 100644
--- a/manager/server/src/main/resources/META-INF/persistence.xml
+++ b/manager/server/src/main/resources/META-INF/persistence.xml
@@ -9,7 +9,7 @@
<class>org.openremote.container.persistence</class>
- <class>org.openremote.manager.shared.agent.PersistentAgent</class>
+ <class>org.openremote.manager.shared.agent.Agent</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/agent/Agent.java b/manager/shared/src/main/java/org/openremote/manager/shared/agent/Agent.java
index 75abc5e9c..6798cd3c2 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/agent/Agent.java
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/agent/Agent.java
@@ -1,101 +1,140 @@
+/*
+ * Copyright 2016, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
package org.openremote.manager.shared.agent;
-import elemental.json.Json;
import elemental.json.JsonObject;
-import org.openremote.manager.shared.ngsi.Attribute;
-import org.openremote.manager.shared.ngsi.AttributeType;
-import org.openremote.manager.shared.ngsi.Entity;
-public class Agent extends Entity {
+import javax.persistence.*;
+import javax.validation.constraints.NotNull;
+import java.util.Date;
- // Persisted properties
- public static final String TYPE = "urn:openremote:agent";
- public static final String ATTRIBUTE_NAME = "name";
- public static final String ATTRIBUTE_DESCRIPTION = "description";
- public static final String ATTRIBUTE_CONNECTOR_TYPE = "connectorType";
- public static final String ATTRIBUTE_CONNECTOR_SETTINGS = "connectorSettings";
- public static final String ATTRIBUTE_ENABLED = "enabled";
+import static org.openremote.manager.shared.Constants.PERSISTENCE_UNIQUE_ID_GENERATOR;
- // State properties that must be read from the agent (agent is context provider for these values)
- protected boolean valid;
- protected boolean available;
- protected boolean connected;
+@Entity
+@Table(name = "AGENT")
+public class Agent {
+
+ public static final String NO_CONNECTOR_ASSIGNED_TYPE = "urn:openremote:connector:none";
+ public static final String NO_DESCRIPTION = "-";
+
+ @Id
+ @Column(name = "ID", length = 22)
+ @GeneratedValue(generator = PERSISTENCE_UNIQUE_ID_GENERATOR)
+ protected String id;
+
+ @Version
+ @Column(name = "OBJ_VERSION")
+ protected long version;
+
+ @Temporal(TemporalType.TIMESTAMP)
+ @Column(name = "CREATED_ON", updatable = false, nullable = false)
+ @org.hibernate.annotations.CreationTimestamp
+ protected Date createdOn;
+
+ @NotNull
+ @Column(name = "NAME")
+ protected String name;
+
+ @NotNull
+ @Column(name = "DESCRIPTION")
+ protected String description = NO_DESCRIPTION;
+
+ @NotNull
+ @Column(name = "ENABLED")
+ protected boolean enabled;
+
+ @NotNull
+ @Column(name = "CONNECTOR_TYPE")
+ protected String connectorType = NO_CONNECTOR_ASSIGNED_TYPE;
+
+ @Column(name = "CONNECTOR_SETTINGS", columnDefinition = "json")
+ @org.hibernate.annotations.Type(type = "json")
+ protected JsonObject connectorSettings;
public Agent() {
- super();
- setType(TYPE);
}
- public Agent(JsonObject jsonObject) {
- super(jsonObject);
+ public String getId() {
+ return id;
}
- public String getName() {
- return getAttributeValueAsString(ATTRIBUTE_NAME);
+ public void setId(String id) {
+ this.id = id;
}
- public void setName(String name) {
- Attribute attr = new Attribute(ATTRIBUTE_NAME, AttributeType.STRING, Json.create(name));
- super.addAttribute(attr);
+ public long getVersion() {
+ return version;
}
- public String getDescription() {
- return getAttributeValueAsString(ATTRIBUTE_DESCRIPTION);
+ public Date getCreatedOn() {
+ return createdOn;
}
- public void setDescription(String description) {
- Attribute attr = new Attribute(ATTRIBUTE_DESCRIPTION, AttributeType.STRING, Json.create(description));
- super.addAttribute(attr);
- }
-
- public String getConnectorType() {
- return getAttributeValueAsString(ATTRIBUTE_CONNECTOR_TYPE);
+ public String getName() {
+ return name;
}
- public void setConnectorType(String connectorType) {
- Attribute attr = new Attribute(ATTRIBUTE_CONNECTOR_TYPE, AttributeType.STRING, Json.create(connectorType));
- super.addAttribute(attr);
+ public void setName(String name) {
+ this.name = name;
}
- public JsonObject getConnectorSettings() {
- return getAttributeValueAsObject(ATTRIBUTE_CONNECTOR_SETTINGS);
+ public String getDescription() {
+ return description;
}
- public void setConnectorSettings(JsonObject settings) {
- Attribute attr = new Attribute(ATTRIBUTE_CONNECTOR_SETTINGS, AttributeType.OBJECT, settings);
- super.addAttribute(attr);
+ public void setDescription(String description) {
+ this.description = description;
}
public boolean isEnabled() {
- return getAttributeValueAsBoolean(ATTRIBUTE_ENABLED);
+ return enabled;
}
public void setEnabled(boolean enabled) {
- Attribute attr = new Attribute(ATTRIBUTE_ENABLED, AttributeType.BOOLEAN, Json.create(enabled));
- super.addAttribute(attr);
- }
-
- public boolean isAvailable() {
- return available;
+ this.enabled = enabled;
}
- public void setAvailable(boolean available) {
- this.available = available;
+ public String getConnectorType() {
+ return connectorType;
}
- public boolean isValid() {
- return valid;
+ public void setConnectorType(String connectorType) {
+ this.connectorType = connectorType;
}
- public void setValid(boolean valid) {
- this.valid = valid;
+ public JsonObject getConnectorSettings() {
+ return connectorSettings;
}
- public boolean isConnected() {
- return connected;
+ public void setConnectorSettings(JsonObject connectorSettings) {
+ this.connectorSettings = connectorSettings;
}
- public void setConnected(boolean connected) {
- this.connected = connected;
+ @Override
+ public String toString() {
+ return getClass().getSimpleName() + "{" +
+ "id='" + id + '\'' +
+ ", name='" + name + '\'' +
+ ", enabled=" + enabled +
+ ", connectorType='" + connectorType + '\'' +
+ ", connectorSettings='" + (connectorSettings != null ? connectorSettings.toJson() : "null") + '\'' +
+ '}';
}
}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/agent/AgentResource.java b/manager/shared/src/main/java/org/openremote/manager/shared/agent/AgentResource.java
new file mode 100644
index 000000000..83c4668b4
--- /dev/null
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/agent/AgentResource.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2016, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.manager.shared.agent;
+
+import jsinterop.annotations.JsType;
+import org.openremote.manager.shared.http.RequestParams;
+import org.openremote.manager.shared.http.SuccessStatusCode;
+
+import javax.ws.rs.*;
+
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
+
+@Path("agent")
+@JsType(isNative = true)
+public interface AgentResource {
+
+ @GET
+ @Produces(APPLICATION_JSON)
+ @SuccessStatusCode(200)
+ // TODO Implement admin roles on server
+ //@RolesAllowed({"read:admin"})
+ Agent[] getAll(@BeanParam RequestParams requestParams);
+
+ @GET
+ @Path("{agentId}")
+ @Produces(APPLICATION_JSON)
+ @SuccessStatusCode(200)
+ Agent get(@BeanParam RequestParams requestParams, @PathParam("agentId") String agentId);
+
+ @POST
+ @Consumes(APPLICATION_JSON)
+ @SuccessStatusCode(204)
+ // TODO Implement admin roles on server
+ //@RolesAllowed({"write:admin"})
+ void create(@BeanParam RequestParams requestParams, Agent agent);
+
+ @PUT
+ @Path("{agentId}")
+ @Consumes(APPLICATION_JSON)
+ @SuccessStatusCode(204)
+ // TODO Implement admin roles on server
+ //@RolesAllowed({"read:admin"})
+ void update(@BeanParam RequestParams requestParams, @PathParam("agentId") String agentId, Agent agent);
+
+ @DELETE
+ @Path("{agentId}")
+ @Produces(APPLICATION_JSON)
+ @SuccessStatusCode(204)
+ // TODO Implement admin roles on server
+ //@RolesAllowed({"read:admin"})
+ void delete(@BeanParam RequestParams requestParams, @PathParam("agentId") String agentId);
+}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/agent/PersistentAgent.java b/manager/shared/src/main/java/org/openremote/manager/shared/agent/PersistentAgent.java
deleted file mode 100644
index 0c1bde643..000000000
--- a/manager/shared/src/main/java/org/openremote/manager/shared/agent/PersistentAgent.java
+++ /dev/null
@@ -1,121 +0,0 @@
-package org.openremote.manager.shared.agent;
-
-import elemental.json.JsonObject;
-
-import javax.persistence.*;
-import javax.validation.constraints.NotNull;
-
-import java.util.Date;
-
-import static org.openremote.manager.shared.Constants.PERSISTENCE_UNIQUE_ID_GENERATOR;
-
-@Entity
-@Table(name = "AGENT")
-public class PersistentAgent {
-
- public static final String NO_CONNECTOR_ASSIGNED_TYPE = "urn:openremote:connector:none";
- public static final String NO_DESCRIPTION = "-";
-
- @Id
- @Column(name = "ID", length = 22)
- @GeneratedValue(generator = PERSISTENCE_UNIQUE_ID_GENERATOR)
- protected String id;
-
- @Version
- @Column(name = "OBJ_VERSION")
- protected long version;
-
- @Temporal(TemporalType.TIMESTAMP)
- @Column(name = "CREATED_ON", updatable = false, nullable = false)
- @org.hibernate.annotations.CreationTimestamp
- protected Date createdOn;
-
- @NotNull
- @Column(name = "NAME")
- protected String name;
-
- @NotNull
- @Column(name = "DESCRIPTION")
- protected String description = NO_DESCRIPTION;
-
- @NotNull
- @Column(name = "ENABLED")
- protected boolean enabled;
-
- @NotNull
- @Column(name = "CONNECTOR_TYPE")
- protected String connectorType = NO_CONNECTOR_ASSIGNED_TYPE;
-
- @Column(name = "CONNECTOR_SETTINGS", columnDefinition = "json")
- @org.hibernate.annotations.Type(type = "json")
- protected JsonObject connectorSettings;
-
- public PersistentAgent() {
- }
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- public long getVersion() {
- return version;
- }
-
- public Date getCreatedOn() {
- return createdOn;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getDescription() {
- return description;
- }
-
- public void setDescription(String description) {
- this.description = description;
- }
-
- public boolean isEnabled() {
- return enabled;
- }
-
- public void setEnabled(boolean enabled) {
- this.enabled = enabled;
- }
-
- public String getConnectorType() {
- return connectorType;
- }
-
- public void setConnectorType(String connectorType) {
- this.connectorType = connectorType;
- }
-
- public JsonObject getConnectorSettings() {
- return connectorSettings;
- }
-
- public void setConnectorSettings(JsonObject connectorSettings) {
- this.connectorSettings = connectorSettings;
- }
-
- @Override
- public String toString() {
- return getClass().getSimpleName() + "{" +
- "id='" + id + '\'' +
- ", name='" + name + '\'' +
- ", enabled=" + enabled +
- ", connectorType='" + connectorType + '\'' +
- '}';
- }
-}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/attribute/Attribute.java b/manager/shared/src/main/java/org/openremote/manager/shared/attribute/Attribute.java
new file mode 100644
index 000000000..74b1d7e89
--- /dev/null
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/attribute/Attribute.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2016, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.manager.shared.attribute;
+
+import elemental.json.JsonObject;
+import elemental.json.JsonValue;
+
+public class Attribute {
+
+ final protected String name;
+ final protected JsonObject jsonObject;
+
+ public Attribute(String name) {
+ this.name = name;
+ jsonObject = elemental.json.Json.createObject();
+ }
+
+ public Attribute(String name, AttributeType type) {
+ this(name);
+ setType(type);
+ }
+
+ public Attribute(String name, JsonObject jsonObject) {
+ this.name = name;
+ this.jsonObject = jsonObject;
+ }
+
+ public Attribute(String name, AttributeType type, JsonValue value) {
+ this(name, elemental.json.Json.createObject());
+ setType(type);
+ setValue(value);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public JsonObject getJsonObject() {
+ return jsonObject;
+ }
+
+ public AttributeType getType() {
+ String typeName = jsonObject.hasKey("type") ? jsonObject.get("type").asString() : null;
+ return typeName != null ? AttributeType.fromName(typeName) : null;
+ }
+
+ public Attribute setType(AttributeType type) {
+ jsonObject.put("type", type.getName());
+ return this;
+ }
+
+ public JsonValue getValue() {
+ return jsonObject.hasKey("value") ? jsonObject.get("value") : null;
+ }
+
+ public JsonObject getValueAsObject() {
+ return jsonObject.hasKey("value") ? jsonObject.getObject("value") : null;
+ }
+
+ public Attribute setValue(JsonValue value) {
+ jsonObject.put("value", value);
+ return this;
+ }
+
+ public Metadata getMetadata() {
+ return jsonObject.hasKey("metadata") ? new Metadata(jsonObject.getObject("metadata")) : null;
+ }
+
+ public Attribute setMetadata(Metadata metadata) {
+ jsonObject.put("metadata", metadata.getJsonObject());
+ return this;
+ }
+
+ @Override
+ public String toString() {
+ return jsonObject.toJson();
+ }
+
+}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/attribute/AttributeType.java b/manager/shared/src/main/java/org/openremote/manager/shared/attribute/AttributeType.java
new file mode 100644
index 000000000..972603697
--- /dev/null
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/attribute/AttributeType.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2016, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.manager.shared.attribute;
+
+import elemental.json.JsonType;
+
+public enum AttributeType {
+
+ OBJECT("Object", JsonType.OBJECT),
+ STRING("String", JsonType.STRING),
+ INTEGER("Integer", JsonType.NUMBER),
+ FLOAT("Float", JsonType.NUMBER),
+ BOOLEAN("Boolean", JsonType.BOOLEAN),
+ OBJECT_ARRAY("Object[]", JsonType.ARRAY),
+ STRING_ARRAY("String[]", JsonType.ARRAY),
+ INTEGER_ARRAY("Integer[]", JsonType.ARRAY),
+ FLOAT_ARRAY("Float[]", JsonType.ARRAY),
+ BOOLEAN_ARRAY("Boolean[]", JsonType.ARRAY),
+ DATETIME("DateTime", JsonType.STRING);
+
+ private String name;
+ private JsonType jsonType;
+
+ AttributeType(String name, JsonType jsonType) {
+ this.name = name;
+ this.jsonType = jsonType;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public JsonType getJsonType() {
+ return jsonType;
+ }
+
+ public static AttributeType fromName(String name) {
+ AttributeType[] values = AttributeType.values();
+ for (AttributeType value : values) {
+ if (value.getName().equalsIgnoreCase(name))
+ return value;
+ }
+ return null;
+ }
+}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/attribute/Attributes.java b/manager/shared/src/main/java/org/openremote/manager/shared/attribute/Attributes.java
new file mode 100644
index 000000000..cee84bf3f
--- /dev/null
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/attribute/Attributes.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2016, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.manager.shared.attribute;
+
+import elemental.json.JsonObject;
+
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+public class Attributes {
+
+ final protected JsonObject jsonObject;
+
+ public Attributes() {
+ this(elemental.json.Json.createObject());
+ }
+
+ public Attributes(JsonObject jsonObject) {
+ this.jsonObject = jsonObject;
+ }
+
+ public JsonObject getJsonObject() {
+ return jsonObject;
+ }
+
+ public boolean hasAttribute(String name) {
+ return jsonObject.hasKey(name);
+ }
+
+ public String[] names() {
+ return jsonObject.keys();
+ }
+
+ public Attribute[] get() {
+ Set<Attribute> attributes = new LinkedHashSet<>();
+ String[] keys = jsonObject.keys();
+ for (String key : keys) {
+ if (hasAttribute(key)) {
+ Attribute attribute = new Attribute(key, jsonObject.getObject(key));
+ attributes.add(attribute);
+ }
+ }
+ return attributes.toArray(new Attribute[attributes.size()]);
+ }
+
+ public Attribute get(String name) {
+ return hasAttribute(name) ? new Attribute(name, jsonObject.getObject(name)) : null;
+ }
+
+ public Attributes add(Attribute attribute) {
+ jsonObject.put(attribute.getName(), attribute.getJsonObject());
+ return this;
+ }
+
+ public Attributes remove(String name) {
+ if (hasAttribute(name)) {
+ jsonObject.remove(name);
+ }
+ return this;
+ }
+
+ public Attributes clear() {
+ for (Attribute attribute : get()) {
+ remove(attribute.getName());
+ }
+ return this;
+ }
+
+ @Override
+ public String toString() {
+ return jsonObject.toJson();
+ }
+}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/attribute/Metadata.java b/manager/shared/src/main/java/org/openremote/manager/shared/attribute/Metadata.java
new file mode 100644
index 000000000..c5f50931d
--- /dev/null
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/attribute/Metadata.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2016, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.manager.shared.attribute;
+
+import elemental.json.JsonObject;
+
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+public class Metadata {
+
+ final protected JsonObject jsonObject;
+
+ public Metadata(JsonObject jsonObject) {
+ this.jsonObject = jsonObject;
+ }
+
+ public JsonObject getJsonObject() {
+ return jsonObject;
+ }
+
+ public MetadataElement[] getElements() {
+ Set<MetadataElement> elements = new LinkedHashSet<>();
+
+ String[] keys = jsonObject.keys();
+ for (String key : keys) {
+ MetadataElement element = new MetadataElement(key, jsonObject.getObject(key));
+ elements.add(element);
+ }
+
+ return elements.toArray(new MetadataElement[elements.size()]);
+ }
+
+ public MetadataElement getElement(String name) {
+ return hasElement(name) ? new MetadataElement(name, jsonObject.getObject(name)) : null;
+ }
+
+ public boolean hasElement(String name) {
+ return jsonObject.hasKey(name);
+ }
+
+ public Metadata addElement(MetadataElement element) {
+ jsonObject.put(element.getName(), element.getJsonObject());
+ return this;
+ }
+
+ @Override
+ public String toString() {
+ return jsonObject.toJson();
+ }
+}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/attribute/MetadataElement.java b/manager/shared/src/main/java/org/openremote/manager/shared/attribute/MetadataElement.java
new file mode 100644
index 000000000..708e08770
--- /dev/null
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/attribute/MetadataElement.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2016, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.manager.shared.attribute;
+
+import elemental.json.JsonObject;
+import elemental.json.JsonValue;
+
+public class MetadataElement {
+
+ final protected String name;
+ final protected JsonObject jsonObject;
+
+ public MetadataElement(String name, JsonObject jsonObject) {
+ this.name = name;
+ this.jsonObject = jsonObject;
+ }
+
+ public JsonObject getJsonObject() {
+ return jsonObject;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public JsonValue getValue() {
+ return jsonObject.hasKey("value") ? jsonObject.get("value") : null;
+ }
+
+ public MetadataElement setValue(JsonValue value) {
+ jsonObject.put("value", value);
+ return this;
+ }
+
+ public String getType() {
+ return jsonObject.hasKey("type") ? jsonObject.get("type").asString() : null;
+ }
+
+ public MetadataElement setType(String type) {
+ jsonObject.put("type", type);
+ return this;
+ }
+
+ @Override
+ public String toString() {
+ return jsonObject.toJson();
+ }
+
+}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/connector/Connector.java b/manager/shared/src/main/java/org/openremote/manager/shared/connector/Connector.java
index 3179f1079..b87ac8a36 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/connector/Connector.java
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/connector/Connector.java
@@ -19,22 +19,18 @@
*/
package org.openremote.manager.shared.connector;
-import elemental.json.Json;
import elemental.json.JsonObject;
import org.openremote.manager.shared.agent.Agent;
-import org.openremote.manager.shared.ngsi.Attribute;
-import org.openremote.manager.shared.ngsi.AttributeType;
-import org.openremote.manager.shared.ngsi.Entity;
+import org.openremote.manager.shared.attribute.Attribute;
+import org.openremote.manager.shared.attribute.Attributes;
-import java.util.Arrays;
-import java.util.List;
import java.util.logging.Logger;
/**
* Interface for agent connectors that is used to connect and communicate with agents
* in an agnostic way. (That is, a Camel component with the required endpoints.)
*/
-public class Connector extends Entity {
+public class Connector {
private static final Logger LOG = Logger.getLogger(Connector.class.getName());
@@ -43,89 +39,121 @@ public class Connector extends Entity {
public static final String PROPERTY_SUPPORTS_DISCOVERY = "openremote-connector-supports-discovery";
public static final String PROPERTY_SUPPORTS_INVENTORY = "openremote-connector-supports-inventory";
- public static final String ATTRIBUTE_NAME = "name";
- public static final String ATTRIBUTE_SYNTAX = "syntax";
- public static final String ATTRIBUTE_SUPPORTS_DISCOVERY = "supportsDiscovery";
- public static final String ATTRIBUTE_SUPPORTS_INVENTORY = "supportsInventory";
-
- public static final List<String> IMMUTABLE_ATTRIBUTES = Arrays.asList(
- ATTRIBUTE_NAME, ATTRIBUTE_SYNTAX, ATTRIBUTE_SUPPORTS_DISCOVERY, ATTRIBUTE_SUPPORTS_INVENTORY
- );
+ protected String id;
+ protected String name;
+ protected String type;
+ protected String syntax;
+ protected boolean supportsDiscovery;
+ protected boolean supportsInventory;
+ protected JsonObject settings;
public Connector() {
}
- public Connector(JsonObject jsonObject) {
- super(jsonObject);
+ public Connector(String id) {
+ this.id = id;
+ }
+
+ public Connector(String id, String name, String type) {
+ this(id);
+ this.name = name;
+ this.type = type;
+ }
+
+ public String getId() {
+ return id;
}
- public Connector(String type) {
- super();
- setType(type);
+ public void setId(String id) {
+ this.id = id;
}
public String getName() {
- return getAttributeValueAsString(ATTRIBUTE_NAME);
+ return name;
}
public void setName(String name) {
- Attribute attr = new Attribute(ATTRIBUTE_NAME, AttributeType.STRING, Json.create(name));
- super.addAttribute(attr);
+ this.name = name;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public void setType(String type) {
+ this.type = type;
}
public String getSyntax() {
- return getAttributeValueAsString(ATTRIBUTE_SYNTAX);
+ return syntax;
}
public void setSyntax(String syntax) {
- Attribute attr = new Attribute(ATTRIBUTE_SYNTAX, AttributeType.STRING, Json.create(syntax));
- super.addAttribute(attr);
+ this.syntax = syntax;
}
public boolean isSupportsDiscovery() {
- return getAttributeValueAsBoolean(ATTRIBUTE_SUPPORTS_DISCOVERY);
+ return supportsDiscovery;
}
public void setSupportsDiscovery(boolean supportsDiscovery) {
- Attribute attr = new Attribute(ATTRIBUTE_SUPPORTS_DISCOVERY, AttributeType.BOOLEAN, Json.create(supportsDiscovery));
- super.addAttribute(attr);
+ this.supportsDiscovery = supportsDiscovery;
}
public boolean isSupportsInventory() {
- return getAttributeValueAsBoolean(ATTRIBUTE_SUPPORTS_INVENTORY);
+ return supportsInventory;
}
public void setSupportsInventory(boolean supportsInventory) {
- Attribute attr = new Attribute(ATTRIBUTE_SUPPORTS_INVENTORY, AttributeType.BOOLEAN, Json.create(supportsInventory));
- super.addAttribute(attr);
+ this.supportsInventory = supportsInventory;
}
+ public JsonObject getSettings() {
+ return settings;
+ }
+
+ public void setSettings(JsonObject settings) {
+ this.settings = settings;
+ }
+
+ /**
+ * Copy all settings which have a value into the {@link Agent}, keeping the value
+ * of each attribute but not the attribute metadata.
+ */
public void writeSettings(Agent agent) {
- JsonObject settings = Json.createObject();
- for (Attribute attribute : getAttributes()) {
- if (IMMUTABLE_ATTRIBUTES.contains(attribute.getName()))
+ if (getSettings() == null) {
+ agent.setConnectorSettings(null);
+ return;
+ }
+ Attributes settings = new Attributes(getSettings());
+ Attributes agentConnectorSettings = new Attributes();
+ for (Attribute attribute : settings.get()) {
+ if (attribute.getValue() == null)
continue;
- settings.put(attribute.getName(), attribute.getJsonObject());
+ Attribute agentConnectorAttribute = new Attribute(attribute.getName(), attribute.getType());
+ agentConnectorAttribute.setValue(attribute.getValue());
+ agentConnectorSettings.add(agentConnectorAttribute);
}
- agent.setConnectorSettings(settings);
+ agent.setConnectorSettings(agentConnectorSettings.getJsonObject());
}
+ /**
+ * Read the value for each settings attribute from the {@link Agent}, skipping any
+ * unknown attribute or attribute that doesn't have the right type. The
+ * {@link Agent#connectorSettings} can be "cleaned up" so they match a {@link Connector}
+ * by calling {@link #readSettings(Agent)} followed by {@link #writeSettings(Agent)}.
+ */
public void readSettings(Agent agent) {
- JsonObject settings = agent.getConnectorSettings();
- if (settings == null)
+ if (agent.getConnectorSettings() == null)
return;
- for (Attribute attribute : getAttributes()) {
- if (IMMUTABLE_ATTRIBUTES.contains(attribute.getName()))
- continue;
-
- if (!settings.hasKey(attribute.getName()))
- continue;
-
- Attribute settingsAttribute =
- new Attribute(attribute.getName(), settings.getObject(attribute.getName()));
- if (settingsAttribute.getType().equals(attribute.getType())) {
- attribute.setValue(settingsAttribute.getValue());
+ Attributes settings = new Attributes(getSettings());
+ Attributes agentConnectorSettings = new Attributes(agent.getConnectorSettings());
+ for (Attribute agentConnectorAttribute : agentConnectorSettings.get()) {
+ Attribute settingsAttribute = settings.get(agentConnectorAttribute.getName());
+ if (settingsAttribute != null
+ && settingsAttribute.getType().equals(agentConnectorAttribute.getType())) {
+ settingsAttribute.setValue(agentConnectorAttribute.getValue());
}
}
}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/ngsi/Attribute.java b/manager/shared/src/main/java/org/openremote/manager/shared/ngsi/Attribute.java
index d5dcf3e49..6e673a475 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/ngsi/Attribute.java
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/ngsi/Attribute.java
@@ -21,8 +21,6 @@ package org.openremote.manager.shared.ngsi;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.databind.annotation.JsonSerialize;
-import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import elemental.json.JsonObject;
import elemental.json.JsonValue;
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/security/Credential.java b/manager/shared/src/main/java/org/openremote/manager/shared/security/Credential.java
index 1dddf07ff..892b8de27 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/security/Credential.java
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/security/Credential.java
@@ -21,7 +21,7 @@ package org.openremote.manager.shared.security;
public class Credential {
- final protected String type = "password";
+ protected String type = "password";
protected String value;
protected Boolean temporary;
diff --git a/test/src/main/groovy/org/openremote/test/ContainerTrait.groovy b/test/src/main/groovy/org/openremote/test/ContainerTrait.groovy
index 894a704f7..390f32333 100644
--- a/test/src/main/groovy/org/openremote/test/ContainerTrait.groovy
+++ b/test/src/main/groovy/org/openremote/test/ContainerTrait.groovy
@@ -29,6 +29,7 @@ import org.keycloak.representations.AccessTokenResponse
import org.openremote.container.Container
import org.openremote.container.ContainerService
import org.openremote.container.message.MessageBrokerService
+import org.openremote.container.persistence.PersistenceService
import org.openremote.container.security.AuthForm
import org.openremote.container.security.IdentityService
import org.openremote.container.web.WebClient
@@ -150,6 +151,7 @@ trait ContainerTrait {
new ManagerWebService(),
new ManagerIdentityService(),
new MessageBrokerService(),
+ new PersistenceService(),
new EventService(),
new ConnectorService(),
new AgentService(),