package com.fp.general.security.rules;

import com.fp.dto.rules.TransactionRule;
import com.fp.dto.save.SaveRequest;
import com.fp.persistence.commondb.PersistenceHelper;
import com.fp.persistence.commondb.helper.FormatDates;
import com.fp.persistence.pgeneral.safe.TsafeUserSession;
import com.fp.persistence.pgeneral.safe.TsafeUserSessionKey;

/**
 * Clase que se encarga de caducar un registro
 * @author scastillo
 */
public class CloseSession extends TransactionRule {

    /**
     * Metodo que caduca el registro vigente del inicio de sesion
     * @param pSaveRequest
     * @return
     * @throws Exception
     */
    public SaveRequest normalProcess(SaveRequest pSaveRequest) throws Exception {
        String usercode = pSaveRequest.getUser();
        TsafeUserSessionKey tsafeUserSessionKey = new TsafeUserSessionKey(usercode, FormatDates.getDefaultExpiryTimestamp());
        TsafeUserSession tsafeUserSession = TsafeUserSession.find(PersistenceHelper.getEntityManager(), tsafeUserSessionKey);
        if (tsafeUserSession != null) {
            PersistenceHelper.expire(tsafeUserSession);
        }
        return pSaveRequest;
    }

    /**
     * Metodo q ue se ejecuta en modo reverso
     * @param pSaveRequest
     * @return
     * @throws Exception
     */
    public SaveRequest reverseProcess(SaveRequest pSaveRequest) throws Exception {
        return pSaveRequest;
    }
}
