001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.kew.documentlink.dao.impl; 017 018import org.kuali.rice.kew.documentlink.DocumentLink; 019import org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO; 020import org.kuali.rice.krad.data.DataObjectService; 021import org.springframework.beans.factory.annotation.Required; 022 023import javax.persistence.EntityManager; 024import javax.persistence.NoResultException; 025import javax.persistence.TypedQuery; 026import java.util.List; 027 028/** 029 * A JPA-based implementation of the {@link DocumentLinkDAO}. 030 * 031 * @author Kuali Rice Team (rice.collab@kuali.org) 032 */ 033public class DocumentLinkDAOJpa implements DocumentLinkDAO { 034 035 private EntityManager entityManager; 036 private DataObjectService dataObjectService; 037 038 @Override 039 public void deleteDocumentLink(DocumentLink link) { 040 getDataObjectService().delete(link); 041 // see if a reverse link exists or not 042 DocumentLink reverseLink = getLinkedDocument(link.getDestDocId(), link.getOrgnDocId()); 043 if (reverseLink != null) { 044 getDataObjectService().delete(reverseLink); 045 } 046 } 047 048 @Override 049 public List<DocumentLink> getLinkedDocumentsByDocId(String docId) { 050 TypedQuery<DocumentLink> query = 051 getEntityManager().createNamedQuery("DocumentLink.GetLinkedDocumentsByDocId", DocumentLink.class); 052 query.setParameter("orgnDocId",docId); 053 return query.getResultList(); 054 055 } 056 057 @Override 058 public List<DocumentLink> getOutgoingLinkedDocumentsByDocId(String docId) { 059 TypedQuery<DocumentLink> query = 060 getEntityManager().createNamedQuery("DocumentLink.GetOutgoingLinkedDocumentsByDocId", DocumentLink.class); 061 query.setParameter("destDocId",docId); 062 return query.getResultList(); 063 } 064 065 @Override 066 public DocumentLink saveDocumentLink(DocumentLink link) { 067 link = saveIfNotExists(link); 068 // create the 2-way linked pair 069 saveIfNotExists(createReverseLink(link)); 070 getDataObjectService().flush(DocumentLink.class); 071 return link; 072 } 073 074 protected DocumentLink saveIfNotExists(DocumentLink link) { 075 // if an existing link already exists for this, we pretty much just ignore the request to save since it's 076 // already there 077 DocumentLink existingLink = getLinkedDocument(link.getOrgnDocId(), link.getDestDocId()); 078 if (existingLink == null) { 079 link = getDataObjectService().save(link); 080 } else { 081 link = existingLink; 082 } 083 return link; 084 } 085 086 protected DocumentLink getLinkedDocument(String orgnDocId, String destDocId) { 087 TypedQuery<DocumentLink> query = 088 getEntityManager().createNamedQuery("DocumentLink.GetLinkedDocument", DocumentLink.class); 089 query.setParameter("orgnDocId", orgnDocId); 090 query.setParameter("destDocId", destDocId); 091 try { 092 return query.getSingleResult(); 093 } catch (NoResultException e) { 094 return null; 095 } 096 } 097 098 099 private DocumentLink createReverseLink(DocumentLink link) { 100 DocumentLink reverseLink = new DocumentLink(); 101 reverseLink.setOrgnDocId(link.getDestDocId()); 102 reverseLink.setDestDocId(link.getOrgnDocId()); 103 return reverseLink; 104 } 105 106 @Override 107 public DocumentLink getDocumentLink(String documentLinkId) { 108 return getDataObjectService().find(DocumentLink.class,documentLinkId); 109 } 110 111 112 public DataObjectService getDataObjectService() { 113 return dataObjectService; 114 } 115 116 @Required 117 public void setDataObjectService(DataObjectService dataObjectService) { 118 this.dataObjectService = dataObjectService; 119 } 120 121 122 public EntityManager getEntityManager() { 123 return this.entityManager; 124 } 125 126 public void setEntityManager(EntityManager entityManager) { 127 this.entityManager = entityManager; 128 } 129 130}