2017-02-09 65 views
0
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; 
     } 
    } 

我不想ducplicate中的代碼,如果(ro.isSuccess()),並希望重構代碼,但不能從這個Java通用功能

我試圖使另一進一步得到任何方法爲它無法使用documentationDTO我應該怎麼辦

+0

問題是什麼? – shmosel

回答

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()); 
     } 
} 
+1

你覺得也許你可以在這個方法中刪除重複的代碼? –

+1

很好找。它們在「if」和「else if」塊中都是基本相同的代碼。編輯了改進的答案。 –