/** * This file is part of Faktotum. * * * Faktotum is free software: you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation, either version 3 of * the License, or (at your option) any later version. * * Faktotum is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with Faktotum. * * If not, see <http://www.gnu.org/licenses/>. */ package de.romankreisel.faktotum.dao; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import javax.ejb.Stateless; import javax.persistence.NoResultException; import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import de.romankreisel.faktotum.datamodel.BundesbruderEntity; import de.romankreisel.faktotum.datamodel.BundesbruderEntity_; /** * The Data Access Object for the BundesbruderEntity * * @author Roman Kreisel <mail@romankreisel.de> */ @Stateless public class BundesbruderDao extends JpaDao<BundesbruderEntity> { /** * Find Bundesbruder with the firstname, lastname or alias matching the * search string * * @param searchString * the search string * @return a list of BundesbruderEntities matching the search */ public List<BundesbruderEntity> findBySearchString(String searchString, boolean searchAlsoInAliases) { CriteriaBuilder criteriaBuilder = this.getCriteriaBuilder(); CriteriaQuery<BundesbruderEntity> criteriaQuery = criteriaBuilder.createQuery(BundesbruderEntity.class); Root<BundesbruderEntity> entityRoot = criteriaQuery.from(BundesbruderEntity.class); ArrayList<Predicate> predicatesForOrStatement = new ArrayList<>(); predicatesForOrStatement.add(criteriaBuilder.like(criteriaBuilder.lower(entityRoot.get(BundesbruderEntity_.firstName)), "%" + searchString.toLowerCase() + "%")); predicatesForOrStatement.add(criteriaBuilder.like(criteriaBuilder.lower(entityRoot.get(BundesbruderEntity_.lastName)), "%" + searchString.toLowerCase() + "%")); if (searchAlsoInAliases) { predicatesForOrStatement.add(criteriaBuilder.like(criteriaBuilder.lower(entityRoot.get(BundesbruderEntity_.alias)), "%" + searchString.toLowerCase() + "%")); } criteriaQuery.where(criteriaBuilder.or(predicatesForOrStatement.toArray(new Predicate[predicatesForOrStatement.size()]))); TypedQuery<BundesbruderEntity> typedQuery = this.createQuery(criteriaQuery); try { List<BundesbruderEntity> searchResult = typedQuery.getResultList(); return searchResult; } catch (NoResultException e) { this.getLogger().log(Level.FINE, "Bundesbruder for search String " + searchString + " not in DB", e); return null; } } /** * Find a single Bundesbruder with the given username. * * @param userName * Username to search for * @return A BundesbruderEntity or null, if no Bundesbruder with the given * username can be found */ public BundesbruderEntity findByUserName(String userName) { CriteriaBuilder criteriaBuilder = this.getCriteriaBuilder(); CriteriaQuery<BundesbruderEntity> criteriaQuery = criteriaBuilder.createQuery(BundesbruderEntity.class); Root<BundesbruderEntity> entityRoot = criteriaQuery.from(BundesbruderEntity.class); criteriaQuery.where(criteriaBuilder.equal(entityRoot.get(BundesbruderEntity_.userName), userName)); TypedQuery<BundesbruderEntity> typedQuery = this.createQuery(criteriaQuery); try { BundesbruderEntity searchResult = typedQuery.getSingleResult(); return searchResult; } catch (NoResultException e) { this.getLogger().log(Level.FINE, "User " + userName + " not in DB", e); return null; } } }