/*
 * Copyright 2011-2016 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.index.change;

import com.b2international.index.revision.RevisionSearcher;
import com.b2international.snowowl.datastore.ICDOCommitChangeSet;
import com.b2international.snowowl.datastore.index.ChangeSetProcessorBase;
import com.b2international.snowowl.snomed.core.mrcm.ConceptModelUtils;
import com.b2international.snowowl.snomed.datastore.snor.SnomedConstraintDocument;
import com.b2international.snowowl.snomed.mrcm.AttributeConstraint;
import com.b2international.snowowl.snomed.mrcm.ConceptModelComponent;
import com.b2international.snowowl.snomed.mrcm.ConceptModelPredicate;
import com.b2international.snowowl.snomed.mrcm.ConceptSetDefinition;
import com.b2international.snowowl.snomed.mrcm.MrcmPackage;
import com.google.common.collect.Iterables;

/**
 * @since 4.3
 */
public class ConstraintChangeProcessor extends ChangeSetProcessorBase {

	public ConstraintChangeProcessor() {
		super("predicate changes");
	}

	@Override
	public void process(ICDOCommitChangeSet commitChangeSet, RevisionSearcher searcher) {

		for (ConceptModelComponent component : Iterables.concat(
				commitChangeSet.getNewComponents(ConceptModelPredicate.class),
				commitChangeSet.getNewComponents(ConceptSetDefinition.class)
				)) {
			final AttributeConstraint constraint = ConceptModelUtils.getContainerConstraint(component);
			indexNewRevision(constraint.cdoID(), SnomedConstraintDocument.builder(constraint).build());
		}

		for (ConceptModelComponent component : Iterables.concat(
				commitChangeSet.getDirtyComponents(ConceptModelPredicate.class),
				commitChangeSet.getDirtyComponents(ConceptSetDefinition.class))) {
			final AttributeConstraint constraint = ConceptModelUtils.getContainerConstraint(component);
			indexChangedRevision(constraint.cdoID(), SnomedConstraintDocument.builder(constraint).build());
		}

		deleteRevisions(SnomedConstraintDocument.class, commitChangeSet.getDetachedComponents(MrcmPackage.Literals.ATTRIBUTE_CONSTRAINT));
	}
	
}

@@ -21,9 +21,9 @@
 -201
-6
+7
  B2i
@@ -689,16 +689,135 @@
 hange;%0A%0A
+import static com.google.common.collect.Sets.newHashSet;%0A%0Aimport java.util.Set;%0A%0Aimport org.eclipse.emf.cdo.CDOState;%0A%0A
 import c
@@ -1811,16 +1811,139 @@
 her) %7B%0A%0A
+%09%09Set%3CAttributeConstraint%3E newConstraints = newHashSet();%0A%09%09Set%3CAttributeConstraint%3E changedConstraints = newHashSet();%0A%09%09%0A
 %09%09for (C
@@ -2128,18 +2128,14 @@
 ass)
-%0A%09%09%09%09
 )) %7B%0A
+%0A
 %09%09%09f
@@ -2233,98 +2233,150 @@
 %09%09%09i
-ndexNewRevision(constraint.cdoID(), SnomedConstraintDocument.builder(constraint).build());
+f (constraint.cdoState().equals(CDOState.NEW)) %7B%0A%09%09%09%09newConstraints.add(constraint);%0A%09%09%09%7D else %7B%0A%09%09%09%09changedConstraints.add(constraint);%09%0A%09%09%09%7D
 %0A%09%09%7D
@@ -2576,16 +2576,20 @@
 ss))) %7B%0A
+%09%09%09%0A
 %09%09%09final
@@ -2671,24 +2671,308 @@
 component);%0A
+%09%09%09changedConstraints.add(constraint);%0A%09%09%7D%0A%09%09%0A%09%09for (AttributeConstraint newConstraint : newConstraints) %7B%0A%09%09%09indexNewRevision(newConstraint.cdoID(), SnomedConstraintDocument.builder(newConstraint).build());%0A%09%09%7D%0A%09%09%0A%09%09for (AttributeConstraint changedConstraint : changedConstraints) %7B%0A
 %09%09%09indexChan
@@ -2984,16 +2984,23 @@
 vision(c
+hangedC
 onstrain
@@ -3044,16 +3044,23 @@
 uilder(c
+hangedC
 onstrain

Fix document indexing issue in ConstraintChangeProcessor