private JSONArray mapRelations ( String oid ) { // We want our parsed data for reading JsonSimple formData = parsedFormData ( oid ) ; if ( formData == null ) { log . error ( "Error parsing form data" ) ; return null ; } // And raw data to see existing relations and write new ones JsonSimple rawData = getDataFromStorage ( oid ) ; if ( rawData == null ) { log . error ( "Error reading data from storage" ) ; return null ; } // Existing relationship data JSONArray relations = rawData . writeArray ( "relationships" ) ; boolean changed = false ; // For all configured relationships for ( String baseField : relationFields . keySet ( ) ) { JsonSimple relationConfig = relationFields . get ( baseField ) ; // Find the path we need to look under for our related object List < String > basePath = relationConfig . getStringList ( "path" ) ; if ( basePath == null || basePath . isEmpty ( ) ) { log . error ( "Ignoring invalid relationship '{}'. No 'path'" + " provided in configuration" , baseField ) ; continue ; } // Get our base object Object object = formData . getPath ( basePath . toArray ( ) ) ; if ( object instanceof JsonObject ) { // And process it JsonObject newRelation = lookForRelation ( oid , baseField , relationConfig , new JsonSimple ( ( JsonObject ) object ) ) ; if ( newRelation != null && ! isKnownRelation ( relations , newRelation ) ) { log . info ( "Adding relation: '{}' => '{}'" , baseField , newRelation . get ( "identifier" ) ) ; relations . add ( newRelation ) ; changed = true ; } } // If base path points at an array if ( object instanceof JSONArray ) { // Try every entry for ( Object loopObject : ( JSONArray ) object ) { if ( loopObject instanceof JsonObject ) { JsonObject newRelation = lookForRelation ( oid , baseField , relationConfig , new JsonSimple ( ( JsonObject ) loopObject ) ) ; if ( newRelation != null && ! isKnownRelation ( relations , newRelation ) ) { log . info ( "Adding relation: '{}' => '{}'" , baseField , newRelation . get ( "identifier" ) ) ; relations . add ( newRelation ) ; changed = true ; } } } } } // Do we need to store our object again? if ( changed ) { try { saveObjectData ( rawData , oid ) ; } catch ( TransactionException ex ) { log . error ( "Error updating object '{}' in storage: " , oid , ex ) ; return null ; } } return relations ; }
Map all the relationships buried in this record s data