001: /**********************************************************************************
002: * $URL: $
003: * $Id: $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2006,2007 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.poll.dao.impl;
021:
022: import java.util.Collection;
023: import java.util.Map;
024: import java.util.List;
025: import java.util.Set;
026: import java.util.Stack;
027: import java.util.Vector;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: import org.hibernate.criterion.DetachedCriteria;
033: import org.hibernate.criterion.Order;
034: import org.hibernate.criterion.Restrictions;
035:
036: import org.sakaiproject.poll.logic.PollListManager;
037: import org.sakaiproject.poll.model.Option;
038: import org.sakaiproject.poll.model.Poll;
039: import org.sakaiproject.poll.model.Vote;
040: import org.springframework.dao.DataAccessException;
041: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
042: import org.sakaiproject.authz.cover.FunctionManager;
043: import org.sakaiproject.event.cover.EventTrackingService;
044: import org.sakaiproject.exception.PermissionException;
045: import org.sakaiproject.authz.cover.SecurityService;
046: import org.sakaiproject.user.cover.UserDirectoryService;
047: import org.sakaiproject.entity.api.Entity;
048: import org.sakaiproject.entity.api.HttpAccess;
049: import org.sakaiproject.entity.api.Reference;
050: import org.sakaiproject.entity.api.ResourceProperties;
051: import org.sakaiproject.entity.cover.EntityManager;
052: import org.sakaiproject.tool.cover.ToolManager;
053: import org.w3c.dom.Document;
054: import org.w3c.dom.Element;
055:
056: public class PollListManagerDaoImpl extends HibernateDaoSupport
057: implements PollListManager {
058:
059: // use commons logger
060: private static Log log = LogFactory
061: .getLog(PollListManagerDaoImpl.class);
062: public static final String REFERENCE_ROOT = Entity.SEPARATOR
063: + "poll";
064:
065: public void init() {
066: try {
067: EntityManager.registerEntityProducer(this , REFERENCE_ROOT);
068: } catch (Throwable t) {
069: log.warn("init(): ", t);
070: }
071:
072: FunctionManager.registerFunction(PERMISSION_VOTE);
073: FunctionManager.registerFunction(PERMISSION_ADD);
074: FunctionManager.registerFunction(PERMISSION_DELETE_OWN);
075: FunctionManager.registerFunction(PERMISSION_DELETE_ANY);
076: FunctionManager.registerFunction(PERMISSION_EDIT_ANY);
077: FunctionManager.registerFunction(PERMISSION_EDIT_OWN);
078: log.info(this + " init()");
079:
080: }
081:
082: public void destroy() {
083:
084: }
085:
086: public boolean savePoll(Poll t) {
087: boolean newPoll = false;
088: if (t.getId() == null)
089: newPoll = true;
090:
091: try {
092: getHibernateTemplate().saveOrUpdate(t);
093:
094: } catch (DataAccessException e) {
095: log.error("Hibernate could not save: " + e.toString());
096: e.printStackTrace();
097: return false;
098: }
099: log.info(" Poll " + t.toString() + "successfuly saved");
100: if (newPoll)
101:
102: EventTrackingService.post(EventTrackingService.newEvent(
103: "poll.add", "poll/site/" + t.getSiteId() + "/poll/"
104: + t.getId(), true));
105: else
106: EventTrackingService.post(EventTrackingService.newEvent(
107: "poll.update", "poll/site/" + t.getSiteId()
108: + " /poll/" + t.getId(), true));
109:
110: return true;
111: }
112:
113: public boolean saveOption(Option t) {
114:
115: try {
116: getHibernateTemplate().saveOrUpdate(t);
117:
118: } catch (DataAccessException e) {
119: log.error("Hibernate could not save: " + e.toString());
120: e.printStackTrace();
121: return false;
122: }
123: log.info("Option " + t.toString() + "successfuly saved");
124: return true;
125: }
126:
127: public boolean deletePoll(Poll t) throws PermissionException {
128: if (!pollCanDelete(t))
129: throw new PermissionException(UserDirectoryService
130: .getCurrentUser().getId(), "poll.delete", "poll."
131: + t.getId().toString());
132:
133: try {
134: getHibernateTemplate().delete(t);
135: } catch (DataAccessException e) {
136: log.error("Hibernate could not delete: " + e.toString());
137: e.printStackTrace();
138: return false;
139: }
140: log.info("Poll id " + t.getId() + " deleted");
141: EventTrackingService.post(EventTrackingService.newEvent(
142: "poll.delete", "poll/site/" + t.getSiteId() + "/poll/"
143: + t.getId(), true));
144: return true;
145: }
146:
147: public List findAllPolls(String siteId) {
148:
149: DetachedCriteria d = DetachedCriteria.forClass(Poll.class).add(
150: Restrictions.eq("siteId", siteId)).addOrder(
151: Order.desc("creationDate"));
152: Collection pollCollection = getHibernateTemplate()
153: .findByCriteria(d);
154: List pollList = PollUtil.pollCollectionToList(pollCollection);
155:
156: return pollList;
157: }
158:
159: public Poll getPollById(Long pollId) {
160: DetachedCriteria d = DetachedCriteria.forClass(Poll.class).add(
161: Restrictions.eq("id", pollId));
162: Poll poll = (Poll) PollUtil.pollCollectionToList(
163: getHibernateTemplate().findByCriteria(d)).get(0);
164:
165: //we need to get the options here
166: List optionList = getOptionsForPoll(poll);
167:
168: poll.setOptions(optionList);
169:
170: return poll;
171: }
172:
173: public List getOptionsForPoll(Poll poll) {
174:
175: //we need to get the options here
176: DetachedCriteria d = DetachedCriteria.forClass(Option.class)
177: .add(Restrictions.eq("pollId", poll.getPollId()));
178: List optionList = PollUtil
179: .optionCollectionToList(getHibernateTemplate()
180: .findByCriteria(d));
181: return optionList;
182:
183: }
184:
185: public Poll getPollWithVotes(Long pollId) {
186: DetachedCriteria d = DetachedCriteria.forClass(Poll.class).add(
187: Restrictions.eq("pollId", pollId));
188: return (Poll) PollUtil.pollCollectionToList(
189: getHibernateTemplate().findByCriteria(d)).get(0);
190:
191: }
192:
193: public Option getOptionById(Long optionId) {
194: DetachedCriteria d = DetachedCriteria.forClass(Option.class)
195: .add(Restrictions.eq("id", optionId));
196: Option option = (Option) getHibernateTemplate().findByCriteria(
197: d).get(0);
198:
199: return option;
200: }
201:
202: public void deleteOption(Option option) {
203: try {
204: getHibernateTemplate().delete(option);
205: } catch (DataAccessException e) {
206: log.error("Hibernate could not delete: " + e.toString());
207: e.printStackTrace();
208: }
209: log.info("Option id " + option.getId() + " deleted");
210:
211: }
212:
213: private boolean pollCanDelete(Poll poll) {
214: if (SecurityService.isSuperUser() || this .isSiteOwner())
215: return true;
216: if (SecurityService.unlock(PERMISSION_DELETE_ANY, "/site/"
217: + ToolManager.getCurrentPlacement().getContext()))
218: return true;
219:
220: if (SecurityService.unlock(PERMISSION_DELETE_OWN, "/site/"
221: + ToolManager.getCurrentPlacement().getContext())
222: && poll.getOwner().equals(
223: UserDirectoryService.getCurrentUser().getId()))
224: return true;
225:
226: return false;
227: }
228:
229: private boolean isSiteOwner() {
230: if (SecurityService.isSuperUser())
231: return true;
232: else if (SecurityService.unlock("site.upd", "/site/"
233: + ToolManager.getCurrentPlacement().getContext()))
234: return true;
235: else
236: return false;
237: }
238:
239: /*
240: * EntityProducer Methods
241: */
242: public String getLabel() {
243: return "poll";
244: }
245:
246: public boolean willArchiveMerge() {
247: return false;
248: }
249:
250: public String archive(String arg0, Document arg1, Stack arg2,
251: String arg3, List arg4) {
252: // TODO Auto-generated method stub
253: return null;
254: }
255:
256: public String merge(String arg0, Element arg1, String arg2,
257: String arg3, Map arg4, Map arg5, Set arg6) {
258: // TODO Auto-generated method stub
259: return null;
260: }
261:
262: public boolean parseEntityReference(String reference, Reference ref) {
263: if (reference.startsWith(REFERENCE_ROOT)) {
264: // /syllabus/siteid/syllabusid
265: String[] parts = split(reference, Entity.SEPARATOR);
266:
267: String subType = null;
268: String context = null;
269: String id = null;
270: String container = null;
271:
272: if (parts.length > 2) {
273: // the site/context
274: context = parts[2];
275:
276: // the id
277: if (parts.length > 3) {
278: id = parts[3];
279: }
280: }
281:
282: ref.set(PollListManager.class.getName(), subType, id,
283: container, context);
284:
285: return true;
286: }
287:
288: return false;
289: }
290:
291: public String getEntityDescription(Reference arg0) {
292: // TODO Auto-generated method stub
293: return null;
294: }
295:
296: public ResourceProperties getEntityResourceProperties(Reference arg0) {
297: // TODO Auto-generated method stub
298: return null;
299: }
300:
301: public Entity getEntity(Reference arg0) {
302: // TODO Auto-generated method stub
303: return null;
304: }
305:
306: public String getEntityUrl(Reference arg0) {
307: // TODO Auto-generated method stub
308: return null;
309: }
310:
311: public Collection getEntityAuthzGroups(Reference arg0, String arg1) {
312: // TODO Auto-generated method stub
313: return null;
314: }
315:
316: public HttpAccess getHttpAccess() {
317: // TODO Auto-generated method stub
318: return null;
319: }
320:
321: protected String[] split(String source, String splitter) {
322: // hold the results as we find them
323: Vector rv = new Vector();
324: int last = 0;
325: int next = 0;
326: do {
327: // find next splitter in source
328: next = source.indexOf(splitter, last);
329: if (next != -1) {
330: // isolate from last thru before next
331: rv.add(source.substring(last, next));
332: last = next + splitter.length();
333: }
334: } while (next != -1);
335: if (last < source.length()) {
336: rv.add(source.substring(last, source.length()));
337: }
338:
339: // convert to array
340: return (String[]) rv.toArray(new String[rv.size()]);
341:
342: } // split
343:
344: }
|