package org.vaelen.contactmanager.test;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.vaelen.contactmanager.ContactRepository;
import org.vaelen.contactmanager.model.Contact;
import org.vaelen.contactmanager.model.EmailAddress;
import org.vaelen.contactmanager.model.PhoneNumber;
@Repository
public class ContactRepositoryTestImpl implements ContactRepository {
private Logger logger = Logger.getLogger(ContactRepositoryTestImpl.class);
Map<String, Contact> contactMap;
public ContactRepositoryTestImpl() {
contactMap = new LinkedHashMap<String, Contact>();
loadTestData();
}
/** This method loads the test data. */
private void loadTestData() {
createContact("Young", "Andrew", "These are the original notes.", "andrew@vaelen.org", "555-555-5555");
createContact("Smith", "Joe", "These are the original notes.", "joe@example.com", "555-555-5555");
createContact("Riker", "William", "Serial Number: SC-231-427\nCurrent Post: USS Titan NCC-80102 (Captain)\nPrevious Posts:\n\tUSS Enterprise NCC-1701-E (First Officer)\n\tUSS Enterprise NCC-1701-D (First Officer)\n\tUSS Hood NCC-42296 (First Officer)\n\tUSS Potemkin NCC-18253\n\tUSS Pegasus NCC-53847 (Conn Officer)", "riker@starfleet.com", "555-555-5555");
}
/** This method creates a Contact object to use for testing purposes. */
private void createContact(String lastName, String firstName, String notes, String email, String phone) {
Contact contact = new Contact();
String contactId = UUID.randomUUID().toString();
contact.setContactId(contactId);
contact.setFirstName(firstName);
contact.setLastName(lastName);
contact.setNotes(notes);
String phoneId = UUID.randomUUID().toString();
PhoneNumber phoneNumber = new PhoneNumber();
phoneNumber.setPhoneId(phoneId);
phoneNumber.setContactId(contactId);
phoneNumber.setPhoneNumber(phone);
Set<PhoneNumber> phoneSet = new LinkedHashSet<PhoneNumber>();
phoneSet.add(phoneNumber);
contact.setPhoneNumbers(phoneSet);
String emailId = UUID.randomUUID().toString();
EmailAddress emailAddress = new EmailAddress();
emailAddress.setEmailId(emailId);
emailAddress.setContactId(contactId);
emailAddress.setEmailAddress(email);
Set<EmailAddress> emailSet = new LinkedHashSet<EmailAddress>();
emailSet.add(emailAddress);
contact.setEmailAddresses(emailSet);
contact.setContactId(contactId);
contactMap.put(contactId, contact);
debug("Test Contact Created. First Name: %s, Last Name: %s, Contact ID: %s", firstName, lastName, contactId);
}
@Override
public List<Contact> listContacts() {
debug("listContacts() Called.");
return new ArrayList<Contact>(contactMap.values());
}
@Override
public List<Contact> findContactsByLastName(String lastName) {
debug("findContactsByLastName() Called. Last Name: %s", lastName);
List<Contact> contacts = new ArrayList<Contact>();
for(Contact contact: contactMap.values()) {
if(lastName == null && contact.getLastName() == null) {
contacts.add(contact);
} else if (lastName != null && lastName.equals(contact.getLastName())) {
contacts.add(contact);
}
}
return contacts;
}
@Override
public List<Contact> findContactsByFirstName(String firstName) {
debug("findContactsByFirstName() Called. Last Name: %s", firstName);
List<Contact> contacts = new ArrayList<Contact>();
for(Contact contact: contactMap.values()) {
if(firstName == null && contact.getFirstName() == null) {
contacts.add(contact);
} else if (firstName != null && firstName.equals(contact.getFirstName())) {
contacts.add(contact);
}
}
return contacts;
}
@Override
public List<Contact> findContactsByPhoneNumber(String phone) {
debug("findContactsByPhoneNumber() Called. Phone Number: %s", phone);
List<Contact> contacts = new ArrayList<Contact>();
for(Contact contact: contactMap.values()) {
for(PhoneNumber phoneNumber: contact.getPhoneNumbers()) {
if(phone == null && phoneNumber.getPhoneNumber() == null) {
contacts.add(contact);
break;
} else if (phone != null && phone.equals(phoneNumber.getPhoneNumber())) {
contacts.add(contact);
break;
}
}
}
return contacts;
}
@Override
public List<Contact> findContactsByEmailAddress(String email) {
debug("findContactsByEmailAddress() Called. Email Address: %s", email);
List<Contact> contacts = new ArrayList<Contact>();
for(Contact contact: contactMap.values()) {
for(EmailAddress emailAddress: contact.getEmailAddresses()) {
if(email == null && emailAddress.getEmailAddress() == null) {
contacts.add(contact);
break;
} else if (email != null && email.equals(emailAddress.getEmailAddress())) {
contacts.add(contact);
break;
}
}
}
return contacts;
}
@Override
public Contact loadContact(String contactId) {
debug("loadContact() Called. Contact ID: %s", contactId);
return contactMap.get(contactId);
}
@Override
public void saveContact(Contact contact) {
debug("saveContact() Called");
if(contact != null) {
if(contact.getContactId() == null) {
contact.setContactId(UUID.randomUUID().toString());
debug("Created Contact ID: " + contact.getContactId());
}
contactMap.put(contact.getContactId(), contact);
debug("Contact Saved. First Name: %s, Last Name: %s, Contact ID: %s", contact.getFirstName(), contact.getLastName(), contact.getContactId());
} else {
warn("Contact Was Null");
}
}
private void debug(String message, Object... params) {
log(Level.DEBUG, message, params);
}
private void warn(String message, Object... params) {
log(Level.WARN, message, params);
}
private void log(Level level, String message, Object... params) {
if(logger.isEnabledFor(level)) {
if(params != null && params.length > 0) {
message = String.format(message, params);
}
logger.log(level, message);
}
}
}