SAP Commerce Veri Tabanına ModelService İle Kayıt Ekleme

Yeni instance oluştururken: //PwrtoolProScoreModel pwrtoolProScoreModel = new PwrtoolProScoreModel(); PwrtoolProScoreModel pwrtoolProScoreModel = modelService.create(PwrtoolProScoreModel.class); Modeli veri tabanına gönderiken: modelService.save(pwrtoolProScoreModel); Birden fazla modeli aynı anda veri tabanına gönderiken: modelService.saveAll(pwrtoolProScoreModel, pwrtoolProScoreSummaryModel); ModelService ile çalışırken roolback kullanımı: [code lang=”java”] @Resource ModelService modelService; @Override public ReqRespData earnProScore(final PwrtoolProScoreRuleModel _pwrtoolProScoreRuleModel, B2BCustomerModel b2BCustomerModel) { String methodName = "addProScore"; validateParameterNotNull(_pwrtoolProScoreRuleModel, "Parameter _pwrtoolProScoreRuleModel must not be null"); // validateParameterNotNull(_b2BCustomerModel, "Parameter _b2BCustomerModel must not be null"); // if don’t send to B2BCustomerModel find current user if (Objects.isNull(b2BCustomerModel)) { b2BCustomerModel = (B2BCustomerModel) userService.getCurrentUser(); } // TODO check PwrtoolProScoreRuleModel period // check the PwrtoolProScoreRuleModel period then continue or return without adding operation ReqRespData reqRespData = new ReqRespData(); switch (_pwrtoolProScoreRuleModel.getPeriodType()) { case EACH_PROCESS: // do continue break; case CREATE: // do continue break; case MONTHLY: { // find record which created on current month LocalDate firstDayOfMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()); Date firstDayOfMonthDate = Date .from(LocalDate.of(firstDayOfMonth.getYear(), firstDayOfMonth.getMonth(), firstDayOfMonth.getDayOfMonth()) .atStartOfDay(ZoneId.systemDefault()).toInstant()); final PwrtoolProScoreModel pwrtoolProScoreModel = pwrtoolScoreDao.findLastEarnedProScore(b2BCustomerModel, _pwrtoolProScoreRuleModel); // if founded record is created on this month then increase promotion score else do nothing if (Objects.nonNull(pwrtoolProScoreModel) && pwrtoolProScoreModel.getCreationtime().after(firstDayOfMonthDate)) { LogMF.info(LOGGER, "{0}: customer {1} ({2} B2BUnit) earned the last promotion score on {3} therefore couldn’t not be earn promotion score on this month", new Object[] { methodName, b2BCustomerModel.getName(), b2BCustomerModel.getDefaultB2BUnit().getName(), pwrtoolProScoreModel.getCreationtime() }); // TODO set localized msg reqRespData.setStatus(false); return reqRespData; } break; } case YEARLY: // find record which created on current year LocalDate firstDayOfYear = LocalDate.now().with(TemporalAdjusters.firstDayOfYear()); Date firstDayOfYearDate = Date .from(LocalDate.of(firstDayOfYear.getYear(), firstDayOfYear.getMonth(), firstDayOfYear.getDayOfMonth()) .atStartOfDay(ZoneId.systemDefault()).toInstant()); final PwrtoolProScoreModel pwrtoolProScoreModel = pwrtoolScoreDao.findLastEarnedProScore(b2BCustomerModel, _pwrtoolProScoreRuleModel); // if founded record is created on this month then increase promotion score else do nothing if (Objects.nonNull(pwrtoolProScoreModel) && pwrtoolProScoreModel.getCreationtime().after(firstDayOfYearDate)) { LogMF.info(LOGGER, "{0}: customer {1} ({2} B2BUnit) earned the last promotion score on {3} therefore couldn’t not be earn promotion score on this year", new Object[] { methodName, b2BCustomerModel.getName(), b2BCustomerModel.getDefaultB2BUnit().getName(), pwrtoolProScoreModel.getCreationtime() }); // TODO @oguzhankinik 20190226 set localized msg reqRespData.setStatus(false); return reqRespData; } break; default: // do nothing; } // insert pro score //PwrtoolProScoreModel pwrtoolProScoreModel = new PwrtoolProScoreModel(); PwrtoolProScoreModel pwrtoolProScoreModel = modelService.create(PwrtoolProScoreModel.class); pwrtoolProScoreModel.setB2BCustomerUid(b2BCustomerModel.getUid()); pwrtoolProScoreModel.setB2BUnit(b2BCustomerModel.getDefaultB2BUnit()); pwrtoolProScoreModel.setB2BUnitLocName(b2BCustomerModel.getDefaultB2BUnit().getLocName()); pwrtoolProScoreModel.setProScore(_pwrtoolProScoreRuleModel.getProScore()); pwrtoolProScoreModel.setTransactionCode(_pwrtoolProScoreRuleModel.getTransactionCode()); pwrtoolProScoreModel.setTransactionType(_pwrtoolProScoreRuleModel.getTransactionType()); // modelService.save(pwrtoolProScoreModel); // increase summary score final List<PwrtoolProScoreSummaryModel> pwrtoolProScoreSummaryModels = pwrtoolScoreDao .findProScoreSummaries(b2BCustomerModel, 0); //validateIfSingleResult(pwrtoolProScoreSummaryModels, // format("No PwrtoolProScoreSummaryModel with given _b2BCustomerModel [%s] was found", _b2BCustomerModel), // format("More than one PwrtoolProScoreSummaryModel with given _b2BCustomerModel [%s] was found, %s pwrtoolProScoreSummaryModels found!", _b2BCustomerModel, Integer.valueOf(pwrtoolProScoreSummaryModels .size()))); PwrtoolProScoreSummaryModel pwrtoolProScoreSummaryModel = null; if (CollectionUtils.isNotEmpty(pwrtoolProScoreSummaryModels)) { pwrtoolProScoreSummaryModel = pwrtoolProScoreSummaryModels.get(0); } // if there isn’t record in DB for related year then insert to record for current year if (Objects.isNull(pwrtoolProScoreSummaryModel)) { pwrtoolProScoreSummaryModel = new PwrtoolProScoreSummaryModel(); pwrtoolProScoreSummaryModel.setB2BUnit(b2BCustomerModel.getDefaultB2BUnit()); pwrtoolProScoreSummaryModel.setB2BUnitLocName(b2BCustomerModel.getDefaultB2BUnit().getLocName()); pwrtoolProScoreSummaryModel.setSpentProScore(0.0f); pwrtoolProScoreSummaryModel.setTotalProScore(0.0f); pwrtoolProScoreSummaryModel.setYear((short) Year.now().getValue()); pwrtoolProScoreSummaryModel.setTotalProScore(_pwrtoolProScoreRuleModel.getProScore()); } else { // increase promotion score for current year pwrtoolProScoreSummaryModel .setTotalProScore(pwrtoolProScoreSummaryModel.getTotalProScore() + _pwrtoolProScoreRuleModel.getProScore()); } // modelService.save(pwrtoolProScoreSummaryModel); // TODO do roolback operation for these operations modelService.saveAll(pwrtoolProScoreModel, pwrtoolProScoreSummaryModel); reqRespData.setStatus(true); return reqRespData; } [/code] Rollback işlemleriniz içim ise yine modelservice’in sağladığı özellikleri kullanabilirsiniz.]]>

Leave a Reply

Your email address will not be published. Required fields are marked *