Created
April 19, 2017 08:43
-
-
Save KDamir/a52e8ef12fec3eb5dabdd1fdbef1c700 to your computer and use it in GitHub Desktop.
repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kz.inessoft.stomagic.common.data.repository; | |
import kz.inessoft.stomagic.common.data.entity.AbstractContactPerson; | |
import kz.inessoft.stomagic.common.data.entity.search.ContactSearchParams; | |
import org.jetbrains.annotations.NotNull; | |
import javax.persistence.NoResultException; | |
import java.util.List; | |
import java.util.Optional; | |
/** | |
* @author Vladislav Zheltovskiy | |
*/ | |
public abstract class AbstractContactPersonRepository<C extends AbstractContactPerson> extends AbstractEntityRepository<C> { | |
public C findByPhoneAndName(String phoneNumber, String name) { | |
try { | |
return getEntityManager() | |
.createNamedQuery("findPersonByNameAndPhone", getEntityClass()) | |
.setParameter("phoneNumber", phoneNumber) | |
.setParameter("name", name) | |
.getSingleResult(); | |
} catch (NoResultException e) { | |
return null; | |
} | |
} | |
public Optional<C> findByParams(@NotNull ContactSearchParams searchParams) { | |
try { | |
if (searchParams.getPersonId() != null) { | |
return Optional.ofNullable(find(searchParams.getPersonId())); | |
} | |
return Optional.ofNullable(getEntityManager().createNamedQuery("findPersonByNameAndPhone", getEntityClass()) | |
.setParameter("phoneNumber", searchParams.getPhone()) | |
.setParameter("name", searchParams.getName()).getSingleResult()); | |
} catch (NoResultException e) { | |
return Optional.empty(); | |
} | |
} | |
public List<C> findAllPersons(Long branchId) { | |
return getEntityManager() | |
.createNamedQuery("findAllPersonsByBranch", getEntityClass()) | |
.setParameter("branchId", branchId).getResultList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment