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 List<Donation> updateMergeTable( Map<String, ChipinDonation> chipinDonationMap, List<Donation> databaseDonations) { List<Donation> donationsToUpdate = new ArrayList<Donation>(); for (Donation donation : databaseDonations) { ChipinDonation found = chipinDonationMap.remove(donation.getDomainId()); if (found != null && !StringUtils.isEmptyOrNull(found.getComment()) && StringUtils.isEmptyOrNull(donation.getComment())) { donation.setComment(found.getComment()); donation.setReadState(DonationReadState.PENDING); donation.setBidState(DonationBidState.PENDING); donationsToUpdate.add(donation); } } return donationsToUpdate; }