GroupModified.java 6.68 KB
Newer Older
1
/**
2 3 4
 * $RCSfile$
 * $Revision: 3144 $
 * $Date: 2005-12-01 14:20:11 -0300 (Thu, 01 Dec 2005) $
5
 *
6 7
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
 *
8 9 10 11 12 13 14 15 16 17 18
 * 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.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 */
package org.jivesoftware.openfire.commands.event;

import org.dom4j.Element;
import org.jivesoftware.openfire.commands.AdHocCommand;
import org.jivesoftware.openfire.commands.SessionData;
import org.jivesoftware.openfire.component.InternalComponentManager;
import org.jivesoftware.openfire.event.GroupEventDispatcher;
import org.jivesoftware.openfire.group.Group;
import org.jivesoftware.openfire.group.GroupManager;
import org.jivesoftware.openfire.group.GroupNotFoundException;
import org.xmpp.forms.DataForm;
import org.xmpp.forms.FormField;
import org.xmpp.packet.JID;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Notifies the that a group was modified. It can be used by user providers to notify Openfire of the
 * modification of a group.
 *
 * @author Gabriel Guarincerri
 */
public class GroupModified extends AdHocCommand {
46 47
    @Override
	public String getCode() {
48 49 50
        return "http://jabber.org/protocol/event#group-modified";
    }

51 52
    @Override
	public String getDefaultLabel() {
53 54 55
        return "Group modified";
    }

56 57
    @Override
	public int getMaxStages(SessionData data) {
58 59 60
        return 1;
    }

61 62
    @Override
	public void execute(SessionData sessionData, Element command) {
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        Element note = command.addElement("note");

        Map<String, List<String>> data = sessionData.getData();

        // Get the group name
        String groupname;
        try {
            groupname = get(data, "groupName", 0);
        }
        catch (NullPointerException npe) {
            note.addAttribute("type", "error");
            note.setText("Group name required parameter.");
            return;
        }

        // Get the modification type
        String type;
        try {
            type = get(data, "changeType", 0);
        }
        catch (NullPointerException npe) {
            note.addAttribute("type", "error");
            note.setText("Change type required parameter.");
            return;
        }

        // Identifies the value variable
        String valueVariable = null;
        String valueVariableName = null;

        if ("nameModified".equals(type) || "descriptionModified".equals(type)) {

            valueVariable = "originalValue";
            valueVariableName = "Original value";

        } else if ("propertyModified".equals(type) || "propertyAdded".equals(type) ||
                "propertyDeleted".equals(type)) {

            valueVariable = "propertyKey";
            valueVariableName = "Property key";

        }

        // Creates event params.
        Map<String, Object> params = new HashMap<String, Object>();

        // Gets the value of the change if it exist
        String value;
        if (valueVariable != null) {
            try {
                // Gets the value
                value = get(data, valueVariable, 0);
                // Adds it to the event params
                params.put(valueVariable, value);

            } catch (NullPointerException npe) {
                note.addAttribute("type", "error");
                note.setText(valueVariableName + " required parameter.");
                return;
            }
        }

        // Adds the type of change
        params.put("type", type);

        // Sends the event
        Group group;
        try {
131
            group = GroupManager.getInstance().getGroup(groupname, true);
132 133 134 135 136 137 138 139 140 141 142 143 144 145

            // Fire event.
            GroupEventDispatcher.dispatchEvent(group, GroupEventDispatcher.EventType.group_modified, params);

        } catch (GroupNotFoundException e) {
            note.addAttribute("type", "error");
            note.setText("Group not found.");
        }

        // Answer that the operation was successful
        note.addAttribute("type", "info");
        note.setText("Operation finished successfully");
    }

146 147
    @Override
	protected void addStageInformation(SessionData data, Element command) {
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
        DataForm form = new DataForm(DataForm.Type.form);
        form.setTitle("Dispatching a group created event.");
        form.addInstruction("Fill out this form to dispatch a group created event.");

        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("The group name of the group that was created");
        field.setVariable("groupName");
        field.setRequired(true);

        field.setType(FormField.Type.list_single);
        field.setLabel("Change type");
        field.setVariable("changeType");
        field.addOption("Name modified", "nameModified");
        field.addOption("Description modified", "descriptionModified");
        field.addOption("Property modified", "propertyModified");
        field.addOption("Property added", "propertyAdded");
        field.addOption("Property deleted", "propertyDeleted");
        field.addOption("Other", "other");
        field.setRequired(true);

        field = form.addField();
        field.setType(FormField.Type.text_single);
        field.setLabel("Original value");
        field.setVariable("originalValue");

        field = form.addField();
        field.setType(FormField.Type.text_single);
        field.setLabel("Name of the property");
        field.setVariable("propertyKey");


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

189 190
    @Override
	protected List<Action> getActions(SessionData data) {
191 192 193
        return Arrays.asList(Action.complete);
    }

194 195
    @Override
	protected Action getExecuteAction(SessionData data) {
196 197 198
        return Action.complete;
    }

199 200
    @Override
	public boolean hasPermission(JID requester) {
201 202 203
        return super.hasPermission(requester) || InternalComponentManager.getInstance().hasComponent(requester);
    }
}