Coverage Report - org.kuali.rice.krad.dao.impl.NoteDaoOjb
 
Classes in this File Line Coverage Branch Coverage Complexity
NoteDaoOjb
0%
0/29
0%
0/10
1.714
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.krad.dao.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Collection;
 20  
 import java.util.List;
 21  
 
 22  
 import org.apache.log4j.Logger;
 23  
 import org.apache.ojb.broker.query.Criteria;
 24  
 import org.apache.ojb.broker.query.Query;
 25  
 import org.apache.ojb.broker.query.QueryByCriteria;
 26  
 import org.apache.ojb.broker.query.QueryFactory;
 27  
 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
 28  
 import org.kuali.rice.krad.bo.Attachment;
 29  
 import org.kuali.rice.krad.bo.Note;
 30  
 import org.kuali.rice.krad.dao.NoteDao;
 31  
 import org.springframework.dao.DataAccessException;
 32  
 
 33  
 /**
 34  
  * This class is the OJB implementation of the NoteDao interface.
 35  
  *
 36  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 37  
  */
 38  
 public class NoteDaoOjb extends PlatformAwareDaoBaseOjb implements NoteDao {
 39  0
     private static Logger LOG = Logger.getLogger(NoteDaoOjb.class);
 40  
 
 41  
     /**
 42  
      * Default constructor.
 43  
      */
 44  
     public NoteDaoOjb() {
 45  0
         super();
 46  0
     }
 47  
 
 48  
     /**
 49  
      * Saves a note to the DB using OJB.
 50  
      *
 51  
      * @param line
 52  
      */
 53  
     public void save(Note note) throws DataAccessException {
 54  
         // Add this check for KRAD to avoid saving the empty Attachments
 55  
         // TODO : look into avoiding the default empty attachments being added to the note
 56  0
         if (note.getAttachment() != null && note.getAttachment().getAttachmentFileName() == null) {
 57  0
             note.setAttachment(null);
 58  
         }
 59  
         //workaround in case sequence is empty  I shouldn't need this but ojb seems to work weird with this case
 60  0
         if (note != null && note.getNoteIdentifier() == null && note.getAttachment() != null) {
 61  0
             Attachment attachment = note.getAttachment();
 62  0
             note.setAttachment(null);
 63  
             //store without attachment
 64  0
             getPersistenceBrokerTemplate().store(note);
 65  0
             attachment.setNoteIdentifier(note.getNoteIdentifier());
 66  
             //put attachment back
 67  0
             note.setAttachment(attachment);
 68  
         }
 69  0
         getPersistenceBrokerTemplate().store(note);
 70  0
     }
 71  
 
 72  
     /**
 73  
      * Deletes a note from the DB using OJB.
 74  
      */
 75  
     public void deleteNote(Note note) throws DataAccessException {
 76  0
         getPersistenceBrokerTemplate().delete(note.getAttachment());
 77  0
         note.setAttachment(null);
 78  0
         getPersistenceBrokerTemplate().delete(note);
 79  
         
 80  0
     }
 81  
 
 82  
     /**
 83  
      * Retrieves document associated with a given object using OJB.
 84  
      *
 85  
      * @param id
 86  
      * @return
 87  
      */
 88  
     public List<Note> findByremoteObjectId(String remoteObjectId) {
 89  0
         Criteria criteria = new Criteria();
 90  
         //TODO: Notes - Chris move remoteObjectId string to constants
 91  0
         criteria.addEqualTo("RMT_OBJ_ID", remoteObjectId);
 92  
 
 93  0
         QueryByCriteria query = QueryFactory.newQuery(Note.class, criteria);
 94  
         //while this is currently called every time these methods could be changed to allow
 95  
         //custom sorting by BO see discussion on Notes confluence page
 96  0
         defaultOrderBy(query);
 97  0
         Collection<Note> notes = findCollection(query);
 98  
 
 99  0
         return new ArrayList<Note>(notes);
 100  
     }
 101  
     
 102  
     public Note getNoteByNoteId(Long noteId) {
 103  0
         Criteria crit = new Criteria();
 104  0
         crit.addEqualTo("noteIdentifier", noteId);
 105  0
         return (Note) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(Note.class, crit));          
 106  
     }
 107  
 
 108  
     /**
 109  
      * This method defines the default sort for notes
 110  
      * @param query
 111  
      */
 112  
     private void defaultOrderBy(QueryByCriteria query) {
 113  
         //TODO: Notes - Chris move remoteObjectId string to constants
 114  0
         query.addOrderBy("notePostedTimestamp", true);
 115  0
     }
 116  
 
 117  
 
 118  
     /**
 119  
      * Retrieve a Collection of note instances found by a query.
 120  
      *
 121  
      * @param query
 122  
      * @return
 123  
      */
 124  
     @SuppressWarnings("unchecked")
 125  
     private Collection<Note> findCollection(Query query) throws DataAccessException {
 126  0
         return getPersistenceBrokerTemplate().getCollectionByQuery(query);
 127  
     }
 128  
 }