/*
 * Copyright 2014 Space Dynamics Laboratory - Utah State University Research Foundation.
 *
 * 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 edu.usu.sdl.openstorefront.web.rest.model;

import edu.usu.sdl.openstorefront.doc.ConsumeField;
import edu.usu.sdl.openstorefront.doc.DataType;
import edu.usu.sdl.openstorefront.storage.model.Component;
import edu.usu.sdl.openstorefront.storage.model.ComponentAttribute;
import edu.usu.sdl.openstorefront.validation.ValidationResult;
import java.util.ArrayList;
import java.util.List;
import javax.validation.constraints.NotNull;

/**
 *
 * @author jlaw
 */
public class RequiredForComponent
{

	@NotNull
	@ConsumeField
	private Component component;

	@NotNull
	@ConsumeField
	@DataType(ComponentAttribute.class)
	private List<ComponentAttribute> attributes = new ArrayList<>();

	public RequiredForComponent()
	{
	}

	public ValidationResult checkForComplete()
	{
		ValidationResult validationResult = new ValidationResult();

//		AttributeServiceImpl attributeService = new AttributeServiceImpl();
//		List<AttributeType> requireds = attributeService.getRequiredAttributes();
//		boolean found = false;
//		for (AttributeType required : requireds) {
//			for (ComponentAttribute attribute : attributes) {
//				if (required.getAttributeType().equals(attribute.getComponentAttributePk().getAttributeType())) {
//					found = true;
//					break;
//				}
//			}
//			if (!found) {
//				return Boolean.FALSE;
//			}
//			found = false;
//		}
		return validationResult;
	}

	public Component getComponent()
	{
		return component;
	}

	public void setComponent(Component component)
	{
		this.component = component;
	}

	public List<ComponentAttribute> getAttributes()
	{
		return attributes;
	}

	public void setAttributes(List<ComponentAttribute> attributes)
	{
		this.attributes = attributes;
	}

}

@@ -800,16 +800,135 @@
 taType;%0A
+import edu.usu.sdl.openstorefront.service.ServiceProxy;%0Aimport edu.usu.sdl.openstorefront.storage.model.AttributeType;%0A
 import e
@@ -978,16 +978,16 @@
 ponent;%0A
-
 import e
@@ -1046,16 +1046,73 @@
 ribute;%0A
+import edu.usu.sdl.openstorefront.validation.RuleResult;%0A
 import e
@@ -1190,24 +1190,50 @@
 .ArrayList;%0A
+import java.util.HashSet;%0A
 import java.
@@ -1239,24 +1239,46 @@
 .util.List;%0A
+import java.util.Set;%0A
 import javax
@@ -1712,82 +1712,59 @@
 );%0A%0A
-//%09%09AttributeServiceImpl attributeS
+%09%09ServiceProxy s
 ervice
+Proxy
  = new 
-Attribute
 Service
-Impl
+Proxy
 ();%0A
-//
+%0A
 %09%09Li
@@ -1789,25 +1789,41 @@
 requireds = 
-a
+serviceProxy.getA
 ttributeServ
@@ -1825,16 +1825,18 @@
 eService
+()
 .getRequ
@@ -1857,37 +1857,57 @@
 ();%0A
-//%09%09boolean found = false;%0A//
+%09%09Set%3CString%3E requiredTypeSet = new HashSet%3C%3E();%0A
 %09%09fo
@@ -1923,24 +1923,29 @@
 uteType 
-required
+attributeType
  : requi
@@ -1952,19 +1952,133 @@
 reds) %7B%0A
-//
 %09
+%09%09requiredTypeSet.add(attributeType.getAttributeType());%0A%09%09%7D%0A%0A%09%09List%3CString%3E matchedAttributes = new ArrayList%3C%3E();%0A
 %09%09for (C
@@ -2125,53 +2125,25 @@
 ) %7B%0A
-//
 %09%09%09
-%09if (required.getAttributeType().equals(
+String type = 
 attr
@@ -2196,139 +2196,546 @@
 pe()
+;%0A%09%09%09if (requiredTypeSet.contains(type
 )) %7B%0A
-//%09
 %09%09%09%09
-found = true;%0A//%09%09%09%09%09break;%0A//
+if (matchedAttributes.add(type)) %7B%0A%09%09%09%09%09matchedAttributes.add(type);%0A
 %09%09%09%09%7D%0A
-//
 %09%09%09%7D%0A
-//%09
+%09%09%7D%0A
 %09%09if (
-!found
+matchedAttributes.size() %3C requiredTypeSet.size()
 ) %7B%0A
-//
 %09%09%09
-%09return Boolean.FALSE;%0A//%09%09%09%7D%0A//%09%09%09found = false;%0A//
+RuleResult ruleResult = new RuleResult();%0A%09%09%09ruleResult.setMessage(%22Missing Required Attributes%22);%0A%09%09%09ruleResult.setEntityClassName(ComponentAttribute.class.getSimpleName());%0A%09%09%09ruleResult.setFieldName(%22componentAttributes%22);%0A%09%09%09ruleResult.setValidationRule(%22Must has required attributes%22);%0A%09%09%09validationResult.getRuleResults().add(ruleResult);%0A
 %09%09%7D%0A

Fix for validation