java常用函数用于重复代码

时间:2017-02-09 09:26:15

标签: java

 public UMRResultObject insertDocumentation(UMRDocumentationDTO documentationDTO)
{
    Session     session = UMRHibernateUtil.getUmrSession();
    Transaction tx      = null;
    List<UMRDTO> duplicateDocumentationList=null;

    String objectType =documentationDTO.getId().getObjectType();
    String objectName =documentationDTO.getId().getObjectName();

    try
    {        
        duplicateDocumentationList = dao.getFilteredDocumentationList(objectType, objectName, false, session);

        if(duplicateDocumentationList.isEmpty())
        {
            tx = session.beginTransaction();
            dao.insertDocumentation(documentationDTO, session);           
            tx.commit();
            ro.setSuccess(true);
            ro.getMessages().add("Record Inserted Successfully");
            if (ro.isSuccess())
            {
                if("Domain".equals(objectType))
                {
                    MMTUtil.getDomainDocumentationMap().put(objectName, documentationDTO.getDocumentationLink());
                }
                else  if("DomainCombo".equals(objectType))
                {
                    MMTUtil.getDomainDocumentationMap().put(objectName, documentationDTO.getDocumentationLink());
                }
                return ro;
            }
        }
        else
        {
            ro.getMessages().add("Documentation for '" + objectName + "' " 
                            + objectType+ " already exists! \n");

            logger.info("Documentation for '" + objectName + "' " 
                            + objectType+ " already exists! \n");
        }
     }        

    return ro;
}

public UMRResultObject updateDocumentation(UMRDocumentationDTO documentationDTO)
{
    Session     session = UMRHibernateUtil.getUmrSession();
    Transaction tx      = null;

    try
    {
        String objectType = documentationDTO.getId().getObjectType();
        if("Domain".equals(objectType))
        {
            MMTUtil.getDomainDocumentationMap().put(documentationDTO.getId().getObjectName(), documentationDTO.getDocumentationLink());
        }
        else  if("DomainCombo".equals(objectType))
        {
            MMTUtil.getDomainDocumentationMap().put(documentationDTO.getId().getObjectName(), documentationDTO.getDocumentationLink());
        }

        tx = session.beginTransaction();
        dao.updateDocumentation(documentationDTO, session);
        tx.commit();
        ro.setSuccess(true);
        ro.getMessages().add("Record Updated Successfully");
        if(ro.isSuccess())
        {
             if("Domain".equals(objectType))
            {
                MMTUtil.getDomainDocumentationMap().put(objectName, documentationDTO.getDocumentationLink());
            }
            else  if("DomainCombo".equals(objectType))
            {
                MMTUtil.getDomainDocumentationMap().put(objectName, documentationDTO.getDocumentationLink());
            }
            return ro;
        }
    }

我不想在if(ro.isSuccess())中复制代码,并希望重构代码但无法从此处获得更多

我尝试了另一种方法,因为它无法使用documentationDTO我应该做什么

1 个答案:

答案 0 :(得分:3)

您可以这样做:

if(ro.isSuccess()){
    populateMap(objectType, objectName, documentationDTO);
    return ro;
}

方法如下:

private void populateMap(String objectType, String objectName, UMRDocumentationDTO documentationDTO){
      if("Domain".equals(objectType) || "DomainCombo".equals(objectType))
      {
          MMTUtil.getDomainDocumentationMap().put(objectName, documentationDTO.getDocumentationLink());
      }
}