001: /* *****************************************************************************
002: * AnnouncementHandler.java - created by aaronz
003: *
004: * Copyright (c) 2006 Virginia Polytechnic Institute and State University
005: * Licensed under the Educational Community License version 1.0
006: *
007: * A copy of the Educational Community License has been included in this
008: * distribution and is available at: http://www.opensource.org/licenses/ecl1.php
009: *
010: * Contributors:
011: * Aaron Zeckoski (aaronz@vt.edu)
012: *
013: * ****************************************************************************/
014:
015: package org.sakaiproject.importer.impl.handlers;
016:
017: import org.apache.commons.logging.Log;
018: import org.apache.commons.logging.LogFactory;
019:
020: import org.sakaiproject.announcement.api.AnnouncementChannel;
021: import org.sakaiproject.announcement.api.AnnouncementService;
022:
023: import org.sakaiproject.exception.IdUnusedException;
024: import org.sakaiproject.exception.PermissionException;
025: import org.sakaiproject.importer.api.HandlesImportable;
026: import org.sakaiproject.importer.api.Importable;
027: import org.sakaiproject.importer.impl.importables.Announcement;
028: import org.sakaiproject.site.api.SiteService;
029:
030: /**
031: * This handler takes the generic announcement objects created by the various
032: * translators and turns them into Sakai announcements in the current course
033: * (created by the current user)
034: * @author Aaron Zeckoski (aaronz@vt.edu)
035: */
036: public class AnnouncementHandler implements HandlesImportable {
037:
038: private static Log log = LogFactory
039: .getLog(AnnouncementHandler.class);
040:
041: protected AnnouncementService announcementService = null;
042:
043: public void setAnnouncementService(
044: AnnouncementService announcementService) {
045: this .announcementService = announcementService;
046: }
047:
048: public boolean canHandleType(String typeName) {
049: return typeName.equals("sakai-announcement");
050: }
051:
052: public void handle(Importable thing, String siteId) {
053: if (canHandleType(thing.getTypeName())) {
054: // Cast the thing as the right type of item
055: Announcement item = (Announcement) thing;
056:
057: // should probably spring inject these
058: // ComponentManager cm = org.sakaiproject.component.cover.ComponentManager.getInstance();
059: // AnnouncementService announcementService = (AnnouncementService) cm.get(AnnouncementService.class);
060: // SiteService siteService = (SiteService) cm.get(SiteService.class);
061:
062: AnnouncementChannel ac = null;
063: try {
064: String channelId = announcementService
065: .channelReference(siteId,
066: SiteService.MAIN_CONTAINER);
067: ac = announcementService
068: .getAnnouncementChannel(channelId);
069: } catch (IdUnusedException e) {
070: log.error("Failed to get announcement channel: " + e);
071: e.printStackTrace();
072: return;
073: } catch (PermissionException e) {
074: log.error("Failed to get announcement channel: " + e);
075: e.printStackTrace();
076: return;
077: }
078:
079: try {
080: ac.addAnnouncementMessage(item.getTitle(), false, null,
081: item.getDescription());
082: } catch (PermissionException e) {
083: log
084: .error("Failed to create announcement message: "
085: + e);
086: e.printStackTrace();
087: return;
088: }
089:
090: // TODO - Need to handle things like email reminders and other possible optional fields
091: /***
092: try {
093: AnnouncementMessageEdit ame = ac.addAnnouncementMessage();
094: ame.setBody(item.getDescription());
095: ResourcePropertiesEdit rpe = ame.getPropertiesEdit();
096: rpe.addProperty(ResourceProperties.PROP_PUBVIEW, Boolean.TRUE.toString());
097: AnnouncementMessageHeaderEdit ahe = ame.getAnnouncementHeaderEdit();
098: ahe.setSubject(item.getTitle());
099: ahe.setDate(new MyTime(item.getCreated().getTime()));
100: ac.commitMessage(ame);
101: } catch (PermissionException e) {
102: // TODO Auto-generated catch block
103: e.printStackTrace();
104: }
105: ***/
106:
107: log.info("Created new announcement (" + item.getTitle()
108: + ") in " + siteId);
109: } else {
110: log
111: .warn("Attempted to import object ("
112: + thing.getClass()
113: + ") using wrong handler ("
114: + this .getClass() + ")");
115: }
116: }
117:
118: }
|