View Javadoc

1   /**
2    * Copyright 2005-2014 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 org.apache.ojb.broker.query.Criteria;
21  import org.apache.ojb.broker.query.QueryByCriteria;
22  import org.kuali.rice.kew.documentlink.DocumentLink;
23  import org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO;
24  import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
25  
26  /**
27   * This is a description of what this class does - g1zhang don't forget to fill this in. 
28   * 
29   * @author Kuali Rice Team (kuali-rice@googlegroups.com)
30   *
31   */
32  public class DocumentLinkDAOOjbImpl extends PersistenceBrokerDaoSupport implements DocumentLinkDAO{
33  
34  	public void saveDocumentLink(DocumentLink link) {
35  		DocumentLink linkedDocument = getLinkedDocument(link);
36  		if(linkedDocument == null)
37  			this.getPersistenceBrokerTemplate().store(link);
38  		else
39  			link.setDocLinkId(linkedDocument.getDocLinkId());
40  		//if we want a 2-way linked pair
41  		DocumentLink rLink = DocumentLinkDaoUtil.reverseLink(link);
42  		if(getLinkedDocument(rLink) == null)
43  			this.getPersistenceBrokerTemplate().store(rLink);
44  	}
45  
46  	public void deleteDocumentLink(DocumentLink link) {
47  		deleteSingleLinkFromOrgnDoc(link);
48  		deleteSingleLinkFromOrgnDoc(DocumentLinkDaoUtil.reverseLink(link));
49  	}
50  
51  	//double delete style
52  	public void deleteDocmentLinksByDocId(String docId){
53  		List<DocumentLink> links = getLinkedDocumentsByDocId(docId);
54  		for(DocumentLink link: links){
55  			deleteDocumentLink(link);
56  		}
57  		
58  	}
59  
60  	/**
61  	 * This overridden method ...
62  	 * 
63  	 * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#getLinkedDocumentByDocId(java.lang.Long)
64  	 */
65  	public List<DocumentLink> getLinkedDocumentsByDocId(String docId) {
66  		Criteria crit = new Criteria();
67  		crit.addEqualTo("orgnDocId", docId);
68  		QueryByCriteria query = new QueryByCriteria(DocumentLink.class, crit);
69  		query.addOrderByAscending("orgnDocId");
70  		return (List<DocumentLink>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);  
71  	}
72  	
73  	public List<DocumentLink> getOutgoingLinkedDocumentsByDocId(String docId) {
74  		Criteria crit = new Criteria();
75  		crit.addEqualTo("destDocId", docId);
76  		QueryByCriteria query = new QueryByCriteria(DocumentLink.class, crit);
77  		query.addOrderByAscending("destDocId");
78  		return (List<DocumentLink>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);  
79  	}
80  
81  	/**
82  	 * This overridden method ...
83  	 * 
84  	 * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#getLinkedDocument(org.kuali.rice.kew.documentlink.DocumentLink)
85  	 */
86  	public DocumentLink getLinkedDocument(DocumentLink link) {
87  		Criteria crit = new Criteria();
88  		crit.addEqualTo("orgnDocId", link.getOrgnDocId());
89  		crit.addEqualTo("destDocId", link.getDestDocId());
90  		QueryByCriteria query = new QueryByCriteria(DocumentLink.class, crit);
91  		query.addOrderByAscending("orgnDocId");
92  		return (DocumentLink) this.getPersistenceBrokerTemplate().getObjectByQuery(query);  
93  	}
94  
95  	private void deleteSingleLinkFromOrgnDoc(DocumentLink link){
96  		Criteria crit = new Criteria();
97  		crit.addEqualTo("orgnDocId", link.getOrgnDocId());
98  		crit.addEqualTo("destDocId", link.getDestDocId());
99  		this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(DocumentLink.class, crit));
100 	}
101 
102 	public void deleteSingleLinksByOrgnDocId(String docId){
103 		Criteria crit = new Criteria();
104 		crit.addEqualTo("orgnDocId", docId);
105 		this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(DocumentLink.class, crit));
106 	}
107 	
108 	public DocumentLink getDocumentLink(Long documentLinkId) {
109 		Criteria crit = new Criteria();
110 		crit.addEqualTo("docLinkId", documentLinkId);
111 		return (DocumentLink) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(DocumentLink.class, crit));
112 	}
113 }