001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.kew.documentlink.dao.impl;
017
018import java.util.List;
019
020import javax.persistence.EntityManager;
021import javax.persistence.NoResultException;
022import javax.persistence.PersistenceContext;
023
024import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
025import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
026import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
027import org.kuali.rice.kew.documentlink.DocumentLink;
028import org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO;
029import org.kuali.rice.krad.util.ObjectUtils;
030
031/**
032 * This is a description of what this class does - g1zhang don't forget to fill this in. 
033 * 
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 *
036 */
037public class DocumentLinkDAOJpaImpl implements DocumentLinkDAO {
038
039        
040    @PersistenceContext(unitName = "kew-unit")
041    private EntityManager entityManager;
042    
043    public EntityManager getEntityManager() {
044        return this.entityManager;
045    }
046
047    public void setEntityManager(EntityManager entityManager) {
048        this.entityManager = entityManager;
049    }
050    
051        /**
052         * double delete all links from orgn doc
053         * 
054         * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#deleteDocmentLinksByDocId(java.lang.Long)
055         */
056        public void deleteDocmentLinksByDocId(String docId) {
057                List<DocumentLink> links = getLinkedDocumentsByDocId(docId);
058                for(DocumentLink link: links){
059                        deleteDocumentLink(link);
060                }
061        }
062
063        /**
064         * double delete a link
065         * 
066         * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#deleteDocumentLink(org.kuali.rice.kew.documentlink.DocumentLink)
067         */
068        public void deleteDocumentLink(DocumentLink link) {
069                deleteSingleLinkFromOrgnDoc(link);
070                deleteSingleLinkFromOrgnDoc(DocumentLinkDaoUtil.reverseLink((DocumentLink)ObjectUtils.deepCopy(link)));
071        }
072
073        /**
074         * get a link from orgn doc
075         * 
076         * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#getLinkedDocument(org.kuali.rice.kew.documentlink.DocumentLink)
077         */
078        public DocumentLink getLinkedDocument(DocumentLink link) {
079                Criteria crit = new Criteria(DocumentLink.class.getName());
080                crit.eq("orgnDocId", link.getOrgnDocId());
081                crit.eq("destDocId", link.getDestDocId());
082                try {
083                        return (DocumentLink) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
084                } catch (NoResultException e) {
085                        return null;
086                }
087        }
088
089        /**
090         * get all links from orgn doc
091         * 
092         * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#getLinkedDocumentsByDocId(java.lang.Long)
093         */
094        public List<DocumentLink> getLinkedDocumentsByDocId(String docId) {
095                Criteria crit = new Criteria(DocumentLink.class.getName());
096                crit.eq("orgnDocId", docId);
097                return (List<DocumentLink>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
098        
099        }
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}