001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/presentation/tags/sakai_2-4-1/api-impl/src/java/org/sakaiproject/component/app/presentation/PrLegacyManager.java $
003: * $Id: PrLegacyManager.java 8654 2006-05-02 01:40:40Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 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.component.app.presentation;
021:
022: import java.util.Collections;
023: import java.util.Hashtable;
024: import java.util.List;
025: import java.util.Vector;
026: import java.util.logging.Logger;
027:
028: import org.osid.id.IdManager;
029: import org.osid.shared.Id;
030: import org.sakaiproject.api.app.presentation.Presentation;
031: import org.sakaiproject.api.app.presentation.Slide;
032: import org.sakaiproject.content.api.ContentCollection;
033: import org.sakaiproject.content.api.ContentCollectionEdit;
034: import org.sakaiproject.content.cover.ContentHostingService;
035: import org.sakaiproject.entity.api.Entity;
036: import org.sakaiproject.entity.api.EntityPropertyNotDefinedException;
037: import org.sakaiproject.entity.api.EntityPropertyTypeException;
038: import org.sakaiproject.entity.api.ResourceProperties;
039: import org.sakaiproject.entity.api.ResourcePropertiesEdit;
040: import org.sakaiproject.exception.IdUnusedException;
041: import org.sakaiproject.exception.InUseException;
042: import org.sakaiproject.exception.PermissionException;
043: import org.sakaiproject.exception.TypeException;
044: import org.sakaiproject.time.api.Time;
045: import org.sakaiproject.tool.cover.ToolManager;
046:
047: /**
048: *
049: * @author Mark Norton
050: *
051: * The PresentationManager allows new presentations to be created and added. It
052: * provides a means to list all known presentations via a PresentationIterator.
053: */
054: public class PrLegacyManager implements
055: org.sakaiproject.api.app.presentation.PresentationManager {
056:
057: public static final String PROP_PRESENT_CURRENT_SLIDE = "Presentation:Current-Slide-Id";
058:
059: /* Service Dependency: IdManager. */
060: protected IdManager idManager = null;
061:
062: /* Service Dependency: Logger - eventually this will be the Sakai Logger. */
063: public static Logger logger = null;
064:
065: /**
066: * Dependency injection of IdManager.
067: *
068: * @author Mark Norton
069: */
070: public IdManager getIdManager() {
071: return this .idManager;
072: }
073:
074: public void setIdManager(IdManager im) {
075: this .idManager = im;
076: }
077:
078: /** Clear out any cached presentations
079: *
080: * @author Charles Severance
081: *
082: */
083: public void clearPresentationCache() {
084: // Not currently needed - there is no cache
085: // System.out.println("Presentation cache cleared");
086: }
087:
088: /**
089: * Substitute for getting a context. This is the Site string.
090: */
091: private String getContext() {
092: String retval = ToolManager.getCurrentPlacement().getContext();
093: // System.out.println("Context="+retval);
094: return retval;
095: }
096:
097: /**
098: * Get the current "home" collection
099: */
100: private String getHomeCollection() {
101: String retval = ContentHostingService
102: .getSiteCollection(getContext());
103: // System.out.println("Home Collection = "+retval);
104: return retval;
105: }
106:
107: public String getHomeReference() {
108: String homeCollection = getHomeCollection();
109: // System.out.println("Home Collection = "+homeCollection);
110: if (homeCollection == null)
111: return null;
112:
113: String refString = ContentHostingService
114: .getReference(homeCollection);
115: // System.out.println("RefString="+refString);
116: return refString;
117: }
118:
119: public String getReference(Presentation pres) {
120: String presId = getPresentationIdString(pres);
121: // System.out.println("Presentation Id = "+presId);
122: if (presId == null)
123: return null;
124:
125: String refString = ContentHostingService.getReference(presId);
126: // System.out.println("RefString="+refString);
127: return refString;
128: }
129:
130: /**
131: * This is just a utility function to eat exceptions and do
132: * common error checking.
133: */
134: private String getPresentationIdString(Presentation pres) {
135:
136: String presId = null;
137: if (pres == null) {
138: // System.out.println("getPresentationIdString - Presentation is null!");
139: return null;
140: }
141:
142: try {
143: presId = pres.getId().getIdString();
144: } catch (Throwable t) {
145: presId = null;
146: }
147: return presId;
148: }
149:
150: /**
151: * Determine if the user is allowed to modify this presentation
152: *
153: * @author Charles Severance
154: */
155:
156: public boolean allowUpdate(Presentation pres) {
157: boolean retval = false;
158:
159: String presId = getPresentationIdString(pres);
160: if (presId == null)
161: return false;
162:
163: retval = ContentHostingService.allowUpdateCollection(presId);
164:
165: // System.out.println("allowUpdate:"+presId+" returns "+retval);
166: return retval;
167: }
168:
169: public String getTitle(Presentation pres) {
170:
171: String retval = null;
172: String presId = getPresentationIdString(pres);
173: if (presId == null)
174: return retval;
175:
176: try {
177: // System.out.println("getModificationDate:"+presId);
178: ContentCollection collection = ContentHostingService
179: .getCollection(presId);
180:
181: if (collection == null)
182: return null;
183: return getTitle(collection);
184: } catch (IdUnusedException e) {
185: // System.out.println("IdUnusedException.");
186: return null;
187: } catch (TypeException e) {
188: // System.out.println("TypeException.");
189: return null;
190: } catch (PermissionException e) {
191: // System.out.println("PermissionException");
192: return null;
193: }
194: }
195:
196: private String getTitle(ContentCollection collection) {
197: String retval = null;
198: if (collection == null)
199: return null;
200:
201: ResourceProperties props = collection.getProperties();
202: retval = props
203: .getProperty(ResourceProperties.PROP_DISPLAY_NAME);
204: // System.out.println("Returning title "+retval);
205: return retval;
206: }
207:
208: /**
209: * Gets the modification time of the presentation - always goes through to the
210: * Content service so as to get the live values.
211: *
212: * @param pres
213: * @return
214: */
215: public Time getModificationTime(Presentation pres) {
216:
217: Time retval = null;
218: String presId = getPresentationIdString(pres);
219: if (presId == null)
220: return retval;
221:
222: try {
223: // System.out.println("getModificationDate:"+presId);
224: ContentCollection collection = ContentHostingService
225: .getCollection(presId);
226:
227: if (collection == null)
228: return null;
229:
230: ResourceProperties props = collection.getProperties();
231: retval = props
232: .getTimeProperty(ResourceProperties.PROP_MODIFIED_DATE);
233: // System.out.println("Returning "+retval);
234: return retval;
235: } catch (EntityPropertyNotDefinedException e) {
236: // System.out.println("EntityPropertyNotDefinedException.");
237: return null;
238: } catch (IdUnusedException e) {
239: // System.out.println("IdUnusedException.");
240: return null;
241: } catch (EntityPropertyTypeException e) {
242: // System.out.println("TypeException.");
243: return null;
244: } catch (TypeException e) {
245: // System.out.println("TypeException.");
246: return null;
247: } catch (PermissionException e) {
248: // System.out.println("PermissionException");
249: return null;
250: }
251: }
252:
253: /**
254: * Gets the modification time of the presentation - always goes through to the
255: * Content service so as to get the live values.
256: *
257: * @param pres
258: * @return
259: */
260: private Time getModificationTime(ContentCollection collection) {
261:
262: Time retval = null;
263: if (collection == null)
264: return null;
265:
266: try {
267: ResourceProperties props = collection.getProperties();
268: retval = props
269: .getTimeProperty(ResourceProperties.PROP_MODIFIED_DATE);
270: // System.out.println("Returning "+retval);
271: return retval;
272: } catch (EntityPropertyNotDefinedException e) {
273: // System.out.println("EntityPropertyNotDefinedException.");
274: return null;
275: } catch (EntityPropertyTypeException e) {
276: // System.out.println("EntityPropertyNotDefinedException.");
277: return null;
278: }
279: }
280:
281: /**
282: * Gets the modification date of the presentation - always goes through to the
283: * Content service so as to get the live values.
284: *
285: * @param pres
286: * @return
287: */
288: public String getModificationDate(Presentation pres) {
289: Time modTime = getModificationTime(pres);
290:
291: return modTime.toStringLocal();
292: }
293:
294: /**
295: * Start a slide show on a presentation
296: *
297: * @author Charles Severance
298: */
299:
300: public boolean startShow(Presentation pres) {
301: // System.out.println("startShow:"+pres);
302: return setCurrentSlideProperty(pres, 0);
303: }
304:
305: /**
306: * Stop a slide show on a presentation
307: *
308: * @author Charles Severance
309: */
310: public boolean stopShow(Presentation pres) {
311: // System.out.println("stopShow:"+pres);
312: return setCurrentSlideProperty(pres, -1);
313: }
314:
315: public boolean rewindShow(Presentation pres) {
316: return setCurrentSlideProperty(pres, 0);
317: }
318:
319: /**
320: * Advance a slide show on a presentation
321: *
322: * @author Charles Severance
323: */
324:
325: public boolean advanceShow(Presentation pres) {
326: // System.out.println("advanceShow:"+pres);
327: if (pres == null)
328: return false;
329:
330: int currSlide = getCurrentSlideProperty(pres);
331:
332: currSlide++;
333:
334: if (currSlide > pres.getSlideCount() - 1) {
335: currSlide = pres.getSlideCount() - 1;
336: }
337: if (currSlide < 0)
338: currSlide = 0;
339:
340: return setCurrentSlideProperty(pres, currSlide);
341: }
342:
343: /**
344: * Reverse a slide show on a presentation
345: *
346: * @author Charles Severance
347: */
348:
349: public boolean backShow(Presentation pres) {
350: if (pres == null)
351: return false;
352:
353: int currSlide = getCurrentSlideProperty(pres);
354:
355: currSlide--;
356:
357: if (currSlide > pres.getSlideCount() - 1) {
358: currSlide = pres.getSlideCount() - 1;
359:
360: }
361: if (currSlide < 0)
362: currSlide = 0;
363: return setCurrentSlideProperty(pres, currSlide);
364: }
365:
366: /**
367: * Returns true if an active show is going on for the presentation
368: *
369: * @author Charles Severance
370: */
371:
372: public boolean isShowing(Presentation pres) {
373: if (pres == null)
374: return false;
375: int currSlide = getCurrentSlideProperty(pres);
376: return (currSlide >= 0);
377: }
378:
379: /**
380: * Get current slide for a show
381: *
382: * @author Charles Severance
383: */
384:
385: public Slide getCurrentSlide(Presentation pres) {
386: if (pres == null)
387: return null;
388:
389: int currSlide = getCurrentSlideNumber(pres);
390:
391: return pres.getSlide(currSlide);
392: }
393:
394: public int getCurrentSlideNumber(Presentation pres) {
395: if (pres == null)
396: return -1;
397:
398: int currSlide = getCurrentSlideProperty(pres);
399: if (currSlide > pres.getSlideCount() - 1)
400: currSlide = pres.getSlideCount() - 1;
401: if (currSlide < 0)
402: currSlide = 0;
403:
404: return currSlide;
405: }
406:
407: /** Get the current property for the current slide - return -1 if not there */
408: private int getCurrentSlideProperty(Presentation pres) {
409: String presId = getPresentationIdString(pres);
410: if (presId == null)
411: return 0;
412:
413: try {
414: // System.out.println("getCurrentSlideProperty:"+presId);
415: ContentCollection collection = ContentHostingService
416: .getCollection(presId);
417:
418: return getCurrentSlideProperty(collection);
419: } catch (IdUnusedException e) {
420: // System.out.println("IdUnusedException.");
421: return -1;
422: } catch (TypeException e) {
423: // System.out.println("TypeException.");
424: return -1;
425: } catch (PermissionException e) {
426: // System.out.println("PermissionException");
427: return -1;
428: }
429: }
430:
431: /** Get the current property for the current slide - return -1 if not there */
432: private int getCurrentSlideProperty(ContentCollection collection) {
433: if (collection == null)
434: return -1;
435:
436: try {
437: ResourceProperties props = collection.getProperties();
438: long longval = props
439: .getLongProperty(PROP_PRESENT_CURRENT_SLIDE);
440: // System.out.println("Property long = "+longval);
441: int retval = (int) longval;
442: return retval;
443: } catch (EntityPropertyNotDefinedException e) {
444: // Normal path
445: // System.out.println("EntityPropertyNotDefinedException.");
446: return -1;
447: } catch (EntityPropertyTypeException e) {
448: // System.out.println("TypeException.");
449: return -1;
450: }
451: }
452:
453: private boolean setCurrentSlideProperty(Presentation pres,
454: int slideno) {
455: String presId = getPresentationIdString(pres);
456: if (presId == null)
457: return false;
458:
459: // System.out.println("setCurrentSlideProperty slideno="+slideno+" id="+presId);
460: try {
461: ContentCollection collection = ContentHostingService
462: .getCollection(presId);
463:
464: ContentCollectionEdit cedit = null;
465: ResourcePropertiesEdit pedit = null;
466: if (ContentHostingService.getProperties(presId)
467: .getBooleanProperty(
468: ResourceProperties.PROP_IS_COLLECTION)) {
469: cedit = ContentHostingService.editCollection(presId);
470: pedit = cedit.getPropertiesEdit();
471: } else {
472: return false;
473: }
474: if (slideno <= -1) {
475: // System.out.println("Removed property"+PROP_PRESENT_CURRENT_SLIDE);
476: pedit.removeProperty(PROP_PRESENT_CURRENT_SLIDE);
477: } else {
478: pedit.addProperty(PROP_PRESENT_CURRENT_SLIDE, ""
479: + slideno);
480: }
481: ContentHostingService.commitCollection(cedit);
482: // System.out.println("Commit Happenned");
483: return true;
484: } catch (IdUnusedException e) {
485: // System.out.println("IdUnusedException.");
486: } catch (EntityPropertyTypeException e) {
487: // System.out.println("TypeException.");
488: } catch (TypeException e) {
489: // System.out.println("TypeException.");
490: } catch (PermissionException e) {
491: // System.out.println("PermissionException");
492: } catch (InUseException e) {
493: // System.out.println("InUseException");
494: } catch (EntityPropertyNotDefinedException e) {
495: // System.out.println("EntityPropertyNotDefinedException");
496: }
497: return false;
498: }
499:
500: /**
501: * Return an interator which lists all known presentations.
502: *
503: * @author Mark Norton
504: */
505: public List getPresentations() throws IdUnusedException,
506: TypeException, PermissionException {
507: String context = getContext();
508:
509: List presentations = new Vector();
510: String home = getHomeCollection();
511: String collectionId = home + "Presentations/";
512:
513: // Get the collection and throw an exception upwards if there is a problem
514: // System.out.println("Getting Presentations From:"+collectionId);
515: ContentCollection collection = ContentHostingService
516: .getCollection(collectionId);
517: List newMembers = collection.getMemberResources();
518: // String sortedBy = ResourceProperties.PROP_DISPLAY_NAME;
519: String sortedBy = ResourceProperties.PROP_MODIFIED_DATE;
520: Collections.sort(newMembers, ContentHostingService
521: .newContentHostingComparator(sortedBy, false));
522: int size = newMembers.size();
523: Hashtable moreMembers = new Hashtable();
524: // System.out.println("Size: "+size);
525: for (int i = 0; i < size; i++) {
526: try {
527: Entity resource = (Entity) newMembers.get(i);
528: String nextId = resource.getId();
529: // System.out.println("Next ID = "+nextId);
530: boolean isCollection = resource.getProperties()
531: .getBooleanProperty(
532: ResourceProperties.PROP_IS_COLLECTION);
533:
534: if (isCollection) {
535: Presentation xpres = loadPresentation(nextId);
536: if (xpres != null && xpres.getSlideCount() > 0)
537: presentations.add(xpres);
538: }
539:
540: } catch (EntityPropertyTypeException e) {
541: // System.out.println("TypeException.");
542: continue;
543: } catch (EntityPropertyNotDefinedException e) {
544: // System.out.println("EntityPropertyNotDefinedException");
545: continue;
546: }
547: }
548:
549: return presentations;
550: }
551:
552: /*
553: * Load a presentation given it's repository directory.
554: */
555: private Presentation loadPresentation(String presId) {
556: // System.out.println("loadPresentaton:"+presId);
557:
558: Id id = null;
559: try {
560: id = this .idManager.getId(presId);
561: } catch (Throwable t) {
562: return null;
563: }
564: Presentation pres = null;
565: try {
566: ContentCollection collection = ContentHostingService
567: .getCollection(presId);
568:
569: List slides = null;
570: String title = getTitle(collection);
571: Time modificationTime = getModificationTime(collection);
572:
573: pres = (Presentation) new PrPresentation(id, slides, title,
574: modificationTime);
575:
576: List colMembers = collection.getMemberResources();
577:
578: String sortedBy = ResourceProperties.PROP_DISPLAY_NAME;
579: Collections.sort(colMembers, ContentHostingService
580: .newContentHostingComparator(sortedBy, true));
581: // System.out.println("Sorted...");
582: int colsize = colMembers.size();
583: // System.out.println("Sub coll size "+colsize);
584:
585: for (int j = 0; j < colsize; j++) {
586: Entity subResource = (Entity) colMembers.get(j);
587: // System.out.println(" Next ID = "+subResource.getId());
588: // System.out.println(" Next URL = "+subResource.getUrl());
589:
590: String slideName = "Slide Name";
591: String slideType = "SlideType";
592: String slideUrl = subResource.getUrl();
593:
594: // Create the slide and add it to the presentation.
595: Slide slide = new PrSlide(slideUrl, slideName,
596: slideType);
597: pres.addSlide(slide);
598: }
599:
600: // System.out.println("Done adding slides...");
601: } catch (IdUnusedException e) {
602: // System.out.println("IdUnusedException.");
603: } catch (TypeException e) {
604: // System.out.println("TypeException.");
605: } catch (PermissionException e) {
606: // System.out.println("PermissionException");
607: }
608:
609: return pres;
610: }
611: }
|