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: @SuppressWarnings("deprecation")
165: public static void addGroup(AuthProfile auth, String groupName,
166: String[] members) throws Exception {
167: IGenericDao dao = null;
168: try {
169: dao = Utility.getDbConnection();
170:
171: ContactGroup grp = new ContactGroup();
172: grp.setShortName(groupName);
173: grp.setDescription("");
174: grp.setUsername(auth.getUsername());
175:
176: IObjectMappingKey myObj = Constants.persistMan
177: .getObjectMappingFactory().createInstance(
178: ContactGroup.class,
179: new AutoGeneratedColumnsMapper(true));
180: dao.insert(myObj, grp);
181:
182: // find the group id for the previous insertion
183: String sql = "SELECT * FROM CONTACT_GROUPS WHERE USERNAME = ? AND SHORT_NAME = ? ORDER BY ID DESC";
184: List added = dao.readList(ContactGroup.class, sql,
185: new Object[] { auth.getUsername(), groupName });
186: if (added != null && added.size() > 0) {
187: ContactGroup tmp = (ContactGroup) added.get(0);
188: Long id = tmp.getId();
189:
190: // add the members to the items list
191: if (members != null) {
192: Long t = null;
193: IObjectMappingKey myObj2 = Constants.persistMan
194: .getObjectMappingFactory()
195: .createInstance(
196: ContactGroupObject.class,
197: new AutoGeneratedColumnsMapper(true));
198: for (int i = 0; i < members.length; i++) {
199: try {
200: t = new Long(members[i]);
201: ContactGroupObject item = new ContactGroupObject();
202: item.setUsername(auth.getUsername());
203: item.setGroupId(id);
204: item.setContactId(t);
205: dao.insert(myObj2, item);
206: } catch (Exception e) {
207: // do nothing sier
208: }
209: }
210: }
211: }
212: } finally {
213: JdbcUtil.close(dao);
214: dao = null;
215: }
216: }
217:
218: /**
219: * @param auth
220: * @param strAdr
221: * @return
222: */
223: public static ContactGroup searchByName(AuthProfile auth,
224: String strAdr) throws Exception {
225: IGenericDao dao = null;
226: ContactGroup group = null;
227: try {
228: dao = Utility.getDbConnection();
229: String username = auth.getUsername();
230: strAdr = strAdr.toUpperCase(new Locale("en", "US"));
231: String sql = "SELECT * FROM CONTACT_GROUPS WHERE USERNAME=? AND UPPER(SHORT_NAME) = ?";
232: group = (ContactGroup) dao.read(ContactGroup.class, sql,
233: new Object[] { username, strAdr });
234: } finally {
235: JdbcUtil.close(dao);
236: dao = null;
237: }
238: return group;
239: }
240:
241: /**
242: * @param username
243: * @param name
244: * @return
245: */
246: public static List searchByLikeName(String username, String name)
247: throws Exception {
248: IGenericDao dao = null;
249: List groups = null;
250: try {
251: dao = Utility.getDbConnection();
252: name = name.toUpperCase(new Locale("en", "US"));
253: String sql = "SELECT * FROM CONTACT_GROUPS WHERE USERNAME=? AND UPPER(SHORT_NAME) LIKE '%"
254: + name + "%'";
255: groups = dao.readList(ContactGroup.class, sql,
256: new Object[] { username });
257: } finally {
258: JdbcUtil.close(dao);
259: dao = null;
260: }
261: return groups;
262: }
263: }
|