public static void buildInsertTables( Map<String, ChipinDonation> chipinDonationMap, List<Donor> allDonors, List<Donor> outDonorsToInsert, List<Donation> outDonationsToInsert) { Map<String, Donor> donorTable = generateDonorSet(allDonors); for (ChipinDonation chipinDonation : chipinDonationMap.values()) { Donor donor = donorTable.get(chipinDonation.getEmail()); if (donor == null) { String[] toks = chipinDonation.getName().trim().split("\\s+"); donor = new Donor(); donor.setEmail(chipinDonation.getEmail()); if (toks.length > 0) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < toks.length - 1; ++i) { builder.append(toks[i]); } donor.setFirstName(builder.toString()); donor.setLastName(toks[toks.length - 1]); } donorTable.put(donor.getEmail(), donor); outDonorsToInsert.add(donor); } String commentString = chipinDonation.getComment(); if (StringUtils.emptyIfNull(chipinDonation.getComment()).length() > ChipinDonation.MAX_COMMENT_LENGTH) { commentString = commentString.substring(0, ChipinDonation.MAX_COMMENT_LENGTH - 1); System.out.println( "Warning, truncating comment with length > " + ChipinDonation.MAX_COMMENT_LENGTH); } boolean signifigant = chipinDonation.getAmount().compareTo(SIGNIFIGANCE_THRESHOLD) >= 0; boolean hasComment = !StringUtils.isEmptyOrNull(commentString); Donation toAdd = new Donation(); toAdd.setDomain(DonationDomain.CHIPIN); toAdd.setDomainId(chipinDonation.getChipinId()); toAdd.setBidState( signifigant && hasComment ? DonationBidState.PENDING : DonationBidState.IGNORED); toAdd.setReadState( signifigant || hasComment ? DonationReadState.PENDING : DonationReadState.IGNORED); toAdd.setCommentState(DonationCommentState.PENDING); toAdd.setComment(StringUtils.nullIfEmpty(commentString)); toAdd.setAmount(chipinDonation.getAmount()); toAdd.setTimeReceived(chipinDonation.getTimeStamp()); toAdd.setDonor(donor); outDonationsToInsert.add(toAdd); } }
public static Map<String, Donor> generateDonorSet(List<Donor> all) { Map<String, Donor> table = new HashMap<String, Donor>(); for (Donor d : all) { table.put(d.getEmail(), d); } return table; }