Java tutorial
/* Copyright 2006 - 2010 Under Dusken 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 no.dusken.aranea.service; import no.dusken.aranea.model.Person; import no.dusken.aranea.model.Role; import no.dusken.common.service.impl.GenericServiceImpl; import org.springframework.dao.DataAccessException; import org.springframework.dao.support.DataAccessUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; /** * * @author Erlend Hamnaberg<erlenha@underdusken.no> */ @Service("personService") @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.DEFAULT, readOnly = true) public class PersonServiceImpl extends GenericServiceImpl<Person> implements PersonService { public PersonServiceImpl() { super(Person.class); } public List<Person> getPersons() { List<Person> list = Collections.emptyList(); try { list = genericDao.getByNamedQuery("person_persons", null); } catch (DataAccessException dae) { log.warn("Unable to get Person", dae); } return list; } public Person getPerson(String firstname, String surname) { Person p = null; try { Map<String, Object> map = new HashMap<String, Object>(); map.put("firstname", firstname); map.put("surname", surname); List<Person> list = genericDao.getByNamedQuery("person_byname", map); p = (Person) DataAccessUtils.uniqueResult(list); } catch (DataAccessException dae) { log.warn("Unable to get Person", dae); } return p; } /** * Gets persons by role * * @param role * the role * @return persons if found, null if not */ public List<Person> getPersons(Role role) { List<Person> list = Collections.emptyList(); try { Map<String, Object> map = new HashMap<String, Object>(); map.put("role", role); list = genericDao.getByNamedQuery("person_byrole", map); } catch (DataAccessException dae) { log.warn("Unable to get persons", dae); } return list; } /** * Gets persons by role and status * * @param role * the role * @param active * the status * @return persons if found, null if not */ public List<Person> getPersons(Role role, Boolean active) { List<Person> list = Collections.emptyList(); try { Map<String, Object> map = new HashMap<String, Object>(); map.put("role", role); map.put("active", active); list = genericDao.getByNamedQuery("person_byroleandstatus", map); } catch (DataAccessException dae) { log.warn("Unable to get persons", dae); } return list; } /** * Gets a person byuser name * * @param username * the name to get * @return a person if found, null if not */ public Person getPersonByUsername(String username) { if (username == null || username.equals("")) { return null; } Person p = null; try { Map<String, Object> map = new HashMap<String, Object>(); map.put("username", username.toLowerCase()); List<Person> list = genericDao.getByNamedQuery("person_byusername", map); p = (Person) DataAccessUtils.uniqueResult(list); } catch (DataAccessException dae) { log.warn("Unable to get Person", dae); } return p; } /** * gets all the persons, ordered by firstname * @param includenonactive if true get both active and not active persons * @return either all persons, or just active */ public List<Person> getPersonsByFirstname(boolean includenonactive) { List<Person> list = Collections.emptyList(); Map map = new HashMap(); try { if (includenonactive) { list = genericDao.getByNamedQuery("person_personsbyfirstname", null); } else { map.put("active", true); list = genericDao.getByNamedQuery("person_personsbyfirstnameandactive", map); } } catch (DataAccessException dae) { log.warn("Unable to get Person", dae); } return list; } /** * Gets all active/inactive persons * * @param active * @return teh list */ public List<Person> getPersonsByActive(boolean active) { List<Person> list = Collections.emptyList(); try { Map map = new HashMap(); map.put("active", active); list = genericDao.getByNamedQuery("person_by_active", map); } catch (DataAccessException dae) { log.warn("Unable to get Person", dae); } return list; } }