package org.jboss.as.console.client.shared.dispatch;

import org.jboss.as.console.client.shared.state.ReloadState;
import org.jboss.dmr.client.ModelNode;
import org.jboss.dmr.client.Property;

import javax.inject.Inject;
import java.util.List;

/**
 * @author Heiko Braun
 * @date 1/17/12
 */
public class DomainResponseProcessor implements ResponseProcessor {

    private static final String RESPONSE_HEADERS = "response-headers";
    private static final String PROCESS_STATE = "process-state";

    private static final String RESTART_REQUIRED = "restart-required";
    private static final String RELOAD_REQUIRED = "reload-required";
    private static final String SERVER_GROUPS = "server-groups";
    private static final String RESPONSE = "response";

    private ReloadState reloadState;

    @Inject
    public DomainResponseProcessor(ReloadState reloadState) {
        this.reloadState = reloadState;
    }

    @Override
    public void process(ModelNode response) {

        parseServerState(response, reloadState);

        reloadState.propagateChanges();

    }

    private static boolean parseServerState(ModelNode response, ReloadState reloadState) {
        boolean staleModel = false;

        if(response.hasDefined(SERVER_GROUPS))
        {
            List<Property> serverGroups = response.get(SERVER_GROUPS).asPropertyList();
            for(Property serverGroup : serverGroups)
            {
                ModelNode serverGroupValue = serverGroup.getValue();

                List<Property> servers = serverGroupValue.asPropertyList();
                for(Property server : servers)
                {
                    ModelNode serverValue = server.getValue();
                    ModelNode serverResponse = serverValue.get(RESPONSE);

                    if(serverResponse.hasDefined(RESPONSE_HEADERS))
                    {
                        List<Property> headers = serverResponse.get(RESPONSE_HEADERS).asPropertyList();
                        for(Property header : headers)
                        {
                            if(PROCESS_STATE.equals(header.getName()))
                            {
                                String headerValue = header.getValue().asString();

                                if(RESTART_REQUIRED.equals(headerValue))
                                {
                                    staleModel=true;
                                    reloadState.setRestartRequired(server.getName(), staleModel);
                                }
                                else if(RELOAD_REQUIRED.equals(headerValue))
                                {
                                    staleModel=true;
                                    reloadState.setReloadRequired(server.getName(), staleModel);
                                }

                                System.out.println(server.getName() +" >> "+staleModel);
                            }
                        }
                    }
                }
            }
        }

        return staleModel;
    }
}

@@ -1516,34 +1516,407 @@
 ty%3E 
-servers = serverGroupValue
+hosts = serverGroupValue.asPropertyList();%0A                for(Property host : hosts)%0A                %7B%0A                    Property hostValue = host.getValue().asProperty();%0A                    final String actualHostName = hostValue.getName();%0A%0A                    //System.out.println(actualHostName +%22 %3E %22+hostValue.getValue());%0A%0A                    List%3CProperty%3E servers = hostValue.getValue()
 .asP
@@ -1922,32 +1922,36 @@
 PropertyList();%0A
+    
                 
@@ -1989,34 +1989,42 @@
                 
+ 
-%7B%0A
+   %7B%0A    
                 
@@ -2094,63 +2094,235 @@
     
-ModelNode serverResponse = serverValue.get(RESPONSE);%0A%0A
+    final String actualServerName = server.getName();%0A%0A                        ModelNode serverResponse = serverValue.get(RESPONSE);%0A                        //System.out.println(actualServerName + %22 %3E %22 + serverResponse);%0A%0A    
     
@@ -2397,34 +2397,42 @@
                 
+   
+ 
 %7B%0A
+    
                 
@@ -2535,32 +2535,36 @@
                 
+    
 for(Property hea
@@ -2594,34 +2594,40 @@
                 
-%7B%0A
+    %7B%0A  
                 
@@ -2638,16 +2638,18 @@
         
+  
 if(PROCE
@@ -2703,34 +2703,40 @@
                 
-%7B%0A
+    %7B%0A  
                 
@@ -2751,16 +2751,18 @@
         
+  
 String h
@@ -2837,16 +2837,20 @@
         
+    
 if(RESTA
@@ -2906,34 +2906,42 @@
                 
+ 
-%7B%0A
+   %7B%0A    
                 
@@ -3005,32 +3005,36 @@
                 
+    
 reloadState.setR
@@ -3048,33 +3048,167 @@
 equired(
-server.getName(),
+%0A                                                %22Host: %22+actualHostName+%22, server: %22+actualServerName,%0A                                               
  staleMo
@@ -3237,34 +3237,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -3356,32 +3356,36 @@
                 
+    
 %7B%0A              
@@ -3398,32 +3398,36 @@
                 
+ 
+   
 staleModel=true;
@@ -3419,32 +3419,36 @@
 taleModel=true;%0A
+    
                 
@@ -3497,32 +3497,167 @@
 equired(
-server.getName()
+%0A                                                %22Host: %22+actualHostName+%22, server: %22+actualServerName%0A                                                
 , staleM
@@ -3687,34 +3687,37 @@
                 
+    
 %7D
-%0A
 %0A               
@@ -3737,64 +3737,9 @@
     
-System.out.println(server.getName() +%22 %3E%3E %22+staleModel);
+%7D
 %0A   

Fix AS7-4570