MacroGroup.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.workgroup.ext.macros;

  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.Collections;
  21. import java.util.Iterator;
  22. import java.util.List;

  23. /**
  24.  * MacroGroup datamodel.
  25.  */
  26. public class MacroGroup {
  27.     private List<Macro> macros;
  28.     private List<MacroGroup> macroGroups;


  29.     // Define MacroGroup
  30.     private String title;

  31.     public MacroGroup() {
  32.         macros = new ArrayList<Macro>();
  33.         macroGroups = new ArrayList<MacroGroup>();
  34.     }

  35.     public void addMacro(Macro macro) {
  36.         macros.add(macro);
  37.     }

  38.     public void removeMacro(Macro macro) {
  39.         macros.remove(macro);
  40.     }

  41.     public Macro getMacroByTitle(String title) {
  42.         Collection<Macro> col = Collections.unmodifiableList(macros);
  43.         Iterator<Macro> iter = col.iterator();
  44.         while (iter.hasNext()) {
  45.             Macro macro = (Macro)iter.next();
  46.             if (macro.getTitle().equalsIgnoreCase(title)) {
  47.                 return macro;
  48.             }
  49.         }
  50.         return null;
  51.     }

  52.     public void addMacroGroup(MacroGroup group) {
  53.         macroGroups.add(group);
  54.     }

  55.     public void removeMacroGroup(MacroGroup group) {
  56.         macroGroups.remove(group);
  57.     }

  58.     public Macro getMacro(int location) {
  59.         return (Macro)macros.get(location);
  60.     }

  61.     public MacroGroup getMacroGroupByTitle(String title) {
  62.         Collection<MacroGroup> col = Collections.unmodifiableList(macroGroups);
  63.         Iterator<MacroGroup> iter = col.iterator();
  64.         while (iter.hasNext()) {
  65.             MacroGroup group = (MacroGroup)iter.next();
  66.             if (group.getTitle().equalsIgnoreCase(title)) {
  67.                 return group;
  68.             }
  69.         }
  70.         return null;
  71.     }

  72.     public MacroGroup getMacroGroup(int location) {
  73.         return (MacroGroup)macroGroups.get(location);
  74.     }


  75.     public List<Macro>  getMacros() {
  76.         return macros;
  77.     }

  78.     public void setMacros(List<Macro> macros) {
  79.         this.macros = macros;
  80.     }

  81.     public List<MacroGroup> getMacroGroups() {
  82.         return macroGroups;
  83.     }

  84.     public void setMacroGroups(List<MacroGroup> macroGroups) {
  85.         this.macroGroups = macroGroups;
  86.     }

  87.     public String getTitle() {
  88.         return title;
  89.     }

  90.     public void setTitle(String title) {
  91.         this.title = title;
  92.     }
  93.    
  94.     public String toXML() {
  95.         StringBuilder buf = new StringBuilder();
  96.         buf.append("<macrogroup>");
  97.         buf.append("<title>" +  getTitle() + "</title>");
  98.         buf.append("<macros>");
  99.         for (Macro macro : getMacros())
  100.         {
  101.             buf.append("<macro>");
  102.             buf.append("<title>" + macro.getTitle() + "</title>");
  103.             buf.append("<type>" + macro.getType() + "</type>");
  104.             buf.append("<description>" + macro.getDescription() + "</description>");
  105.             buf.append("<response>" + macro.getResponse() + "</response>");
  106.             buf.append("</macro>");
  107.         }
  108.         buf.append("</macros>");
  109.        
  110.         if (getMacroGroups().size() > 0) {
  111.             buf.append("<macroGroups>");
  112.             for (MacroGroup groups : getMacroGroups()) {
  113.                 buf.append(groups.toXML());
  114.             }
  115.             buf.append("</macroGroups>");
  116.         }
  117.         buf.append("</macrogroup>");
  118.         return buf.toString();
  119.     }
  120. }