package com.fp.bpm.actions;

import java.util.List;

import com.fp.bpm.ActionExecutor;
import com.fp.bpm.query.QueryProcessor;
import com.fp.common.fin.MessageGenerator;
import com.fp.dto.Response;
import com.fp.dto.helper.FileHelper;
import com.fp.dto.json.Serializer;
import com.fp.dto.query.QueryRequest;

/**
 * Clase que se encarga del logon del usuario.
 * 
 * @author Santiago Castillo.
 * @version 2.1
 */
public class LogonAction extends ActionExecutor {

    @Override
    public String execute(String pRequest, List<FileHelper> fileItem) throws Exception {
        String[] globaldata = pRequest.split(",");
        String[] datacompany = globaldata[0].split(":");
        String dataAux = globaldata[1].substring(8, globaldata[1].length() - 3);
        String[] data = dataAux.split(":");
        if (data.length != 5) {
            return "";
        }
        String company = datacompany[1];
        String login = data[0];
        String password = data[1].substring(16, 56);
        String jsessionid = data[2];
        String ip = data[3];
        String host = data[4];
        String newRequest = "\"{" + "\"company\":" + company + "," + "\"transactionModule\":\"1\"," + "\"transactionCode\":9998,"
                + "\"transactionVersion\":1," + "\"login\":\"" + login + "\"," + "\"ip\":\"" + ip + "\"," + "\"itemValue\":\"" + password + "\"," + "\"jsessionid\":\"" + jsessionid
                + "\"," + "\"host\":\"" + host + "\"," + "\"TSAFEUSERDETAIL\":{" + "\"automatic\":true,"
                + "\"beanName\":\"com.fp.persistence.pgeneral.safe.TsafeUserDetail\"," + "\"criteria\":[" + "{" + "\"operation\":\"=\","
                + "\"order\":null," + "\"value\":" + company + "," + "\"property\":\"companycode\","
                + "\"transportBeanClass\":\"com.fp.dto.query.QueryCriteria\"" + "}," + "{" + "\"operation\":\"=\"," + "\"order\":null,"
                + "\"value\":\"" + login + "\"," + "\"property\":\"pk.usercode\"," + "\"transportBeanClass\":\"com.fp.dto.query.QueryCriteria\""
                + "}," + "{" + "\"operation\":\"=\"," + "\"order\":null," + "\"value\":\"2999-12-31\"," + "\"property\":\"pk.dateto\","
                + "\"transportBeanClass\":\"com.fp.dto.query.QueryCriteria\"" + "}" + "]," + "\"page\":1," + "\"pageSize\":10,"
                + "\"transportBeanClass\":\"com.fp.dto.query.QueryBean\"," + "\"multirecord\":false" + "}," + "\"querytype\":\"T\"" + "}\"";
        Serializer ser = new Serializer(newRequest);
        QueryRequest qr = (QueryRequest) ser.toObject(QueryRequest.class);
        MessageGenerator mg = MessageGenerator.getInstance();
        qr.setJournalId(mg.generateId(qr.getUser()));
        // Log de transaccion
        super.request = qr;
        super.requestToLog();
        // fin log transaccion
        Response res = new Response(Response.RESPONSE_OK, "OK");
        QueryProcessor qp = new QueryProcessor();
        qr.setResponse(res);
        qp.process(qr);
        ser = new Serializer(res);
        return ser.toJSON();
    }
}
