View Javadoc
1   package ca.uhn.fhir.jpa.util.jsonpatch;
2   
3   /*
4    * #%L
5    * HAPI FHIR JPA Server
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import org.hl7.fhir.instance.model.api.IBaseResource;
24  
25  import com.google.gson.Gson;
26  import com.google.gson.JsonArray;
27  import com.google.gson.JsonElement;
28  import com.google.gson.JsonObject;
29  
30  import ca.uhn.fhir.context.FhirContext;
31  import ca.uhn.fhir.parser.JsonParser;
32  import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
33  import net.riotopsys.json_patch.JsonPatch;
34  import net.riotopsys.json_patch.JsonPath;
35  import net.riotopsys.json_patch.operation.AddOperation;
36  import net.riotopsys.json_patch.operation.MoveOperation;
37  import net.riotopsys.json_patch.operation.RemoveOperation;
38  import net.riotopsys.json_patch.operation.ReplaceOperation;
39  
40  public class JsonPatchUtils {
41  
42  	public static <T extends IBaseResource> T apply(FhirContext theCtx, T theResourceToUpdate, String thePatchBody) {
43  		JsonPatch parsedPatch = new JsonPatch();
44  		
45  		// Parse the patch
46  		Gson gson = JsonParser.newGson();
47  		JsonElement jsonElement = gson.fromJson(thePatchBody, JsonElement.class);
48  		JsonArray array = jsonElement.getAsJsonArray();
49  		for (JsonElement nextElement : array) {
50  			JsonObject nextElementAsObject = (JsonObject) nextElement;
51  			
52  			String opName = nextElementAsObject.get("op").getAsString();
53  			if ("add".equals(opName)) {
54  				AddOperation op = new AddOperation(toPath(nextElementAsObject), nextElementAsObject.get("value"));
55  				parsedPatch.add(op);
56  			} else if ("remove".equals(opName)) {
57  				RemoveOperation op = new RemoveOperation(toPath(nextElementAsObject));
58  				parsedPatch.add(op);
59  			} else if ("replace".equals(opName)) {
60  				ReplaceOperation op = new ReplaceOperation(toPath(nextElementAsObject), nextElementAsObject.get("value"));
61  				parsedPatch.add(op);
62  			} else if ("copy".equals(opName)) {
63  				CopyOperation op = new CopyOperation(toPath(nextElementAsObject), toFromPath(nextElementAsObject));
64  				parsedPatch.add(op);
65  			} else if ("move".equals(opName)) {
66  				MoveOperation op = new MoveOperation(toPath(nextElementAsObject), toFromPath(nextElementAsObject));
67  				parsedPatch.add(op);
68  			} else {
69  				throw new InvalidRequestException("Invalid JSON PATCH operation: " + opName);
70  			}
71  			
72  		}
73  		
74  		@SuppressWarnings("unchecked")
75  		Class<T> clazz = (Class<T>) theResourceToUpdate.getClass();
76  		
77  		JsonElement originalJsonDocument = gson.fromJson(theCtx.newJsonParser().encodeResourceToString(theResourceToUpdate), JsonElement.class);
78  		JsonElement target = parsedPatch.apply(originalJsonDocument);
79  		T retVal = theCtx.newJsonParser().parseResource(clazz, gson.toJson(target));
80  		
81  		return retVal;
82  	}
83  
84  	private static JsonPath toFromPath(JsonObject nextElementAsObject) {
85  		return new JsonPath(nextElementAsObject.get("from").getAsString());
86  	}
87  
88  	private static JsonPath toPath(JsonObject nextElementAsObject) {
89  		return new JsonPath(nextElementAsObject.get("path").getAsString());
90  	}
91  
92  }