View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.documentlink.dao.impl;
17  
18  import java.util.List;
19  
20  import javax.persistence.EntityManager;
21  import javax.persistence.NoResultException;
22  import javax.persistence.PersistenceContext;
23  
24  import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
25  import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
26  import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
27  import org.kuali.rice.kew.documentlink.DocumentLink;
28  import org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO;
29  import org.kuali.rice.krad.util.ObjectUtils;
30  
31  /**
32   * This is a description of what this class does - g1zhang don't forget to fill this in. 
33   * 
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   *
36   */
37  public class DocumentLinkDAOJpaImpl implements DocumentLinkDAO {
38  
39  	
40      @PersistenceContext(unitName = "kew-unit")
41      private EntityManager entityManager;
42      
43      public EntityManager getEntityManager() {
44          return this.entityManager;
45      }
46  
47      public void setEntityManager(EntityManager entityManager) {
48          this.entityManager = entityManager;
49      }
50      
51  	/**
52  	 * double delete all links from orgn doc
53  	 * 
54  	 * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#deleteDocmentLinksByDocId(java.lang.Long)
55  	 */
56  	public void deleteDocmentLinksByDocId(String docId) {
57  		List<DocumentLink> links = getLinkedDocumentsByDocId(docId);
58  		for(DocumentLink link: links){
59  			deleteDocumentLink(link);
60  		}
61  	}
62  
63  	/**
64  	 * double delete a link
65  	 * 
66  	 * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#deleteDocumentLink(org.kuali.rice.kew.documentlink.DocumentLink)
67  	 */
68  	public void deleteDocumentLink(DocumentLink link) {
69  		deleteSingleLinkFromOrgnDoc(link);
70  		deleteSingleLinkFromOrgnDoc(DocumentLinkDaoUtil.reverseLink((DocumentLink)ObjectUtils.deepCopy(link)));
71  	}
72  
73  	/**
74  	 * get a link from orgn doc
75  	 * 
76  	 * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#getLinkedDocument(org.kuali.rice.kew.documentlink.DocumentLink)
77  	 */
78  	public DocumentLink getLinkedDocument(DocumentLink link) {
79  		Criteria crit = new Criteria(DocumentLink.class.getName());
80  		crit.eq("orgnDocId", link.getOrgnDocId());
81  		crit.eq("destDocId", link.getDestDocId());
82  		try {
83  			return (DocumentLink) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
84  		} catch (NoResultException e) {
85  			return null;
86  		}
87  	}
88  
89  	/**
90  	 * get all links from orgn doc
91  	 * 
92  	 * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#getLinkedDocumentsByDocId(java.lang.Long)
93  	 */
94  	public List<DocumentLink> getLinkedDocumentsByDocId(String docId) {
95  		Criteria crit = new Criteria(DocumentLink.class.getName());
96  		crit.eq("orgnDocId", docId);
97  		return (List<DocumentLink>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
98  	
99  	}
100 	
101 	public List<DocumentLink> getOutgoingLinkedDocumentsByDocId(String docId) {
102 		Criteria crit = new Criteria(DocumentLink.class.getName());
103 		crit.eq("destDocId", docId);
104 		return (List<DocumentLink>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
105 	}
106 
107 	/**
108 	 * add double link
109 	 * 
110 	 * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#saveDocumentLink(org.kuali.rice.kew.documentlink.DocumentLink)
111 	 */
112 	public void saveDocumentLink(DocumentLink link) {
113 		DocumentLink linkedDocument = getLinkedDocument(link);
114 		if(linkedDocument == null) {
115 			if (link.getDocLinkId() == null) {
116 				entityManager.persist(link);
117 			} else {
118 				OrmUtils.merge(entityManager, link);
119 			}
120 		} else {
121 			link.setDocLinkId(linkedDocument.getDocLinkId());
122 		}
123 //		//if we want a 2-way linked pair
124 		DocumentLink rLink = DocumentLinkDaoUtil.reverseLink((DocumentLink)ObjectUtils.deepCopy(link));
125 		if(getLinkedDocument(rLink) == null) {
126 			if (link.getDocLinkId() == null) {
127 				entityManager.persist(rLink);
128 			} else {
129 				OrmUtils.merge(entityManager, rLink);
130 			}
131 		}
132 
133 	}
134 	
135 	private void deleteSingleLinkFromOrgnDoc(DocumentLink link){
136 		DocumentLink cur = getLinkedDocument(link);
137 		entityManager.remove(cur) ;
138 	}
139 
140 	@Override
141 	public DocumentLink getDocumentLink(Long documentLinkId) {
142 		Criteria crit = new Criteria(DocumentLink.class.getName());
143 		crit.eq("docLinkId", documentLinkId);
144 		return (DocumentLink) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
145 	}
146 	
147 }