01: package com.technoetic.xplanner.db;
02:
03: import com.technoetic.xplanner.domain.*;
04: import com.technoetic.xplanner.file.File;
05: import net.sf.hibernate.Hibernate;
06: import net.sf.hibernate.HibernateException;
07: import net.sf.hibernate.Session;
08: import net.sf.hibernate.type.NullableType;
09:
10: import java.util.Arrays;
11: import java.util.Iterator;
12: import java.util.List;
13:
14: /**
15: * User: Mateusz Prokopowicz
16: * Date: Dec 16, 2004
17: * Time: 2:10:17 PM
18: */
19: public class NoteHelper {
20: //FIXME: Externalize all these query in the Note.xml mapping file
21: //DEBT: Turn this into a NoteRepository that is spring loaded. No more static entry point. A session is passed in at construction time.
22: static final String SELECT_NOTES_FILE_IS_ATTACHED_TO = "select note from Note note where note.file.id= ?";
23: static final String FILE_DELETE_QUERY = "from file in "
24: + File.class + " where file.id= ?";
25: static final String NOTE_DELETE_QUERY = "from note in "
26: + Note.class + " where note.id = ?";
27: static final String SELECT_NOTE_ATTACHED_TO = "select note from Note note where note.attachedToId = ?";
28: static final String SELECT_ATTACHED_TO_ID = "SELECT t.id, s.id, i.id, p.id FROM "
29: + Project.class.getName()
30: + " as p left join p.iterations as i left join i.userStories as s left join s.tasks as t"
31: + " WHERE p.id = ? or i.id = ? or s.id = ? or t.id = ? order by i.id, s.id, t.id";
32: static final String NOTE_DELETION_QUERY = "from note in "
33: + Note.class + " where note.attachedToId = ?";
34:
35: static final Class[] DELETE_ORDER = { Task.class, UserStory.class,
36: Iteration.class, Project.class };
37:
38: public static int deleteNote(Note note, Session session)
39: throws HibernateException {
40: if (note.getFile() != null) {
41: List noteList = session.find(
42: SELECT_NOTES_FILE_IS_ATTACHED_TO, new Integer(note
43: .getFile().getId()), Hibernate.INTEGER);
44: if (noteList != null && noteList.size() == 1) {
45: session.delete(FILE_DELETE_QUERY, new Integer(
46: ((Note) noteList.get(0)).getFile().getId()),
47: Hibernate.INTEGER);
48: }
49: }
50: return session.delete(NOTE_DELETE_QUERY, new Integer(note
51: .getId()), Hibernate.INTEGER);
52: }
53:
54: public static int deleteNotesFor(NoteAttachable obj, Session session)
55: throws HibernateException {
56: return deleteNotesFor(obj.getClass(), obj.getId(), session);
57: }
58:
59: public static int deleteNotesFor(Class clazz, int objectId,
60: Session session) throws HibernateException {
61: Integer[] ids = new Integer[4];
62: Arrays.fill(ids, new Integer(objectId));
63: NullableType[] types = new NullableType[4];
64: Arrays.fill(types, Hibernate.INTEGER);
65: Iterator objIt = session.iterate(SELECT_ATTACHED_TO_ID, ids,
66: types);
67: Object[] oldRow = new Object[4];
68: Arrays.fill(oldRow, new Integer(-1));
69: int retVal = 0;
70: while (objIt.hasNext()) {
71: Object[] row = (Object[]) objIt.next();
72: for (int i = 0; i <= Arrays.asList(DELETE_ORDER).indexOf(
73: clazz); i++) {
74: if (row[i] != null && !row[i].equals(oldRow[i])) {
75: List noteList = session.find(
76: SELECT_NOTE_ATTACHED_TO, row[i],
77: Hibernate.INTEGER);
78: for (Iterator noteIt = noteList.iterator(); noteIt
79: .hasNext();) {
80: deleteNote((Note) noteIt.next(), session);
81: }
82: }
83: }
84: oldRow = row;
85: }
86: return retVal;
87: }
88:
89: }
|