001/**
002 *
003 * Copyright 2016 Fernando Ramirez
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.muclight.element;
018
019import java.util.HashMap;
020import java.util.Iterator;
021import java.util.Map;
022
023import javax.xml.namespace.QName;
024
025import org.jivesoftware.smack.packet.Element;
026import org.jivesoftware.smack.packet.ExtensionElement;
027import org.jivesoftware.smack.packet.Message;
028import org.jivesoftware.smack.util.XmlStringBuilder;
029
030import org.jivesoftware.smackx.muclight.MUCLightAffiliation;
031import org.jivesoftware.smackx.muclight.MUCLightRoomConfiguration;
032import org.jivesoftware.smackx.muclight.MultiUserChatLight;
033import org.jivesoftware.smackx.xdata.packet.DataForm;
034
035import org.jxmpp.jid.Jid;
036
037public abstract class MUCLightElements {
038
039    /**
040     * Affiliations change extension element class.
041     *
042     * @author Fernando Ramirez
043     *
044     */
045    public static class AffiliationsChangeExtension implements ExtensionElement {
046
047        public static final String ELEMENT = DataForm.ELEMENT;
048        public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.AFFILIATIONS;
049        public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
050
051        private final HashMap<Jid, MUCLightAffiliation> affiliations;
052        private final String prevVersion;
053        private final String version;
054
055        public AffiliationsChangeExtension(HashMap<Jid, MUCLightAffiliation> affiliations, String prevVersion,
056                String version) {
057            this.affiliations = affiliations;
058            this.prevVersion = prevVersion;
059            this.version = version;
060        }
061
062        @Override
063        public String getElementName() {
064            return ELEMENT;
065        }
066
067        @Override
068        public String getNamespace() {
069            return NAMESPACE;
070        }
071
072        /**
073         * Get the affiliations.
074         *
075         * @return the affiliations
076         */
077        public HashMap<Jid, MUCLightAffiliation> getAffiliations() {
078            return affiliations;
079        }
080
081        /**
082         * Get the previous version.
083         *
084         * @return the previous version
085         */
086        public String getPrevVersion() {
087            return prevVersion;
088        }
089
090        /**
091         * Get the version.
092         *
093         * @return the version
094         */
095        public String getVersion() {
096            return version;
097        }
098
099        @Override
100        public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
101            XmlStringBuilder xml = new XmlStringBuilder(this);
102            xml.rightAngleBracket();
103
104            xml.optElement("prev-version", prevVersion);
105            xml.optElement("version", version);
106
107            Iterator<Map.Entry<Jid, MUCLightAffiliation>> it = affiliations.entrySet().iterator();
108            while (it.hasNext()) {
109                Map.Entry<Jid, MUCLightAffiliation> pair = it.next();
110                xml.append(new UserWithAffiliationElement(pair.getKey(), pair.getValue()));
111            }
112
113            xml.closeElement(this);
114            return xml;
115        }
116
117        public static AffiliationsChangeExtension from(Message message) {
118            return message.getExtension(AffiliationsChangeExtension.class);
119        }
120
121    }
122
123    /**
124     * Configurations change extension element class.
125     *
126     * @author Fernando Ramirez
127     *
128     */
129    public static class ConfigurationsChangeExtension implements ExtensionElement {
130
131        public static final String ELEMENT = DataForm.ELEMENT;
132        public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.CONFIGURATION;
133
134        private final String prevVersion;
135        private final String version;
136        private final String roomName;
137        private final String subject;
138        private final HashMap<String, String> customConfigs;
139
140        /**
141         * Configurations change extension constructor.
142         *
143         * @param prevVersion TODO javadoc me please
144         * @param version TODO javadoc me please
145         * @param roomName TODO javadoc me please
146         * @param subject TODO javadoc me please
147         * @param customConfigs TODO javadoc me please
148         */
149        public ConfigurationsChangeExtension(String prevVersion, String version, String roomName, String subject,
150                HashMap<String, String> customConfigs) {
151            this.prevVersion = prevVersion;
152            this.version = version;
153            this.roomName = roomName;
154            this.subject = subject;
155            this.customConfigs = customConfigs;
156        }
157
158        @Override
159        public String getElementName() {
160            return ELEMENT;
161        }
162
163        @Override
164        public String getNamespace() {
165            return NAMESPACE;
166        }
167
168        /**
169         * Get the previous version.
170         *
171         * @return the previous version
172         */
173        public String getPrevVersion() {
174            return prevVersion;
175        }
176
177        /**
178         * Get the version.
179         *
180         * @return the version
181         */
182        public String getVersion() {
183            return version;
184        }
185
186        /**
187         * Get the room name.
188         *
189         * @return the room name
190         */
191        public String getRoomName() {
192            return roomName;
193        }
194
195        /**
196         * Get the room subject.
197         *
198         * @return the room subject
199         */
200        public String getSubject() {
201            return subject;
202        }
203
204        /**
205         * Get the room custom configurations.
206         *
207         * @return the room custom configurations
208         */
209        public HashMap<String, String> getCustomConfigs() {
210            return customConfigs;
211        }
212
213        @Override
214        public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
215            XmlStringBuilder xml = new XmlStringBuilder(this);
216            xml.rightAngleBracket();
217
218            xml.optElement("prev-version", prevVersion);
219            xml.optElement("version", version);
220            xml.optElement("roomname", roomName);
221            xml.optElement("subject", subject);
222
223            if (customConfigs != null) {
224                Iterator<Map.Entry<String, String>> it = customConfigs.entrySet().iterator();
225                while (it.hasNext()) {
226                    Map.Entry<String, String> pair = it.next();
227                    xml.element(pair.getKey(), pair.getValue());
228                }
229            }
230
231            xml.closeElement(this);
232            return xml;
233        }
234
235        public static ConfigurationsChangeExtension from(Message message) {
236            return (ConfigurationsChangeExtension) message.getExtensionElement(ConfigurationsChangeExtension.ELEMENT, ConfigurationsChangeExtension.NAMESPACE);
237        }
238
239    }
240
241    /**
242     * Configuration element class.
243     *
244     * @author Fernando Ramirez
245     *
246     */
247    public static class ConfigurationElement implements Element {
248
249        private MUCLightRoomConfiguration configuration;
250
251        /**
252         * Configuration element constructor.
253         *
254         * @param configuration TODO javadoc me please
255         */
256        public ConfigurationElement(MUCLightRoomConfiguration configuration) {
257            this.configuration = configuration;
258        }
259
260        @Override
261        public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
262            XmlStringBuilder xml = new XmlStringBuilder();
263            xml.openElement("configuration");
264
265            xml.element("roomname", configuration.getRoomName());
266            xml.optElement("subject", configuration.getSubject());
267
268            if (configuration.getCustomConfigs() != null) {
269                Iterator<Map.Entry<String, String>> it = configuration.getCustomConfigs().entrySet().iterator();
270                while (it.hasNext()) {
271                    Map.Entry<String, String> pair = it.next();
272                    xml.element(pair.getKey(), pair.getValue());
273                }
274            }
275
276            xml.closeElement("configuration");
277            return xml;
278        }
279
280    }
281
282    /**
283     * Occupants element class.
284     *
285     * @author Fernando Ramirez
286     *
287     */
288    public static class OccupantsElement implements Element {
289
290        private HashMap<Jid, MUCLightAffiliation> occupants;
291
292        /**
293         * Occupants element constructor.
294         *
295         * @param occupants TODO javadoc me please
296         */
297        public OccupantsElement(HashMap<Jid, MUCLightAffiliation> occupants) {
298            this.occupants = occupants;
299        }
300
301        @Override
302        public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
303            XmlStringBuilder xml = new XmlStringBuilder();
304            xml.openElement("occupants");
305
306            Iterator<Map.Entry<Jid, MUCLightAffiliation>> it = occupants.entrySet().iterator();
307            while (it.hasNext()) {
308                Map.Entry<Jid, MUCLightAffiliation> pair = it.next();
309                xml.append(new UserWithAffiliationElement(pair.getKey(), pair.getValue()));
310            }
311
312            xml.closeElement("occupants");
313            return xml;
314        }
315
316    }
317
318    /**
319     * User with affiliation element class.
320     *
321     * @author Fernando Ramirez
322     *
323     */
324    public static class UserWithAffiliationElement implements Element {
325
326        private Jid user;
327        private MUCLightAffiliation affiliation;
328
329        /**
330         * User with affiliations element constructor.
331         *
332         * @param user TODO javadoc me please
333         * @param affiliation TODO javadoc me please
334         */
335        public UserWithAffiliationElement(Jid user, MUCLightAffiliation affiliation) {
336            this.user = user;
337            this.affiliation = affiliation;
338        }
339
340        @Override
341        public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
342            XmlStringBuilder xml = new XmlStringBuilder();
343            xml.halfOpenElement("user");
344            xml.attribute("affiliation", affiliation);
345            xml.rightAngleBracket();
346            xml.escape(user);
347            xml.closeElement("user");
348            return xml;
349        }
350
351    }
352
353    /**
354     * Blocking element class.
355     *
356     * @author Fernando Ramirez
357     *
358     */
359    public static class BlockingElement implements Element {
360
361        private Jid jid;
362        private Boolean allow;
363        private Boolean isRoom;
364
365        /**
366         * Blocking element constructor.
367         *
368         * @param jid TODO javadoc me please
369         * @param allow TODO javadoc me please
370         * @param isRoom TODO javadoc me please
371         */
372        public BlockingElement(Jid jid, Boolean allow, Boolean isRoom) {
373            this.jid = jid;
374            this.allow = allow;
375            this.isRoom = isRoom;
376        }
377
378        @Override
379        public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
380            XmlStringBuilder xml = new XmlStringBuilder();
381
382            String tag = isRoom ? "room" : "user";
383            xml.halfOpenElement(tag);
384
385            String action = allow ? "allow" : "deny";
386            xml.attribute("action", action);
387            xml.rightAngleBracket();
388
389            xml.escape(jid);
390
391            xml.closeElement(tag);
392            return xml;
393        }
394
395    }
396
397}