001: package org.claros.intouch.notes.controllers;
002:
003: import java.math.BigDecimal;
004: import java.math.BigInteger;
005: import java.util.HashMap;
006: import java.util.List;
007:
008: import org.apache.commons.dbutils.QueryRunner;
009: import org.apache.commons.dbutils.handlers.MapHandler;
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012: import org.claros.commons.auth.models.AuthProfile;
013: import org.claros.commons.db.DbConfigList;
014: import org.claros.commons.exception.NoPermissionException;
015: import org.claros.intouch.common.utility.Constants;
016: import org.claros.intouch.common.utility.Utility;
017: import org.claros.intouch.notes.models.NotesFolder;
018:
019: import com.jenkov.mrpersister.impl.mapping.AutoGeneratedColumnsMapper;
020: import com.jenkov.mrpersister.itf.IGenericDao;
021: import com.jenkov.mrpersister.itf.mapping.IObjectMappingKey;
022: import com.jenkov.mrpersister.util.JdbcUtil;
023:
024: /**
025: * @author Umut Gokbayrak
026: */
027: public class NotesFolderController {
028: private static Log log = LogFactory
029: .getLog(NotesFolderController.class);
030:
031: /**
032: *
033: * @param auth
034: * @return
035: * @throws Exception
036: */
037: public static List getFolders(AuthProfile auth) throws Exception {
038: IGenericDao dao = null;
039: List folders = null;
040: try {
041: dao = Utility.getDbConnection();
042: String username = auth.getUsername();
043:
044: String sql = "SELECT * FROM NOTES_FOLDERS WHERE USERNAME = ?";
045:
046: folders = dao.readList(NotesFolder.class, sql,
047: new Object[] { username });
048: } finally {
049: JdbcUtil.close(dao);
050: dao = null;
051: }
052: return folders;
053: }
054:
055: /**
056: *
057: * @param auth
058: * @param folder
059: * @throws Exception
060: */
061: @SuppressWarnings("deprecation")
062: public static Long saveFolder(AuthProfile auth, NotesFolder folder)
063: throws Exception {
064: IGenericDao dao = null;
065: Long resId = new Long(-1);
066: try {
067: dao = Utility.getDbConnection();
068:
069: Long id = folder.getId();
070: if (id == null) {
071: // it is an insert
072: IObjectMappingKey myObj = Constants.persistMan
073: .getObjectMappingFactory().createInstance(
074: NotesFolder.class,
075: new AutoGeneratedColumnsMapper(true));
076: dao.insert(myObj, folder);
077:
078: QueryRunner run = new QueryRunner(DbConfigList
079: .getDataSourceById("file"));
080: String username = auth.getUsername();
081: String sql = "SELECT MAX(ID) AS NUMBER FROM NOTES_FOLDERS WHERE USERNAME=?";
082: HashMap result = (HashMap) run.query(sql,
083: new Object[] { username }, new MapHandler());
084: Object hbl = result.get("number");
085: if (hbl instanceof Long) {
086: resId = new Long(((Long) hbl).longValue());
087: } else if (hbl instanceof Integer) {
088: resId = new Long(((Integer) hbl).longValue());
089: } else if (hbl instanceof BigDecimal) {
090: resId = new Long(((BigDecimal) hbl).longValue());
091: } else if (hbl instanceof BigInteger) {
092: resId = new Long(((BigInteger) hbl).longValue());
093: }
094: } else {
095: // it is an update
096: NotesFolder tmp = getFolderById(auth, folder.getId());
097: String user = tmp.getUsername();
098: if (!auth.getUsername().equals(user)) {
099: throw new NoPermissionException();
100: }
101: dao.update(folder);
102: }
103: } finally {
104: JdbcUtil.close(dao);
105: dao = null;
106: }
107: return resId;
108: }
109:
110: /**
111: *
112: * @param auth
113: * @param folderId
114: * @throws Exception
115: */
116: public static void deleteFolder(AuthProfile auth, Long folderId)
117: throws Exception {
118: IGenericDao dao = null;
119: try {
120: dao = Utility.getTxnDbConnection();
121: String username = auth.getUsername();
122:
123: NotesFolder folder = getFolderById(auth, folderId);
124: if (!folder.getUsername().equals(auth.getUsername())) {
125: throw new NoPermissionException();
126: }
127:
128: String sql = "DELETE FROM NOTES WHERE USERNAME=? AND FOLDER_ID = ?";
129: // delete the notes under folder
130: dao.executeUpdate(sql, new Object[] { username, folderId });
131: // delete the folder
132: dao.deleteByPrimaryKey(NotesFolder.class, folderId);
133: dao.commit();
134: } catch (Exception e) {
135: // dao.rollback();
136: // throw e;
137: log.warn("error while deleting folder", e);
138: } finally {
139: JdbcUtil.close(dao);
140: dao = null;
141: }
142: }
143:
144: /**
145: * @param auth
146: * @param folderId
147: * @return
148: */
149: public static NotesFolder getFolderById(AuthProfile auth,
150: Long folderId) throws Exception {
151: IGenericDao dao = null;
152: NotesFolder folder = null;
153: try {
154: dao = Utility.getDbConnection();
155: String username = auth.getUsername();
156:
157: String sql = "SELECT * FROM NOTES_FOLDERS WHERE USERNAME=? AND ID = ?";
158:
159: folder = (NotesFolder) dao.read(NotesFolder.class, sql,
160: new Object[] { username, folderId });
161: } finally {
162: JdbcUtil.close(dao);
163: dao = null;
164: }
165: return folder;
166: }
167:
168: /**
169: *
170: * @param auth
171: * @param folderId
172: * @throws Exception
173: */
174: public static void emptyFolder(AuthProfile auth, Long folderId)
175: throws Exception {
176: QueryRunner run = new QueryRunner(DbConfigList
177: .getDataSourceById("file"));
178: String username = auth.getUsername();
179: String sql = "DELETE FROM NOTES WHERE USERNAME=? AND FOLDER_ID=?";
180: run.update(sql, new Object[] { username, folderId });
181: }
182: }
|