List of usage examples for org.joda.time LocalDate isBefore
public boolean isBefore(ReadablePartial partial)
From source file:com.gst.portfolio.shareaccounts.serialization.ShareAccountDataSerializer.java
License:Apache License
@SuppressWarnings("null") public Map<String, Object> validateAndApprove(JsonCommand jsonCommand, ShareAccount account) { Map<String, Object> actualChanges = new HashMap<>(); if (StringUtils.isBlank(jsonCommand.json())) { throw new InvalidJsonException(); }//from w w w . ja v a 2s . c om final Type typeOfMap = new TypeToken<Map<String, Object>>() { }.getType(); this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, jsonCommand.json(), ShareAccountApiConstants.approvalParameters); final List<ApiParameterError> dataValidationErrors = new ArrayList<>(); final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors) .resource("sharesaccount"); JsonElement element = jsonCommand.parsedJson(); if (!account.status().equals(ShareAccountStatusType.SUBMITTED_AND_PENDING_APPROVAL.getValue())) { baseDataValidator.failWithCodeNoParameterAddedToErrorCode("is.not.pending.for.approval"); } LocalDate approvedDate = this.fromApiJsonHelper .extractLocalDateNamed(ShareAccountApiConstants.approveddate_paramname, element); final LocalDate submittalDate = new LocalDate(account.getSubmittedDate()); if (approvedDate != null && approvedDate.isBefore(submittalDate)) { final DateTimeFormatter formatter = DateTimeFormat.forPattern(jsonCommand.dateFormat()) .withLocale(jsonCommand.extractLocale()); final String submittalDateAsString = formatter.print(submittalDate); baseDataValidator.reset().parameter(ShareAccountApiConstants.approveddate_paramname) .value(submittalDateAsString) .failWithCodeNoParameterAddedToErrorCode("approved.date.cannot.be.before.submitted.date"); } Set<ShareAccountTransaction> transactions = account.getShareAccountTransactions(); for (ShareAccountTransaction transaction : transactions) { if (transaction.isActive() && transaction.isPendingForApprovalTransaction()) { validateTotalSubsribedShares(account, transaction, baseDataValidator); } } if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); } AppUser approvedUser = this.platformSecurityContext.authenticatedUser(); account.approve(approvedDate.toDate(), approvedUser); actualChanges.put(ShareAccountApiConstants.id_paramname, account.getId()); updateTotalChargeDerived(account); return actualChanges; }
From source file:com.gst.portfolio.shareaccounts.serialization.ShareAccountDataSerializer.java
License:Apache License
@SuppressWarnings("null") public Map<String, Object> validateAndActivate(JsonCommand jsonCommand, ShareAccount account) { Map<String, Object> actualChanges = new HashMap<>(); if (StringUtils.isBlank(jsonCommand.json())) { throw new InvalidJsonException(); }/*from www . ja v a 2 s . c o m*/ final Type typeOfMap = new TypeToken<Map<String, Object>>() { }.getType(); this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, jsonCommand.json(), ShareAccountApiConstants.activateParameters); final List<ApiParameterError> dataValidationErrors = new ArrayList<>(); final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors) .resource("sharesaccount"); JsonElement element = jsonCommand.parsedJson(); if (!account.status().equals(ShareAccountStatusType.APPROVED.getValue())) { baseDataValidator.failWithCodeNoParameterAddedToErrorCode("is.not.in.approved.status"); } LocalDate activatedDate = this.fromApiJsonHelper .extractLocalDateNamed(ShareAccountApiConstants.activatedate_paramname, element); baseDataValidator.reset().parameter(ShareAccountApiConstants.activatedate_paramname).value(activatedDate) .notNull(); final LocalDate approvedDate = new LocalDate(account.getApprovedDate()); if (activatedDate != null && activatedDate.isBefore(approvedDate)) { final DateTimeFormatter formatter = DateTimeFormat.forPattern(jsonCommand.dateFormat()) .withLocale(jsonCommand.extractLocale()); final String submittalDateAsString = formatter.print(approvedDate); baseDataValidator.reset().parameter(ShareAccountApiConstants.activatedate_paramname) .value(submittalDateAsString) .failWithCodeNoParameterAddedToErrorCode("cannot.be.before.approved.date"); } if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); } AppUser approvedUser = this.platformSecurityContext.authenticatedUser(); account.activate(activatedDate.toDate(), approvedUser); handlechargesOnActivation(account); actualChanges.put(ShareAccountApiConstants.charges_paramname, activatedDate.toDate()); return actualChanges; }
From source file:com.gst.portfolio.shareaccounts.serialization.ShareAccountDataSerializer.java
License:Apache License
public Map<String, Object> validateAndApplyAddtionalShares(JsonCommand jsonCommand, ShareAccount account) { Map<String, Object> actualChanges = new HashMap<>(); if (StringUtils.isBlank(jsonCommand.json())) { throw new InvalidJsonException(); }// w w w. ja va2 s . c o m final Type typeOfMap = new TypeToken<Map<String, Object>>() { }.getType(); this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, jsonCommand.json(), ShareAccountApiConstants.addtionalSharesParameters); final List<ApiParameterError> dataValidationErrors = new ArrayList<>(); final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors) .resource("sharesaccount"); JsonElement element = jsonCommand.parsedJson(); if (!account.status().equals(ShareAccountStatusType.ACTIVE.getValue())) { baseDataValidator.failWithCodeNoParameterAddedToErrorCode("is.not.in.active.state"); } LocalDate requestedDate = this.fromApiJsonHelper .extractLocalDateNamed(ShareAccountApiConstants.requesteddate_paramname, element); baseDataValidator.reset().parameter(ShareAccountApiConstants.requesteddate_paramname).value(requestedDate) .notNull(); final Long sharesRequested = this.fromApiJsonHelper .extractLongNamed(ShareAccountApiConstants.requestedshares_paramname, element); baseDataValidator.reset().parameter(ShareAccountApiConstants.requestedshares_paramname) .value(sharesRequested).notNull(); ShareProduct shareProduct = account.getShareProduct(); if (sharesRequested != null) { Long totalSharesAfterapproval = account.getTotalApprovedShares() + sharesRequested; if (shareProduct.getMaximumClientShares() != null && totalSharesAfterapproval > shareProduct.getMaximumClientShares()) { baseDataValidator.reset().parameter(ShareAccountApiConstants.requestedshares_paramname) .value(sharesRequested).failWithCode("exceeding.maximum.limit.defined.in.the.shareproduct", "Existing and requested shares count is more than product definition"); } } boolean isTransactionBeforeExistingTransactions = false; Set<ShareAccountTransaction> transactions = account.getShareAccountTransactions(); for (ShareAccountTransaction transaction : transactions) { if (!transaction.isChargeTransaction()) { LocalDate transactionDate = new LocalDate(transaction.getPurchasedDate()); if (requestedDate.isBefore(transactionDate)) { isTransactionBeforeExistingTransactions = true; break; } } } if (isTransactionBeforeExistingTransactions) { baseDataValidator.reset().parameter(ShareAccountApiConstants.requesteddate_paramname) .value(requestedDate).failWithCodeNoParameterAddedToErrorCode( "purchase.transaction.date.cannot.be.before.existing.transactions"); } if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); } final BigDecimal unitPrice = shareProduct.deriveMarketPrice(requestedDate.toDate()); ShareAccountTransaction purchaseTransaction = new ShareAccountTransaction(requestedDate.toDate(), sharesRequested, unitPrice); account.addAdditionalPurchasedShares(purchaseTransaction); handleAdditionalSharesChargeTransactions(account, purchaseTransaction); actualChanges.put(ShareAccountApiConstants.additionalshares_paramname, purchaseTransaction); return actualChanges; }
From source file:com.gst.portfolio.shareaccounts.serialization.ShareAccountDataSerializer.java
License:Apache License
public Map<String, Object> validateAndRedeemShares(JsonCommand jsonCommand, ShareAccount account) { Map<String, Object> actualChanges = new HashMap<>(); if (StringUtils.isBlank(jsonCommand.json())) { throw new InvalidJsonException(); }/* w ww .java2 s . c om*/ final Type typeOfMap = new TypeToken<Map<String, Object>>() { }.getType(); this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, jsonCommand.json(), ShareAccountApiConstants.addtionalSharesParameters); final List<ApiParameterError> dataValidationErrors = new ArrayList<>(); final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors) .resource("sharesaccount"); JsonElement element = jsonCommand.parsedJson(); LocalDate requestedDate = this.fromApiJsonHelper .extractLocalDateNamed(ShareAccountApiConstants.requesteddate_paramname, element); baseDataValidator.reset().parameter(ShareAccountApiConstants.requesteddate_paramname).value(requestedDate) .notNull(); final Long sharesRequested = this.fromApiJsonHelper .extractLongNamed(ShareAccountApiConstants.requestedshares_paramname, element); baseDataValidator.reset().parameter(ShareAccountApiConstants.requestedshares_paramname) .value(sharesRequested).notNull().longGreaterThanZero(); boolean isTransactionBeforeExistingTransactions = false; Set<ShareAccountTransaction> transactions = account.getShareAccountTransactions(); for (ShareAccountTransaction transaction : transactions) { if (!transaction.isChargeTransaction() && transaction.isActive()) { LocalDate transactionDate = new LocalDate(transaction.getPurchasedDate()); if (requestedDate.isBefore(transactionDate)) { isTransactionBeforeExistingTransactions = true; break; } } } if (isTransactionBeforeExistingTransactions) { baseDataValidator.reset().parameter(ShareAccountApiConstants.requesteddate_paramname) .value(requestedDate).failWithCodeNoParameterAddedToErrorCode( "redeem.transaction.date.cannot.be.before.existing.transactions"); } if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); } BigDecimal unitPrice = account.getShareProduct().deriveMarketPrice(requestedDate.toDate()); ShareAccountTransaction transaction = ShareAccountTransaction .createRedeemTransaction(requestedDate.toDate(), sharesRequested, unitPrice); validateRedeemRequest(account, transaction, baseDataValidator, dataValidationErrors); account.addAdditionalPurchasedShares(transaction); actualChanges.put(ShareAccountApiConstants.requestedshares_paramname, transaction); handleRedeemSharesChargeTransactions(account, transaction); return actualChanges; }
From source file:com.gst.portfolio.shareaccounts.serialization.ShareAccountDataSerializer.java
License:Apache License
public Map<String, Object> validateAndClose(JsonCommand jsonCommand, ShareAccount account) { Map<String, Object> actualChanges = new HashMap<>(); if (StringUtils.isBlank(jsonCommand.json())) { throw new InvalidJsonException(); }/*from ww w.ja v a 2 s .co m*/ final Type typeOfMap = new TypeToken<Map<String, Object>>() { }.getType(); this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, jsonCommand.json(), ShareAccountApiConstants.closeParameters); final List<ApiParameterError> dataValidationErrors = new ArrayList<>(); final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors) .resource("sharesaccount"); JsonElement element = jsonCommand.parsedJson(); LocalDate closedDate = this.fromApiJsonHelper .extractLocalDateNamed(ShareAccountApiConstants.closeddate_paramname, element); baseDataValidator.reset().parameter(ShareAccountApiConstants.approveddate_paramname).value(closedDate) .notNull(); if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); } boolean isTransactionBeforeExistingTransactions = false; Set<ShareAccountTransaction> transactions = account.getShareAccountTransactions(); for (ShareAccountTransaction transaction : transactions) { if (!transaction.isChargeTransaction()) { LocalDate transactionDate = new LocalDate(transaction.getPurchasedDate()); if (closedDate.isBefore(transactionDate)) { isTransactionBeforeExistingTransactions = true; break; } } } if (isTransactionBeforeExistingTransactions) { baseDataValidator.reset().parameter(ShareAccountApiConstants.closeddate_paramname).value(closedDate) .failWithCodeNoParameterAddedToErrorCode( "share.account.cannot.be.closed.before.existing.transactions"); } if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); } AppUser approvedUser = this.platformSecurityContext.authenticatedUser(); final BigDecimal unitPrice = account.getShareProduct().deriveMarketPrice(DateUtils.getDateOfTenant()); ShareAccountTransaction transaction = ShareAccountTransaction.createRedeemTransaction(closedDate.toDate(), account.getTotalApprovedShares(), unitPrice); account.addAdditionalPurchasedShares(transaction); account.close(closedDate.toDate(), approvedUser); handleRedeemSharesChargeTransactions(account, transaction); actualChanges.put(ShareAccountApiConstants.requestedshares_paramname, transaction); return actualChanges; }
From source file:com.gst.portfolio.shareproducts.service.ShareProductDividendAssembler.java
License:Apache License
private long calculateNumberOfShareDays(final LocalDate postingDate, final LocalDate lastDividendPostDate, final int minimumActivePeriod, final Collection<ShareAccountData> shareAccountDatas, final Map<Long, Long> numberOfSharesdaysPerAccount) { long numberOfShareDays = 0; for (ShareAccountData accountData : shareAccountDatas) { long numberOfShareDaysPerAccount = 0; Collection<ShareAccountTransactionData> purchasedShares = accountData.getPurchasedShares(); long numberOfShares = 0; LocalDate lastDividendAppliedDate = null; for (ShareAccountTransactionData purchasedSharesData : purchasedShares) { final PurchasedSharesStatusType status = PurchasedSharesStatusType .fromInt(purchasedSharesData.getStatus().getId().intValue()); final PurchasedSharesStatusType type = PurchasedSharesStatusType .fromInt(purchasedSharesData.getType().getId().intValue()); if (status.isApproved() && !type.isChargePayment()) { LocalDate shareStartDate = purchasedSharesData.getPurchasedDate(); if (shareStartDate.isBefore(lastDividendPostDate)) { shareStartDate = lastDividendPostDate; }/* w ww . j a v a 2s . co m*/ int numberOfPurchseDays = Days.daysBetween(shareStartDate, postingDate).getDays(); if (type.isPurchased() && numberOfPurchseDays < minimumActivePeriod) { continue; } if (lastDividendAppliedDate != null) { numberOfShareDaysPerAccount += (Days.daysBetween(lastDividendAppliedDate, shareStartDate) .getDays() * numberOfShares); } lastDividendAppliedDate = shareStartDate; if (type.isPurchased()) { numberOfShares += purchasedSharesData.getNumberOfShares(); } else { numberOfShares -= purchasedSharesData.getNumberOfShares(); } } } if (lastDividendAppliedDate != null) { numberOfShareDaysPerAccount += (Days.daysBetween(lastDividendAppliedDate, postingDate).getDays() * numberOfShares); } numberOfShareDays += numberOfShareDaysPerAccount; numberOfSharesdaysPerAccount.put(accountData.getId(), numberOfShareDaysPerAccount); } return numberOfShareDays; }
From source file:com.helger.datetime.holiday.HolidayUtils.java
License:Apache License
/** * Get the number of working days between start date (incl.) and end date * (incl.). An optional holiday calculator can be used as well. * //from w w w . ja v a 2s.c o m * @param aStartDate * The start date. May not be <code>null</code>. * @param aEndDate * The end date. May not be <code>null</code>. * @param aHolidayMgr * The holiday calculator to use. May not be <code>null</code>. * @return The number of working days. If start date is after end date, the * value will be negative! If start date equals end date the return * will be 1 if it is a working day. */ public static int getWorkingDays(@Nonnull final LocalDate aStartDate, @Nonnull final LocalDate aEndDate, @Nonnull final IHolidayManager aHolidayMgr) { if (aStartDate == null) throw new NullPointerException("startDate"); if (aEndDate == null) throw new NullPointerException("endDate"); if (aHolidayMgr == null) throw new NullPointerException("holidayCalc"); final boolean bFlip = aStartDate.isAfter(aEndDate); LocalDate aCurDate = bFlip ? aEndDate : aStartDate; final LocalDate aRealEndDate = bFlip ? aStartDate : aEndDate; int ret = 0; while (!aRealEndDate.isBefore(aCurDate)) { if (isWorkDay(aCurDate, aHolidayMgr)) ret++; aCurDate = aCurDate.plusDays(1); } return bFlip ? -1 * ret : ret; }
From source file:com.helger.datetime.period.LocalDatePeriod.java
License:Apache License
public final boolean isValidFor(@Nonnull final LocalDate aDate) { if (aDate == null) throw new NullPointerException("date"); final LocalDate aStart = getStart(); if (aStart != null && aStart.isAfter(aDate)) return false; final LocalDate aEnd = getEnd(); if (aEnd != null && aEnd.isBefore(aDate)) return false; return true;/*from ww w . j a va2 s . c om*/ }
From source file:com.helger.masterdata.postal.PostalCodeListReader.java
License:Apache License
public void readFromFile(@Nonnull final IReadableResource aRes) { ValueEnforcer.notNull(aRes, "Resource"); final IMicroDocument aDoc = MicroReader.readMicroXML(aRes); if (aDoc == null) throw new IllegalArgumentException("Passed resource is not an XML file: " + aRes); final IMicroElement eBody = aDoc.getDocumentElement().getFirstChildElement(ELEMENT_BODY); if (eBody == null) throw new IllegalArgumentException("Missing body element in file " + aRes); final LocalDate aNow = PDTFactory.getCurrentLocalDate(); // Read all countries for (final IMicroElement eCountry : eBody.getAllChildElements(ELEMENT_COUNTRY)) { final String sCountryName = eCountry.getAttributeValue(ATTR_NAME); final String sISO = eCountry.getAttributeValue(ATTR_ISO); final PostalCodeCountry aCountry = new PostalCodeCountry(sISO); // Read all postal code definitions for (final IMicroElement ePostalCode : eCountry.getAllChildElements(ELEMENT_POSTALCODES)) { final String sValidFrom = ePostalCode.getAttributeValue(ATTR_VALIDFROM); final LocalDate aValidFrom = sValidFrom == null ? null : ISODateTimeFormat.date().parseLocalDate(sValidFrom); final String sValidTo = ePostalCode.getAttributeValue(ATTR_VALIDTO); final LocalDate aValidTo = sValidTo == null ? null : ISODateTimeFormat.date().parseLocalDate(sValidTo); if (aValidFrom != null && aValidFrom.isAfter(aNow)) { MasterdataLogger.getInstance().info("Ignoring some postal code definitions of " + sCountryName + " because they are valid from " + aValidFrom.toString()); continue; }// ww w . j av a 2 s. co m if (aValidTo != null && aValidTo.isBefore(aNow)) { MasterdataLogger.getInstance().info("Ignoring some postal code definitions of " + sCountryName + " because they are valid until " + aValidTo.toString()); continue; } // Read all formats for (final IMicroElement eFormat : ePostalCode.getAllChildElements(ELEMENT_FORMAT)) { final String sFormat = eFormat.getTextContent(); if (StringHelper.hasNoText(sFormat)) throw new IllegalArgumentException( "The country " + sISO + " contains an empty postal code format!"); // Parse into tokens final List<EPostalCodeFormatElement> aElements = _parseFormat(sFormat); if (aElements.isEmpty()) throw new IllegalStateException( "The country " + sISO + " contains an invalid format '" + sFormat + "'"); aCountry.addFormat(new PostalCodeFormat(sISO, aElements)); } // Is exactly one code present? for (final IMicroElement eOneCode : ePostalCode.getAllChildElements(ELEMENT_SPECIFIC)) aCountry.addSpecificPostalCode(eOneCode.getTextContent()); // Is a note present final IMicroElement eNote = ePostalCode.getFirstChildElement(ELEMENT_NOTE); if (eNote != null) aCountry.setNote(eNote.getTextContent()); } if (aCountry.getFormatCount() == 0 && aCountry.getSpecificPostalCodeCount() == 0) throw new IllegalStateException("Country " + sISO + " has no formats defined!"); m_aMgr.addCountry(aCountry); } }
From source file:com.helger.masterdata.vat.VATItem.java
License:Apache License
public VATItem(@Nonnull @Nonempty final String sID, @Nonnull final EVATType eType, @Nonnull @Nonnegative final BigDecimal aPercentage, final boolean bDeprecated, @Nullable final LocalDate aValidFrom, @Nullable final LocalDate aValidTo) { ValueEnforcer.notEmpty(sID, "ID"); ValueEnforcer.notNull(eType, "Type"); ValueEnforcer.notNull(aPercentage, "Percentage"); ValueEnforcer.isBetweenInclusive(aPercentage, "Percentage", BigDecimal.ZERO, CGlobal.BIGDEC_100); if (aValidFrom != null && aValidTo != null && aValidTo.isBefore(aValidFrom)) throw new IllegalArgumentException("ValidFrom date must be <= validTo date"); m_sID = sID;/*w w w . ja v a 2 s . c o m*/ m_eType = eType; m_aPercentage = aPercentage; m_aPercentageFactor = m_aPercentage.divide(CGlobal.BIGDEC_100); m_aMultiplicationFactorNetToGross = BigDecimal.ONE.add(m_aPercentageFactor); m_bDeprecated = bDeprecated; setStart(aValidFrom); setEnd(aValidTo); }