001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     ataillefer
018 */
019package org.nuxeo.ecm.platform.task.core.helpers;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import org.nuxeo.ecm.core.api.NuxeoGroup;
025import org.nuxeo.ecm.core.api.NuxeoPrincipal;
026
027/**
028 * Helper class for dealing with task actors.
029 * <p>
030 * Provides a method to build the task actors list.
031 * 
032 * @author ataillefer
033 */
034public final class TaskActorsHelper {
035
036    /**
037     * Prevent instantiation.
038     */
039    private TaskActorsHelper() {
040        // Final class
041    }
042
043    /**
044     * Gets the task actors list: prefixed and unprefixed names of the principal and all its groups.
045     * 
046     * @param principal the principal
047     * @return the actors and group
048     */
049    public static List<String> getTaskActors(NuxeoPrincipal principal) {
050
051        List<String> actors = new ArrayList<String>();
052
053        String name = principal.getName();
054        actors.add(name);
055        if (!name.startsWith(NuxeoPrincipal.PREFIX)) {
056            actors.add(NuxeoPrincipal.PREFIX + name);
057        } else {
058            actors.add(name.substring(NuxeoPrincipal.PREFIX.length()));
059        }
060        for (String group : principal.getAllGroups()) {
061            actors.add(group);
062            if (!group.startsWith(NuxeoGroup.PREFIX)) {
063                actors.add(NuxeoGroup.PREFIX + group);
064            } else {
065                actors.add(group.substring(NuxeoGroup.PREFIX.length()));
066            }
067        }
068
069        return actors;
070    }
071}