/*
 * Copyright 2017 Wultra s.r.o.
 *
 * 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 io.getlime.security.powerauth.lib.webflow.authentication.mtoken.model.converter;

import io.getlime.security.powerauth.lib.mtoken.model.entity.FormData;
import io.getlime.security.powerauth.lib.nextstep.model.entity.OperationFormData;
import io.getlime.security.powerauth.lib.nextstep.model.entity.attribute.OperationFormFieldAttribute;

/**
 * Converter for the form data objects used for mobile API.
 *
 * @author Petr Dvorak, petr@wultra.com
 */
public class FormDataConverter {

    private AttributeConverter attributeConverter = new AttributeConverter();

    public FormData fromOperationFormData(OperationFormData input) {
        if (input == null) {
            return null;
        }
        FormData result = new FormData();
        result.setTitle(input.getTitle().getMessage());
        result.setMessage(input.getGreeting().getMessage());
        for (OperationFormFieldAttribute attribute : input.getParameters()) {
            result.getAttributes().add(attributeConverter.fromOperationFormFieldAttribute(attribute));
        }
        return result;
    }

}

@@ -739,32 +739,115 @@
 ntity.FormData;%0A
+import io.getlime.security.powerauth.lib.mtoken.model.entity.attributes.Attribute;%0A
 import io.getlim
@@ -1015,16 +1015,40 @@
 ibute;%0A%0A
+import java.util.List;%0A%0A
 /**%0A * C
@@ -1567,180 +1567,822 @@
     
-for (OperationFormFieldAttribute attribute : input.getParameters()) %7B%0A            result.getAttributes().add(attributeConverter.fromOperationFormFieldAttribute(attribute));
+List%3CAttribute%3E attributes = result.getAttributes();%0A        Integer existingIndex = null;%0A        for (OperationFormFieldAttribute attribute : input.getParameters()) %7B%0A            int counter = 0;%0A            for (Attribute attr: attributes) %7B%0A                // Make sure attribute with already existing ID is present only once%0A                if (attr.getId().equals(attribute.getId())) %7B%0A                    existingIndex = counter;%0A                    break;%0A                %7D%0A                counter++;%0A            %7D%0A            Attribute attributeToSave = attributeConverter.fromOperationFormFieldAttribute(attribute);%0A            if (existingIndex != null) %7B%0A                attributes.set(existingIndex, attributeToSave);%0A            %7D else %7B%0A                attributes.add(attributeToSave);%0A            %7D
 %0A   

Fix #388: Mobile API returns multiple attributes with the same ID