Java tutorial
/* * Copyright (C) 2007 Unicon, Inc. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this distribution. It is also available here: * http://www.fsf.org/licensing/licenses/gpl.html * * As a special exception to the terms and conditions of version * 2 of the GPL, you may redistribute this Program in connection * with Free/Libre and Open Source Software ("FLOSS") applications * as described in the GPL FLOSS exception. You should have received * a copy of the text describing the FLOSS exception along with this * distribution. */ package net.unicon.academus.apps.briefcase.engine; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; import org.dom4j.Attribute; import org.dom4j.Element; import net.unicon.penelope.IChoiceCollection; import net.unicon.penelope.IDecision; import net.unicon.penelope.IDecisionCollection; import net.unicon.penelope.ISelection; import net.unicon.warlock.Handle; import net.unicon.warlock.IAction; import net.unicon.warlock.IActionResponse; import net.unicon.warlock.IScreen; import net.unicon.warlock.IUserContext; import net.unicon.warlock.IWarlockFactory; import net.unicon.warlock.StateMachine; import net.unicon.warlock.WarlockException; import net.unicon.warlock.XmlFormatException; import net.unicon.warlock.fac.AbstractWarlockFactory; import net.unicon.warlock.fac.SimpleActionResponse; import net.unicon.academus.apps.ErrorMessage; import net.unicon.academus.apps.briefcase.BriefcaseAccessType; import net.unicon.academus.apps.briefcase.BriefcaseApplicationContext; import net.unicon.academus.apps.briefcase.BriefcaseUserContext; import net.unicon.alchemist.access.AccessRule; import net.unicon.alchemist.encrypt.EncryptionService; import net.unicon.demetrius.IResource; import net.unicon.demetrius.fac.AbstractResourceFactory; public final class ConfirmDeleteAction extends AbstractWarlockFactory.AbstractAction { // Instance Members. private BriefcaseApplicationContext app; private IScreen screen; private IScreen errScreen; /* * Public API. */ public static IAction parse(Element e, IWarlockFactory owner) throws WarlockException { // Assertions. if (e == null) { String msg = "Argument 'e [Element]' cannot be null."; throw new IllegalArgumentException(msg); } if (!e.getName().equals("action")) { String msg = "Argument 'e [Element]' must be an <action> element."; throw new IllegalArgumentException(msg); } if (owner == null) { String msg = "Argument 'owner' cannot be null."; throw new IllegalArgumentException(msg); } // Handle. Attribute h = e.attribute("handle"); if (h == null) { String msg = "Element <action> is missing required attribute " + "'handle'."; throw new XmlFormatException(msg); } Handle handle = Handle.create(h.getValue()); // Choices. String[] choices = new String[0]; Attribute p = e.attribute("inpt"); if (p != null) { StringTokenizer tokens = new StringTokenizer(p.getValue(), ","); choices = new String[tokens.countTokens()]; for (int i = 0; tokens.hasMoreTokens(); i++) { choices[i] = tokens.nextToken(); } } return new ConfirmDeleteAction(owner, handle, choices); } public void init(StateMachine m) { // Assertions. if (m == null) { String msg = "Argument 'm [StateMachine]' cannot be null."; throw new IllegalArgumentException(msg); } app = (BriefcaseApplicationContext) m.getContext(); screen = m.getScreen(Handle.create("delete")); errScreen = m.getScreen(Handle.create("folderview")); } public IActionResponse invoke(IDecisionCollection[] decisions, IUserContext ctx) throws WarlockException { // Assertions. if (decisions == null) { String msg = "Argument 'decisions' cannot be null."; throw new IllegalArgumentException(msg); } if (ctx == null) { String msg = "Argument 'ctx' cannot be null."; throw new IllegalArgumentException(msg); } if (log.isDebugEnabled()) { log.debug("Invoking Briefcase Action: " + getClass().getName()); } List err = new ArrayList(); BriefcaseUserContext user = (BriefcaseUserContext) ctx; // Build the selection. IDecisionCollection dCol = decisions[0]; IChoiceCollection cCol = dCol.getChoiceCollection(); IDecision d = dCol.getDecision(cCol.getChoice(net.unicon.penelope.Handle.create("selectedItems"))); IResource[] targets = null; ISelection[] sel = d.getSelections(); EncryptionService encryptionService = app.getEncryptionService(); String problem = ""; String msg = ""; String solution = null; try { if (sel.length == 0) { // choose the parent folder... targets = new IResource[] { (IResource) user.getHistory().getLocation().getObject() }; } else { // the user selected targets... targets = new IResource[sel.length]; for (int i = 0; i < sel.length; i++) { String url = sel[i].getOption().getHandle().getValue(); // Decrypt url before using it url = encryptionService.decrypt(url); targets[i] = AbstractResourceFactory.resourceFromUrl(url); } } } catch (Throwable t) { msg = "Resource with the given name was not found. "; problem = "The given resource may have been remamed or deleted. " + "'. " + msg; err.add(new ErrorMessage("other", problem, "")); t.printStackTrace(); return new SimpleActionResponse(screen, new FolderQuery(user.getHistory().getLocation(), app, user, (ErrorMessage[]) err.toArray(new ErrorMessage[err.size()]))); } boolean hasPermission = true; // Make sure that a root folder was not selected. Iterator it = Arrays.asList(targets).iterator(); while (it.hasNext()) { IResource r = (IResource) it.next(); if (r.getOwner().getRoot().equals(r)) { problem = "Unable to delete folder '" + r.getName() + "'. Root folders may not be " + "moved, copied, deleted, or shared."; err.add(new ErrorMessage("other", problem, solution)); } } // Check to see if any of the selected folders are shared roots. /* ToDo: Restore this functionality... user.updateSharedResources(); List sharedResources = user.getSharedResources(); for (int i = 0; i < targets.length; i++) { if (sharedResources.contains(targets[i].getUrl())) { problem = "Unable to delete folder '" + targets[i].getName() + "'. Shared folder roots may not be" + "moved, copied, or deleted."; solution = "Please remove any sharing from this folder" + " before deleting."; err.add(new ErrorMessage("other", problem, solution)); } } */ // Check delete permissions List access = Arrays.asList(user.getAccessRules(targets[0].getOwner())); if (!access.contains(new AccessRule(BriefcaseAccessType.DELETE, true))) { msg = "User does not have 'delete' access to the context folder."; problem = "Unable to delete '" + targets[0].getName() + "' folder. " + msg; err.add(new ErrorMessage("other", problem, solution)); hasPermission = false; } // Choose success or failure. if (err.size() == 0) { // Retain the selection. user.setResourceSelection(targets); return new SimpleActionResponse(screen, new ConfirmDeleteQuery(targets)); } else { // Display the error. return new SimpleActionResponse(errScreen, new FolderQuery(user.getHistory().getLocation(), app, user, (ErrorMessage[]) err.toArray(new ErrorMessage[0]))); } } /* * Package API. */ private ConfirmDeleteAction(IWarlockFactory owner, Handle handle, String[] choices) { super(owner, handle, choices); // Instance Members. this.screen = null; this.errScreen = null; } }