package com.nedap.archie.adlparser.modelconstraints;

import com.nedap.archie.aom.CAttribute;
import com.nedap.archie.aom.CComplexObject;
import com.nedap.archie.aom.CObject;

import java.util.Stack;

/**
 * Imposes constraint on an ArchetypeModel that come from the model structure that is being described by this archetype
 *
 * Required because according to the ADL 2 specification some fields in the archetype inherit cardinality and isMultiple
 * fields from the actual model.
 *
 * As instances could be quite expensive to create, any implementing classes should be thread-safe.
 *
 *
 * Created by pieter.bos on 04/11/15.
 */
public interface ModelConstraintImposer {

    /**
     * Get the default attribute specs for a specific model, with a given typeId and attribute
     * @param typeId
     * @param attribute
     * @return
     */
    CAttribute getDefaultAttribute(String typeId, String attribute);


    default void imposeConstraints(CComplexObject rootNode) {
        if(rootNode == null) {
            return;
        }
        Stack<CObject> workList = new Stack<>();
        workList.add(rootNode);
        while(!workList.isEmpty()) {
            CObject object = workList.pop();
            for(CAttribute attribute:object.getAttributes()) {
                CAttribute defaultAttribute = getDefaultAttribute(object.getRmTypeName(), attribute.getRmAttributeName());
                if(defaultAttribute == null) {
                    //TODO: log
                    System.out.println("could not find attribute for " + object.getRmTypeName() + "." + attribute.getRmAttributeName());
                } else {
                    attribute.setMultiple(defaultAttribute.isMultiple());
                    if (attribute.getCardinality() == null) {
                        attribute.setCardinality(defaultAttribute.getCardinality());
                    }
                }
                for (CObject child : attribute.getChildren()) {
                    workList.push(child);
                }
            }
        }
    }
}

@@ -167,16 +167,73 @@
 CObject;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -726,16 +726,92 @@
 oser %7B%0A%0A
+    Logger logger = LoggerFactory.getLogger(ModelConstraintImposer.class);%0A%0A
     /**%0A
@@ -1389,24 +1389,335 @@
 ibutes()) %7B%0A
+                //if a differential path is present, we cannot set any default values because we do not know what kind of%0A                // object this is. This is only known after flattening, and it will be handled correclty by the Flattener%0A                if(attribute.getDifferentialPath() == null) %7B%0A%0A    
             
@@ -1843,16 +1843,20 @@
         
+    
 if(defau
@@ -1870,32 +1870,36 @@
 bute == null) %7B%0A
+    
                 
@@ -1938,26 +1938,23 @@
     
-System.out.println
+    logger.info
 (%22co
@@ -2064,16 +2064,20 @@
         
+    
 %7D else %7B
@@ -2077,16 +2077,20 @@
  else %7B%0A
+    
         
@@ -2151,24 +2151,28 @@
 ultiple());%0A
+    
             
@@ -2237,32 +2237,36 @@
                 
+    
 attribute.setCar
@@ -2341,32 +2341,51 @@
                %7D
+%0A%0A                %7D
 %0A               

Fix attributes with differential paths in ModelConstraintImposer