View Javadoc

1   /**
2    * Copyright 2005-2014 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      private static Logger LOG = Logger.getLogger(NoteDaoOjb.class);
40  
41      /**
42       * Default constructor.
43       */
44      public NoteDaoOjb() {
45          super();
46      }
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          if (note.getAttachment() != null && note.getAttachment().getAttachmentFileName() == null) {
57              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          if (note != null && note.getNoteIdentifier() == null && note.getAttachment() != null) {
61              Attachment attachment = note.getAttachment();
62              note.setAttachment(null);
63              //store without attachment
64              getPersistenceBrokerTemplate().store(note);
65              attachment.setNoteIdentifier(note.getNoteIdentifier());
66              //put attachment back
67              note.setAttachment(attachment);
68          }
69          getPersistenceBrokerTemplate().store(note);
70      }
71  
72      /**
73       * Deletes a note from the DB using OJB.
74       */
75      public void deleteNote(Note note) throws DataAccessException {
76          getPersistenceBrokerTemplate().delete(note.getAttachment());
77          note.setAttachment(null);
78          getPersistenceBrokerTemplate().delete(note);
79          
80      }
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          Criteria criteria = new Criteria();
90          //TODO: Notes - Chris move remoteObjectId string to constants
91          criteria.addEqualTo("RMT_OBJ_ID", remoteObjectId);
92  
93          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          defaultOrderBy(query);
97          Collection<Note> notes = findCollection(query);
98  
99          return new ArrayList<Note>(notes);
100     }
101     
102     public Note getNoteByNoteId(Long noteId) {
103         Criteria crit = new Criteria();
104         crit.addEqualTo("noteIdentifier", noteId);
105         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         query.addOrderBy("notePostedTimestamp", true);
115     }
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         return getPersistenceBrokerTemplate().getCollectionByQuery(query);
127     }
128 }