Java tutorial
/** * 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.BinaryAttachment; import com.siriusit.spezg.multilib.domain.Person; import com.siriusit.spezg.multilib.domain.Unit; import com.siriusit.spezg.multilib.domain.jpa.BinaryAttachmentDomainObject; import com.siriusit.spezg.multilib.domain.jpa.PersonDomainObject; import com.siriusit.spezg.multilib.domain.jpa.UnitDomainObject; import com.siriusit.spezg.multilib.storage.UnitRepository; /** * * @author Tommy B <Tommy.Bo@SiriusIT.com> * @author Thao Nguyen <thao.nguyen@SiriusIT.com> */ @Repository("unitRepository") public class JpaUnitRepository implements UnitRepository { @PersistenceContext(unitName = "multilib-db") private EntityManager entityManager; public EntityManager getEntityManager() { return entityManager; } public void setEntityManager(EntityManager entityManager) { this.entityManager = entityManager; } @Override public Unit createUnit(Person owner, String title, String description) { Unit unit = new UnitDomainObject((PersonDomainObject) owner, title, description); entityManager.persist(unit); return unit; } @Override public void updateUnit(Unit unit) { entityManager.persist(unit); } @Override public void deleteUnit(Unit unit) { entityManager.remove(unit); } @Override public List<? extends Unit> findUnitsByOwner(Person owner) { TypedQuery<UnitDomainObject> query = entityManager.createNamedQuery("findUnitsByOwner", UnitDomainObject.class); query.setParameter("owner", owner); return query.getResultList(); } @Override public List<? extends Unit> findUnitsByTitle(String title) { TypedQuery<UnitDomainObject> query = entityManager.createNamedQuery("findUnitsByTitle", UnitDomainObject.class); query.setParameter("title", title); return query.getResultList(); } @Override public List<String> findUnitTitlesBySubstring(String partialSearchQuery) { String transformedSearchQuery = partialSearchQuery == null ? null : "%" + partialSearchQuery.toUpperCase() + "%"; return entityManager.createQuery( "select distinct u.title from UnitDomainObject u where UPPER(u.title) like :title order by u.title", String.class).setParameter("title", transformedSearchQuery).getResultList(); } @Override public List<? extends Unit> searchForAvailableUnits() { TypedQuery<UnitDomainObject> query = entityManager.createNamedQuery("findAllAvailableUnits", UnitDomainObject.class); return query.getResultList(); } @Override public BinaryAttachment findAttachmentByType(String unitId, String attachmentType) { TypedQuery<BinaryAttachmentDomainObject> query = entityManager .createNamedQuery("findAttachmentByUnitAndType", BinaryAttachmentDomainObject.class); query.setParameter("unitId", Integer.parseInt(unitId)); query.setParameter("type", attachmentType); List<BinaryAttachmentDomainObject> resultList = query.getResultList(); if (resultList.size() == 1) return resultList.get(0); return null; } }