I wonder there is any method of refactoring nested condition. It's just the same content with different contents and different classes which looks bad. I really want to reform this code which looks bad.
My sample code consists of the same conditions with different classes and contents.
public class AService extends BaseService {
public Map action(AModel aModel throws Exception{
Map model = new HashMap();
try {
if(aModel.getTrStatus().equals(TrStatus.CREATE))
{
aDao.add(acrAccessAreaModel);
aModel.setTrStatus(Constants.TrStatus.UPDATE.toString());
int attrNo = 0;
if(aModel.getImgPath()!=null && aModel.getImgPath().getSize()>0)
attrNo = fileService.fileWrite(Constants.ServiceId.ACR_AREA, aModel.getAccssAreaId().toString(), aModel.getImgPath());
if(attrNo != 0)
{
aModel.setAttrNo(attrNo);
aDao.modify(acrAccessAreaModel);
}
model.put("accAreaInfo", acrAccessAreaModel);
}
else if(aModel.getTrStatus().equals(TrStatus.UPDATE))
{
if(aModel.getImgPath()!=null && aModel.getImgPath().getSize()>0)
{
if(aModel.getAttrNo() != null)
{
FileModel fileModel = new FileModel();
fileModel.setFileSeq(aModel.getAccssAreaId().toString());
fileModel.setAttrNo(aModel.getAttrNo());
fileService.remove(fileModel);
}
int attrNo = 0;
attrNo = fileService.fileWrite(Constants.ServiceId.ACR_COLOR, aModel.getAccssAreaId().toString(), aModel.getImgPath());
if(attrNo != 0)
aModel.setAttrNo(attrNo);
}
aDao.modify(acrAccessAreaModel);
}
else if(aModel.getTrStatus().equals(TrStatus.DELETE))
{
for (Map<String,String> accInfo : aModel.getAccssAreaList()) {
if(accInfo.get("attrNo") != null && !accInfo.get("attrNo").equals(""))
{
FileModel fileModel = new FileModel();
fileModel.setFileSeq(accInfo.get("accssAreaId").toString());
fileModel.setAttrNo(Integer.parseInt(accInfo.get("attrNo")));
fileService.remove(fileModel);
}
aDao.remove(accInfo);
aDao.removeAccArea(accInfo);
aDao.removeGate(accInfo);
}
}
} catch (Exception e) {
throw e;
}
return model;
}
}
public class BService extends BaseService {
public Map action(BModel bModel) throws Exception{
Map model = new HashMap();
try{
if(bModel.getTrStatus().equals(TrStatus.CREATE)){
model.put("save",bDao.add(acrAccCtrlStdmModel));
} else if(bModel.getTrStatus().equals(TrStatus.UPDATE)){
model.put("save",bDao.up(acrAccCtrlStdmModel));
} else if (bModel.getTrStatus().equals(TrStatus.DELETE)){
for(Map<String, String> stdmId : bModel.getAcrDelList()){
bDao.del(stdmId);
}
}
}catch (Exception e) {
throw new Exception(e);
}
return model;
}
}
public class CService extends BaseService {
public Map aciton(CModel cModel) throws Exception{
Map model = new HashMap();
List<Map<String,String>> acrAreaList = null;
String gateId = "";
try{
if(cModel.getTrStatus().equals(TrStatus.CREATE)){
cDao.saveGate(cModel);
cModel.setGateId(cModel.getGateId());
gateId = acrAccCtrlcModel.getGateId().toString();
acrAreaList = cModel.getAcrAreaList();
for (Map<String,String> acrArea : acrAreaList) {
acrArea.put("gateId", gateId);
acrArea.put("crerId", cModel.getCrerId());
acrArea.put("updrId", cModel.getUpdrId());
cDao.save(acrArea);
}
model.put("save",acrAccCtrlGateModel);
}else if (cModel.getTrStatus().equals(TrStatus.DELETE)){
for(Map<String, String> acrDelList : acrAccCtrlGateModel.getAcrDelList()){
cDao.delGate(acrDelList);
cDao.del(acrDelList);
}
}
}catch (Exception e) {
throw new Exception(e);
}
return model;
}
}