Skip to content

Commit 7e16463

Browse files
committed
Reworked complex properties, extracted overlap with entities
1 parent c74ab3d commit 7e16463

83 files changed

Lines changed: 451 additions & 317 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

FROST-Server.Core.Model/src/main/java/de/fraunhofer/iosb/ilt/frostserver/model/ComplexValue.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
*/
1818
package de.fraunhofer.iosb.ilt.frostserver.model;
1919

20+
import com.fasterxml.jackson.annotation.JsonIgnore;
21+
import de.fraunhofer.iosb.ilt.frostserver.model.core.ContainerType;
2022
import de.fraunhofer.iosb.ilt.frostserver.property.Property;
2123

2224
/**
2325
* Interface that values of complex properties should implement to make it
24-
* easier to access sub-properties.
26+
* easier to access sub-properties. An Entity is also a ComplexValue.
2527
*
2628
* @param <S> The type of the complex value (for fluent API)
2729
*/
@@ -65,4 +67,15 @@ public interface ComplexValue<S extends ComplexValue<S>> {
6567
*/
6668
public S setProperty(String name, Object value);
6769

70+
/**
71+
* Returns true if the property is explicitly set to a value, even if this
72+
* value is null.
73+
*
74+
* @param property the property to check.
75+
* @return true if the property is explicitly set.
76+
*/
77+
public boolean isSetProperty(Property property);
78+
79+
@JsonIgnore
80+
public ContainerType getType();
6881
}

FROST-Server.Core.Model/src/main/java/de/fraunhofer/iosb/ilt/frostserver/model/ComplexValueImpl.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import com.fasterxml.jackson.annotation.JsonAnyGetter;
2121
import com.fasterxml.jackson.annotation.JsonAnySetter;
22-
import com.fasterxml.jackson.annotation.JsonIgnore;
2322
import de.fraunhofer.iosb.ilt.frostserver.property.Property;
2423
import de.fraunhofer.iosb.ilt.frostserver.property.type.TypeComplex;
2524
import java.util.Collections;
@@ -40,7 +39,7 @@ public ComplexValueImpl(TypeComplex type) {
4039
this.type = type;
4140
}
4241

43-
@JsonIgnore
42+
@Override
4443
public TypeComplex getType() {
4544
return type;
4645
}
@@ -80,8 +79,13 @@ public ComplexValueImpl setProperty(String name, Object value) {
8079
return this;
8180
}
8281

82+
@Override
83+
public boolean isSetProperty(Property property) {
84+
return properties.containsKey(property.getJsonName());
85+
}
86+
8387
public static TypeComplex.Instantiator createFor(TypeComplex type) {
84-
return () -> new ComplexValueImpl(type);
88+
return t -> new ComplexValueImpl(t);
8589
}
8690

8791
}

FROST-Server.Core.Model/src/main/java/de/fraunhofer/iosb/ilt/frostserver/model/DefaultEntity.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
import java.util.Set;
3838

3939
/**
40-
*
41-
* @author hylke
40+
* The default entity implementation.
4241
*/
4342
public class DefaultEntity implements Entity {
4443

@@ -132,12 +131,12 @@ public DefaultEntity setSelfLink(String selfLink) {
132131
}
133132

134133
@Override
135-
public EntityType getEntityType() {
134+
public EntityType getType() {
136135
return entityType;
137136
}
138137

139138
@Override
140-
public DefaultEntity setEntityType(EntityType entityType) {
139+
public DefaultEntity setType(EntityType entityType) {
141140
if (this.entityType != null) {
142141
throw new IllegalArgumentException("the type of this entity is alread yet to " + this.entityType.entityName);
143142
}

FROST-Server.Core.Model/src/main/java/de/fraunhofer/iosb/ilt/frostserver/model/EntityChangedMessage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public Entity getEntity() {
145145

146146
public EntityChangedMessage setEntity(Entity entity) {
147147
this.entity = entity;
148-
this.entityType = entity.getEntityType();
148+
this.entityType = entity.getType();
149149
return this;
150150
}
151151

FROST-Server.Core.Model/src/main/java/de/fraunhofer/iosb/ilt/frostserver/model/EntityType.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import static de.fraunhofer.iosb.ilt.frostserver.util.Constants.NOT_IMPLEMENTED_MULTI_VALUE_PK;
2121

22+
import de.fraunhofer.iosb.ilt.frostserver.model.core.ContainerType;
2223
import de.fraunhofer.iosb.ilt.frostserver.model.core.Entity;
2324
import de.fraunhofer.iosb.ilt.frostserver.model.core.EntitySet;
2425
import de.fraunhofer.iosb.ilt.frostserver.model.core.EntityValidator;
@@ -53,10 +54,8 @@
5354

5455
/**
5556
* The types of entities in STA.
56-
*
57-
* @author jab, scf
5857
*/
59-
public class EntityType implements Annotatable, Comparable<EntityType> {
58+
public class EntityType implements Annotatable, Comparable<EntityType>, ContainerType<EntityType> {
6059

6160
private static final Logger LOGGER = LoggerFactory.getLogger(EntityType.class.getName());
6261

@@ -139,6 +138,12 @@ public EntityType(String singular, String plural, boolean adminOnly) {
139138
this.adminOnly = adminOnly;
140139
}
141140

141+
@Override
142+
public boolean isOpenType() {
143+
return false;
144+
}
145+
146+
@Override
142147
public EntityType registerProperty(Property property) {
143148
properties.add(property);
144149
propertiesByName.put(property.getName(), property);
@@ -288,6 +293,12 @@ public Property getProperty(String name) {
288293
return propertiesByName.get(name);
289294
}
290295

296+
@Override
297+
public Map<String, Property> getPropertiesByName() {
298+
return propertiesByName;
299+
}
300+
301+
@Override
291302
public EntityPropertyMain getEntityProperty(String name) {
292303
Property<?> property = propertiesByName.get(name);
293304
if (property instanceof EntityPropertyMain entityPropertyMain) {
@@ -325,7 +336,7 @@ public NavigationPropertyEntitySet getNavigationPropertyEntitySet(String name) {
325336
*
326337
* @return The Set of PROPERTIES that Entities of this type have.
327338
*/
328-
public Set<Property> getPropertySet() {
339+
public Set<Property> getProperties() {
329340
return properties;
330341
}
331342

@@ -334,6 +345,7 @@ public Set<Property> getPropertySet() {
334345
*
335346
* @return The set of Entity properties.
336347
*/
348+
@Override
337349
public Set<EntityPropertyMain> getEntityProperties() {
338350
return entityProperties;
339351
}
@@ -377,7 +389,7 @@ public Set<NavigationPropertyMain<EntitySet>> getNavigationSets() {
377389
* incorrect (i.e. Observation with both a Datastream and a MultiDatastream.
378390
*/
379391
public void validateCreate(Entity entity) throws IncompleteEntityException {
380-
for (Property property : getPropertySet()) {
392+
for (Property property : getProperties()) {
381393
if (entity.isSetProperty(property)) {
382394
if (property.isReadOnly()) {
383395
entity.unsetProperty(property);
@@ -398,7 +410,7 @@ public void validateCreate(Entity entity) throws IncompleteEntityException {
398410
}
399411

400412
public void validateUpdate(Entity entity) throws IncompleteEntityException {
401-
for (Property property : getPropertySet()) {
413+
for (Property property : getProperties()) {
402414
if (!(property instanceof EntityPropertyMain)) {
403415
continue;
404416
}
@@ -419,8 +431,8 @@ public void validateUpdate(Entity entity) throws IncompleteEntityException {
419431

420432
public void setParent(PathElementEntitySet containingSet, Entity entity) throws IncompleteEntityException {
421433
EntityType setType = containingSet.getEntityType();
422-
if (setType != entity.getEntityType()) {
423-
throw new IllegalArgumentException("Set of type " + setType + " can not contain a " + entity.getEntityType());
434+
if (setType != entity.getType()) {
435+
throw new IllegalArgumentException("Set of type " + setType + " can not contain a " + entity.getType());
424436
}
425437
PathElement parent = containingSet.getParent();
426438
if (parent == null) {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (C) 2024 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131
3+
* Karlsruhe, Germany.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package de.fraunhofer.iosb.ilt.frostserver.model.core;
19+
20+
import de.fraunhofer.iosb.ilt.frostserver.property.EntityPropertyMain;
21+
import de.fraunhofer.iosb.ilt.frostserver.property.Property;
22+
import java.util.Map;
23+
import java.util.Set;
24+
25+
/**
26+
* Interface for Complex Types and Entity Types that contain sub-properties.
27+
*
28+
* @param <T> The type of the implementing class, for method chaining.
29+
*/
30+
public interface ContainerType<T extends ContainerType<T>> {
31+
32+
/**
33+
* Register a new Property on this container type.
34+
*
35+
* @param property The property to register.
36+
* @return this.
37+
*/
38+
public T registerProperty(Property property);
39+
40+
/**
41+
* Check if this type allows user-defined properties.
42+
*
43+
* @return true if this is an open type, false otherwise.
44+
*/
45+
public boolean isOpenType();
46+
47+
/**
48+
* Get all the entity properties registered on this container type.
49+
*
50+
* @return all the properties.
51+
*/
52+
public Set<EntityPropertyMain> getEntityProperties();
53+
54+
/**
55+
* Get the entity property with the given name.
56+
*
57+
* @param name The name of the property.
58+
* @return The property with the given name, or null.
59+
*/
60+
public EntityPropertyMain getEntityProperty(String name);
61+
62+
/**
63+
* Get the map of all properties by their name.
64+
*
65+
* @return The name-property map.
66+
*/
67+
public Map<String, Property> getPropertiesByName();
68+
69+
}

FROST-Server.Core.Model/src/main/java/de/fraunhofer/iosb/ilt/frostserver/model/core/Entity.java

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package de.fraunhofer.iosb.ilt.frostserver.model.core;
1919

2020
import com.fasterxml.jackson.annotation.JsonIgnore;
21+
import de.fraunhofer.iosb.ilt.frostserver.model.ComplexValue;
2122
import de.fraunhofer.iosb.ilt.frostserver.model.EntityChangedMessage;
2223
import de.fraunhofer.iosb.ilt.frostserver.model.EntityType;
2324
import de.fraunhofer.iosb.ilt.frostserver.path.PathElementEntity;
@@ -30,11 +31,8 @@
3031

3132
/**
3233
* Interface defining basic methods of an Entity.
33-
*
34-
* @author jab
35-
* @author scf
3634
*/
37-
public interface Entity extends NavigableElement {
35+
public interface Entity extends NavigableElement, ComplexValue<Entity> {
3836

3937
/**
4038
* Get the primary key definition of the (EntityType of the) Entity. This is
@@ -62,11 +60,9 @@ public interface Entity extends NavigableElement {
6260

6361
public Entity setSelfLink(String selfLink);
6462

65-
/**
66-
* @return The type of this entity.
67-
*/
6863
@JsonIgnore
69-
public EntityType getEntityType();
64+
@Override
65+
public EntityType getType();
7066

7167
/**
7268
* Sets the type of the entity if it had not been previously set. This will
@@ -75,25 +71,7 @@ public interface Entity extends NavigableElement {
7571
* @param entityType The type of the entity to set.
7672
* @return this;
7773
*/
78-
public Entity setEntityType(EntityType entityType);
79-
80-
/**
81-
* Returns true if the property is explicitly set to a value, even if this
82-
* value is null.
83-
*
84-
* @param property the property to check.
85-
* @return true if the property is explicitly set.
86-
*/
87-
public boolean isSetProperty(Property property);
88-
89-
/**
90-
* Get the value of the given Property as it is set on this Entity.
91-
*
92-
* @param <P> The type of the value of the property.
93-
* @param property The property to get
94-
* @return The value of the property.
95-
*/
96-
public <P> P getProperty(Property<P> property);
74+
public Entity setType(EntityType entityType);
9775

9876
/**
9977
* Get the value pointed to by the given Path.
@@ -103,15 +81,15 @@ public interface Entity extends NavigableElement {
10381
*/
10482
public Object getProperty(Path path);
10583

106-
/**
107-
* Set the value of the given property.
108-
*
109-
* @param <P> The type of the value of the property.
110-
* @param property The property to set the value for.
111-
* @param value The value to set for the given property.
112-
* @return This.
113-
*/
114-
public <P> Entity setProperty(Property<P> property, P value);
84+
@Override
85+
public default Object getProperty(String name) {
86+
return null;
87+
}
88+
89+
@Override
90+
public default Entity setProperty(String name, Object value) {
91+
return this;
92+
}
11593

11694
public Entity unsetProperty(Property property);
11795

@@ -169,7 +147,7 @@ public default void setEntityPropertiesSet() {
169147
* incorrect (i.e. Observation with both a Datastream and a MultiDatastream.
170148
*/
171149
public default void validateCreate() throws IncompleteEntityException {
172-
getEntityType().validateCreate(this);
150+
getType().validateCreate(this);
173151
}
174152

175153
/**
@@ -182,7 +160,7 @@ public default void validateCreate() throws IncompleteEntityException {
182160
* incorrect (i.e. Observation with both a Datastream and a MultiDatastream.
183161
*/
184162
public default void validateUpdate() throws IncompleteEntityException {
185-
getEntityType().validateUpdate(this);
163+
getType().validateUpdate(this);
186164
}
187165

188166
/**
@@ -206,7 +184,7 @@ public default void validateUpdate() throws IncompleteEntityException {
206184
*/
207185
@JsonIgnore
208186
public default ResourcePath getPath() {
209-
EntityType type = getEntityType();
187+
EntityType type = getType();
210188
PathElementEntity epe = new PathElementEntity(type, null);
211189
epe.setPkValues(getPrimaryKeyValues());
212190
ResourcePath resourcePath = new ResourcePath();

FROST-Server.Core.Model/src/main/java/de/fraunhofer/iosb/ilt/frostserver/model/core/EntityReferenceList.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public List<Entity> resolve(final EntityType reqType, final ModelRegistry mr, fi
4040
return value.stream()
4141
.map(r -> {
4242
final Entity e = r.resolve(mr, isAdmin);
43-
if (reqType != e.getEntityType()) {
44-
throw new IllegalArgumentException("Entity of type " + e.getEntityType() + " can not be added to set of " + reqType);
43+
if (reqType != e.getType()) {
44+
throw new IllegalArgumentException("Entity of type " + e.getType() + " can not be added to set of " + reqType);
4545
}
4646
return e;
4747
})

FROST-Server.Core.Model/src/main/java/de/fraunhofer/iosb/ilt/frostserver/model/core/TimeDefaultValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class TimeDefaultValidator implements EntityValidator {
4040
@Override
4141
public void validate(Entity entity) throws IncompleteEntityException {
4242
if (epTime == null) {
43-
epTime = entity.getEntityType().getEntityProperty(propertyName);
43+
epTime = entity.getType().getEntityProperty(propertyName);
4444
}
4545
if (entity.getProperty(epTime) == null) {
4646
entity.setProperty(epTime, new TimeInstant(Moment.nowInSystemTime()));

0 commit comments

Comments
 (0)