~tigase/tigase/dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Tigase Jabber/XMPP Server
 * Copyright (C) 2004-2007 "Artur Hefczyc" <artur.hefczyc@tigase.org>
 *
 * 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 3 of the License.
 *
 * 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 program. Look for COPYING file in the top folder.
 * If not, see http://www.gnu.org/licenses/.
 *
 * $Rev: 1280 $
 * Last modified by $Author: kobit $
 * $Date: 2008-12-11 04:17:28 +0300 (Thu, 11 Dec 2008) $
 */
package tigase.server.sreceiver;

import static tigase.server.sreceiver.PropertyConstants.TASK_OWNER_PROP_KEY;
import static tigase.server.sreceiver.TaskCommons.propertyItems2Command;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Logger;

import tigase.server.Command;
import tigase.server.Packet;
import tigase.util.JIDUtils;
import tigase.xml.XMLUtils;

/**
 * Describe class NewTaskCommand here.
 *
 *
 * Created: Thu May 17 22:19:28 2007
 *
 * @author <a href="mailto:artur.hefczyc@tigase.org">Artur Hefczyc</a>
 * @version $Rev: 1280 $
 */
public class NewTaskCommand implements TaskCommandIfc {

  private static Logger log =
		Logger.getLogger("tigase.server.sreceiver.NewTaskCommand");

	/**
	 * Describe <code>getNodeName</code> method here.
	 *
	 * @return a <code>String</code> value
	 */
	public String getNodeName() {
		return "new-task";
	}

	/**
	 * Describe <code>getDescription</code> method here.
	 *
	 * @return a <code>String</code> value
	 */
	public String getDescription() {
		return "Add new task...";
	}

	private boolean checkTaskName(String task_name, Packet result,
		StanzaReceiver receiv) {
		String msg = JIDUtils.checkNickName(task_name);
		if (msg != null) {
			Command.addFieldValue(result, "Info",
				"Note!! " + msg + ", please provide valid task name.", "fixed");
			return false;
		} // end of if (msg != null)
		ReceiverTaskIfc task = receiv.getTaskInstances().get(task_name);
		if (task != null) {
				Command.addFieldValue(result, "Info",
					"Note!! Task with provided name already exists.", "fixed");
				Command.addFieldValue(result, "Info",
					"Please provide different task name.",	"fixed");
			return false;
		} // end of if (task != null)
		return true;
	}

	private void newTask_Step1(Packet result, StanzaReceiver receiv) {
		Command.addFieldValue(result, "Info",	"Press:", "fixed");
		Command.addFieldValue(result, "Info",
			"'Next' to set all parameters for the new task.", "fixed");
		Command.setStatus(result, Command.Status.executing);
		Command.addAction(result, Command.Action.next);
		Command.addFieldValue(result, TASK_NAME_FIELD,
			"", "text-single", TASK_NAME_FIELD);
		String[] task_types =
			receiv.getTaskTypes().keySet().toArray(new String[0]);
		Command.addFieldValue(result, TASK_TYPE_FIELD, task_types[0],
			TASK_TYPE_FIELD, task_types, task_types);
	}

	private void newTask_Step2(Packet packet, Packet result,
		StanzaReceiver receiv) {
		String task_type = Command.getFieldValue(packet, TASK_TYPE_FIELD);
		ReceiverTaskIfc task_t =
			receiv.getTaskTypes().get(task_type).getTaskType();
		int start = 0;
		int line_len = 60;
		do {
			int end = ((start + line_len < task_t.getHelp().length()) ?
				start + line_len :
				task_t.getHelp().length());
			while (end < task_t.getHelp().length()
				&& task_t.getHelp().charAt(end) != ' ') {
				++end;
			}
			Command.addFieldValue(result, "task" + start,
				task_t.getHelp().substring(start, end), "fixed");
			start = end;
		} while (start < task_t.getHelp().length());
		Command.addFieldValue(result, "Info2",
			"1. 'Finish' to create component with this parameters.", "fixed");
		Command.addFieldValue(result, "Info3",
			"2. 'Previous' to go back and select different component.", "fixed");
		Command.setStatus(result, Command.Status.executing);
		Command.addAction(result, Command.Action.complete);
		Command.addAction(result, Command.Action.prev);
		Map<String, PropertyItem> default_props = task_t.getDefaultParams();
		PropertyItem pi = default_props.get(TASK_OWNER_PROP_KEY);
		if (pi != null) {
			pi.setValue(JIDUtils.getNodeID(packet.getElemFrom()));
		}
		propertyItems2Command(default_props, result);
	}

	private void newTask_Step3(Packet packet, Packet result,
		StanzaReceiver receiv) {
		String task_name = Command.getFieldValue(packet, TASK_NAME_FIELD);
		String task_type = Command.getFieldValue(packet, TASK_TYPE_FIELD);
		Map<String, PropertyItem> default_props =
			receiv.getTaskTypes().get(task_type).getTaskType().getDefaultParams();
		Map<String, Object> new_params = new LinkedHashMap<String, Object>();
		for (Entry<String,PropertyItem> entry: default_props.entrySet()) {
			if (entry.getValue().isMultiValue()) {
				String[] values = Command.getFieldValues(packet, XMLUtils.escape(entry.getKey()));
				new_params.put(entry.getKey(), values);
			} else {
				String value = Command.getFieldValue(packet, XMLUtils.escape(entry.getKey()));
				if (value == null) {
					value = "";
				} // end of if (value == null)
				value = XMLUtils.unescape(value);
				new_params.put(entry.getKey(), value);
			}
		} // end of for (String key: default_props.keySet())
		receiv.addTaskInstance(task_type, task_name, new_params);
		Command.addFieldValue(result, "Info",
			"Created task: " + task_name, "fixed");
	}

	/**
	 * Describe <code>processCommand</code> method here.
	 *
	 * @param packet a <code>Packet</code> value
	 * @param result a <code>Packet</code> value
	 * @param receiv
	 * @param reciv a <code>StanzaReceiver</code> value
	 */
	public void processCommand(Packet packet, Packet result,
		StanzaReceiver receiv) {
		String task_name = Command.getFieldValue(packet, TASK_NAME_FIELD);
		if (task_name == null || !checkTaskName(task_name, result, receiv)
			|| (Command.getAction(packet) == Command.Action.prev)) {
			Command.addFieldValue(result, STEP, "step1", "hidden");
			newTask_Step1(result, receiv);
			return;
		} // end of if (!checkTaskName(receiv, task_name))
		String task_type = Command.getFieldValue(packet, TASK_TYPE_FIELD);
		TaskType tt = receiv.getTaskTypes().get(task_type);
		if (tt == null) {
			Command.addFieldValue(result, "Info",
				"I am sorry there is a problem with instantiating task of this type: "
				+ task_type, "fixed");
			return;
		}
		if (tt.getMaxInstancesNo() <= tt.getInstancesNo()) {
			Command.addFieldValue(result, "Info",
				"I am sorry, maximum number of tasks instances for this type has been"
				+ " exceeded: "
				+ task_type, "fixed");
			return;
		}
		if (!receiv.isAllowedCreate(packet.getFrom(), task_type)) {
			Command.addFieldValue(result, "Info",
				"I am sorry, you are not allowed to create task of this type: "
				+ task_type, "fixed");
			return;
		}
		Command.addFieldValue(result, TASK_NAME_FIELD, task_name, "hidden");
		Command.addFieldValue(result, TASK_TYPE_FIELD, task_type, "hidden");
		String step = Command.getFieldValue(packet, STEP);
		if (step == null || step.equals("step1")) {
			Command.addFieldValue(result, STEP, "step2", "hidden");
			newTask_Step2(packet, result, receiv);
			return;
		} // end of if (step == null || step.equals("step1"))
		newTask_Step3(packet, result, receiv);
	}

} // NewTaskCommand