package org.ovirt.engine.core.bll;

import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.core.common.AuditLogType;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.compat.backendcompat.XmlDocument;
import org.ovirt.engine.core.compat.backendcompat.XmlNode;
import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogDirector;
import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogableBase;
import org.ovirt.engine.core.utils.log.Log;
import org.ovirt.engine.core.utils.log.LogFactory;

public class InstallerMessages {
    private Guid _vdsId;

    public InstallerMessages(Guid vdsId) {
        _vdsId = vdsId;
    }

    public void AddMessage(String message) {
        if (StringUtils.isEmpty(message)) {
            return;
        }
        String[] msgs = message.split("[\\n]", -1);
        if (msgs.length > 1) {
            for (String msg : msgs) {
                AddMessage(msg);
            }
            return;
        }

        if (StringUtils.isNotEmpty(message)) {
            if (message.charAt(0) == '<') {
                try {
                    parseMessage(message);
                } catch (RuntimeException e) {
                    log.errorFormat(
                            "Installation of Host. Received illegal XML from Host. Message: {1}, Exception: {2}",
                            message, e.toString());
                }
            } else {
                log.info("VDS message: " + message);
            }
        }
    }

    private void parseMessage(String message) {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(message);
        XmlNode node = doc.ChildNodes[0];
        if (node != null) {
            StringBuilder sb = new StringBuilder();
            // check status
            AuditLogType logType;
            if (node.Attributes.get("status") == null) {
                logType = AuditLogType.VDS_INSTALL_IN_PROGRESS_WARNING;
            } else if (node.Attributes.get("status").getValue().equals("OK")) {
                logType = AuditLogType.VDS_INSTALL_IN_PROGRESS;
            } else if (node.Attributes.get("status").getValue().equals("WARN")) {
                logType = AuditLogType.VDS_INSTALL_IN_PROGRESS_WARNING;
            } else {
                logType = AuditLogType.VDS_INSTALL_IN_PROGRESS_ERROR;
            }

            if ((node.Attributes.get("component") != null)
                    && (StringUtils.isNotEmpty(node.Attributes.get("component").getValue()))) {
                sb.append("Step: " + node.Attributes.get("component").getValue());
            }

            if ((node.Attributes.get("message") != null)
                    && (StringUtils.isNotEmpty(node.Attributes.get("message").getValue()))) {
                sb.append("; ");
                sb.append("Details: " + node.Attributes.get("message").getValue());
                sb.append(" ");
            }

            if ((node.Attributes.get("result") != null)
                    && (StringUtils.isNotEmpty(node.Attributes.get("result").getValue()))) {
                sb.append(" (" + node.Attributes.get("result").getValue() + ")");
            }
            AuditLogableBase logable = new AuditLogableBase(_vdsId);
            logable.AddCustomValue("Message", StringUtils.stripEnd(sb.toString(), " "));
            AuditLogDirector.log(logable, logType);
        }
    }

    private static Log log = LogFactory.getLog(InstallerMessages.class);
}

@@ -681,20 +681,23 @@
  public 
-void
+boolean
  AddMess
@@ -710,32 +710,63 @@
 ring message) %7B%0A
+        boolean error = false;%0A
         if (Stri
@@ -803,32 +803,38 @@
           return
+ error
 ;%0A        %7D%0A    
@@ -965,16 +965,24 @@
         
+ error =
  AddMess
@@ -989,16 +989,25 @@
 age(msg)
+ %7C%7C error
 ;%0A      
@@ -1032,16 +1032,22 @@
   return
+ error
 ;%0A      
@@ -1183,16 +1183,24 @@
         
+ error =
  parseMe
@@ -1258,24 +1258,58 @@
 eption e) %7B%0A
+                    error = true;%0A
             
@@ -1357,20 +1357,16 @@
         
-    
 %22Install
@@ -1426,33 +1426,13 @@
 e: %7B
-1%7D, Exception: %7B2%7D%22,%0A    
+0%7D%22,%0A
     
@@ -1463,21 +1463,55 @@
 age,
- e.toString()
+%0A                        e%0A                    
 );%0A 
@@ -1617,32 +1617,54 @@
     %7D%0A        %7D%0A
+        return error;%0A
     %7D%0A%0A    priva
@@ -1670,12 +1670,15 @@
 ate 
-void
+boolean
  par
@@ -1697,32 +1697,63 @@
 ring message) %7B%0A
+        boolean error = false;%0A
         XmlDocum
@@ -2435,32 +2435,62 @@
        %7D else %7B%0A
+                error = true;%0A
                 
@@ -3586,24 +3586,47 @@
 );%0A        %7D
+%0A%0A        return error;
 %0A    %7D%0A%0A    

allow distinguish error from non error messages