DeleteGroup.java 3.5 KB
Newer Older
Gaston Dombiak's avatar
Gaston Dombiak committed
1 2 3 4
/**
 * $Revision: $
 * $Date: $
 *
5
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
Gaston Dombiak's avatar
Gaston Dombiak committed
6
 *
7 8 9 10 11 12 13 14 15 16 17
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
Gaston Dombiak's avatar
Gaston Dombiak committed
18 19
 */

20
package org.jivesoftware.openfire.commands.admin.group;
Gaston Dombiak's avatar
Gaston Dombiak committed
21 22

import org.dom4j.Element;
23 24 25 26 27
import org.jivesoftware.openfire.commands.AdHocCommand;
import org.jivesoftware.openfire.commands.SessionData;
import org.jivesoftware.openfire.group.Group;
import org.jivesoftware.openfire.group.GroupManager;
import org.jivesoftware.openfire.group.GroupNotFoundException;
Gaston Dombiak's avatar
Gaston Dombiak committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41
import org.xmpp.forms.DataForm;
import org.xmpp.forms.FormField;

import java.util.Arrays;
import java.util.List;

/**
 * Command that allows to delete existing groups.
 *
 * @author Gaston Dombiak
 *
 * TODO Use i18n
 */
public class DeleteGroup extends AdHocCommand {
42 43
    @Override
	protected void addStageInformation(SessionData data, Element command) {
Gaston Dombiak's avatar
Gaston Dombiak committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        DataForm form = new DataForm(DataForm.Type.form);
        form.setTitle("Delete group");
        form.addInstruction("Fill out this form to delete a group.");

        FormField field = form.addField();
        field.setType(FormField.Type.hidden);
        field.setVariable("FORM_TYPE");
        field.addValue("http://jabber.org/protocol/admin");

        field = form.addField();
        field.setType(FormField.Type.text_single);
        field.setLabel("Group Name");
        field.setVariable("group");
        field.setRequired(true);

        // Add the form to the command
        command.add(form.getElement());
    }

63 64
    @Override
	public void execute(SessionData data, Element command) {
Gaston Dombiak's avatar
Gaston Dombiak committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        Element note = command.addElement("note");
        // Check if groups cannot be modified (backend is read-only)
        if (GroupManager.getInstance().isReadOnly()) {
            note.addAttribute("type", "error");
            note.setText("Groups are read only");
            return;
        }
        // Get requested group
        Group group;
        try {
            group = GroupManager.getInstance().getGroup(data.getData().get("group").get(0));
        } catch (GroupNotFoundException e) {
            // Group not found
            note.addAttribute("type", "error");
            note.setText("Group name does not exist");
            return;
        }

        GroupManager.getInstance().deleteGroup(group);

        note.addAttribute("type", "info");
        note.setText("Operation finished successfully");
    }

89 90
    @Override
	public String getCode() {
Gaston Dombiak's avatar
Gaston Dombiak committed
91 92 93
        return "http://jabber.org/protocol/admin#delete-group";
    }

94 95
    @Override
	public String getDefaultLabel() {
Gaston Dombiak's avatar
Gaston Dombiak committed
96 97 98
        return "Delete group";
    }

99 100
    @Override
	protected List<Action> getActions(SessionData data) {
Gaston Dombiak's avatar
Gaston Dombiak committed
101 102 103
        return Arrays.asList(AdHocCommand.Action.complete);
    }

104 105
    @Override
	protected AdHocCommand.Action getExecuteAction(SessionData data) {
Gaston Dombiak's avatar
Gaston Dombiak committed
106 107 108
        return AdHocCommand.Action.complete;
    }

109 110
    @Override
	public int getMaxStages(SessionData data) {
Gaston Dombiak's avatar
Gaston Dombiak committed
111 112 113
        return 1;
    }
}