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: public static Long saveFolder(AuthProfile auth, NotesFolder folder)
062: throws Exception {
063: IGenericDao dao = null;
064: Long resId = new Long(-1);
065: try {
066: dao = Utility.getDbConnection();
067:
068: Long id = folder.getId();
069: if (id == null) {
070: // it is an insert
071: IObjectMappingKey myObj = Constants.persistMan
072: .getObjectMappingFactory().createInstance(
073: NotesFolder.class,
074: new AutoGeneratedColumnsMapper(true));
075: dao.insert(myObj, folder);
076:
077: QueryRunner run = new QueryRunner(DbConfigList
078: .getDataSourceById("file"));
079: String username = auth.getUsername();
080: String sql = "SELECT MAX(ID) AS NUMBER FROM NOTES_FOLDERS WHERE USERNAME=?";
081: HashMap result = (HashMap) run.query(sql,
082: new Object[] { username }, new MapHandler());
083: Object hbl = result.get("number");
084: if (hbl instanceof Long) {
085: resId = new Long(((Long) hbl).longValue());
086: } else if (hbl instanceof Integer) {
087: resId = new Long(((Integer) hbl).longValue());
088: } else if (hbl instanceof BigDecimal) {
089: resId = new Long(((BigDecimal) hbl).longValue());
090: } else if (hbl instanceof BigInteger) {
091: resId = new Long(((BigInteger) hbl).longValue());
092: }
093: } else {
094: // it is an update
095: NotesFolder tmp = getFolderById(auth, folder.getId());
096: String user = tmp.getUsername();
097: if (!auth.getUsername().equals(user)) {
098: throw new NoPermissionException();
099: }
100: dao.update(folder);
101: }
102: } finally {
103: JdbcUtil.close(dao);
104: dao = null;
105: }
106: return resId;
107: }
108:
109: /**
110: *
111: * @param auth
112: * @param folderId
113: * @throws Exception
114: */
115: public static void deleteFolder(AuthProfile auth, Long folderId)
116: throws Exception {
117: IGenericDao dao = null;
118: try {
119: dao = Utility.getTxnDbConnection();
120: String username = auth.getUsername();
121:
122: NotesFolder folder = getFolderById(auth, folderId);
123: if (!folder.getUsername().equals(auth.getUsername())) {
124: throw new NoPermissionException();
125: }
126:
127: String sql = "DELETE FROM NOTES WHERE USERNAME=? AND FOLDER_ID = ?";
128: // delete the notes under folder
129: dao.executeUpdate(sql, new Object[] { username, folderId });
130: // delete the folder
131: dao.deleteByPrimaryKey(NotesFolder.class, folderId);
132: dao.commit();
133: } catch (Exception e) {
134: // dao.rollback();
135: // throw e;
136: log.warn("error while deleting folder", e);
137: } finally {
138: JdbcUtil.close(dao);
139: dao = null;
140: }
141: }
142:
143: /**
144: * @param auth
145: * @param folderId
146: * @return
147: */
148: public static NotesFolder getFolderById(AuthProfile auth,
149: Long folderId) throws Exception {
150: IGenericDao dao = null;
151: NotesFolder folder = null;
152: try {
153: dao = Utility.getDbConnection();
154: String username = auth.getUsername();
155:
156: String sql = "SELECT * FROM NOTES_FOLDERS WHERE USERNAME=? AND ID = ?";
157:
158: folder = (NotesFolder) dao.read(NotesFolder.class, sql,
159: new Object[] { username, folderId });
160: } finally {
161: JdbcUtil.close(dao);
162: dao = null;
163: }
164: return folder;
165: }
166:
167: /**
168: *
169: * @param auth
170: * @param folderId
171: * @throws Exception
172: */
173: public static void emptyFolder(AuthProfile auth, Long folderId)
174: throws Exception {
175: QueryRunner run = new QueryRunner(DbConfigList
176: .getDataSourceById("file"));
177: String username = auth.getUsername();
178: String sql = "DELETE FROM NOTES WHERE USERNAME=? AND FOLDER_ID=?";
179: run.update(sql, new Object[] { username, folderId });
180: }
181: }
|