Coverage Report - org.kuali.rice.kew.documentlink.dao.impl.DocumentLinkDAOJpaImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentLinkDAOJpaImpl
0%
0/41
0%
0/10
1.7
 
 1  
 /*
 2  
  * Copyright 2007-2010 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 javax.persistence.EntityManager;
 21  
 import javax.persistence.NoResultException;
 22  
 import javax.persistence.PersistenceContext;
 23  
 
 24  
 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
 25  
 import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
 26  
 import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
 27  
 import org.kuali.rice.kew.documentlink.DocumentLink;
 28  
 import org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO;
 29  
 import org.kuali.rice.krad.util.ObjectUtils;
 30  
 
 31  
 /**
 32  
  * This is a description of what this class does - g1zhang don't forget to fill this in. 
 33  
  * 
 34  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 35  
  *
 36  
  */
 37  0
 public class DocumentLinkDAOJpaImpl implements DocumentLinkDAO {
 38  
 
 39  
         
 40  
     @PersistenceContext(unitName = "kew-unit")
 41  
     private EntityManager entityManager;
 42  
     
 43  
     public EntityManager getEntityManager() {
 44  0
         return this.entityManager;
 45  
     }
 46  
 
 47  
     public void setEntityManager(EntityManager entityManager) {
 48  0
         this.entityManager = entityManager;
 49  0
     }
 50  
     
 51  
         /**
 52  
          * double delete all links from orgn doc
 53  
          * 
 54  
          * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#deleteDocmentLinksByDocId(java.lang.Long)
 55  
          */
 56  
         public void deleteDocmentLinksByDocId(String docId) {
 57  0
                 List<DocumentLink> links = getLinkedDocumentsByDocId(docId);
 58  0
                 for(DocumentLink link: links){
 59  0
                         deleteDocumentLink(link);
 60  
                 }
 61  0
         }
 62  
 
 63  
         /**
 64  
          * double delete a link
 65  
          * 
 66  
          * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#deleteDocumentLink(org.kuali.rice.kew.documentlink.DocumentLink)
 67  
          */
 68  
         public void deleteDocumentLink(DocumentLink link) {
 69  0
                 deleteSingleLinkFromOrgnDoc(link);
 70  0
                 deleteSingleLinkFromOrgnDoc(DocumentLinkDaoUtil.reverseLink((DocumentLink)ObjectUtils.deepCopy(link)));
 71  0
         }
 72  
 
 73  
         /**
 74  
          * get a link from orgn doc
 75  
          * 
 76  
          * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#getLinkedDocument(org.kuali.rice.kew.documentlink.DocumentLink)
 77  
          */
 78  
         public DocumentLink getLinkedDocument(DocumentLink link) {
 79  0
                 Criteria crit = new Criteria(DocumentLink.class.getName());
 80  0
                 crit.eq("orgnDocId", link.getOrgnDocId());
 81  0
                 crit.eq("destDocId", link.getDestDocId());
 82  
                 try {
 83  0
                         return (DocumentLink) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
 84  0
                 } catch (NoResultException e) {
 85  0
                         return null;
 86  
                 }
 87  
         }
 88  
 
 89  
         /**
 90  
          * get all links from orgn doc
 91  
          * 
 92  
          * @see org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO#getLinkedDocumentsByDocId(java.lang.Long)
 93  
          */
 94  
         public List<DocumentLink> getLinkedDocumentsByDocId(String docId) {
 95  0
                 Criteria crit = new Criteria(DocumentLink.class.getName());
 96  0
                 crit.eq("orgnDocId", docId);
 97  0
                 return (List<DocumentLink>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 98  
         
 99  
         }
 100  
         
 101  
         public List<DocumentLink> getOutgoingLinkedDocumentsByDocId(String docId) {
 102  0
                 Criteria crit = new Criteria(DocumentLink.class.getName());
 103  0
                 crit.eq("destDocId", docId);
 104  0
                 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  0
                 DocumentLink linkedDocument = getLinkedDocument(link);
 114  0
                 if(linkedDocument == null) {
 115  0
                         if (link.getDocLinkId() == null) {
 116  0
                                 entityManager.persist(link);
 117  
                         } else {
 118  0
                                 OrmUtils.merge(entityManager, link);
 119  
                         }
 120  
                 } else {
 121  0
                         link.setDocLinkId(linkedDocument.getDocLinkId());
 122  
                 }
 123  
 //                //if we want a 2-way linked pair
 124  0
                 DocumentLink rLink = DocumentLinkDaoUtil.reverseLink((DocumentLink)ObjectUtils.deepCopy(link));
 125  0
                 if(getLinkedDocument(rLink) == null) {
 126  0
                         if (link.getDocLinkId() == null) {
 127  0
                                 entityManager.persist(rLink);
 128  
                         } else {
 129  0
                                 OrmUtils.merge(entityManager, rLink);
 130  
                         }
 131  
                 }
 132  
 
 133  0
         }
 134  
         
 135  
         private void deleteSingleLinkFromOrgnDoc(DocumentLink link){
 136  0
                 DocumentLink cur = getLinkedDocument(link);
 137  0
                 entityManager.remove(cur) ;
 138  0
         }
 139  
 
 140  
         @Override
 141  
         public DocumentLink getDocumentLink(Long documentLinkId) {
 142  0
                 Criteria crit = new Criteria(DocumentLink.class.getName());
 143  0
                 crit.eq("docLinkId", documentLinkId);
 144  0
                 return (DocumentLink) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
 145  
         }
 146  
         
 147  
 }