001: package org.claros.intouch.contacts.controllers;
002:
003: import java.sql.SQLException;
004: import java.util.HashMap;
005: import java.util.List;
006: import java.util.Locale;
007:
008: import org.apache.commons.dbutils.QueryRunner;
009: import org.apache.commons.dbutils.handlers.MapHandler;
010: import org.claros.commons.auth.models.AuthProfile;
011: import org.claros.commons.db.DbConfigList;
012: import org.claros.commons.exception.NoPermissionException;
013: import org.claros.intouch.common.utility.Constants;
014: import org.claros.intouch.common.utility.Utility;
015: import org.claros.intouch.contacts.models.ContactGroup;
016: import org.claros.intouch.contacts.models.ContactGroupObject;
017:
018: import com.jenkov.mrpersister.impl.mapping.AutoGeneratedColumnsMapper;
019: import com.jenkov.mrpersister.itf.IGenericDao;
020: import com.jenkov.mrpersister.itf.mapping.IObjectMappingKey;
021: import com.jenkov.mrpersister.util.JdbcUtil;
022:
023: /**
024: * @author Umut Gokbayrak
025: */
026: public class GroupsController {
027:
028: /**
029: *
030: * @param auth
031: * @return
032: * @throws Exception
033: */
034: public static List getGroupsByUser(AuthProfile auth)
035: throws Exception {
036: IGenericDao dao = null;
037: List groups = null;
038: try {
039: dao = Utility.getDbConnection();
040: String username = auth.getUsername();
041: String sql = "SELECT * FROM CONTACT_GROUPS WHERE USERNAME=?";
042: groups = dao.readList(ContactGroup.class, sql,
043: new Object[] { username });
044: } finally {
045: JdbcUtil.close(dao);
046: dao = null;
047: }
048: return groups;
049: }
050:
051: /**
052: *
053: * @param auth
054: * @param id
055: * @return
056: * @throws Exception
057: */
058: public static List getGroupItemsByGroup(AuthProfile auth, Long id)
059: throws Exception {
060: IGenericDao dao = null;
061: List items = null;
062: try {
063: dao = Utility.getDbConnection();
064: String username = auth.getUsername();
065: String sql = "SELECT * FROM CONTACT_GROUP_OBJECTS WHERE USERNAME=? AND GROUP_ID = ?";
066: items = dao.readList(ContactGroupObject.class, sql,
067: new Object[] { username, id });
068: } finally {
069: JdbcUtil.close(dao);
070: dao = null;
071: }
072: return items;
073: }
074:
075: /**
076: *
077: * @param auth
078: * @param id
079: * @return
080: * @throws Exception
081: */
082: public static Integer getMemberCountByGroup(AuthProfile auth,
083: Long id) throws Exception {
084: QueryRunner run = new QueryRunner(DbConfigList
085: .getDataSourceById("file"));
086: HashMap result = null;
087: String username = auth.getUsername();
088: try {
089: String sql = "SELECT COUNT(*) AS NUMBER FROM CONTACT_GROUP_OBJECTS WHERE USERNAME=? AND GROUP_ID = ? ";
090: result = (HashMap) run.query(sql, new Object[] { username,
091: id }, new MapHandler());
092: } catch (SQLException e) {
093: return new Integer(0);
094: }
095: if (result != null) {
096: return new Integer(result.get("number").toString());
097: }
098: return new Integer(0);
099: }
100:
101: /**
102: * @param auth
103: * @param long1
104: */
105: public static void deleteGroups(AuthProfile auth, Long id[])
106: throws Exception {
107: if (id != null) {
108: Long tmp = null;
109: ContactGroup cgTmp = null;
110: String sql = null;
111: for (int i = 0; i < id.length; i++) {
112: tmp = id[i];
113: cgTmp = getGroupById(auth, tmp);
114: if (!cgTmp.getUsername().equals(auth.getUsername())) {
115: throw new NoPermissionException();
116: }
117:
118: IGenericDao dao = null;
119: try {
120: dao = Utility.getDbConnection();
121: sql = "DELETE FROM CONTACT_GROUP_OBJECTS WHERE USERNAME = ? AND GROUP_ID = ?";
122: dao.executeUpdate(sql, new Object[] {
123: auth.getUsername(), tmp });
124: dao.deleteByPrimaryKey(ContactGroup.class, tmp);
125: } catch (Exception e) {
126: e.printStackTrace();
127: } finally {
128: JdbcUtil.close(dao);
129: dao = null;
130: }
131: }
132: }
133: }
134:
135: /**
136: * @param auth
137: * @param tmp
138: * @return
139: */
140: public static ContactGroup getGroupById(AuthProfile auth, Long id)
141: throws Exception {
142: IGenericDao dao = null;
143: ContactGroup result = null;
144: try {
145: dao = Utility.getDbConnection();
146: result = (ContactGroup) dao.readByPrimaryKey(
147: ContactGroup.class, id);
148:
149: if (!result.getUsername().equals(auth.getUsername())) {
150: throw new NoPermissionException();
151: }
152: } finally {
153: JdbcUtil.close(dao);
154: dao = null;
155: }
156: return result;
157: }
158:
159: /**
160: * @param auth
161: * @param string
162: * @param strings
163: */
164: public static void addGroup(AuthProfile auth, String groupName,
165: String[] members) throws Exception {
166: IGenericDao dao = null;
167: try {
168: dao = Utility.getDbConnection();
169:
170: ContactGroup grp = new ContactGroup();
171: grp.setShortName(groupName);
172: grp.setDescription("");
173: grp.setUsername(auth.getUsername());
174:
175: IObjectMappingKey myObj = Constants.persistMan
176: .getObjectMappingFactory().createInstance(
177: ContactGroup.class,
178: new AutoGeneratedColumnsMapper(true));
179: dao.insert(myObj, grp);
180:
181: // find the group id for the previous insertion
182: String sql = "SELECT * FROM CONTACT_GROUPS WHERE USERNAME = ? AND SHORT_NAME = ? ORDER BY ID DESC";
183: List added = dao.readList(ContactGroup.class, sql,
184: new Object[] { auth.getUsername(), groupName });
185: if (added != null && added.size() > 0) {
186: ContactGroup tmp = (ContactGroup) added.get(0);
187: Long id = tmp.getId();
188:
189: // add the members to the items list
190: if (members != null) {
191: Long t = null;
192: IObjectMappingKey myObj2 = Constants.persistMan
193: .getObjectMappingFactory()
194: .createInstance(
195: ContactGroupObject.class,
196: new AutoGeneratedColumnsMapper(true));
197: for (int i = 0; i < members.length; i++) {
198: try {
199: t = new Long(members[i]);
200: ContactGroupObject item = new ContactGroupObject();
201: item.setUsername(auth.getUsername());
202: item.setGroupId(id);
203: item.setContactId(t);
204: dao.insert(myObj2, item);
205: } catch (Exception e) {
206: // do nothing sier
207: }
208: }
209: }
210: }
211: } finally {
212: JdbcUtil.close(dao);
213: dao = null;
214: }
215: }
216:
217: /**
218: * @param auth
219: * @param strAdr
220: * @return
221: */
222: public static ContactGroup searchByName(AuthProfile auth,
223: String strAdr) throws Exception {
224: IGenericDao dao = null;
225: ContactGroup group = null;
226: try {
227: dao = Utility.getDbConnection();
228: String username = auth.getUsername();
229: strAdr = strAdr.toUpperCase(new Locale("en", "US"));
230: String sql = "SELECT * FROM CONTACT_GROUPS WHERE USERNAME=? AND UPPER(SHORT_NAME) = ?";
231: group = (ContactGroup) dao.read(ContactGroup.class, sql,
232: new Object[] { username, strAdr });
233: } finally {
234: JdbcUtil.close(dao);
235: dao = null;
236: }
237: return group;
238: }
239:
240: /**
241: * @param username
242: * @param name
243: * @return
244: */
245: public static List searchByLikeName(String username, String name)
246: throws Exception {
247: IGenericDao dao = null;
248: List groups = null;
249: try {
250: dao = Utility.getDbConnection();
251: name = name.toUpperCase(new Locale("en", "US"));
252: String sql = "SELECT * FROM CONTACT_GROUPS WHERE USERNAME=? AND UPPER(SHORT_NAME) LIKE '%"
253: + name + "%'";
254: groups = dao.readList(ContactGroup.class, sql,
255: new Object[] { username });
256: } finally {
257: JdbcUtil.close(dao);
258: dao = null;
259: }
260: return groups;
261: }
262: }
|