/**
 * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U <br>
 * This file is part of FI-WARE project.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License.
 * </p>
 * <p>
 * You may obtain a copy of the License at:<br>
 * <br>
 * http://www.apache.org/licenses/LICENSE-2.0
 * </p>
 * <p>
 * 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.
 * </p>
 * <p>
 * See the License for the specific language governing permissions and limitations under the License.
 * </p>
 * <p>
 * For those usages not covered by the Apache version 2.0 License please contact with opensource@tid.es
 * </p>
 */
package com.telefonica.euro_iaas.paasmanager.rest.validation;

import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.telefonica.euro_iaas.paasmanager.exception.InvalidEnvironmentInstanceException;
import com.telefonica.euro_iaas.paasmanager.model.Attribute;
import com.telefonica.euro_iaas.paasmanager.model.dto.ProductReleaseDto;
import com.telefonica.euro_iaas.paasmanager.model.dto.TierDto;
import com.telefonica.euro_iaas.paasmanager.util.SystemPropertiesProvider;

public class ProductValidatorImpl implements ProductValidator {

    private SystemPropertiesProvider systemPropertiesProvider;

    @Override
    public void validateAttributes(TierDto tierDto) throws InvalidEnvironmentInstanceException {
        
        for (ProductReleaseDto p : tierDto.getProductReleaseDtos()) {
            for (Attribute att : p.getPrivateAttributes()) {
                if (att.getType() == null) {
                    att.setType("Plain");
                }

                checkType(att);
                checkValue(att);

            }
        }
    }

    private void checkValue(Attribute att) throws InvalidEnvironmentInstanceException {
        String msg = "Attribute value is incorrect.";
        boolean error=true;
        if (att.getValue().startsWith("IP(") && att.getValue().endsWith(")") && "IP".equals(att.getType())) {
            error = false;
        } else if (att.getValue().startsWith("IPALL(") && att.getValue().endsWith(")") && "IPALL".equals(att.getType())) {
            error = false;
        } else if ("Plain".equals(att.getType())) {
            error = false;
        }
        if (error) {
            throw new InvalidEnvironmentInstanceException(msg);
        }
    }

    private void checkType(Attribute att) throws InvalidEnvironmentInstanceException {
        String msg = "Attribute type is incorrect.";
        String availableTypes = systemPropertiesProvider
                .getProperty(SystemPropertiesProvider.AVAILABLE_ATTRIBUTE_TYPES);

        StringTokenizer st2 = new StringTokenizer(availableTypes, "|");
        boolean error = true;
        while (st2.hasMoreElements()) {
            if (att.getType().equals(st2.nextElement())) {
                error = false;
                break;
            }
        }
        if(error){
            throw new InvalidEnvironmentInstanceException(msg);
        }
        
    }

    public void setSystemPropertiesProvider(SystemPropertiesProvider systemPropertiesProvider) {
        this.systemPropertiesProvider = systemPropertiesProvider;
    }

}

@@ -941,72 +941,8 @@
 zer;
-%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;
 %0A%0Aim
@@ -1543,24 +1543,16 @@
 ption %7B%0A
-        
 %0A       
@@ -1610,24 +1610,80 @@
 seDtos()) %7B%0A
+            if (p.getPrivateAttributes() != null) %7B%0A    
             
@@ -1739,32 +1739,36 @@
                 
+   
+ 
 if (att.getType(
@@ -1772,32 +1772,36 @@
 pe() == null) %7B%0A
+    
                 
@@ -1838,27 +1838,34 @@
             
-%7D%0A%0A
+    %7D%0A%0A   
             
@@ -1864,24 +1864,25 @@
             
+ 
 checkType(at
@@ -1885,16 +1885,20 @@
 e(att);%0A
+    
         
@@ -1919,24 +1919,42 @@
 alue(att);%0A%0A
+                %7D%0A
             
@@ -2135,17 +2135,19 @@
 an error
-=
+ = 
 true;%0A  
@@ -3191,23 +3191,25 @@
       if
+ 
 (error)
+ 
 %7B%0A      
@@ -3276,24 +3276,16 @@
       %7D%0A
-        
 %0A    %7D%0A%0A

fix error