11/*
22 * SysML v2 REST/HTTP Pilot Implementation
3- * Copyright (C) 2021-2022 Twingineer LLC
3+ * Copyright (C) 2021-2023 Twingineer LLC
44 *
55 * This program is free software: you can redistribute it and/or modify
66 * it under the terms of the GNU Lesser General Public License as published by
2020
2121package dao .impl .jpa ;
2222
23+ import com .fasterxml .jackson .databind .JsonNode ;
2324import com .google .common .collect .Streams ;
2425import config .MetamodelProvider ;
2526import dao .DataDao ;
4041import org .omg .sysml .lifecycle .impl .*;
4142import org .omg .sysml .query .*;
4243import org .omg .sysml .query .impl .QueryImpl ;
44+ import play .libs .Json ;
4345
4446import javax .inject .Inject ;
4547import javax .persistence .EntityManager ;
4850import javax .persistence .TypedQuery ;
4951import javax .persistence .criteria .*;
5052import java .beans .PropertyDescriptor ;
53+ import java .io .IOException ;
5154import java .util .*;
5255import java .util .concurrent .ConcurrentHashMap ;
5356import java .util .function .Function ;
@@ -232,10 +235,23 @@ else if (constraint instanceof PrimitiveConstraint) {
232235 return data -> {
233236 Object actualValue ;
234237 Object constrainedValue ;
238+
239+ JsonNode constrainedValueJson ;
240+ try {
241+ constrainedValueJson = primitiveConstraint .getValue () != null ?
242+ Json .mapper ().readTree (primitiveConstraint .getValue ()) :
243+ null ;
244+ } catch (IOException e ) {
245+ throw new IllegalArgumentException (e );
246+ }
247+
235248 switch (primitiveConstraint .getProperty ()) {
236249 case "@id" :
237250 actualValue = data .getId ();
238- constrainedValue = JavaBeanHelper .convert (primitiveConstraint .getValue (), UUID .class );
251+ constrainedValue = JavaBeanHelper .convert (
252+ constrainedValueJson != null ? constrainedValueJson .asText () : null ,
253+ UUID .class
254+ );
239255 break ;
240256 case "@type" :
241257 try {
@@ -246,19 +262,36 @@ else if (constraint instanceof PrimitiveConstraint) {
246262 } catch (ClassNotFoundException e ) {
247263 throw new IllegalStateException (e );
248264 }
249- constrainedValue = primitiveConstraint . getValue () ;
265+ constrainedValue = constrainedValueJson != null ? constrainedValueJson . asText () : null ;
250266 break ;
251267 default :
252268 PropertyDescriptor property = JavaBeanHelper .getBeanProperties (data ).get (primitiveConstraint .getProperty ());
253269 if (property == null ) {
254270 return false ;
255271 }
256272 if (SUPPORTED_PRIMITIVE_CONSTRAINT_CLASSES .stream ()
257- .noneMatch (supported -> supported .isAssignableFrom (property .getPropertyType ()))) {
273+ .anyMatch (supported -> supported .isAssignableFrom (property .getPropertyType ()))) {
274+ actualValue = JavaBeanHelper .getBeanPropertyValue (data , property );
275+ constrainedValue = JavaBeanHelper .convert (
276+ constrainedValueJson != null ? constrainedValueJson .asText () : null ,
277+ property .getPropertyType ()
278+ );
279+ } else if (Data .class .isAssignableFrom (property .getPropertyType ())) {
280+ Object _actualValue = JavaBeanHelper .getBeanPropertyValue (data , property );
281+ actualValue = _actualValue != null ? ((Data ) _actualValue ).getId () : null ;
282+ constrainedValue = constrainedValueJson != null ?
283+ JavaBeanHelper .convert (
284+ // intentionally `textValue` instead of `asText` to get a null value for
285+ // improved error reporting
286+ constrainedValueJson .path ("@id" ).textValue (),
287+ UUID .class
288+ ) : null ;
289+ if (constrainedValue == null ) {
290+ throw new IllegalArgumentException ();
291+ }
292+ } else {
258293 return false ;
259294 }
260- actualValue = JavaBeanHelper .getBeanPropertyValue (data , property );
261- constrainedValue = JavaBeanHelper .convert (primitiveConstraint .getValue (), property .getPropertyType ());
262295 break ;
263296 }
264297 if (actualValue == null || constrainedValue == null ) {
0 commit comments