View Javadoc

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