Java tutorial
/* * Copyright 2012 the original author or authors. * 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. */ package gemfire.practice.service; import gemfire.practice.domain.Customer; import gemfire.practice.domain.EmailAddressFactory; import gemfire.practice.repository.CustomerRepository; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.annotation.Resource; import org.perf4j.aop.Profiled; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.Assert; /** * A simple Customer service * * @author David Turanski * */ @Component public class DefaultCustomerService implements CustomerService { private static Logger LOGGER = LoggerFactory.getLogger(DefaultCustomerService.class); @Autowired CustomerRepository customerRepository; @Resource(name = "emailAddressFactory") private EmailAddressFactory emailAddressFactory; /** * Create a new customer * * @param customer * @return */ @Override @Profiled(tag = "createCustomer") public Customer create(Customer customer) { Assert.notNull(customer, "customer cannot be null"); Assert.notNull(customer.getId(), "customer ID cannot be null"); Assert.hasText(customer.getFirstname(), "customer first name must contain text"); Assert.hasText(customer.getLastname(), "customer last name must contain text"); Assert.notNull(customer.getEmailAddress(), "customer email address must not be null"); Customer newCustomer = customerRepository.save(customer); LOGGER.info("New customer created with id: {}, thread {} is {}", customer.getId(), Thread.currentThread().getName(), customer); return newCustomer; } /** * Delete a customer with a given id; * * @param id * @return */ @Override public boolean delete(long id) { Customer customer = customerRepository.findOne(id); if (customer != null) { customerRepository.delete(customer); LOGGER.debug("successfully deleted customer with id: {}, thread {} and customer is :{} ", customer.getId(), Thread.currentThread().getName(), customer); return true; } LOGGER.warn("Customer not found for id {} for thread {}", id, Thread.currentThread().getName()); return false; } /** * Retrieve a customer by id * * @param id * @return */ @Override @Profiled(tag = "getCustomer") public Customer get(long id) { Customer customer = customerRepository.findOne(id); LOGGER.info("Retrieved customer for id: {} is {}", id, customer); return customer; } /** * Retrieve a list of customers by last name * * @param lastname * @return */ @Override public List<Customer> findCustomersByLastName(String lastname) { return customerRepository.findByLastname(lastname); } /** * Retrieve a customer by emailAddress * * @param emailAddress * @return */ @Override public Customer findByEmailAddress(String emailAddress) { return customerRepository.findByEmailAddress(emailAddressFactory.create(emailAddress)); } /** * Return all customers * * @return */ @Override public List<Customer> findAllCustomers() { Iterable<Customer> customersIterable = customerRepository.findAll(); Iterator<Customer> iterator = customersIterable.iterator(); List<Customer> customersList = new ArrayList<>(); while (iterator.hasNext()) { customersList.add(iterator.next()); } return customersList; } @Override public long count() { return customerRepository.count(); } @Override public void clear() { LOGGER.info("Deleting all customers"); customerRepository.deleteAll(); } }