/*
 *  The MIT License (MIT)
 *
 *  Copyright (c) 2016 Lachlan Dowding
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in all
 *  copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *  SOFTWARE.
 */

package permafrost.tundra.server.security;

import com.wm.app.b2b.server.Group;
import com.wm.app.b2b.server.ServiceException;
import com.wm.app.b2b.server.UserManager;
import permafrost.tundra.lang.ExceptionHelper;

/**
 * Collection of convenience methods for working with Integration Server groups.
 */
public final class GroupHelper {
    /**
     * Disallow instantiation of this class.
     */
    private GroupHelper() {}

    /**
     * Creates a new group with the given name unless it already exists.
     *
     * @param name              The group name.
     * @throws ServiceException If the group cannot be created.
     */
    public static synchronized void create(String name) throws ServiceException {
        try {
            UserManager.addGroup(name);
        } catch(Exception ex) {
            ExceptionHelper.raise(ex);
        }
    }

    /**
     * Returns the group with the given name if it exists, otherwise creates a new group with the given name.
     *
     * @param name              The group name.
     * @return                  Either the existing group with the given name, or a newly created group with the name.
     * @throws ServiceException If the group cannot be created.
     */
    public static synchronized Group findOrCreate(String name) throws ServiceException {
        Group group = get(name);
        if (group == null) {
            create(name);
            group = get(name);
        }
        return group;
    }

    /**
     * Returns the groups with the given names if they exists, otherwise creates new groups with the given names.
     *
     * @param names             The group names.
     * @return                  Either the existing groups with the given names, or newly created groups with the names.
     * @throws ServiceException If a group cannot be created.
     */
    public static synchronized Group[] findOrCreate(String[] names) throws ServiceException {
        if (names == null) return null;

        Group[] groups = new Group[names.length];

        for(int i = 0; i < names.length; i++) {
            groups[i] = findOrCreate(names[i]);
        }

        return groups;
    }

    /**
     * Returns true if a group with the given name exists.
     *
     * @param name  The group name.
     * @return      True if a group with the given name exists.
     */
    public static boolean exists(String name) {
        return get(name) != null;
    }

    /**
     * Returns the group with the given name.
     *
     * @param name  The group name.
     * @return      The group with the given name, or null if no group with the given name exists.
     */
    public static Group get(String name) {
        return UserManager.getGroup(name);
    }

    /**
     * Returns the list of groups that correspond to the given list of names.
     *
     * @param names The list of group names.
     * @return      The list of groups that correspond to the given list of names.
     */
    public static Group[] get(String[] names) {
        if (names == null) return null;

        Group[] groups = new Group[names.length];

        for(int i = 0; i < names.length; i++) {
            groups[i] = get(names[i]);
        }

        return groups;
    }
}

@@ -1374,16 +1374,129 @@
 nHelper;
+%0Aimport java.util.Comparator;%0Aimport java.util.Enumeration;%0Aimport java.util.SortedSet;%0Aimport java.util.TreeSet;
 %0A%0A/**%0A *
@@ -4500,11 +4500,1285 @@
 s;%0A    %7D
+%0A%0A    /**%0A     * Returns the list of all groups defined on this Integration Server.%0A     * @return The list of all groups defined on this Integration Server.%0A     */%0A    public static Group%5B%5D list() %7B%0A        Enumeration enumeration = UserManager.listGroups(true);%0A        SortedSet%3CGroup%3E set = new TreeSet%3CGroup%3E(new GroupComparator());%0A        while(enumeration.hasMoreElements()) %7B%0A            Object element = enumeration.nextElement();%0A            if (element instanceof String) %7B%0A                Group group = get((String)element);%0A                if (group != null) %7B%0A                    set.add(group);%0A                %7D%0A            %7D%0A        %7D%0A        return set.toArray(new Group%5B0%5D);%0A    %7D%0A%0A    /**%0A     * Compares instance of Group class.%0A     */%0A    private static class GroupComparator implements Comparator%3CGroup%3E %7B%0A        /**%0A         * Compares two instances of the Group class.%0A         *%0A         * @param thisGroup  The first object to be compared.%0A         * @param otherGroup The second object to be compared.%0A         * @return           The result of the comparison.%0A         */%0A        @Override%0A        public int compare(Group thisGroup, Group otherGroup) %7B%0A            return thisGroup.getName().compareTo(otherGroup.getName());%0A        %7D%0A    %7D
 %0A%7D%0A

Add GroupHelper.list() method to return a list of all IS groups