Java tutorial
package com.github.jrh3k5.membership.renewal.mailer.service.jpa; /* * #%L * KCAC Membership Management Application * %% * Copyright (C) 2014 Kansas City Atheist Coalition * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * #L% */ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.List; import javax.inject.Inject; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Query; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.github.jrh3k5.membership.renewal.mailer.service.EmailService; import com.github.jrh3k5.membership.renewal.mailer.service.jpa.data.JpaEmailRecord; import com.googlecode.jcsv.CSVStrategy; import com.googlecode.jcsv.reader.internal.CSVReaderBuilder; import com.googlecode.jcsv.reader.internal.DefaultCSVEntryParser; public class JpaEmailService extends AbstractJpaService implements EmailService<JpaEmailRecord> { private static final Logger LOGGER = LoggerFactory.getLogger(JpaEmailService.class); private EntityManagerFactory entityManagerFactory; @Inject public JpaEmailService(EntityManagerFactory entityManagerFactory) { super(entityManagerFactory); this.entityManagerFactory = entityManagerFactory; } @Override public void loadEmails(InputStream emailCsv) throws IOException { final Reader emailCsvReader = new InputStreamReader(emailCsv); try { int rowIndex = 0; for (String[] row : new CSVReaderBuilder<String[]>(emailCsvReader) .entryParser(new DefaultCSVEntryParser()).strategy(CSVStrategy.UK_DEFAULT).build()) { if (rowIndex++ == 0) { // Skip the first row, which, presumably, has the column names in it continue; } // Skip any malformed rows if (row.length < 3) { continue; } // If someone signs up through the site, they won't have a first and last name // We can't match on that, so ignore it if (StringUtils.isNoneBlank(row[1], row[2])) { addEmail(row[1], row[2], row[0]); } } } finally { emailCsvReader.close(); } } @Override public JpaEmailRecord addEmail(String givenName, String familyName, String emailAddress) { LOGGER.info("Adding email {} for {} {}", emailAddress, givenName, familyName); // If it exists by e-mail address, update the first name and last name final JpaEmailRecord byEmail = getByAddress(emailAddress); if (byEmail != null) { byEmail.setGivenName(givenName); byEmail.setFamilyName(familyName); updateEmail(byEmail); return byEmail; } // If the person exists in the database by name, update their e-mail address final JpaEmailRecord byName = getByName(givenName, familyName); if (byName != null) { byName.setEmailAddress(emailAddress); updateEmail(byName); return byName; } // Otherwise, it's a brand-spankin' new entry! final JpaEmailRecord emailRecord = new JpaEmailRecord(); emailRecord.setFamilyName(familyName); emailRecord.setGivenName(givenName); emailRecord.setEmailAddress(emailAddress); persistEntity(emailRecord); return emailRecord; } @Override public JpaEmailRecord getByAddress(String emailAddress) { final EntityManager entityManager = entityManagerFactory.createEntityManager(); try { final Query query = entityManager .createQuery("select e from EmailRecord e where e.emailAddress = :emailAddress"); query.setParameter("emailAddress", emailAddress); @SuppressWarnings("unchecked") final List<JpaEmailRecord> results = query.getResultList(); return results.size() == 1 ? results.get(0) : null; } finally { entityManager.close(); } } @Override public void updateEmail(JpaEmailRecord emailRecord) { mergeEntity(emailRecord); } @Override public JpaEmailRecord getByName(String givenName, String familyName) { LOGGER.info("Looking up email info for {} {}", givenName, familyName); final EntityManager entityManager = entityManagerFactory.createEntityManager(); try { final Query query = entityManager.createQuery( "select e from EmailRecord e where e.givenName = :givenName and e.familyName = :familyName"); query.setParameter("givenName", givenName); query.setParameter("familyName", familyName); @SuppressWarnings("unchecked") final List<JpaEmailRecord> results = query.getResultList(); return results.size() == 1 ? results.get(0) : null; } finally { entityManager.close(); } } @Override public long getCount() { return getEntityCount(JpaEmailRecord.class); } }