/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/
package org.olat.repository.controllers;
import org.olat.basesecurity.Constants;
import org.olat.basesecurity.Manager;
import org.olat.basesecurity.ManagerFactory;
import org.olat.basesecurity.SecurityGroup;
import org.olat.core.commons.persistence.DBFactory;
import org.olat.core.gui.UserRequest;
import org.olat.core.gui.components.Component;
import org.olat.core.gui.components.link.Link;
import org.olat.core.gui.components.link.LinkFactory;
import org.olat.core.gui.components.velocity.VelocityContainer;
import org.olat.core.gui.control.Controller;
import org.olat.core.gui.control.DefaultController;
import org.olat.core.gui.control.Event;
import org.olat.core.gui.control.WindowControl;
import org.olat.core.gui.translator.PackageTranslator;
import org.olat.core.gui.translator.Translator;
import org.olat.core.id.OLATResourceable;
import org.olat.core.logging.DBRuntimeException;
import org.olat.core.logging.Tracing;
import org.olat.core.util.Util;
import org.olat.repository.DetailsForm;
import org.olat.repository.RepositoryEntry;
import org.olat.repository.RepositoryManager;
import org.olat.repository.handlers.RepositoryHandler;
import org.olat.repository.handlers.RepositoryHandlerFactory;
import org.olat.resource.OLATResource;
import org.olat.resource.OLATResourceManager;
import org.olat.resource.references.ReferenceManager;
/**
* Description:<br>
*
*
* @author Felix Jost
*/
public class RepositoryCopyController extends DefaultController {
private static final String PACKAGE = Util.getPackageName(RepositoryManager.class);
private static final String VELOCITY_ROOT = Util.getPackageVelocityRoot(RepositoryManager.class);
private Translator translator;
private VelocityContainer mainContainer;
private Link cancelButton;
private Link forwardButton;
private RepositoryEditDescriptionController descriptionController;
private Manager securityManager;
private RepositoryEntry sourceEntry;
private RepositoryEntry newEntry;
// flag is true when workflow has been finished successfully,
// otherwhise when disposing the controller or in a case of
// user abort / cancel the system will delete temporary data
private boolean workflowSuccessful = false;
/**
* Create a repository add controller that adds the given resourceable.
* @param ureq
* @param wControl
* @param sourceEntry
*/
public RepositoryCopyController(UserRequest ureq, WindowControl wControl, RepositoryEntry sourceEntry) {
super(wControl);
this.sourceEntry = sourceEntry;
this.newEntry = null;
translator = new PackageTranslator(PACKAGE, ureq.getLocale());
securityManager = ManagerFactory.getManager();
mainContainer = new VelocityContainer("vcCopy", VELOCITY_ROOT + "/copy.html", translator, this);
cancelButton = LinkFactory.createButton("cmd.cancel", mainContainer, this);
forwardButton = LinkFactory.createButton("cmd.forward", mainContainer, this);
forwardButton.setEnabled(false);
LinkFactory.markDownloadLink(forwardButton); // TODO:cg: for copy of large repositoryEntries => Remove when new long-running task is implemented
forwardButton.setTextReasonForDisabling(translator.translate("disabledforwardreason"));
newEntry = createNewRepositoryEntry(sourceEntry, ureq);
descriptionController = new RepositoryEditDescriptionController(ureq, getWindowControl(), newEntry, true);
descriptionController.addControllerListener(this);
mainContainer.put("details", descriptionController.getInitialComponent());
setInitialComponent(mainContainer);
}
/**
* @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest, org.olat.core.gui.control.Controller, org.olat.core.gui.control.Event)
*/
public void event(UserRequest ureq, Controller source, Event event) {
if (source == descriptionController) {
if (event == Event.CANCELLED_EVENT) {
// abort transaction
cleanup();
fireEvent(ureq, Event.CANCELLED_EVENT);
return;
} else if (event == Event.DONE_EVENT) {
forwardButton.setEnabled(true);
}
}
}
/**
* @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest, org.olat.core.gui.components.Component, org.olat.core.gui.control.Event)
*/
public void event(UserRequest ureq, Component source, Event event) {
if (source == forwardButton){
// check if repository entry is still available
RepositoryManager rm = RepositoryManager.getInstance();
RepositoryEntry checkEntry = rm.lookupRepositoryEntry(sourceEntry.getKey());
if (checkEntry == null) { // entry has been deleted meanwhile
getWindowControl().setError(translator.translate("error.createcopy"));
fireEvent(ureq, Event.FAILED_EVENT);
fireEvent(ureq, new EntryChangedEvent(sourceEntry, EntryChangedEvent.DELETED));
return;
}
newEntry = descriptionController.getRepositoryEntry();
RepositoryHandler typeToCopy = RepositoryHandlerFactory.getInstance().getRepositoryHandler(sourceEntry);
IAddController addController = typeToCopy.getAddController(null, null, ureq, getWindowControl());
addController.repositoryEntryCreated(newEntry);
getWindowControl().setInfo(translator.translate("add.success"));
workflowSuccessful = true;
fireEvent(ureq, Event.DONE_EVENT);
return;
} else if (source == cancelButton){
// abort transaction
cleanup();
fireEvent(ureq, Event.CANCELLED_EVENT);
return;
}
}
private RepositoryEntry createNewRepositoryEntry(RepositoryEntry src, UserRequest ureq) {
RepositoryEntry preparedEntry = RepositoryManager.getInstance()
.createRepositoryEntryInstance(ureq.getIdentity().getName());
preparedEntry.setCanDownload(src.getCanDownload());
preparedEntry.setCanLaunch(src.getCanLaunch());
// FIXME:pb:ms translation for COPY OF
String newDispalyname = "Copy of " + src.getDisplayname();
if (newDispalyname.length() > DetailsForm.MAX_DISPLAYNAME) newDispalyname = newDispalyname.substring(0,
DetailsForm.MAX_DISPLAYNAME - 1);
preparedEntry.setDisplayname(newDispalyname);
preparedEntry.setDescription(src.getDescription());
String resName = src.getResourcename();
if (resName == null) resName = "";
preparedEntry.setResourcename(resName);
RepositoryHandler typeToCopy = RepositoryHandlerFactory.getInstance().getRepositoryHandler(src);
OLATResourceable newResourceable = typeToCopy.createCopy(sourceEntry.getOlatResource(), ureq);
if (newResourceable == null) {
getWindowControl().setError(translator.translate("error.createcopy"));
fireEvent(ureq, Event.FAILED_EVENT);
return null;
}
OLATResource ores = OLATResourceManager.getInstance().findOrPersistResourceable(newResourceable);
preparedEntry.setOlatResource(ores);
// create security group
SecurityGroup newGroup = securityManager.createAndPersistSecurityGroup();
// member of this group may modify member's membership
securityManager.createAndPersistPolicy(newGroup, Constants.PERMISSION_ACCESS, newGroup);
// members of this group are always authors also
securityManager.createAndPersistPolicy(newGroup, Constants.PERMISSION_HASROLE, Constants.ORESOURCE_AUTHOR);
securityManager.addIdentityToSecurityGroup(ureq.getIdentity(), newGroup);
preparedEntry.setOwnerGroup(newGroup);
RepositoryManager.getInstance().saveRepositoryEntry(preparedEntry);
// copy image if available
RepositoryEntryImageController.copyImage(src, preparedEntry);
return preparedEntry;
}
protected RepositoryEntry getNewEntry() {
return newEntry;
}
private void cleanup() {
Tracing.logDebug("Cleanup : newEntry=" + newEntry, RepositoryCopyController.class);
if (newEntry != null) {
RepositoryEntry entry = RepositoryManager.getInstance().lookupRepositoryEntry(newEntry.getKey());
if (entry != null) {
try {
Tracing.logDebug("Cleanup : started", RepositoryCopyController.class);
newEntry = (RepositoryEntry) DBFactory.getInstance().loadObject(newEntry,true);
SecurityGroup secGroup = newEntry.getOwnerGroup();
RepositoryHandler repositoryHandler = RepositoryHandlerFactory.getInstance().getRepositoryHandler(newEntry);
Tracing.logDebug("Cleanup : repositoryHandler.cleanupOnDelete for olat-resource=" + newEntry.getOlatResource(), RepositoryCopyController.class);
repositoryHandler.cleanupOnDelete(newEntry.getOlatResource(), null, this.getWindowControl());
Tracing.logDebug("Cleanup : deleteRepositoryEntry", RepositoryCopyController.class);
RepositoryManager.getInstance().deleteRepositoryEntry(newEntry);
if (secGroup != null) { // delete owner group
Tracing.logDebug("Cleanup : deleteSecurityGroup secGroup=" + secGroup, RepositoryCopyController.class);
securityManager.deleteSecurityGroup(secGroup);
}
newEntry = null;
} catch (DBRuntimeException ex) {
Tracing.logError("Can not cleanup properly ", ex, RepositoryCopyController.class);
}
}
}
Tracing.logDebug("Cleanup : finished" , RepositoryCopyController.class);
}
/**
*
* @see org.olat.core.gui.control.DefaultController#doDispose(boolean)
*/
protected void doDispose() {
Tracing.logDebug("doDispose : newEntry=" + newEntry , RepositoryCopyController.class);
if (!workflowSuccessful) {
cleanup();
}
if (descriptionController != null) {
descriptionController.dispose();
descriptionController = null;
}
}
}