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.TemplateLocalServiceUtil;

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 com.liferay.portal.util.PortalUtil;

import java.io.Serializable;

import java.lang.reflect.Method;

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


public class TemplateClp extends BaseModelImpl<Template> implements Template {
    private String _uuid;
    private long _templateId;
    private long _companyId;
    private long _groupId;
    private long _userId;
    private String _userUuid;
    private String _nome;
    private int _estilo;
    private boolean _ativo;
    private String _cabecalho;
    private String _rodape;
    private String _corpo;
    private Date _dataCriacao;
    private Date _dataAtualizacao;
    private long _thumbnailFileEntryId;
    private String _thumbnailURL;
    private BaseModel<?> _templateRemoteModel;
    private Class<?> _clpSerializerClass = br.mp.mprj.portal.portlet.gestaomensagens.service.service.ClpSerializer.class;

    public TemplateClp() {
    }

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

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

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

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

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

    @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("templateId", getTemplateId());
        attributes.put("companyId", getCompanyId());
        attributes.put("groupId", getGroupId());
        attributes.put("userId", getUserId());
        attributes.put("nome", getNome());
        attributes.put("estilo", getEstilo());
        attributes.put("ativo", getAtivo());
        attributes.put("cabecalho", getCabecalho());
        attributes.put("rodape", getRodape());
        attributes.put("corpo", getCorpo());
        attributes.put("dataCriacao", getDataCriacao());
        attributes.put("dataAtualizacao", getDataAtualizacao());
        attributes.put("thumbnailFileEntryId", getThumbnailFileEntryId());
        attributes.put("thumbnailURL", getThumbnailURL());

        return attributes;
    }

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

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

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

        if (templateId != null) {
            setTemplateId(templateId);
        }

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

        if (companyId != null) {
            setCompanyId(companyId);
        }

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

        if (groupId != null) {
            setGroupId(groupId);
        }

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

        if (userId != null) {
            setUserId(userId);
        }

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

        if (nome != null) {
            setNome(nome);
        }

        Integer estilo = (Integer) attributes.get("estilo");

        if (estilo != null) {
            setEstilo(estilo);
        }

        Boolean ativo = (Boolean) attributes.get("ativo");

        if (ativo != null) {
            setAtivo(ativo);
        }

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

        if (cabecalho != null) {
            setCabecalho(cabecalho);
        }

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

        if (rodape != null) {
            setRodape(rodape);
        }

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

        if (corpo != null) {
            setCorpo(corpo);
        }

        Date dataCriacao = (Date) attributes.get("dataCriacao");

        if (dataCriacao != null) {
            setDataCriacao(dataCriacao);
        }

        Date dataAtualizacao = (Date) attributes.get("dataAtualizacao");

        if (dataAtualizacao != null) {
            setDataAtualizacao(dataAtualizacao);
        }

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

        if (thumbnailFileEntryId != null) {
            setThumbnailFileEntryId(thumbnailFileEntryId);
        }

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

        if (thumbnailURL != null) {
            setThumbnailURL(thumbnailURL);
        }
    }

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

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

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

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

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

    @Override
    public long getTemplateId() {
        return _templateId;
    }

    @Override
    public void setTemplateId(long templateId) {
        _templateId = templateId;

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

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

                method.invoke(_templateRemoteModel, templateId);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public long getCompanyId() {
        return _companyId;
    }

    @Override
    public void setCompanyId(long companyId) {
        _companyId = companyId;

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

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

                method.invoke(_templateRemoteModel, companyId);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public long getGroupId() {
        return _groupId;
    }

    @Override
    public void setGroupId(long groupId) {
        _groupId = groupId;

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

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

                method.invoke(_templateRemoteModel, groupId);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public long getUserId() {
        return _userId;
    }

    @Override
    public void setUserId(long userId) {
        _userId = userId;

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

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

                method.invoke(_templateRemoteModel, userId);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public String getUserUuid() throws SystemException {
        return PortalUtil.getUserValue(getUserId(), "uuid", _userUuid);
    }

    @Override
    public void setUserUuid(String userUuid) {
        _userUuid = userUuid;
    }

    @Override
    public String getNome() {
        return _nome;
    }

    @Override
    public void setNome(String nome) {
        _nome = nome;

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

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

                method.invoke(_templateRemoteModel, nome);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public int getEstilo() {
        return _estilo;
    }

    @Override
    public void setEstilo(int estilo) {
        _estilo = estilo;

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

                Method method = clazz.getMethod("setEstilo", int.class);

                method.invoke(_templateRemoteModel, estilo);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public boolean getAtivo() {
        return _ativo;
    }

    @Override
    public boolean isAtivo() {
        return _ativo;
    }

    @Override
    public void setAtivo(boolean ativo) {
        _ativo = ativo;

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

                Method method = clazz.getMethod("setAtivo", boolean.class);

                method.invoke(_templateRemoteModel, ativo);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public String getCabecalho() {
        return _cabecalho;
    }

    @Override
    public void setCabecalho(String cabecalho) {
        _cabecalho = cabecalho;

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

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

                method.invoke(_templateRemoteModel, cabecalho);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public String getRodape() {
        return _rodape;
    }

    @Override
    public void setRodape(String rodape) {
        _rodape = rodape;

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

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

                method.invoke(_templateRemoteModel, rodape);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public String getCorpo() {
        return _corpo;
    }

    @Override
    public void setCorpo(String corpo) {
        _corpo = corpo;

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

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

                method.invoke(_templateRemoteModel, corpo);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public Date getDataCriacao() {
        return _dataCriacao;
    }

    @Override
    public void setDataCriacao(Date dataCriacao) {
        _dataCriacao = dataCriacao;

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

                Method method = clazz.getMethod("setDataCriacao", Date.class);

                method.invoke(_templateRemoteModel, dataCriacao);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public Date getDataAtualizacao() {
        return _dataAtualizacao;
    }

    @Override
    public void setDataAtualizacao(Date dataAtualizacao) {
        _dataAtualizacao = dataAtualizacao;

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

                Method method = clazz.getMethod("setDataAtualizacao", Date.class);

                method.invoke(_templateRemoteModel, dataAtualizacao);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public long getThumbnailFileEntryId() {
        return _thumbnailFileEntryId;
    }

    @Override
    public void setThumbnailFileEntryId(long thumbnailFileEntryId) {
        _thumbnailFileEntryId = thumbnailFileEntryId;

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

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

                method.invoke(_templateRemoteModel, thumbnailFileEntryId);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    @Override
    public String getThumbnailURL() {
        return _thumbnailURL;
    }

    @Override
    public void setThumbnailURL(String thumbnailURL) {
        _thumbnailURL = thumbnailURL;

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

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

                method.invoke(_templateRemoteModel, thumbnailURL);
            } catch (Exception e) {
                throw new UnsupportedOperationException(e);
            }
        }
    }

    public BaseModel<?> getTemplateRemoteModel() {
        return _templateRemoteModel;
    }

    public void setTemplateRemoteModel(BaseModel<?> templateRemoteModel) {
        _templateRemoteModel = templateRemoteModel;
    }

    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 = _templateRemoteModel.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(_templateRemoteModel,
                remoteParameterValues);

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

        return returnValue;
    }

    @Override
    public void persist() throws SystemException {
        if (this.isNew()) {
            TemplateLocalServiceUtil.addTemplate(this);
        } else {
            TemplateLocalServiceUtil.updateTemplate(this);
        }
    }

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

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

        clone.setUuid(getUuid());
        clone.setTemplateId(getTemplateId());
        clone.setCompanyId(getCompanyId());
        clone.setGroupId(getGroupId());
        clone.setUserId(getUserId());
        clone.setNome(getNome());
        clone.setEstilo(getEstilo());
        clone.setAtivo(getAtivo());
        clone.setCabecalho(getCabecalho());
        clone.setRodape(getRodape());
        clone.setCorpo(getCorpo());
        clone.setDataCriacao(getDataCriacao());
        clone.setDataAtualizacao(getDataAtualizacao());
        clone.setThumbnailFileEntryId(getThumbnailFileEntryId());
        clone.setThumbnailURL(getThumbnailURL());

        return clone;
    }

    @Override
    public int compareTo(Template template) {
        long primaryKey = template.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 TemplateClp)) {
            return false;
        }

        TemplateClp template = (TemplateClp) obj;

        long primaryKey = template.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(31);

        sb.append("{uuid=");
        sb.append(getUuid());
        sb.append(", templateId=");
        sb.append(getTemplateId());
        sb.append(", companyId=");
        sb.append(getCompanyId());
        sb.append(", groupId=");
        sb.append(getGroupId());
        sb.append(", userId=");
        sb.append(getUserId());
        sb.append(", nome=");
        sb.append(getNome());
        sb.append(", estilo=");
        sb.append(getEstilo());
        sb.append(", ativo=");
        sb.append(getAtivo());
        sb.append(", cabecalho=");
        sb.append(getCabecalho());
        sb.append(", rodape=");
        sb.append(getRodape());
        sb.append(", corpo=");
        sb.append(getCorpo());
        sb.append(", dataCriacao=");
        sb.append(getDataCriacao());
        sb.append(", dataAtualizacao=");
        sb.append(getDataAtualizacao());
        sb.append(", thumbnailFileEntryId=");
        sb.append(getThumbnailFileEntryId());
        sb.append(", thumbnailURL=");
        sb.append(getThumbnailURL());
        sb.append("}");

        return sb.toString();
    }

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

        sb.append("<model><model-name>");
        sb.append(
            "br.mp.mprj.portal.portlet.gestaomensagens.service.model.Template");
        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>templateId</column-name><column-value><![CDATA[");
        sb.append(getTemplateId());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>companyId</column-name><column-value><![CDATA[");
        sb.append(getCompanyId());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>groupId</column-name><column-value><![CDATA[");
        sb.append(getGroupId());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>userId</column-name><column-value><![CDATA[");
        sb.append(getUserId());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>nome</column-name><column-value><![CDATA[");
        sb.append(getNome());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>estilo</column-name><column-value><![CDATA[");
        sb.append(getEstilo());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>ativo</column-name><column-value><![CDATA[");
        sb.append(getAtivo());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>cabecalho</column-name><column-value><![CDATA[");
        sb.append(getCabecalho());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>rodape</column-name><column-value><![CDATA[");
        sb.append(getRodape());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>corpo</column-name><column-value><![CDATA[");
        sb.append(getCorpo());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>dataCriacao</column-name><column-value><![CDATA[");
        sb.append(getDataCriacao());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>dataAtualizacao</column-name><column-value><![CDATA[");
        sb.append(getDataAtualizacao());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>thumbnailFileEntryId</column-name><column-value><![CDATA[");
        sb.append(getThumbnailFileEntryId());
        sb.append("]]></column-value></column>");
        sb.append(
            "<column><column-name>thumbnailURL</column-name><column-value><![CDATA[");
        sb.append(getThumbnailURL());
        sb.append("]]></column-value></column>");

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

        return sb.toString();
    }
}
