/*
 * Copyright 2020 B2i Healthcare Pte Ltd, http://b2i.sg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.b2international.snowowl.core.request;

import com.b2international.commons.exceptions.ApiException;
import com.b2international.snowowl.core.api.SnowowlRuntimeException;
import com.b2international.snowowl.core.domain.RepositoryContext;
import com.b2international.snowowl.core.events.Request;
import com.b2international.snowowl.core.internal.locks.DatastoreLockContextDescriptions;
import com.b2international.snowowl.core.locks.Locks;

/**
 * @since 7.5.1
 */
public abstract class LockRequest<C extends RepositoryContext, R> implements Request<C, R> {

	private static final long serialVersionUID = 1L;
		
	protected abstract R doExecute(C context);
	
	private final String lockContext;
	private final String parentLockContext;
	
	protected String lockContext() {
		return lockContext;
	}
	
	protected LockRequest(final String lockContext) {
	  this(lockContext, DatastoreLockContextDescriptions.ROOT);
	}
	
	protected LockRequest(final String lockContext, final String parentLockContext) {
	  this.lockContext = lockContext;
	  this.parentLockContext = parentLockContext;
	}
	
	@Override
	public R execute(C context) {
		try (Locks locks = Locks.on(context).lock(lockContext(), parentLockContext)) {
			return doExecute(context);
		} catch (Exception e) {
			if (e instanceof ApiException) {
				throw (ApiException) e;
			}
			throw SnowowlRuntimeException.wrap(e);
		}
	}

}

@@ -1232,55 +1232,8 @@
 1L;%0A
-%09%09%0A%09protected abstract R doExecute(C context);%0A
 %09%0A%09p
@@ -1312,69 +1312,8 @@
 ;%0A%09%0A
-%09protected String lockContext() %7B%0A%09%09return lockContext;%0A%09%7D%0A%09%0A
 %09pro
@@ -1358,24 +1358,26 @@
 ntext) %7B%0A%09  
+  
 this(lockCon
@@ -1509,24 +1509,26 @@
 ntext) %7B%0A%09  
+  
 this.lockCon
@@ -1550,16 +1550,18 @@
 ext;%0A%09  
+ 
+ 
 this.par
@@ -1601,16 +1601,86 @@
 t;%0A%09%7D%0A%09%0A
+%09protected final String lockContext() %7B%0A%09    return lockContext;%0A%09%7D%0A%09%0A
 %09@Overri
@@ -1967,12 +1967,58 @@
 ;%0A%09%09%7D%0A%09%7D
+%0A%09%0A%09protected abstract R doExecute(C context);
 %0A%0A%7D%0A

Update LockRequest.java