001: /**********************************************************************************
002: *
003: * $Id: GradebookEntityProducer.java 20760 2007-01-29 18:51:21Z ray@media.berkeley.edu $
004: *
005: ***********************************************************************************
006: *
007: * Copyright (c) 2005, 2006 The Regents of the University of California, The MIT Corporation
008: *
009: * Licensed under the Educational Community License, Version 1.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.opensource.org/licenses/ecl1.php
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: *
021: **********************************************************************************/package org.sakaiproject.tool.gradebook.facades.sakai2impl;
022:
023: import java.util.ArrayList;
024: import java.util.List;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.sakaiproject.entity.api.ContextObserver;
029: import org.sakaiproject.entity.api.EntityTransferrer;
030: import org.sakaiproject.importer.api.HandlesImportable;
031: import org.sakaiproject.importer.api.Importable;
032: import org.sakaiproject.service.gradebook.shared.GradebookFrameworkService;
033: import org.sakaiproject.service.gradebook.shared.GradebookNotFoundException;
034: import org.sakaiproject.service.gradebook.shared.GradebookService;
035:
036: /**
037: * Implements the Sakai EntityProducer approach to integration of tool-specific
038: * storage with site management.
039: */
040: public class GradebookEntityProducer extends BaseEntityProducer
041: implements ContextObserver, EntityTransferrer,
042: HandlesImportable {
043: private static final Log log = LogFactory
044: .getLog(GradebookEntityProducer.class);
045:
046: private String[] toolIdArray;
047: private GradebookFrameworkService gradebookFrameworkService;
048: private GradebookService gradebookService;
049:
050: public void setToolIds(List toolIds) {
051: if (log.isDebugEnabled())
052: log.debug("setToolIds(" + toolIds + ")");
053: if (toolIds != null) {
054: toolIdArray = (String[]) toolIds.toArray(new String[0]);
055: }
056: }
057:
058: public String[] myToolIds() {
059: return toolIdArray;
060: }
061:
062: public void contextCreated(String context, boolean toolPlacement) {
063: // Only create Gradebook storage if the Gradebook tool is actually
064: // part of the new site.
065: if (toolPlacement
066: && !gradebookFrameworkService
067: .isGradebookDefined(context)) {
068: if (log.isInfoEnabled())
069: log.info("Gradebook being added to context " + context);
070: gradebookFrameworkService.addGradebook(context, context);
071: }
072: }
073:
074: public void contextUpdated(String context, boolean toolPlacement) {
075: if (toolPlacement) {
076: if (!gradebookFrameworkService.isGradebookDefined(context)) {
077: if (log.isInfoEnabled())
078: log.info("Gradebook being added to context "
079: + context);
080: gradebookFrameworkService
081: .addGradebook(context, context);
082: }
083: } else {
084: if (gradebookFrameworkService.isGradebookDefined(context)) {
085: // We've been directed to leave Gradebook data in place when
086: // the tool is removed from a site, just in case the site
087: // owner changes their mind later.
088: if (log.isInfoEnabled())
089: log
090: .info("Gradebook removed from context "
091: + context
092: + " but associated data will remain until context deletion");
093: }
094: }
095: }
096:
097: public void contextDeleted(String context, boolean toolPlacement) {
098: if (gradebookFrameworkService.isGradebookDefined(context)) {
099: if (log.isInfoEnabled())
100: log.info("Gradebook being deleted from context "
101: + context);
102: try {
103: gradebookFrameworkService.deleteGradebook(context);
104: } catch (GradebookNotFoundException e) {
105: if (log.isWarnEnabled())
106: log.warn(e);
107: }
108: }
109: }
110:
111: public void transferCopyEntities(String fromContext,
112: String toContext, List ids) {
113: String fromGradebookXml = gradebookService
114: .getGradebookDefinitionXml(fromContext);
115: gradebookService.mergeGradebookDefinitionXml(toContext,
116: fromGradebookXml);
117: }
118:
119: public void setGradebookFrameworkService(
120: GradebookFrameworkService gradebookFrameworkService) {
121: this .gradebookFrameworkService = gradebookFrameworkService;
122: }
123:
124: public void setGradebookService(GradebookService gradebookService) {
125: this .gradebookService = gradebookService;
126: }
127:
128: ////////////////////////////////////////////////////////////////
129: // TODO Speculative support for future migration / import / export starts here.
130:
131: public static final String GRADEBOOK_DEFINITION_TYPE = "sakai-gradebook";
132:
133: public boolean canHandleType(String typeName) {
134: return (typeName.equals(GRADEBOOK_DEFINITION_TYPE));
135: }
136:
137: public void handle(Importable importable, String siteId) {
138: if (importable.getTypeName().equals(GRADEBOOK_DEFINITION_TYPE)) {
139: gradebookService.mergeGradebookDefinitionXml(siteId,
140: ((XmlImportable) importable).getXmlData());
141: }
142: }
143:
144: public List<Importable> getAllImportables(String contextId) {
145: List<Importable> importables = new ArrayList<Importable>();
146: importables.add(new XmlImportable(GRADEBOOK_DEFINITION_TYPE,
147: gradebookService.getGradebookDefinitionXml(contextId)));
148: return importables;
149: }
150: }
|