001/**
002 *
003 * Copyright 2003-2007 Jive Software.
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 */
017
018package org.jivesoftware.smackx.workgroup.ext.macros;
019
020import java.util.ArrayList;
021import java.util.Collection;
022import java.util.Collections;
023import java.util.Iterator;
024import java.util.List;
025
026/**
027 * MacroGroup datamodel.
028 */
029public class MacroGroup {
030    private List<Macro> macros;
031    private List<MacroGroup> macroGroups;
032
033
034    // Define MacroGroup
035    private String title;
036
037    public MacroGroup() {
038        macros = new ArrayList<>();
039        macroGroups = new ArrayList<>();
040    }
041
042    public void addMacro(Macro macro) {
043        macros.add(macro);
044    }
045
046    public void removeMacro(Macro macro) {
047        macros.remove(macro);
048    }
049
050    public Macro getMacroByTitle(String title) {
051        Collection<Macro> col = Collections.unmodifiableList(macros);
052        Iterator<Macro> iter = col.iterator();
053        while (iter.hasNext()) {
054            Macro macro = iter.next();
055            if (macro.getTitle().equalsIgnoreCase(title)) {
056                return macro;
057            }
058        }
059        return null;
060    }
061
062    public void addMacroGroup(MacroGroup group) {
063        macroGroups.add(group);
064    }
065
066    public void removeMacroGroup(MacroGroup group) {
067        macroGroups.remove(group);
068    }
069
070    public Macro getMacro(int location) {
071        return macros.get(location);
072    }
073
074    public MacroGroup getMacroGroupByTitle(String title) {
075        Collection<MacroGroup> col = Collections.unmodifiableList(macroGroups);
076        Iterator<MacroGroup> iter = col.iterator();
077        while (iter.hasNext()) {
078            MacroGroup group = iter.next();
079            if (group.getTitle().equalsIgnoreCase(title)) {
080                return group;
081            }
082        }
083        return null;
084    }
085
086    public MacroGroup getMacroGroup(int location) {
087        return macroGroups.get(location);
088    }
089
090
091    public List<Macro>  getMacros() {
092        return macros;
093    }
094
095    public void setMacros(List<Macro> macros) {
096        this.macros = macros;
097    }
098
099    public List<MacroGroup> getMacroGroups() {
100        return macroGroups;
101    }
102
103    public void setMacroGroups(List<MacroGroup> macroGroups) {
104        this.macroGroups = macroGroups;
105    }
106
107    public String getTitle() {
108        return title;
109    }
110
111    public void setTitle(String title) {
112        this.title = title;
113    }
114
115    public String toXML() {
116        // CHECKSTYLE:OFF
117        StringBuilder buf = new StringBuilder();
118        buf.append("<macrogroup>");
119        buf.append("<title>" +  getTitle() + "</title>");
120        buf.append("<macros>");
121        for (Macro macro : getMacros())
122                {
123                buf.append("<macro>");
124                buf.append("<title>" + macro.getTitle() + "</title>");
125                buf.append("<type>" + macro.getType() + "</type>");
126                buf.append("<description>" + macro.getDescription() + "</description>");
127                buf.append("<response>" + macro.getResponse() + "</response>");
128                buf.append("</macro>");
129                }
130        buf.append("</macros>");
131
132        if (getMacroGroups().size() > 0) {
133                buf.append("<macroGroups>");
134                for (MacroGroup groups : getMacroGroups()) {
135                        buf.append(groups.toXML());
136                }
137                buf.append("</macroGroups>");
138        }
139        buf.append("</macrogroup>");
140        return buf.toString();
141        // CHECKSTYLE:ON
142    }
143}