/*
 * Copyright 2017-2018 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.snomed.datastore.request;

import com.b2international.snowowl.core.date.DateFormats;
import com.b2international.snowowl.core.date.EffectiveTimes;
import com.b2international.snowowl.core.domain.TransactionContext;
import com.b2international.snowowl.snomed.common.SnomedRf2Headers;
import com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry;

/**
 * @since 5.0
 */
final class SnomedModuleDependencyMemberUpdateDelegate extends SnomedRefSetMemberUpdateDelegate {

	SnomedModuleDependencyMemberUpdateDelegate(SnomedRefSetMemberUpdateRequest request) {
		super(request);
	}

	@Override
	boolean execute(SnomedRefSetMemberIndexEntry original, SnomedRefSetMemberIndexEntry.Builder member, TransactionContext context) {

		boolean changed = false;

		if (hasProperty(SnomedRf2Headers.FIELD_SOURCE_EFFECTIVE_TIME)) {
			long newSourceEffectiveTime = EffectiveTimes.parse(getProperty(SnomedRf2Headers.FIELD_SOURCE_EFFECTIVE_TIME), DateFormats.SHORT).getTime();
			if (newSourceEffectiveTime != original.getSourceEffectiveTime()) {
				member.field(SnomedRf2Headers.FIELD_SOURCE_EFFECTIVE_TIME, newSourceEffectiveTime);
				changed |= true;
			}
		}

		if (hasProperty(SnomedRf2Headers.FIELD_TARGET_EFFECTIVE_TIME)) {
			long newTargetEffectiveTime = EffectiveTimes.parse(getProperty(SnomedRf2Headers.FIELD_TARGET_EFFECTIVE_TIME), DateFormats.SHORT).getTime();
			if (newTargetEffectiveTime != original.getTargetEffectiveTime()) {
				member.field(SnomedRf2Headers.FIELD_TARGET_EFFECTIVE_TIME, newTargetEffectiveTime);
				changed |= true;
			}
		}

		return changed;
	}

}

@@ -684,16 +684,66 @@
 quest;%0A%0A
+import java.util.Date;%0Aimport java.util.Objects;%0A%0A
 import c
@@ -1549,28 +1549,28 @@
 TIME)) %7B%0A%09%09%09
-long
+Date
  newSourceEf
@@ -1681,34 +1681,119 @@
 s.SHORT)
-.get
+;%0A%09%09%09Date currentSourceEffectiveTime = EffectiveTimes.toDate(original.getSourceEffective
 Time()
+)
 ;%0A%09%09%09if 
 (newSour
@@ -1780,24 +1780,40 @@
 e());%0A%09%09%09if 
+(!Objects.equals
 (newSourceEf
@@ -1823,31 +1823,24 @@
 tiveTime
- != original.ge
+, curren
 tSourceE
@@ -1843,33 +1843,32 @@
 rceEffectiveTime
-(
 )) %7B%0A%09%09%09%09member.
@@ -1919,16 +1919,48 @@
 E_TIME, 
+EffectiveTimes.getEffectiveTime(
 newSourc
@@ -1966,32 +1966,33 @@
 ceEffectiveTime)
+)
 ;%0A%09%09%09%09changed %7C=
@@ -2082,12 +2082,12 @@
 %0A%09%09%09
-long
+Date
  new
@@ -2210,26 +2210,111 @@
 ORT)
-.get
+;%0A%09%09%09Date currentTargetEffectiveTime = EffectiveTimes.toDate(original.getTargetEffective
 Time()
+)
 ;%0A%09%09%09if 
 (new
@@ -2309,16 +2309,32 @@
 ;%0A%09%09%09if 
+(!Objects.equals
 (newTarg
@@ -2352,23 +2352,16 @@
 Time
- != original.ge
+, curren
 tTar
@@ -2376,17 +2376,16 @@
 tiveTime
-(
 )) %7B%0A%09%09%09
@@ -2444,16 +2444,48 @@
 E_TIME, 
+EffectiveTimes.getEffectiveTime(
 newTarge
@@ -2499,16 +2499,17 @@
 iveTime)
+)
 ;%0A%09%09%09%09ch

fix SnomedModuleDependencyMemberUpdateDelegate bug