/*
* MicrocallerServer.java
*
* Created on 14 de julio de 2005, 16:20
*
*/

package com.omatech.utils;

import java.util.Iterator;


public final class MicrocallerServer
{

 public MicrocallerServer()
 {
 }


 public static String resultError(String p_procedure, String p_retcode, String p_errbuff)
 {
   String l_res="";
   l_res=l_res+"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
   l_res=l_res+"<result procedure=\""+p_procedure+"\" value=\"ERROR\" retcode=\""+p_retcode+"\" errbuf=\""+p_errbuff.replaceAll("\"","'")+"\"/>";
   return l_res;
 }

 public static String resultOK(String p_procedure, String p_retcode)
 {
   String l_res="";
   l_res=l_res+"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
   l_res=l_res+"<result procedure=\""+p_procedure+"\" value=\"OK\" retcode=\""+p_retcode+"\"/>";
   return l_res;
 }

 public static String resultOK(String p_procedure, String p_retcode, String p_content)
 {
   String l_res="";
   l_res=l_res+"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
   l_res=l_res+"<result procedure=\""+p_procedure+"\" value=\"OK\" retcode=\""+p_retcode+"\">\n";
   l_res=l_res+"  <content name=\"default\" type=\"text/html\"><![CDATA["+p_content+"]]></content>\n";
   l_res=l_res+"</result>";
      return l_res;
 }

 public static String resultOKHeader(String p_procedure, String p_retcode)
 {
   String l_res="";
   l_res=l_res+"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
   l_res=l_res+"<result procedure=\""+p_procedure+"\" value=\"OK\" retcode=\""+p_retcode+"\">\n";
   return l_res;
 }

 public static String resultOKFooter()
 {
   String l_res="";
   l_res=l_res+"</result>\n";
   return l_res;
 }

 public static String content(String p_name, int p_int_content)
 {
   return(content(p_name, null, null, new Integer(p_int_content).toString()));
 }

 public static String content(String p_name, long p_long_content)
 {
   return(content(p_name, null, null, new Long(p_long_content).toString()));
 }

 public static String content(String p_name, String p_content)
 {
   return(content(p_name, null, null, p_content));
 }

 public static String content(String p_name, String p_type, String p_transformation, String p_content)
 {
   String l_type="text/html";
   if (p_type!=null)
   {
     l_type=p_type;
   }
   return(contentHeader(p_name, l_type, p_transformation)+p_content+contentFooter(l_type));
 }

 public static String repetitiveContents(String p_prefix, String p_counter_name, String p_type, Iterator p_it)
 {
   String l_type="text/html";
   String l_ret="";
   if (p_type!=null)
   {
     l_type=p_type;
   }
   int i=0;
   while (p_it.hasNext())
   {
     l_ret=l_ret+contentHeader(p_prefix+i, l_type, null)+p_it.next().toString()+contentFooter(l_type);
     i++;
   }
   l_ret=l_ret+contentHeader(p_counter_name, l_type, null)+i+contentFooter(l_type);
   return(l_ret);
 }

 public static String CjtContents(String p_cjt_name, String p_type, Iterator p_it)
 {
   String l_type="text/html";
   String l_ret="";
   if (p_type!=null)
   {
     l_type=p_type;
   }

   l_ret=l_ret+"<cjt name=\""+p_cjt_name+"\">";
   int i=0;
   while (p_it.hasNext())
   {
     l_ret=l_ret+"<element id=\""+i+"\">"+contentHeader(i, l_type, null)+p_it.next().toString()+contentFooter(l_type)+"</element>";
     i++;
   }       l_ret=l_ret+"</cjt>";
   return(l_ret);
 }

 public static String contentHeader(String p_name, String p_type, String p_transformation)
 {
   String l_res="";
   if (p_transformation==null)
   {
     l_res=l_res+"  <content name=\""+p_name+"\" type=\""+p_type+"\">\n";
   }
   else
   {
     l_res=l_res+"  <content name=\""+p_name+"\" type=\""+p_type+"\" transformation=\""+p_transformation+"\">\n";
   }
   if (!p_type.equalsIgnoreCase("text/xml"))
   {
     l_res=l_res+"<![CDATA[";
   }
   return l_res;
 }

 public static String contentHeader(int p_name, String p_type, String p_transformation)
 {
   return contentHeader(new Integer(p_name).toString(), p_type, p_transformation);
 }

 public static String contentHeader()
 {
   return contentHeader("default", "text/html", null);
 }

 public static String contentFooter(String p_type)
 {
   String l_res="";
   if (!p_type.equalsIgnoreCase("text/xml"))
   {
     l_res=l_res+"]]>";
   }
   l_res=l_res+"</content>\n";
   return l_res;
 }

 public static String contentFooter()
 {
   return contentFooter("text/html");
 }

} 

