package kz.cafe.client;

import java.io.*;
import javax.xml.bind.*;
import kz.cafe.constants.*;
import kz.cafe.exceptions.*;
import org.apache.commons.logging.*;

public abstract class JRAbstractResponse{
   private static Log log = LogFactory.getLog(JRAbstractResponse.class);

   private static final String LOG = "[JR ABSTRACT] ";

   public String  requestID;
   public Integer operationCode;
   public String  message;





   public JRAbstractResponse(){
      this.operationCode = OperationConstants.SUCCESS;
      this.requestID = Thread.currentThread().getName();
   }










   public JRAbstractResponse(Throwable e){

//      if(e instanceof se.vocab.v4.exceptions.security.SessionInvalidException){
//      }
//      else {
//         log.error(LOG + e, e);
//      }

      log.error(LOG + e, e);



      this.requestID = Thread.currentThread().getName();
      //this.message = e.toString();
      this.operationCode = OperationConstants.UNKNOWN_ERROR;



//      if(e instanceof UserIsAlreadyLoggedException){
//         this.operationCode = OperationConstants.USER_ALREADY_LOGGED;
//      }
//      else if(e instanceof UserExpiredException){
//         this.operationCode = OperationConstants.USER_EXPIRED;
//      }
//      else if(e instanceof UserNotRegisteredException){
//         this.operationCode = OperationConstants.USER_NOT_EXISTS;
//      }
//      else if(e instanceof VT3SecurityException){
//         this.operationCode = OperationConstants.SECURITY_NOT_ENOUGH_RIGHTS;
//      }


      if(e instanceof AbstractException){
         AbstractException e1 = (AbstractException) e;
         this.operationCode = e1.getErrorCode();
         this.message = e1.getsMsg();

//         if(e1 instanceof ServerDataCorruptedException){
//            // ServerDataCorruptedException e2 = (ServerDataCorruptedException)
//            // e1;
//
//            if(e1.getOriginalException() == null){
//            }
//            else{
//               // this.message = e2.getOriginalException().getMessage();
//            }
//         }
      }
      else{
         this.message = e.getMessage();
      }
   }













//   @Override
//   public String toString(){
//      StringBuilder builder = new StringBuilder();
//      builder.append("requestID=");
//      builder.append(requestID);
//      builder.append(", operationCode=");
//      builder.append(operationCode);
//      builder.append(", message=");
//      builder.append(message);
//      return builder.toString();
//   }





   @Override
   public String toString(){
      try{
         JAXBContext context = JAXBContext.newInstance(this.getClass());
         Marshaller marshaller = context.createMarshaller();

         marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

         StringWriter sw = new StringWriter();

         marshaller.marshal(this, sw);

         return sw.toString();
      }
      catch (Throwable e){
         throw new Error("Can not marshall", e);
      }
   }
}

