2525import static de .fraunhofer .iosb .ilt .frostclient .model .property .type .TypePrimitive .EDM_STRING ;
2626import static de .fraunhofer .iosb .ilt .frostclient .utils .SpecialNames .AT_IOT_SELF_LINK ;
2727
28+ import de .fraunhofer .iosb .ilt .frostclient .exception .Exceptions ;
2829import de .fraunhofer .iosb .ilt .frostclient .model .property .EntityPropertyMain ;
2930import de .fraunhofer .iosb .ilt .frostclient .model .property .type .TypeComplex ;
3031import de .fraunhofer .iosb .ilt .frostclient .model .property .type .TypePrimitive ;
@@ -46,13 +47,17 @@ public class ModelRegistry {
4647
4748 private static final Logger LOGGER = LoggerFactory .getLogger (ModelRegistry .class .getName ());
4849
50+ public static final String DEFAULT_NAMESPACE = "de.FROST" ;
51+
52+ private final Set <String > namespaces = new LinkedHashSet <>();
53+
4954 /**
5055 * The global EntityProperty SelfLink.
5156 */
5257 public static final EntityPropertyMain <String > EP_SELFLINK = new EntityPropertyMain <String >(AT_IOT_SELF_LINK , EDM_STRING ).setAliases ("selfLink" );
5358
5459 /**
55- * All entity types, by their entityName (both singular and mainSet).
60+ * All entity types, by their name (both singular and mainSet).
5661 */
5762 private final Map <String , EntityType > entityTypesByName = new TreeMap <>();
5863
@@ -78,6 +83,40 @@ public class ModelRegistry {
7883
7984 private boolean initialised ;
8085
86+ private String ensureNamespace (PropertyType type ) {
87+ String namespace = type .getNamespace ();
88+ if (StringHelper .isNullOrEmpty (namespace )) {
89+ namespace = maybeAddNamespace (namespace );
90+ type .setNamespace (namespace );
91+ return namespace ;
92+ }
93+ return maybeAddNamespace (namespace );
94+ }
95+
96+ private String ensureNamespace (EntityType type ) {
97+ String namespace = type .getNamespace ();
98+ if (StringHelper .isNullOrEmpty (namespace )) {
99+ namespace = maybeAddNamespace (namespace );
100+ type .setNamespace (namespace );
101+ return namespace ;
102+ }
103+ return maybeAddNamespace (namespace );
104+ }
105+
106+ public String maybeAddNamespace (String namespace ) {
107+ if (StringHelper .isNullOrEmpty (namespace )) {
108+ namespace = DEFAULT_NAMESPACE ;
109+ }
110+ if (namespaces .add (namespace )) {
111+ LOGGER .info ("Registered namespace {}" , namespace );
112+ }
113+ return namespace ;
114+ }
115+
116+ public Set <String > getNamespaces () {
117+ return namespaces ;
118+ }
119+
81120 /**
82121 * Register a new entity type. Registering the same type twice is a no-op,
83122 * registering a new entity type with a name that already exists causes an
@@ -87,16 +126,15 @@ public class ModelRegistry {
87126 * @return this ModelRegistry.
88127 */
89128 public final ModelRegistry registerEntityType (EntityType type ) {
90- EntityType existing = entityTypesByName .get (type .entityName );
129+ String namespace = ensureNamespace (type );
130+ String fullName = ModelRegistry .fullName (namespace , type .getEntityName ());
131+ EntityType existing = entityTypesByName .get (fullName );
91132 if (existing == type ) {
92- LOGGER .info ("Entity type {} already registered." , type . entityName );
133+ LOGGER .info ("Entity type {} already registered." , fullName );
93134 return this ;
94135 }
95- if (existing != null ) {
96- LOGGER .error ("Duplicate entity type name: {}" , type .entityName );
97- throw new IllegalArgumentException ("An entity type named " + type .entityName + " is already registered" );
98- }
99- entityTypesByName .put (type .entityName , type );
136+ Exceptions .illegalArgumentIf (existing != null , "Duplicate entity type name: {}" , fullName );
137+ entityTypesByName .put (fullName , type );
100138 entityTypes .add (type );
101139 type .setModelRegistry (this );
102140 return this ;
@@ -107,14 +145,38 @@ public final ModelRegistry registerEntityContainer(String name, EntityType type)
107145 return this ;
108146 }
109147
148+ private boolean hasNamespace (String name ) {
149+ return name .contains ("." );
150+ }
151+
152+ public final EntityType getEntityTypeForName (String namespace , String typeName ) {
153+ return getEntityTypeForName (fullName (namespace , typeName ));
154+ }
155+
110156 /**
111157 * Get the entity type with the given name.
112158 *
113159 * @param typeName The name of the entity type to find.
114160 * @return the entity type with the given name, or null.
115161 */
116162 public final EntityType getEntityTypeForName (String typeName ) {
117- final EntityType type = entityTypesByName .get (typeName );
163+ EntityType type = entityTypesByName .get (typeName );
164+ if (type != null ) {
165+ return type ;
166+ }
167+ if (hasNamespace (typeName )) {
168+ LOGGER .info ("No entity type found for name {}" , typeName );
169+ return null ;
170+ }
171+ for (String namespace : namespaces ) {
172+ final String fullName = namespace + '.' + typeName ;
173+ type = entityTypesByName .get (fullName );
174+ if (type != null ) {
175+ LOGGER .info ("Resolved entity type {} to {}" , typeName , fullName );
176+ return type ;
177+ }
178+ }
179+ LOGGER .info ("No entity type found for name {}" , typeName );
118180 return type ;
119181 }
120182
@@ -131,12 +193,27 @@ public EntityType getEntityTypeForContainer(String name) {
131193 }
132194
133195 public ModelRegistry registerPropertyType (PropertyType type ) {
134- propertyTypes .put (type .getName (), type );
196+ ensureNamespace (type );
197+ String fullName = fullName (type .getNamespace (), type .getName ());
198+ propertyTypes .put (fullName , type );
135199 return this ;
136200 }
137201
202+ private PropertyType findPropertyType (String name ) {
203+ if (name .contains ("." )) {
204+ return propertyTypes .get (name );
205+ }
206+ for (var namespace : namespaces ) {
207+ PropertyType type = propertyTypes .get (fullName (namespace , name ));
208+ if (type != null ) {
209+ return type ;
210+ }
211+ }
212+ return null ;
213+ }
214+
138215 public final PropertyType getPropertyType (String name ) {
139- PropertyType type = propertyTypes . get (name );
216+ PropertyType type = findPropertyType (name );
140217 if (type != null ) {
141218 return type ;
142219 }
@@ -187,4 +264,7 @@ public boolean isInitialised() {
187264 return initialised ;
188265 }
189266
267+ public static final String fullName (String namespace , String name ) {
268+ return namespace == null ? name : namespace + '.' + name ;
269+ }
190270}
0 commit comments