001 /** 002 * Copyright 2005-2013 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 */ 016 package org.kuali.rice.krad.dao.impl; 017 018 import java.util.ArrayList; 019 import java.util.Collection; 020 import java.util.List; 021 022 import org.apache.log4j.Logger; 023 import org.apache.ojb.broker.query.Criteria; 024 import org.apache.ojb.broker.query.Query; 025 import org.apache.ojb.broker.query.QueryByCriteria; 026 import org.apache.ojb.broker.query.QueryFactory; 027 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb; 028 import org.kuali.rice.krad.bo.Attachment; 029 import org.kuali.rice.krad.bo.Note; 030 import org.kuali.rice.krad.dao.NoteDao; 031 import org.springframework.dao.DataAccessException; 032 033 /** 034 * This class is the OJB implementation of the NoteDao interface. 035 * 036 * @author Kuali Rice Team (rice.collab@kuali.org) 037 */ 038 public class NoteDaoOjb extends PlatformAwareDaoBaseOjb implements NoteDao { 039 private static Logger LOG = Logger.getLogger(NoteDaoOjb.class); 040 041 /** 042 * Default constructor. 043 */ 044 public NoteDaoOjb() { 045 super(); 046 } 047 048 /** 049 * Saves a note to the DB using OJB. 050 * 051 * @param line 052 */ 053 public void save(Note note) throws DataAccessException { 054 // Add this check for KRAD to avoid saving the empty Attachments 055 // TODO : look into avoiding the default empty attachments being added to the note 056 if (note.getAttachment() != null && note.getAttachment().getAttachmentFileName() == null) { 057 note.setAttachment(null); 058 } 059 //workaround in case sequence is empty I shouldn't need this but ojb seems to work weird with this case 060 if (note != null && note.getNoteIdentifier() == null && note.getAttachment() != null) { 061 Attachment attachment = note.getAttachment(); 062 note.setAttachment(null); 063 //store without attachment 064 getPersistenceBrokerTemplate().store(note); 065 attachment.setNoteIdentifier(note.getNoteIdentifier()); 066 //put attachment back 067 note.setAttachment(attachment); 068 } 069 getPersistenceBrokerTemplate().store(note); 070 } 071 072 /** 073 * Deletes a note from the DB using OJB. 074 */ 075 public void deleteNote(Note note) throws DataAccessException { 076 getPersistenceBrokerTemplate().delete(note.getAttachment()); 077 note.setAttachment(null); 078 getPersistenceBrokerTemplate().delete(note); 079 080 } 081 082 /** 083 * Retrieves document associated with a given object using OJB. 084 * 085 * @param id 086 * @return 087 */ 088 public List<Note> findByremoteObjectId(String remoteObjectId) { 089 Criteria criteria = new Criteria(); 090 //TODO: Notes - Chris move remoteObjectId string to constants 091 criteria.addEqualTo("RMT_OBJ_ID", remoteObjectId); 092 093 QueryByCriteria query = QueryFactory.newQuery(Note.class, criteria); 094 //while this is currently called every time these methods could be changed to allow 095 //custom sorting by BO see discussion on Notes confluence page 096 defaultOrderBy(query); 097 Collection<Note> notes = findCollection(query); 098 099 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 }