/*
 * 
 */
package com.fp.bpmlib.messages;

import java.util.Map;
import java.util.Map.Entry;

import com.fp.bpmlib.BPMException;
import com.fp.common.messages.MessageUtil;
import com.fp.persistence.commondb.PersistenceHelper;
import com.fp.persistence.pgeneral.message.TgeneMessageTemplatesDesc;
import com.fp.persistence.pgeneral.message.TgeneMessageTemplatesDescKey;

// TODO: Auto-generated Javadoc
/**
 * Class MessageManager encargada de.
 * 
 * @author gfiallos
 */
public class MessageManager {

    /** El valor de msg. */
    private MessageUtil msg;

    /**
     * Crea una nueva instancia de message manager.
     * 
     * @param pMessageCode the message code
     * @param pLanguage the language
     * @param pReferences the references
     * @throws Exception la exception
     */
    public MessageManager(Integer pMessageCode, String pLanguage, Map<String, Object> pReferences) throws Exception {
        TgeneMessageTemplatesDescKey tk = new TgeneMessageTemplatesDescKey(pMessageCode, pLanguage);
        TgeneMessageTemplatesDesc t = TgeneMessageTemplatesDesc.find(PersistenceHelper.getEntityManager(), tk);
        if (t == null) {
            throw new BPMException("BPM-0011", "NO SE HA PODIDO ENCONTRAR LA PLANTILLA {0}", pMessageCode);
        }
        String template = t.getTemplate();
        this.msg = new MessageUtil(pLanguage, template);
        if (pReferences != null) {
            for (Entry<String, Object> e : pReferences.entrySet()) {
                this.msg.setValue(e.getKey(), e.getValue());
            }
        }
    }

    /**
     * Fija el valor de value.
     * 
     * @param pName the name
     * @param pValue the value
     * @throws Exception la exception
     */
    public void setValue(String pName, Object pValue) throws Exception {
        this.msg.setValue(pName, pValue);
    }

    /**
     * Obtiene el valor de message.
     * 
     * @return Valor de message
     * @throws Exception la exception
     */
    public String getMessage() throws Exception {
        return this.msg.getMessage();
    }
}