001 /* 002 * Copyright 2007-2010 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 */ 016 package org.kuali.rice.kew.documentlink.dao.impl; 017 018 import java.util.List; 019 020 import javax.persistence.EntityManager; 021 import javax.persistence.PersistenceContext; 022 023 import org.kuali.rice.core.jpa.criteria.Criteria; 024 import org.kuali.rice.core.jpa.criteria.QueryByCriteria; 025 import org.kuali.rice.kew.doctype.bo.DocumentType; 026 import org.kuali.rice.kew.documentlink.DocumentLink; 027 import org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO; 028 029 /** 030 * This is a description of what this class does - g1zhang don't forget to fill this in. 031 * 032 * @author Kuali Rice Team (rice.collab@kuali.org) 033 * 034 */ 035 public class DocumentLinkDAOJpaImpl implements DocumentLinkDAO { 036 037 038 @PersistenceContext(unitName = "kew-unit") 039 private EntityManager entityManager; 040 041 public EntityManager getEntityManager() { 042 return this.entityManager; 043 } 044 045 public void setEntityManager(EntityManager entityManager) { 046 this.entityManager = entityManager; 047 } 048 049 /** 050 * double delete all links from orgn doc 051 * 052 * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#deleteDocmentLinksByDocId(java.lang.Long) 053 */ 054 public void deleteDocmentLinksByDocId(Long docId) { 055 List<DocumentLink> links = getLinkedDocumentsByDocId(docId); 056 for(DocumentLink link: links){ 057 deleteDocumentLink(link); 058 } 059 } 060 061 /** 062 * double delete a link 063 * 064 * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#deleteDocumentLink(org.kuali.rice.kew.documentlink.DocumentLink) 065 */ 066 public void deleteDocumentLink(DocumentLink link) { 067 deleteSingleLinkFromOrgnDoc(link); 068 deleteSingleLinkFromOrgnDoc(DocumentLinkDaoUtil.reverseLink(link)); 069 } 070 071 /** 072 * get a link from orgn doc 073 * 074 * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#getLinkedDocument(org.kuali.rice.kew.documentlink.DocumentLink) 075 */ 076 public DocumentLink getLinkedDocument(DocumentLink link) { 077 Criteria crit = new Criteria(DocumentLink.class.getName()); 078 crit.eq("orgnDocId", link.getOrgnDocId()); 079 crit.eq("destDocId", link.getDestDocId()); 080 return (DocumentLink) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult(); 081 } 082 083 /** 084 * get all links from orgn doc 085 * 086 * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#getLinkedDocumentsByDocId(java.lang.Long) 087 */ 088 public List<DocumentLink> getLinkedDocumentsByDocId(Long docId) { 089 Criteria crit = new Criteria(DocumentLink.class.getName()); 090 crit.eq("orgnDocId", docId); 091 return (List<DocumentLink>) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult(); 092 093 } 094 095 /** 096 * add double link 097 * 098 * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#saveDocumentLink(org.kuali.rice.kew.documentlink.DocumentLink) 099 */ 100 public void saveDocumentLink(DocumentLink link) { 101 if(getLinkedDocument(link) == null) 102 entityManager.persist(link); 103 // //if we want a 2-way linked pair 104 DocumentLink rLink = DocumentLinkDaoUtil.reverseLink(link); 105 if(getLinkedDocument(rLink) == null) 106 entityManager.persist(link); 107 108 } 109 110 private void deleteSingleLinkFromOrgnDoc(DocumentLink link){ 111 DocumentLink cur = getLinkedDocument(link); 112 entityManager.remove(cur) ; 113 } 114 115 }