package br.mp.mprj.portal.portlet.gestaomensagens.service.model;

import br.mp.mprj.portal.portlet.gestaomensagens.service.service.ClpSerializer;
import br.mp.mprj.portal.portlet.gestaomensagens.service.service.EmailDestinatarioLocalServiceUtil;

import com.liferay.portal.kernel.bean.AutoEscapeBeanHandler;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ProxyUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.model.BaseModel;
import com.liferay.portal.model.impl.BaseModelImpl;

import java.io.Serializable;

import java.lang.reflect.Method;

import java.util.HashMap;
import java.util.Map;


public class EmailDestinatarioClp extends BaseModelImpl<EmailDestinatario>
    implements EmailDestinatario {
    private String _uuid;
    private long _emailDestinatarioId;
    private String _email;
    private long _grupoDestinatarioId;
    private BaseModel<?> _emailDestinatarioRemoteModel;
    private Class<?> _clpSerializerClass = br.mp.mprj.portal.portlet.gestaomensagens.service.service.ClpSerializer.class;

    public EmailDestinatarioClp() {
    }

    @Override
    public Class<?> getModelClass() {
        return EmailDestinatario.class;
    }

    @Override
    public String getModelClassName() {
        return EmailDestinatario.class.getName();
    }

    @Override
    public long getPrimaryKey() {
        return _emailDestinatarioId;
    }

    @Override
    public void setPrimaryKey(long primaryKey) {
        setEmailDestinatarioId(primaryKey);
    }

    @Override
    public Serializable getPrimaryKeyObj() {
        return _emailDestinatarioId;
    }

    @Override
    public void setPrimaryKeyObj(Serializable primaryKeyObj) {
        setPrimaryKey(((Long) primaryKeyObj).longValue());
    }

    @Override
    public Map<String, Object> getModelAttributes() {
        Map<String, Object> attributes = new HashMap<String, Object>();

        attributes.put("uuid", getUuid());
        attributes.put("emailDestinatarioId", getEmailDestinatarioId());
        attributes.put("email", getEmail());
        attributes.put("grupoDestinatarioId", getGrupoDestinatarioId());

        return attributes;
    }

    @Override
    public void setModelAttributes(Map<String, Object> attributes) {
        String uuid = (String) attributes.get("uuid");

        if (uuid != null) {
            setUuid(uuid);
        }

        Long emailDestinatarioId = (Long) attributes.get("emailDestinatarioId");

        if (emailDestinatarioId != null) {
            setEmailDestinatarioId(emailDestinatarioId);
        }

        String email = (String) attributes.get("email");

        if (email != null) {
            setEmail(email);
        }

        Long grupoDestinatarioId = (Long) attributes.get("grupoDestinatarioId");

        if (grupoDestinatarioId != null) {
            setGrupoDestinatarioId(grupoDestinatarioId);
        }
    }

    @Override
    public String getUuid() {
        return _uuid;
    }

    @Override
    public void setUuid(String uuid) {
        _uuid = uuid;

        if (_emailDestinatarioRemoteModel != null) {
            try {
                Class<?> clazz = _emailDestinatarioRemoteModel.getClass();

                Method method = clazz.getMethod("setUuid", String.class);

                method.invoke(_emailDestinatarioRemoteModel, uuid);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public long getEmailDestinatarioId() {
        return _emailDestinatarioId;
    }

    @Override
    public void setEmailDestinatarioId(long emailDestinatarioId) {
        _emailDestinatarioId = emailDestinatarioId;

        if (_emailDestinatarioRemoteModel != null) {
            try {
                Class<?> clazz = _emailDestinatarioRemoteModel.getClass();

                Method method = clazz.getMethod("setEmailDestinatarioId",
                        long.class);

                method.invoke(_emailDestinatarioRemoteModel, emailDestinatarioId);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public String getEmail() {
        return _email;
    }

    @Override
    public void setEmail(String email) {
        _email = email;

        if (_emailDestinatarioRemoteModel != null) {
            try {
                Class<?> clazz = _emailDestinatarioRemoteModel.getClass();

                Method method = clazz.getMethod("setEmail", String.class);

                method.invoke(_emailDestinatarioRemoteModel, email);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public long getGrupoDestinatarioId() {
        return _grupoDestinatarioId;
    }

    @Override
    public void setGrupoDestinatarioId(long grupoDestinatarioId) {
        _grupoDestinatarioId = grupoDestinatarioId;

        if (_emailDestinatarioRemoteModel != null) {
            try {
                Class<?> clazz = _emailDestinatarioRemoteModel.getClass();

                Method method = clazz.getMethod("setGrupoDestinatarioId",
                        long.class);

                method.invoke(_emailDestinatarioRemoteModel, grupoDestinatarioId);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    public BaseModel<?> getEmailDestinatarioRemoteModel() {
        return _emailDestinatarioRemoteModel;
    }

    public void setEmailDestinatarioRemoteModel(
        BaseModel<?> emailDestinatarioRemoteModel) {
        _emailDestinatarioRemoteModel = emailDestinatarioRemoteModel;
    }

    public Object invokeOnRemoteModel(String methodName,
        Class<?>[] parameterTypes, Object[] parameterValues)
        throws Exception {
        Object[] remoteParameterValues = new Object[parameterValues.length];

        for (int i = 0; i < parameterValues.length; i++) {
            if (parameterValues[i] != null) {
                remoteParameterValues[i] = ClpSerializer.translateInput(parameterValues[i]);
            }
        }

        Class<?> remoteModelClass = _emailDestinatarioRemoteModel.getClass();

        ClassLoader remoteModelClassLoader = remoteModelClass.getClassLoader();

        Class<?>[] remoteParameterTypes = new Class[parameterTypes.length];

        for (int i = 0; i < parameterTypes.length; i++) {
            if (parameterTypes[i].isPrimitive()) {
                remoteParameterTypes[i] = parameterTypes[i];
            } else {
                String parameterTypeName = parameterTypes[i].getName();

                remoteParameterTypes[i] = remoteModelClassLoader.loadClass(parameterTypeName);
            }
        }

        Method method = remoteModelClass.getMethod(methodName,
                remoteParameterTypes);

        Object returnValue = method.invoke(_emailDestinatarioRemoteModel,
                remoteParameterValues);

        if (returnValue != null) {
            returnValue = ClpSerializer.translateOutput(returnValue);
        }

        return returnValue;
    }

    @Override
    public void persist() throws SystemException {
        if (this.isNew()) {
            EmailDestinatarioLocalServiceUtil.addEmailDestinatario(this);
        } else {
            EmailDestinatarioLocalServiceUtil.updateEmailDestinatario(this);
        }
    }

    @Override
    public EmailDestinatario toEscapedModel() {
        return (EmailDestinatario) ProxyUtil.newProxyInstance(EmailDestinatario.class.getClassLoader(),
            new Class[] { EmailDestinatario.class },
            new AutoEscapeBeanHandler(this));
    }

    @Override
    public Object clone() {
        EmailDestinatarioClp clone = new EmailDestinatarioClp();

        clone.setUuid(getUuid());
        clone.setEmailDestinatarioId(getEmailDestinatarioId());
        clone.setEmail(getEmail());
        clone.setGrupoDestinatarioId(getGrupoDestinatarioId());

        return clone;
    }

    @Override
    public int compareTo(EmailDestinatario emailDestinatario) {
        long primaryKey = emailDestinatario.getPrimaryKey();

        if (getPrimaryKey() < primaryKey) {
            return -1;
        } else if (getPrimaryKey() > primaryKey) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (!(obj instanceof EmailDestinatarioClp)) {
            return false;
        }

        EmailDestinatarioClp emailDestinatario = (EmailDestinatarioClp) obj;

        long primaryKey = emailDestinatario.getPrimaryKey();

        if (getPrimaryKey() == primaryKey) {
            return true;
        } else {
            return false;
        }
    }

    public Class<?> getClpSerializerClass() {
        return _clpSerializerClass;
    }

    @Override
    public int hashCode() {
        return (int) getPrimaryKey();
    }

    @Override
    public String toString() {
        StringBundler sb = new StringBundler(9);

        sb.append("{uuid=");
        sb.append(getUuid());
        sb.append(", emailDestinatarioId=");
        sb.append(getEmailDestinatarioId());
        sb.append(", email=");
        sb.append(getEmail());
        sb.append(", grupoDestinatarioId=");
        sb.append(getGrupoDestinatarioId());
        sb.append("}");

        return sb.toString();
    }

    @Override
    public String toXmlString() {
        StringBundler sb = new StringBundler(16);

        sb.append("<model><model-name>");
        sb.append(
            "br.mp.mprj.portal.portlet.gestaomensagens.service.model.EmailDestinatario");
        sb.append("</model-name>");

        sb.append(
            "<column><column-name>uuid</column-name><column-value><![CDATA[");
        sb.append(getUuid());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>emailDestinatarioId</column-name><column-value><![CDATA[");
        sb.append(getEmailDestinatarioId());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>email</column-name><column-value><![CDATA[");
        sb.append(getEmail());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>grupoDestinatarioId</column-name><column-value><![CDATA[");
        sb.append(getGrupoDestinatarioId());
        sb.append("]]></column-value></column>");

        sb.append("</model>");

        return sb.toString();
    }
}
