001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.core.dao.ojb;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020:
021: import org.apache.log4j.Logger;
022: import org.apache.ojb.broker.query.Criteria;
023: import org.apache.ojb.broker.query.Query;
024: import org.apache.ojb.broker.query.QueryByCriteria;
025: import org.apache.ojb.broker.query.QueryFactory;
026: import org.kuali.core.bo.Attachment;
027: import org.kuali.core.bo.Note;
028: import org.kuali.core.dao.NoteDao;
029: import org.springframework.dao.DataAccessException;
030:
031: /**
032: * This class is the OJB implementation of the NoteDao interface.
033: *
034: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
035: */
036: public class NoteDaoOjb extends PlatformAwareDaoBaseOjb implements
037: NoteDao {
038: private static Logger LOG = Logger.getLogger(NoteDaoOjb.class);
039:
040: /**
041: * Default constructor.
042: */
043: public NoteDaoOjb() {
044: super ();
045: }
046:
047: /**
048: * Saves a note to the DB using OJB.
049: *
050: * @param line
051: */
052: public void save(Note note) throws DataAccessException {
053: //workaround in case sequence is empty I shouldn't need this but ojb seems to work weird with this case
054: if (note != null && note.getNoteIdentifier() == null
055: && note.getAttachment() != null) {
056: Attachment attachment = note.getAttachment();
057: note.setAttachment(null);
058: //store without attachment
059: getPersistenceBrokerTemplate().store(note);
060: attachment.setNoteIdentifier(note.getNoteIdentifier());
061: //put attachment back
062: note.setAttachment(attachment);
063: }
064: getPersistenceBrokerTemplate().store(note);
065: }
066:
067: /**
068: * Deletes a note from the DB using OJB.
069: */
070: public void deleteNote(Note note) throws DataAccessException {
071: getPersistenceBrokerTemplate().delete(note.getAttachment());
072: note.setAttachment(null);
073: getPersistenceBrokerTemplate().delete(note);
074:
075: }
076:
077: /**
078: * Retrieves document associated with a given object using OJB.
079: *
080: * @param id
081: * @return
082: */
083: public ArrayList findByremoteObjectId(String remoteObjectId) {
084: Criteria criteria = new Criteria();
085: //TODO: Notes - Chris move remoteObjectId string to constants
086: criteria.addEqualTo("RMT_OBJ_ID", remoteObjectId);
087:
088: QueryByCriteria query = QueryFactory.newQuery(Note.class,
089: criteria);
090: //while this is currently called every time these methods could be changed to allow
091: //custom sorting by BO see discussion on Notes confluence page
092: defaultOrderBy(query);
093: Collection notes = findCollection(query);
094:
095: return new ArrayList(notes);
096: }
097:
098: /**
099: * This method defines the default sort for notes
100: * @param query
101: */
102: private void defaultOrderBy(QueryByCriteria query) {
103: //TODO: Notes - Chris move remoteObjectId string to constants
104: query.addOrderBy("notePostedTimestamp", true);
105: }
106:
107: /**
108: * Retrieve a Collection of note instances found by a query.
109: *
110: * @param query
111: * @return
112: */
113: private Collection findCollection(Query query)
114: throws DataAccessException {
115: return getPersistenceBrokerTemplate().getCollectionByQuery(
116: query);
117: }
118: }
|