001    /*
002     * Copyright 2007-2009 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 org.kuali.rice.kew.documentlink.DocumentLink;
021    import org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO;
022    import org.apache.ojb.broker.query.Criteria;
023    import org.apache.ojb.broker.query.QueryByCriteria;
024    import org.kuali.rice.kew.notes.Note;
025    import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
026    
027    /**
028     * This is a description of what this class does - g1zhang don't forget to fill this in. 
029     * 
030     * @author Kuali Rice Team (kuali-rice@googlegroups.com)
031     *
032     */
033    public class DocumentLinkDAOOjbImpl extends PersistenceBrokerDaoSupport implements DocumentLinkDAO{
034    
035            public void saveDocumentLink(DocumentLink link) {
036                    if(getLinkedDocument(link) == null)
037                            this.getPersistenceBrokerTemplate().store(link);        
038                    //if we want a 2-way linked pair
039                    DocumentLink rLink = DocumentLinkDaoUtil.reverseLink(link);
040                    if(getLinkedDocument(rLink) == null)
041                            this.getPersistenceBrokerTemplate().store(rLink);
042            }
043    
044            public void deleteDocumentLink(DocumentLink link) {
045                    deleteSingleLinkFromOrgnDoc(link);
046                    deleteSingleLinkFromOrgnDoc(DocumentLinkDaoUtil.reverseLink(link));
047            }
048    
049            //double delete style
050            public void deleteDocmentLinksByDocId(Long docId){
051                    List<DocumentLink> links = getLinkedDocumentsByDocId(docId);
052                    for(DocumentLink link: links){
053                            deleteDocumentLink(link);
054                    }
055                    
056            }
057    
058            /**
059             * This overridden method ...
060             * 
061             * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#getLinkedDocumentByDocId(java.lang.Long)
062             */
063            public List<DocumentLink> getLinkedDocumentsByDocId(Long docId) {
064                    Criteria crit = new Criteria();
065                    crit.addEqualTo("orgnDocId", docId);
066                    QueryByCriteria query = new QueryByCriteria(DocumentLink.class, crit);
067                    query.addOrderByAscending("orgnDocId");
068                    return (List<DocumentLink>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);  
069            }
070    
071            /**
072             * This overridden method ...
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();
078                    crit.addEqualTo("orgnDocId", link.getOrgnDocId());
079                    crit.addEqualTo("destDocId", link.getDestDocId());
080                    QueryByCriteria query = new QueryByCriteria(DocumentLink.class, crit);
081                    query.addOrderByAscending("orgnDocId");
082                    return (DocumentLink) this.getPersistenceBrokerTemplate().getObjectByQuery(query);  
083            }
084    
085            private void deleteSingleLinkFromOrgnDoc(DocumentLink link){
086                    Criteria crit = new Criteria();
087                    crit.addEqualTo("orgnDocId", link.getOrgnDocId());
088                    crit.addEqualTo("destDocId", link.getDestDocId());
089                    this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(DocumentLink.class, crit));
090            }
091    
092            public void deleteSingleLinksByOrgnDocId(Long docId){
093                    Criteria crit = new Criteria();
094                    crit.addEqualTo("orgnDocId", docId);
095                    this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(DocumentLink.class, crit));
096            }
097    }