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: public static void saveNote(AuthProfile auth, Note note)
065: throws Exception {
066: IGenericDao dao = null;
067: try {
068: dao = Utility.getDbConnection();
069:
070: Long id = note.getId();
071: if (id == null) {
072: // it is an insert
073: IObjectMappingKey myObj = Constants.persistMan
074: .getObjectMappingFactory().createInstance(
075: Note.class,
076: new AutoGeneratedColumnsMapper(true));
077: dao.insert(myObj, note);
078: } else {
079: // it is an update
080: Note tmp = getNoteById(auth, note.getId());
081: if (!auth.getUsername().equals(tmp.getUsername())) {
082: throw new NoPermissionException();
083: }
084: dao.update(note);
085: }
086: } finally {
087: JdbcUtil.close(dao);
088: dao = null;
089: }
090: }
091:
092: /**
093: *
094: * @param auth
095: * @param noteId
096: * @throws Exception
097: */
098: public static void deleteNote(AuthProfile auth, Long noteId)
099: throws Exception {
100: Note tmp = getNoteById(auth, noteId);
101: if (!auth.getUsername().equals(tmp.getUsername())) {
102: throw new NoPermissionException();
103: }
104:
105: IGenericDao dao = null;
106: try {
107: dao = Utility.getDbConnection();
108: dao.deleteByPrimaryKey(Note.class, noteId);
109: } catch (Exception e) {
110: // do nothing sier
111: } finally {
112: JdbcUtil.close(dao);
113: dao = null;
114: }
115: }
116:
117: /**
118: *
119: * @param auth
120: * @param folderId
121: * @throws Exception
122: */
123: public static void deleteNotesByFolderId(AuthProfile auth,
124: Long folderId) throws Exception {
125: if (!folderId.equals(new Long(0))) {
126: IGenericDao dao = null;
127: try {
128: dao = Utility.getDbConnection();
129: String username = auth.getUsername();
130:
131: String sql = "DELETE FROM NOTES WHERE USERNAME=? AND FOLDER_ID = ?";
132:
133: dao.executeUpdate(sql, new Object[] { username,
134: folderId });
135: } catch (Exception e) {
136: // do nothing sier
137: } finally {
138: JdbcUtil.close(dao);
139: dao = null;
140: }
141: }
142: }
143:
144: /**
145: *
146: * @param auth
147: * @param noteId
148: * @return
149: * @throws Exception
150: */
151: public static Note getNoteById(AuthProfile auth, Long noteId)
152: throws Exception {
153: IGenericDao dao = null;
154: Note note = null;
155: try {
156: dao = Utility.getDbConnection();
157: String username = auth.getUsername();
158:
159: String sql = "SELECT * FROM NOTES WHERE USERNAME=? AND ID = ?";
160: note = (Note) dao.read(Note.class, sql, new Object[] {
161: username, noteId });
162: } finally {
163: JdbcUtil.close(dao);
164: dao = null;
165: }
166: return note;
167: }
168: }
|