if ("LOCAL".equals(data.getCustGrp()) && StringUtils.isNotBlank(data.getTaxCd1())) {
doCreateARDefaultTaxRecord("01", data.getTaxCd1(), data.getId().getReqId(), entityManager, true, true, true);
doCreateARDefaultTaxRecord("11", data.getTaxCd1(), data.getId().getReqId(), entityManager, false, false, false);
doCreateARDefaultTaxRecord("02", data.getTaxCd1(), data.getId().getReqId(), entityManager, false, false, false);
doCreateARDefaultTaxRecord("07", data.getTaxCd1(), data.getId().getReqId(), entityManager, false, false, false);
doCreateARDefaultTaxRecord("12", data.getTaxCd1(), data.getId().getReqId(), entityManager, false, false, false);
} else {
doCreateDefaultTaxRecord("", "1", data.getId().getReqId(), entityManager, true, true);
}
I have the if() statement above in which it creates a single entry if it fails to meet the condition. However, when the user make changes from the UI, if() statement above will be executed again and check the conditions, if it now does meet the conditions, it now creates a set of predefined entries. BUT the problem is, the first entry that was created is there alongside with the predefined entries that was created after that.
Now, what I am trying to achieve is to delete the single entry that was created by doCreateDefaultTaxRecord. I have tried entityManager.merge() and entityManager.remove() but I still can't figure it out to work.
Are there any ways to implement the behavior? So that when it goes to execute doCreateARDefaultTaxRecord, it will first delete the single entry that has been created by doCreateDefaultTaxRecord and proceed to do the rest.
private void doCreateARDefaultTaxRecord(String defaultTaxcd, String dataTaxCd, long reqId, EntityManager entityManager, boolean taxSepIndc,
boolean contPntIndc, boolean cntryUse) {
TaxInfoService taxService = new TaxInfoService();
GeoTaxInfo taxInfo = new GeoTaxInfo();
GeoTaxInfoPK taxInfoPK = new GeoTaxInfoPK();
Admin reqAdmin = null;
try {
reqAdmin = new AdminService().getCurrentRecordById(reqId, entityManager);
} catch (Exception ex) {
LOG.error("doCreateARDefaultTaxRecord : " + ex.getMessage());
reqAdmin = new Admin();
}
if (taxService.getTaxInfoCountByReqId(entityManager, reqId) <= 4 && reqAdmin.getReqType().equalsIgnoreCase(CmrConstants.REQ_TYPE_CREATE)) {
entityManager.remove(taxInfo);
taxInfoPK.setReqId(reqId);
taxInfoPK.setGeoTaxInfoId(taxService.generateGeoTaxInfoID(entityManager, reqId));
taxInfo.setTaxCd(defaultTaxcd); /* predefined value */
if (taxSepIndc) {
taxInfo.setTaxSeparationIndc("07"); /* predefined value */
} else {
taxInfo.setTaxSeparationIndc("N"); /* predefined value */
}
if (cntryUse) {
taxInfo.setCntryUse(""); /* predefined value */
} else {
taxInfo.setCntryUse("0000"); /* predefined value */
}
if (contPntIndc) {
taxInfo.setContractPrintIndc(""); /* predefined value */
} else {
taxInfo.setContractPrintIndc("N"); /* predefined value */
}
taxInfo.setTaxNum(dataTaxCd);
taxInfo.setCreateById(reqAdmin.getRequesterId());
taxInfo.setCreateTs(SystemUtil.getCurrentTimestamp());
taxInfo.setId(taxInfoPK);
entityManager.merge(taxInfo);
entityManager.flush();
}
}
private void doCreateDefaultTaxRecord(String defaultBillingPrintIndc, String defaultTaxSepIndc, long reqId, EntityManager entMan, boolean indcFlag,
boolean dumTaxCd) {
TaxInfoService taxService = new TaxInfoService();
GeoTaxInfo taxInfo = new GeoTaxInfo();
GeoTaxInfoPK taxInfoPK = new GeoTaxInfoPK();
Admin reqAdmin = null;
try {
reqAdmin = new AdminService().getCurrentRecordById(reqId, entMan);
} catch (Exception ex) {
LOG.error("doCreateDefaultTaxRecord : " + ex.getMessage());
reqAdmin = new Admin();
}
if (taxService.getTaxInfoCountByReqId(entMan, reqId) <= 0 && reqAdmin.getReqType().equalsIgnoreCase(CmrConstants.REQ_TYPE_CREATE)) {
taxInfoPK.setReqId(reqId);
taxInfoPK.setGeoTaxInfoId(taxService.generateGeoTaxInfoID(entMan, reqId));
if (indcFlag) {
taxInfo.setTaxSeparationIndc(defaultTaxSepIndc);
} else {
taxInfo.setTaxSeparationIndc(defaultTaxSepIndc);
taxInfo.setBillingPrintIndc(defaultBillingPrintIndc);
}
taxInfo.setContractPrintIndc("");/* dummy value */
taxInfo.setCntryUse("");/* dummy value */
if (dumTaxCd) {
taxInfo.setTaxCd("");/* dummy value */
} else {
taxInfo.setTaxCd("01");/* dummy value */
}
taxInfo.setTaxNum("");/* dummy value */
taxInfo.setCreateById(reqAdmin.getRequesterId());
taxInfo.setCreateTs(SystemUtil.getCurrentTimestamp());
taxInfo.setId(taxInfoPK);
entMan.persist(taxInfo);
entMan.flush();
}
}