1/**
2 * $RCSfile$
3 * $Revision$
4 * $Date$
5 *
6 * Copyright 2003-2007 Jive Software.
7 *
8 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 *     http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21package org.jivesoftware.smack.packet;
22
23import java.util.ArrayList;
24import java.util.HashMap;
25import java.util.List;
26import java.util.Map;
27
28/**
29 * Represents registration packets. An empty GET query will cause the server to return information
30 * about it's registration support. SET queries can be used to create accounts or update
31 * existing account information. XMPP servers may require a number of attributes to be set
32 * when creating a new account. The standard account attributes are as follows:
33 * <ul>
34 *      <li>name -- the user's name.
35 *      <li>first -- the user's first name.
36 *      <li>last -- the user's last name.
37 *      <li>email -- the user's email address.
38 *      <li>city -- the user's city.
39 *      <li>state -- the user's state.
40 *      <li>zip -- the user's ZIP code.
41 *      <li>phone -- the user's phone number.
42 *      <li>url -- the user's website.
43 *      <li>date -- the date the registration took place.
44 *      <li>misc -- other miscellaneous information to associate with the account.
45 *      <li>text -- textual information to associate with the account.
46 *      <li>remove -- empty flag to remove account.
47 * </ul>
48 *
49 * @author Matt Tucker
50 */
51public class Registration extends IQ {
52
53    private String instructions = null;
54    private Map<String, String> attributes = new HashMap<String,String>();
55    private List<String> requiredFields = new ArrayList<String>();
56    private boolean registered = false;
57    private boolean remove = false;
58
59    /**
60     * Returns the registration instructions, or <tt>null</tt> if no instructions
61     * have been set. If present, instructions should be displayed to the end-user
62     * that will complete the registration process.
63     *
64     * @return the registration instructions, or <tt>null</tt> if there are none.
65     */
66    public String getInstructions() {
67        return instructions;
68    }
69
70    /**
71     * Sets the registration instructions.
72     *
73     * @param instructions the registration instructions.
74     */
75    public void setInstructions(String instructions) {
76        this.instructions = instructions;
77    }
78
79    /**
80     * Returns the map of String key/value pairs of account attributes.
81     *
82     * @return the account attributes.
83     */
84    public Map<String, String> getAttributes() {
85        return attributes;
86    }
87
88    /**
89     * Sets the account attributes. The map must only contain String key/value pairs.
90     *
91     * @param attributes the account attributes.
92     */
93    public void setAttributes(Map<String, String> attributes) {
94        this.attributes = attributes;
95    }
96
97    public List<String> getRequiredFields(){
98    	return requiredFields;
99    }
100
101    public void addAttribute(String key, String value){
102    	attributes.put(key, value);
103    }
104
105    public void setRegistered(boolean registered){
106    	this.registered = registered;
107    }
108
109    public boolean isRegistered(){
110    	return this.registered;
111    }
112
113    public String getField(String key){
114    	return attributes.get(key);
115    }
116
117    public List<String> getFieldNames(){
118    	return new ArrayList<String>(attributes.keySet());
119    }
120
121    public void setUsername(String username){
122    	attributes.put("username", username);
123    }
124
125    public void setPassword(String password){
126    	attributes.put("password", password);
127    }
128
129    public void setRemove(boolean remove){
130    	this.remove = remove;
131    }
132
133    public String getChildElementXML() {
134        StringBuilder buf = new StringBuilder();
135        buf.append("<query xmlns=\"jabber:iq:register\">");
136        if (instructions != null && !remove) {
137            buf.append("<instructions>").append(instructions).append("</instructions>");
138        }
139        if (attributes != null && attributes.size() > 0 && !remove) {
140            for (String name : attributes.keySet()) {
141                String value = attributes.get(name);
142                buf.append("<").append(name).append(">");
143                buf.append(value);
144                buf.append("</").append(name).append(">");
145            }
146        }
147        else if(remove){
148        	buf.append("</remove>");
149        }
150        // Add packet extensions, if any are defined.
151        buf.append(getExtensionsXML());
152        buf.append("</query>");
153        return buf.toString();
154    }
155}