001: package org.claros.intouch.notes.controllers;
002:
003: import java.util.List;
004:
005: import org.claros.commons.auth.models.AuthProfile;
006: import org.claros.commons.exception.NoPermissionException;
007: import org.claros.intouch.common.utility.Constants;
008: import org.claros.intouch.common.utility.Utility;
009: import org.claros.intouch.notes.models.Note;
010:
011: import com.jenkov.mrpersister.impl.mapping.AutoGeneratedColumnsMapper;
012: import com.jenkov.mrpersister.itf.IGenericDao;
013: import com.jenkov.mrpersister.itf.mapping.IObjectMappingKey;
014: import com.jenkov.mrpersister.util.JdbcUtil;
015:
016: /**
017: * @author Umut Gokbayrak
018: */
019: public class NotesController {
020:
021: /**
022: *
023: * @param auth
024: * @return
025: * @throws Exception
026: */
027: public static List getUnfiledNotes(AuthProfile auth)
028: throws Exception {
029: return getNotesByFolderId(auth, new Long(0));
030: }
031:
032: /**
033: *
034: * @param auth
035: * @param folderId
036: * @return
037: * @throws Exception
038: */
039: public static List getNotesByFolderId(AuthProfile auth,
040: Long folderId) throws Exception {
041: IGenericDao dao = null;
042: List notes = null;
043: try {
044: dao = Utility.getDbConnection();
045: String username = auth.getUsername();
046:
047: String sql = "SELECT * FROM NOTES WHERE USERNAME=? AND FOLDER_ID = ? ORDER BY ID DESC";
048:
049: notes = dao.readList(Note.class, sql, new Object[] {
050: username, folderId });
051: } finally {
052: JdbcUtil.close(dao);
053: dao = null;
054: }
055: return notes;
056: }
057:
058: /**
059: *
060: * @param auth
061: * @param note
062: * @throws Exception
063: */
064: @SuppressWarnings("deprecation")
065: public static void saveNote(AuthProfile auth, Note note)
066: throws Exception {
067: IGenericDao dao = null;
068: try {
069: dao = Utility.getDbConnection();
070:
071: Long id = note.getId();
072: if (id == null) {
073: // it is an insert
074: IObjectMappingKey myObj = Constants.persistMan
075: .getObjectMappingFactory().createInstance(
076: Note.class,
077: new AutoGeneratedColumnsMapper(true));
078: dao.insert(myObj, note);
079: } else {
080: // it is an update
081: Note tmp = getNoteById(auth, note.getId());
082: if (!auth.getUsername().equals(tmp.getUsername())) {
083: throw new NoPermissionException();
084: }
085: dao.update(note);
086: }
087: } finally {
088: JdbcUtil.close(dao);
089: dao = null;
090: }
091: }
092:
093: /**
094: *
095: * @param auth
096: * @param noteId
097: * @throws Exception
098: */
099: public static void deleteNote(AuthProfile auth, Long noteId)
100: throws Exception {
101: Note tmp = getNoteById(auth, noteId);
102: if (!auth.getUsername().equals(tmp.getUsername())) {
103: throw new NoPermissionException();
104: }
105:
106: IGenericDao dao = null;
107: try {
108: dao = Utility.getDbConnection();
109: dao.deleteByPrimaryKey(Note.class, noteId);
110: } catch (Exception e) {
111: // do nothing sier
112: } finally {
113: JdbcUtil.close(dao);
114: dao = null;
115: }
116: }
117:
118: /**
119: *
120: * @param auth
121: * @param folderId
122: * @throws Exception
123: */
124: public static void deleteNotesByFolderId(AuthProfile auth,
125: Long folderId) throws Exception {
126: if (!folderId.equals(new Long(0))) {
127: IGenericDao dao = null;
128: try {
129: dao = Utility.getDbConnection();
130: String username = auth.getUsername();
131:
132: String sql = "DELETE FROM NOTES WHERE USERNAME=? AND FOLDER_ID = ?";
133:
134: dao.executeUpdate(sql, new Object[] { username,
135: folderId });
136: } catch (Exception e) {
137: // do nothing sier
138: } finally {
139: JdbcUtil.close(dao);
140: dao = null;
141: }
142: }
143: }
144:
145: /**
146: *
147: * @param auth
148: * @param noteId
149: * @return
150: * @throws Exception
151: */
152: public static Note getNoteById(AuthProfile auth, Long noteId)
153: throws Exception {
154: IGenericDao dao = null;
155: Note note = null;
156: try {
157: dao = Utility.getDbConnection();
158: String username = auth.getUsername();
159:
160: String sql = "SELECT * FROM NOTES WHERE USERNAME=? AND ID = ?";
161: note = (Note) dao.read(Note.class, sql, new Object[] {
162: username, noteId });
163: } finally {
164: JdbcUtil.close(dao);
165: dao = null;
166: }
167: return note;
168: }
169: }
|