| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.student.core.document.dao.impl; | 
  | 17 |  |   | 
  | 18 |  |  import java.util.ArrayList; | 
  | 19 |  |  import java.util.List; | 
  | 20 |  |  import java.util.ListIterator; | 
  | 21 |  |   | 
  | 22 |  |  import javax.persistence.EntityManager; | 
  | 23 |  |  import javax.persistence.PersistenceContext; | 
  | 24 |  |  import javax.persistence.Query; | 
  | 25 |  |   | 
  | 26 |  |  import org.apache.log4j.Logger; | 
  | 27 |  |  import org.kuali.student.common.dao.impl.AbstractSearchableCrudDaoImpl; | 
  | 28 |  |  import org.kuali.student.common.exceptions.DoesNotExistException; | 
  | 29 |  |  import org.kuali.student.core.document.dao.DocumentDao; | 
  | 30 |  |  import org.kuali.student.core.document.entity.Document; | 
  | 31 |  |  import org.kuali.student.core.document.entity.DocumentCategory; | 
  | 32 |  |  import org.kuali.student.core.document.entity.RefDocRelation; | 
  | 33 |  |   | 
  | 34 |  |   | 
  | 35 |  |   | 
  | 36 |  |   | 
  | 37 |  |   | 
  | 38 |  |   | 
  | 39 | 2 |  public class DocumentDaoImpl extends AbstractSearchableCrudDaoImpl implements DocumentDao { | 
  | 40 |  |       | 
  | 41 | 2 |      final Logger logger = Logger.getLogger(DocumentDaoImpl.class); | 
  | 42 |  |       | 
  | 43 |  |      @PersistenceContext(unitName = "Document") | 
  | 44 |  |      @Override | 
  | 45 |  |      public void setEm(EntityManager em) { | 
  | 46 | 1 |          super.setEm(em); | 
  | 47 | 1 |      } | 
  | 48 |  |   | 
  | 49 |  |      @Override | 
  | 50 |  |      public Boolean addDocumentCategoryToDocument(String documentId, String documentCategoryKey) throws DoesNotExistException { | 
  | 51 |  |   | 
  | 52 | 3 |          DocumentCategory category = fetch(DocumentCategory.class, documentCategoryKey); | 
  | 53 | 3 |          Document doc = fetch(Document.class, documentId); | 
  | 54 | 3 |          List<DocumentCategory> categoryList = doc.getCategoryList(); | 
  | 55 | 3 |          categoryList.add(category); | 
  | 56 | 3 |          doc.setCategoryList(categoryList); | 
  | 57 | 3 |          update(doc); | 
  | 58 | 3 |          return true; | 
  | 59 |  |      } | 
  | 60 |  |   | 
  | 61 |  |      @Override | 
  | 62 |  |      public List<DocumentCategory> getCategoriesByDocument(String documentId) { | 
  | 63 |  |   | 
  | 64 | 6 |          Document doc = null; | 
  | 65 |  |          try { | 
  | 66 | 6 |              doc = fetch(Document.class, documentId); | 
  | 67 | 0 |          } catch (DoesNotExistException e) { | 
  | 68 | 0 |              logger.error("Exception occured: ", e); | 
  | 69 | 6 |          } | 
  | 70 | 6 |          List<DocumentCategory> categories = doc.getCategoryList(); | 
  | 71 |  |   | 
  | 72 | 6 |          return categories; | 
  | 73 |  |      } | 
  | 74 |  |   | 
  | 75 |  |      @Override | 
  | 76 |  |      public List<Document> getDocumentsByIdList(List<String> documentIdList) throws DoesNotExistException { | 
  | 77 | 1 |          List<Document> documents = new ArrayList<Document>(); | 
  | 78 | 1 |          for (String documentId : documentIdList) { | 
  | 79 |  |              Document document; | 
  | 80 | 1 |              document = fetch(Document.class, documentId); | 
  | 81 | 1 |              documents.add(document); | 
  | 82 | 1 |          } | 
  | 83 | 1 |          return documents; | 
  | 84 |  |      } | 
  | 85 |  |   | 
  | 86 |  |      @Override | 
  | 87 |  |      public Boolean removeDocumentCategoryFromDocument(String documentId, String documentCategoryKey) throws DoesNotExistException { | 
  | 88 | 3 |          Document document = fetch(Document.class, documentId); | 
  | 89 | 3 |          List<DocumentCategory> categories = document.getCategoryList(); | 
  | 90 | 3 |          ListIterator<DocumentCategory> listIterator = categories.listIterator(); | 
  | 91 |  |           | 
  | 92 |  |           | 
  | 93 | 3 |          if(categories.size()<=1){ | 
  | 94 | 1 |              return false; | 
  | 95 |  |          } | 
  | 96 | 6 |          for (int intIndex = 0; intIndex < categories.size(); intIndex++) { | 
  | 97 |  |   | 
  | 98 | 4 |              DocumentCategory category = listIterator.next(); | 
  | 99 | 4 |              if (category.getId().equals(documentCategoryKey)) { | 
  | 100 | 2 |                listIterator.remove(); | 
  | 101 |  |              } | 
  | 102 |  |          } | 
  | 103 | 2 |          document.setCategoryList(categories); | 
  | 104 | 2 |          update(document); | 
  | 105 | 2 |          return true; | 
  | 106 |  |      } | 
  | 107 |  |   | 
  | 108 |  |          @Override | 
  | 109 |  |          public List<RefDocRelation> getRefDocRelationsByRef( | 
  | 110 |  |                          String refObjectTypeKey, String refObjectId) { | 
  | 111 | 1 |                  Query query = em.createNamedQuery("RefDocRelation.getRefDocRelationsByRef"); | 
  | 112 | 1 |                  query.setParameter("refObjectTypeKey", refObjectTypeKey); | 
  | 113 | 1 |                  query.setParameter("refObjectId", refObjectId); | 
  | 114 |  |                  @SuppressWarnings("unchecked") | 
  | 115 | 1 |                  List<RefDocRelation> resultList = query.getResultList(); | 
  | 116 | 1 |                  return resultList; | 
  | 117 |  |          } | 
  | 118 |  |   | 
  | 119 |  |          @Override | 
  | 120 |  |          public List<RefDocRelation> getRefDocRelationsByDoc(String documentId) { | 
  | 121 | 1 |                  Query query = em.createNamedQuery("RefDocRelation.getRefDocRelationsByDoc"); | 
  | 122 | 1 |                  query.setParameter("documentId", documentId); | 
  | 123 |  |                  @SuppressWarnings("unchecked") | 
  | 124 | 1 |                  List<RefDocRelation> resultList = query.getResultList(); | 
  | 125 | 1 |                  return resultList; | 
  | 126 |  |          } | 
  | 127 |  |   | 
  | 128 |  |  } |