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