privide generic entity factory based on implementation of entity mangager
diff --git a/src/main/java/org/eclipse/mdm/api/base/BaseEntityManager.java b/src/main/java/org/eclipse/mdm/api/base/BaseEntityManager.java
index 85a6487..36a9257 100644
--- a/src/main/java/org/eclipse/mdm/api/base/BaseEntityManager.java
+++ b/src/main/java/org/eclipse/mdm/api/base/BaseEntityManager.java
@@ -13,11 +13,11 @@
import java.util.Optional;
import org.eclipse.mdm.api.base.massdata.ReadRequest;
+import org.eclipse.mdm.api.base.model.BaseEntityFactory;
import org.eclipse.mdm.api.base.model.ContextDescribable;
import org.eclipse.mdm.api.base.model.ContextRoot;
import org.eclipse.mdm.api.base.model.ContextType;
import org.eclipse.mdm.api.base.model.Entity;
-import org.eclipse.mdm.api.base.model.EntityFactory;
import org.eclipse.mdm.api.base.model.Environment;
import org.eclipse.mdm.api.base.model.MeasuredValues;
import org.eclipse.mdm.api.base.model.Measurement;
@@ -31,11 +31,12 @@
* Provides business layer CRUD operations and services (CREATE, READ, UPDATE,
* INSERT).
*
+ * @param <S> Concreted type of the provided entity factory.
* @since 1.0.0
* @author Viktor Stoehr, Gigatronik Ingolstadt GmbH
* @author Sebastian Dirsch, Gigatronik Ingolstadt GmbH
*/
-public interface BaseEntityManager {
+public interface BaseEntityManager<S extends BaseEntityFactory> {
// ======================================================================
// Public methods
@@ -44,10 +45,10 @@
/**
* The returned service creates new entities.
*
+ * @param <S> Concreted type of the provided entity factory.
* @return {@code Optional} is empty if no such service is available.
- * @see EntityFactory
*/
- default Optional<EntityFactory> getEntityFactory() {
+ default Optional<S> getEntityFactory() {
return Optional.empty();
}
diff --git a/src/main/java/org/eclipse/mdm/api/base/EntityManagerFactory.java b/src/main/java/org/eclipse/mdm/api/base/EntityManagerFactory.java
index 546b6a1..df2f35b 100644
--- a/src/main/java/org/eclipse/mdm/api/base/EntityManagerFactory.java
+++ b/src/main/java/org/eclipse/mdm/api/base/EntityManagerFactory.java
@@ -10,6 +10,8 @@
import java.util.Map;
+import org.eclipse.mdm.api.base.model.BaseEntityFactory;
+
/**
* Takes connection parameters and produces a corresponding entity manager.
*
@@ -18,7 +20,7 @@
* @author Viktor Stoehr, Gigatronik Ingolstadt GmbH
* @author Sebastian Dirsch, Gigatronik Ingolstadt GmbH
*/
-public interface EntityManagerFactory<T extends BaseEntityManager> {
+public interface EntityManagerFactory<T extends BaseEntityManager<? extends BaseEntityFactory>> {
// ======================================================================
// Public methods
diff --git a/src/main/java/org/eclipse/mdm/api/base/model/BaseEntityFactory.java b/src/main/java/org/eclipse/mdm/api/base/model/BaseEntityFactory.java
index 56025d1..bbf53fa 100644
--- a/src/main/java/org/eclipse/mdm/api/base/model/BaseEntityFactory.java
+++ b/src/main/java/org/eclipse/mdm/api/base/model/BaseEntityFactory.java
@@ -22,16 +22,36 @@
* @author Viktor Stoehr, Gigatronik Ingolstadt GmbH
* @author Sebastian Dirsch, Gigatronik Ingolstadt GmbH
*/
-public abstract class BaseEntityFactory implements EntityFactory {
+public abstract class BaseEntityFactory {
// ======================================================================
// Public methods
// ======================================================================
/**
- * {@inheritDoc}
+ * Creates a new {@link Channel}. The name of the returned {@code Channel}
+ * is retrieved from given {@link Quantity}.
+ *
+ * @param measurement The parent {@link Measurement}.
+ * @param quantity The {@code Quantity} is used for default initialization.
+ * @return The created {@code Channel} is returned.
+ * @throws IllegalArgumentException Thrown if a related entity is not yet
+ * persisted.
*/
- @Override
+ public Channel createChannel(Measurement measurement, Quantity quantity) {
+ return createChannel(quantity.getDefaultChannelName(), measurement, quantity);
+ }
+
+ /**
+ * Creates a new {@link Channel}.
+ *
+ * @param name Name of the created {@code Channel}.
+ * @param measurement The parent {@link Measurement}.
+ * @param quantity The {@code Quantity} is used for default initialization.
+ * @return The created {@code Channel} is returned.
+ * @throws IllegalArgumentException Thrown if a related entity is not yet
+ * persisted.
+ */
public Channel createChannel(String name, Measurement measurement, Quantity quantity) {
Channel channel = new Channel(createCore(Channel.class));
@@ -57,9 +77,15 @@
}
/**
- * {@inheritDoc}
+ * Creates a new {@link ChannelGroup}.
+ *
+ * @param name Name of the created {@code ChannelGroup}.
+ * @param numberOfValues The number of values per each related {@link Channel}.
+ * @param measurement The parent {@link Measurement}.
+ * @return The created {@code ChannelGroup} is returned.
+ * @throws IllegalArgumentException Thrown if a related entity is not yet
+ * persisted.
*/
- @Override
public ChannelGroup createChannelGroup(String name, int numberOfValues, Measurement measurement) {
if(numberOfValues < 0) {
throw new IllegalArgumentException("Number of values must be equal or greater than 0.");
@@ -79,9 +105,14 @@
}
/**
- * {@inheritDoc}
+ * Creates a new {@link Measurement}.
+ *
+ * @param name Name of the created {@code Measurement}.
+ * @param testStep The parent {@link TestStep}.
+ * @return The created {@code Measurement} is returned.
+ * @throws IllegalArgumentException Thrown if a related entity is not yet
+ * persisted.
*/
- @Override
public Measurement createMeasurement(String name, TestStep testStep) {
Measurement measurement = new Measurement(createCore(Measurement.class));
@@ -102,9 +133,17 @@
}
/**
- * {@inheritDoc}
+ * Creates a new {@link Parameter} with initialized with given value.
+ *
+ * @param name Name of the created {@code Parameter}.
+ * @param value The value of the created {@code Parameter}.
+ * @param unit An optionally related {@link Unit}.
+ * @param parameterSet The parent {@link ParameterSet}.
+ * @return The created {@code Parameter} is returned.
+ * @throws IllegalArgumentException Thrown if a related entity is not yet
+ * persisted.
+ * @see Parameter#setObjectValue(Object, Unit)
*/
- @Override
public Parameter createParameter(String name, Object value, Unit unit, ParameterSet parameterSet) {
if(parameterSet.getParameter(name).isPresent()) {
throw new IllegalArgumentException("Parameter with name '" + name + "' already exists.");
@@ -123,7 +162,6 @@
return parameter;
}
- @Override
public ParameterSet createParameterSet(String name, String version, Measurement measurement) {
ParameterSet parameterSet = new ParameterSet(createCore(ParameterSet.class));
@@ -138,7 +176,6 @@
return parameterSet;
}
- @Override
public ParameterSet createParameterSet(String name, String version, Channel channel) {
ParameterSet parameterSet = new ParameterSet(createCore(ParameterSet.class));
@@ -154,9 +191,11 @@
}
/**
- * {@inheritDoc}
+ * Creates a new {@link PhysicalDimension}.
+ *
+ * @param name Name of the created {@code PhysicalDimension}.
+ * @return The created {@code PhysicalDimension} is returned.
*/
- @Override
public PhysicalDimension createPhysicalDimension(String name) {
PhysicalDimension physicalDimension = new PhysicalDimension(createCore(PhysicalDimension.class));
@@ -174,9 +213,14 @@
}
/**
- * {@inheritDoc}
+ * Creates a new {@link Quantity}.
+ *
+ * @param name Name of the created {@code Quantity}.
+ * @param defaultUnit The default {@link Unit}.
+ * @return The created {@code Quantity} is returned.
+ * @throws IllegalArgumentException Thrown if a related entity is not yet
+ * persisted.
*/
- @Override
public Quantity createQuantity(String name, Unit defaultUnit) {
Quantity quantity = new Quantity(createCore(Quantity.class));
@@ -200,16 +244,22 @@
}
/**
- * {@inheritDoc}
+ * Creates a new {@link Test} with a reference to the logged in {@link
+ * User}, if there is one.
+ *
+ * @param name Name of the created {@code Test}.
+ * @return The created {@code Test} is returned.
+ * @throws IllegalArgumentException Thrown if a related entity is not yet
+ * persisted.
*/
- @Override
- public Test createTest(String name, User responsiblePerson) {
+ public Test createTest(String name) {
Test test = new Test(createCore(Test.class));
// relations
- if(responsiblePerson != null) {
+ Optional<User> responsiblePerson = getLoggedInUser();
+ if(responsiblePerson.isPresent()) {
// may be null if user entities are not available
- getMutableStore(test).set(responsiblePerson);
+ getMutableStore(test).set(responsiblePerson.get());
}
/**
@@ -228,9 +278,14 @@
}
/**
- * {@inheritDoc}
+ * Creates a new {@link TestStep}.
+ *
+ * @param name Name of the created {@code TestStep}.
+ * @param test The parent {@link Test}.
+ * @return The created {@code TestStep} is returned.
+ * @throws IllegalArgumentException Thrown if a related entity is not yet
+ * persisted.
*/
- @Override
// TODO if test already exists sortindex is set to -1
// as soon as test step is written with a negative sort index
// current max index is queried before test step fields are written
@@ -263,9 +318,14 @@
}
/**
- * {@inheritDoc}
+ * Creates a new {@link Unit}.
+ *
+ * @param name Name of the created {@code Unit}.
+ * @param physicalDimension The {@link PhysicalDimension}.
+ * @return The created {@code Unit} is returned.
+ * @throws IllegalArgumentException Thrown if a related entity is not yet
+ * persisted.
*/
- @Override
public Unit createUnit(String name, PhysicalDimension physicalDimension) {
Unit unit = new Unit(createCore(Unit.class));
@@ -282,9 +342,13 @@
}
/**
- * {@inheritDoc}
+ * Creates a new {@link User}.
+ *
+ * @param name Name of the created {@code User}.
+ * @param givenName Given name of the created {@code User}.
+ * @param surname Surname of the created {@code User}.
+ * @return The created {@code User} is returned.
*/
- @Override
public User createUser(String name, String givenName, String surname) {
User user = new User(createCore(User.class));
@@ -317,12 +381,52 @@
return getCore(entity).getPermanentStore();
}
+ protected abstract Optional<User> getLoggedInUser();
+
protected abstract <T extends Entity> Core createCore(Class<T> entityClass);
protected abstract <T extends Entity> Core createCore(Class<T> entityClass, ContextType contextType);
- private final Core getCore(BaseEntity entity) {
+ protected final Core getCore(BaseEntity entity) {
return entity.getCore();
}
+ // ######################################### CONTEXTS #########################################
+
+ protected ContextRoot createContextRoot(String name, ContextType contextType) {
+ ContextRoot contextRoot = new ContextRoot(createCore(ContextRoot.class, contextType));
+
+ // properties
+ contextRoot.setName(name);
+
+ return contextRoot;
+ }
+
+ protected ContextComponent createContextComponent(String name, ContextRoot contextRoot) {
+ ContextComponent contextComponent = new ContextComponent(createCore(name, ContextComponent.class));
+
+ // relations
+ getPermanentStore(contextComponent).set(contextRoot);
+ getChildrenStore(contextRoot).add(contextComponent);
+
+ // properties
+ contextComponent.setName(name);
+
+ return contextComponent;
+ }
+
+ protected ContextSensor createContextSensor(String name, ContextComponent contextComponent) {
+ ContextSensor contextSensor = new ContextSensor(createCore(name, ContextSensor.class));
+ // relations
+ getPermanentStore(contextSensor).set(contextComponent);
+ getChildrenStore(contextComponent).add(contextSensor);
+
+ // properties
+ contextSensor.setName(name);
+
+ return contextSensor;
+ }
+
+ protected abstract <T extends Entity> Core createCore(String name, Class<T> entityClass);
+
}
diff --git a/src/main/java/org/eclipse/mdm/api/base/model/EntityFactory.java b/src/main/java/org/eclipse/mdm/api/base/model/EntityFactory.java
deleted file mode 100644
index f81602c..0000000
--- a/src/main/java/org/eclipse/mdm/api/base/model/EntityFactory.java
+++ /dev/null
@@ -1,173 +0,0 @@
-/*
- * Copyright (c) 2016 Gigatronik Ingolstadt GmbH
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.eclipse.mdm.api.base.model;
-
-/**
- * The entity factory interface.
- *
- * @since 1.0.0
- * @author Viktor Stoehr, Gigatronik Ingolstadt GmbH
- * @author Sebastian Dirsch, Gigatronik Ingolstadt GmbH
- */
-public interface EntityFactory {
-
- // ======================================================================
- // Public methods
- // ======================================================================
-
- /**
- * Creates a new {@link Channel}. The name of the returned {@code Channel}
- * is retrieved from given {@link Quantity}.
- *
- * @param measurement The parent {@link Measurement}.
- * @param quantity The {@code Quantity} is used for default initialization.
- * @return The created {@code Channel} is returned.
- * @throws IllegalArgumentException Thrown if a related entity is not yet
- * persisted.
- */
- default Channel createChannel(Measurement measurement, Quantity quantity) {
- return createChannel(quantity.getDefaultChannelName(), measurement, quantity);
- }
-
- /**
- * Creates a new {@link Channel}.
- *
- * @param name Name of the created {@code Channel}.
- * @param measurement The parent {@link Measurement}.
- * @param quantity The {@code Quantity} is used for default initialization.
- * @return The created {@code Channel} is returned.
- * @throws IllegalArgumentException Thrown if a related entity is not yet
- * persisted.
- */
- Channel createChannel(String name, Measurement measurement, Quantity quantity);
-
- /**
- * Creates a new {@link ChannelGroup}.
- *
- * @param name Name of the created {@code ChannelGroup}.
- * @param numberOfValues The number of values per each related {@link Channel}.
- * @param measurement The parent {@link Measurement}.
- * @return The created {@code ChannelGroup} is returned.
- * @throws IllegalArgumentException Thrown if a related entity is not yet
- * persisted.
- */
- ChannelGroup createChannelGroup(String name, int numberOfValues, Measurement measurement);
-
- // public ContextComponent createContextComponent(String name, EntityType entityType, ContextRoot contextRoot) {
- // // TODO: we need to know the entity type name
- // // in the base application model we do not have templates
- // throw new UnsupportedOperationException();
- // }
- //
- // public ContextRoot createContextRoot(String name, ContextType contextType, TestStep testStep) {
- // throw new UnsupportedOperationException();
- // }
- //
- // public ContextRoot createContextRoot(String name, ContextType contextType, Measurement... measurements) {
- // throw new UnsupportedOperationException();
- // }
- //
- // public ContextSensor createContextSensor(String name, ContextComponent contextComponent) {
- // // TODO: we need to know the entity type name
- // // in the base application model we do not have templates
- // throw new UnsupportedOperationException();
- // }
-
- /**
- * Creates a new {@link Measurement}.
- *
- * @param name Name of the created {@code Measurement}.
- * @param testStep The parent {@link TestStep}.
- * @return The created {@code Measurement} is returned.
- * @throws IllegalArgumentException Thrown if a related entity is not yet
- * persisted.
- */
- Measurement createMeasurement(String name, TestStep testStep);
-
- /**
- * Creates a new {@link Parameter} with initialized with given value.
- *
- * @param name Name of the created {@code Parameter}.
- * @param value The value of the created {@code Parameter}.
- * @param unit An optionally related {@link Unit}.
- * @param parameterSet The parent {@link ParameterSet}.
- * @return The created {@code Parameter} is returned.
- * @throws IllegalArgumentException Thrown if a related entity is not yet
- * persisted.
- * @see Parameter#setObjectValue(Object, Unit)
- */
- Parameter createParameter(String name, Object value, Unit unit, ParameterSet parameterSet);
-
- ParameterSet createParameterSet(String name, String version, Measurement measurement);
-
- ParameterSet createParameterSet(String name, String version, Channel channel);
-
- /**
- * Creates a new {@link PhysicalDimension}.
- *
- * @param name Name of the created {@code PhysicalDimension}.
- * @return The created {@code PhysicalDimension} is returned.
- */
- PhysicalDimension createPhysicalDimension(String name);
-
- /**
- * Creates a new {@link Quantity}.
- *
- * @param name Name of the created {@code Quantity}.
- * @param defaultUnit The default {@link Unit}.
- * @return The created {@code Quantity} is returned.
- * @throws IllegalArgumentException Thrown if a related entity is not yet
- * persisted.
- */
- Quantity createQuantity(String name, Unit defaultUnit);
-
- /**
- * Creates a new {@link Test}.
- *
- * @param name Name of the created {@code Test}.
- * @param responsiblePerson The responsible {@link User}.
- * @return The created {@code Test} is returned.
- * @throws IllegalArgumentException Thrown if a related entity is not yet
- * persisted.
- */
- Test createTest(String name, User responsiblePerson);
-
- /**
- * Creates a new {@link TestStep}.
- *
- * @param name Name of the created {@code TestStep}.
- * @param test The parent {@link Test}.
- * @return The created {@code TestStep} is returned.
- * @throws IllegalArgumentException Thrown if a related entity is not yet
- * persisted.
- */
- TestStep createTestStep(String name, Test test);
-
- /**
- * Creates a new {@link Unit}.
- *
- * @param name Name of the created {@code Unit}.
- * @param physicalDimension The {@link PhysicalDimension}.
- * @return The created {@code Unit} is returned.
- * @throws IllegalArgumentException Thrown if a related entity is not yet
- * persisted.
- */
- Unit createUnit(String name, PhysicalDimension physicalDimension);
-
- /**
- * Creates a new {@link User}.
- *
- * @param name Name of the created {@code User}.
- * @param givenName Given name of the created {@code User}.
- * @param surname Surname of the created {@code User}.
- * @return The created {@code User} is returned.
- */
- User createUser(String name, String givenName, String surname);
-
-}