com.siriusit.spezg.multilib.storage.jpa.JpaPersonRepository.java Source code

Java tutorial

Introduction

Here is the source code for com.siriusit.spezg.multilib.storage.jpa.JpaPersonRepository.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 com.siriusit.spezg.multilib.storage.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import com.siriusit.spezg.multilib.domain.Person;
import com.siriusit.spezg.multilib.domain.jpa.PersonDomainObject;
import com.siriusit.spezg.multilib.storage.PersonRepository;

/**
 * 
 * @author Tommy B <Tommy.Bo@SiriusIT.com>
 */
@Repository("personRepository")
public class JpaPersonRepository implements PersonRepository {

    @PersistenceContext(unitName = "multilib-db")
    private EntityManager entityManager;

    public EntityManager getEntityManager() {
        return entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    public Person createPerson(String username, String phone, String mail, String title) {
        Person person = new PersonDomainObject(username, phone, mail, title);
        entityManager.persist(person);
        return person;
    }

    @Override
    public Person updatePerson(Person person) {
        Person persistedPerson = person;
        if (!(person instanceof PersonDomainObject)) {
            persistedPerson = findPersonByUsername(person.getUsername());
            persistedPerson.setMail(person.getMail());
            persistedPerson.setPhone(person.getPhone());
        }
        entityManager.persist(persistedPerson);
        return persistedPerson;
    }

    @Override
    public void deletePerson(Person person) {
        entityManager.remove(person);
    }

    @Override
    public Person findPersonByUsername(String username) {
        TypedQuery<PersonDomainObject> query = entityManager.createNamedQuery("findPersonByUsername",
                PersonDomainObject.class);
        query.setParameter("username", username);
        return query.getSingleResult();
    }

    @Override
    public boolean contains(Person person) {
        TypedQuery<Long> query = entityManager.createNamedQuery("countPersonsByUsername", Long.class);
        return query.setParameter("username", person.getUsername()).getSingleResult() == 1;
    }

    @Override
    public List<? extends Person> findPersonsByTitle(String title) {
        TypedQuery<PersonDomainObject> query = entityManager.createNamedQuery("findPersonsByTitle",
                PersonDomainObject.class);
        query.setParameter("title", title);
        return query.getResultList();
    }

}