001: package org.claros.intouch.calendar.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.calendar.models.CalendarObject;
008: import org.claros.intouch.common.utility.Constants;
009: import org.claros.intouch.common.utility.Utility;
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 CalendarDBController {
020:
021: public static List getCalendarObjectsByUser(AuthProfile auth,
022: boolean getAll) throws Exception {
023: IGenericDao dao = null;
024: List items = null;
025: try {
026: dao = Utility.getDbConnection();
027:
028: String sql = null;
029: if (getAll) {
030: sql = "SELECT * FROM CALENDAR_OBJECTS";
031: items = dao.readList(CalendarObject.class, sql,
032: new Object[] {});
033: } else {
034: String username = auth.getUsername();
035: sql = "SELECT * FROM CALENDAR_OBJECTS WHERE USERNAME=? ORDER BY ID DESC";
036: items = dao.readList(CalendarObject.class, sql,
037: new Object[] { username });
038: }
039: } finally {
040: JdbcUtil.close(dao);
041: dao = null;
042: }
043: return items;
044: }
045:
046: /**
047: * @param auth
048: * @param item
049: */
050: @SuppressWarnings("deprecation")
051: public static void saveEvent(AuthProfile auth, CalendarObject item)
052: throws Exception {
053: IGenericDao dao = null;
054: try {
055: dao = Utility.getDbConnection();
056:
057: Long id = item.getId();
058: if (id == null) {
059: // it is an insert
060: IObjectMappingKey myObj = Constants.persistMan
061: .getObjectMappingFactory().createInstance(
062: CalendarObject.class,
063: new AutoGeneratedColumnsMapper(true));
064: dao.insert(myObj, item);
065: } else {
066: // it is an update
067: String username = auth.getUsername();
068: if (!item.getUsername().equals(username)) {
069: throw new NoPermissionException();
070: }
071: dao.update(CalendarObject.class, item);
072: }
073: } finally {
074: JdbcUtil.close(dao);
075: dao = null;
076: }
077: }
078:
079: /**
080: *
081: * @param auth
082: * @param eventId
083: * @throws Exception
084: */
085: public static void deleteEvent(AuthProfile auth, Long eventId)
086: throws Exception {
087: CalendarObject tmp = getEventById(auth, eventId);
088: if (!tmp.getUsername().equals(auth.getUsername())) {
089: throw new NoPermissionException();
090: }
091:
092: IGenericDao dao = null;
093: try {
094: dao = Utility.getDbConnection();
095: dao.deleteByPrimaryKey(CalendarObject.class, eventId);
096: } catch (Exception e) {
097: // do nothing sier
098: } finally {
099: JdbcUtil.close(dao);
100: dao = null;
101: }
102: }
103:
104: /**
105: *
106: * @param auth
107: * @param eventId
108: * @return
109: * @throws Exception
110: */
111: public static CalendarObject getEventById(AuthProfile auth,
112: Long eventId) throws Exception {
113: IGenericDao dao = null;
114: CalendarObject event = null;
115: try {
116: dao = Utility.getDbConnection();
117: String username = auth.getUsername();
118:
119: String sql = "SELECT * FROM CALENDAR_OBJECTS WHERE USERNAME=? AND ID = ?";
120: event = (CalendarObject) dao.read(CalendarObject.class,
121: sql, new Object[] { username, eventId });
122: } finally {
123: JdbcUtil.close(dao);
124: dao = null;
125: }
126: return event;
127: }
128:
129: }
|