2020import java .io .IOException ;
2121import java .io .StringWriter ;
2222import java .net .URL ;
23+ import java .util .ArrayList ;
24+ import java .util .Collection ;
2325import java .util .HashMap ;
2426import java .util .Map ;
2527import java .util .Map .Entry ;
3739
3840import com .fasterxml .jackson .core .JsonGenerator ;
3941import com .fasterxml .jackson .core .type .TypeReference ;
42+ import com .fasterxml .jackson .databind .JsonNode ;
4043import com .fasterxml .jackson .databind .node .ObjectNode ;
4144import com .google .common .base .Charsets ;
4245import com .google .common .base .Optional ;
6164import org .dswarm .graph .GraphIndexStatics ;
6265import org .dswarm .graph .GraphProcessingStatics ;
6366import org .dswarm .graph .deprecate .DataModelNeo4jDeprecator ;
67+ import org .dswarm .graph .deprecate .RecordsNeo4jDeprecator ;
6468import org .dswarm .graph .deprecate .RelationshipDeprecator ;
6569import org .dswarm .graph .index .MapDBUtils ;
6670import org .dswarm .graph .index .NamespaceIndex ;
@@ -87,6 +91,8 @@ public class MaintainResource extends GraphResource {
8791
8892 private static final String DEPRECATE_DATA_MODEL_TYPE = "deprecate data model" ;
8993
94+ private static final String DEPRECATE_RECORDS_TYPE = "deprecate records" ;
95+
9096 public MaintainResource () {
9197
9298 }
@@ -121,19 +127,19 @@ public Response deprecateDataModel(final String jsonObjectString, @Context final
121127
122128 final BasicNeo4jProcessor processor = new DataModelNeo4jProcessor (database , tx , namespaceIndex , prefixedDataModelUri );
123129
124- final RelationshipDeprecator statementDeprecator = new DataModelNeo4jDeprecator (processor , true , prefixedDataModelUri );
130+ final RelationshipDeprecator dataModelDeprecator = new DataModelNeo4jDeprecator (processor , true , prefixedDataModelUri );
125131
126- statementDeprecator .work ();
132+ dataModelDeprecator .work ();
127133
128- final int relationshipsDeprecated = statementDeprecator .getRelationshipsDeprecated ();
134+ final int relationshipsDeprecated = dataModelDeprecator .getRelationshipsDeprecated ();
129135
130136 if (relationshipsDeprecated > 0 ) {
131137
132138 // update data model version only when some statements are deprecated the DB
133- statementDeprecator .getVersionHandler ().updateLatestVersion ();
139+ dataModelDeprecator .getVersionHandler ().updateLatestVersion ();
134140 }
135141
136- statementDeprecator .closeTransaction ();
142+ dataModelDeprecator .closeTransaction ();
137143
138144 LOG .info ("deprecated '{}' relationships in data model '{}' ('{}') in graph db" , relationshipsDeprecated , dataModelUri , prefixedDataModelUri );
139145
@@ -144,6 +150,57 @@ public Response deprecateDataModel(final String jsonObjectString, @Context final
144150 return Response .ok (result , MediaType .APPLICATION_JSON_TYPE ).build ();
145151 }
146152
153+ @ POST
154+ @ Path ("/deprecate/records" )
155+ @ Consumes (MediaType .APPLICATION_JSON )
156+ @ Produces (MediaType .APPLICATION_JSON )
157+ public Response deprecateRecords (final String jsonObjectString , @ Context final GraphDatabaseService database ) throws DMPGraphException {
158+
159+ MaintainResource .LOG .info ("try to deprecate records in a data model in graph db" );
160+
161+ final ObjectNode requestJSON = deserializeJSON (jsonObjectString , DEPRECATE_RECORDS_TYPE );
162+
163+ final String dataModelUri = requestJSON .get (DMPStatics .DATA_MODEL_URI_IDENTIFIER ).asText ();
164+
165+ final Collection <String > recordURIs = getRecordURIs (requestJSON );
166+
167+ final TransactionHandler tx = new Neo4jTransactionHandler (database );
168+ final NamespaceIndex namespaceIndex = new NamespaceIndex (database , tx );
169+
170+ final String prefixedDataModelUri = namespaceIndex .createPrefixedURI (dataModelUri );
171+
172+ final Collection <String > prefixedRecordURIs = prefixRecordURIs (recordURIs , namespaceIndex );
173+
174+ MaintainResource .LOG .info ("try to deprecate '{}' records in data model '{}' ('{}') in graph db" , prefixedRecordURIs .size (), dataModelUri ,
175+ prefixedDataModelUri );
176+
177+ final BasicNeo4jProcessor processor = new DataModelNeo4jProcessor (database , tx , namespaceIndex , prefixedDataModelUri );
178+
179+ final RelationshipDeprecator recordsDeprecator = new RecordsNeo4jDeprecator (processor , true , prefixedDataModelUri , prefixedRecordURIs );
180+
181+ recordsDeprecator .work ();
182+
183+ final int relationshipsDeprecated = recordsDeprecator .getRelationshipsDeprecated ();
184+
185+ if (relationshipsDeprecated > 0 ) {
186+
187+ // update data model version only when some statements are deprecated the DB
188+ recordsDeprecator .getVersionHandler ().updateLatestVersion ();
189+ }
190+
191+ recordsDeprecator .closeTransaction ();
192+
193+ LOG .info ("deprecated '{}' records with '{}' relationships in data model '{}' ('{}') in graph db" , prefixedRecordURIs .size (),
194+ relationshipsDeprecated , dataModelUri , prefixedDataModelUri );
195+
196+ final ObjectNode resultJSON = simpleObjectMapper .createObjectNode ();
197+ resultJSON .put ("deprecated" , relationshipsDeprecated );
198+ final String result = serializeJSON (resultJSON , DEPRECATE_DATA_MODEL_TYPE );
199+
200+ return Response .ok (result , MediaType .APPLICATION_JSON_TYPE ).build ();
201+
202+ }
203+
147204 /**
148205 * note utilise this endpoint with care, because it cleans your complete db!
149206 *
@@ -512,4 +569,34 @@ private void initPrefixes(final GraphDatabaseService database) throws DMPGraphEx
512569
513570 MaintainResource .LOG .debug ("finished initialising namespaces index" );
514571 }
572+
573+ private Collection <String > getRecordURIs (final ObjectNode json ) {
574+
575+ final JsonNode recordsNode = json .get (DMPStatics .RECORDS_IDENTIFIER );
576+
577+ final ArrayList <String > recordURIs = new ArrayList <>();
578+
579+ for (final JsonNode recordNode : recordsNode ) {
580+
581+ final String recordURI = recordNode .asText ();
582+
583+ recordURIs .add (recordURI );
584+ }
585+
586+ return recordURIs ;
587+ }
588+
589+ private Collection <String > prefixRecordURIs (final Collection <String > recordURIs , final NamespaceIndex namespaceIndex ) throws DMPGraphException {
590+
591+ final ArrayList <String > prefixedRecordURIs = new ArrayList <>();
592+
593+ for (final String recordURI : recordURIs ) {
594+
595+ final String prefixedRecordURI = namespaceIndex .createPrefixedURI (recordURI );
596+
597+ prefixedRecordURIs .add (prefixedRecordURI );
598+ }
599+
600+ return prefixedRecordURIs ;
601+ }
515602}
0 commit comments