Package org.eurekastreams.server.action.execution.profile

Source Code of org.eurekastreams.server.action.execution.profile.GetFollowersExecution

/*
* Copyright (c) 2010 Lockheed Martin Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.eurekastreams.server.action.execution.profile;

import org.apache.commons.logging.Log;
import org.eurekastreams.commons.actions.ExecutionStrategy;
import org.eurekastreams.commons.actions.context.PrincipalActionContext;
import org.eurekastreams.commons.logging.LogFactory;
import org.eurekastreams.server.action.request.profile.GetFollowersFollowingRequest;
import org.eurekastreams.server.domain.EntityType;
import org.eurekastreams.server.domain.PagedSet;
import org.eurekastreams.server.domain.Person;
import org.eurekastreams.server.persistence.DomainGroupMapper;
import org.eurekastreams.server.persistence.FollowMapper;
import org.eurekastreams.server.persistence.PersonMapper;

/**
* Action to get the followers of a person or group or the people or groups followed by a person.
*
*/
public class GetFollowersExecution implements ExecutionStrategy<PrincipalActionContext>
{
    /**
     * Logger.
     */
    private Log log = LogFactory.make();

    /**
     * PersonMapper used to retrieve person from the db.
     */
    private PersonMapper personMapper = null;

    /**
     * GroupMapper used to retrieve person from the db.
     */
    private DomainGroupMapper groupMapper = null;

    /**
     * Constructor that sets up the mapper.
     *
     * @param inPersonMapper
     *            - instance of PersonMapper
     * @param inGroupMapper
     *            - instance of DomainGroupMapper
     */
    public GetFollowersExecution(final PersonMapper inPersonMapper, final DomainGroupMapper inGroupMapper)
    {
        personMapper = inPersonMapper;
        groupMapper = inGroupMapper;
    }

    /**
     * Returns true or false if the group exists and the current user is a coordinator.
     *
     * @param inActionContext
     *            The action context.
     * @return true if the group exists and the user is authorized, false otherwise
     */
    @Override
    public PagedSet<Person> execute(final PrincipalActionContext inActionContext)
    {
        // get the request
        GetFollowersFollowingRequest inRequest = (GetFollowersFollowingRequest) inActionContext.getParams();

        // get the unique entity Id
        final String uniqueEntityId = inRequest.getEntityId();

        // get the entity type.
        EntityType targetType = inRequest.getEntityType();

        // get the start value.
        Integer startValue = (inRequest.getStartIndex()).intValue();

        // get the end value.
        Integer endValue = (inRequest.getEndIndex()).intValue();

        // given the entity type, get the follow mapper.
        FollowMapper mapper = pickMapper(targetType);

        PagedSet<Person> connections = mapper.getFollowers(uniqueEntityId, startValue, endValue);

        if (log.isTraceEnabled())
        {
            log.trace("Retrieved " + connections.getFromIndex() + " to " + connections.getToIndex() + " of "
                    + connections.getTotal() + " followers");
        }

        return connections;
    }

    /**
     * Pick a mapper based on the type of the target.
     *
     * @param targetType
     *            the target type
     * @return the selected mapper
     */
    private FollowMapper pickMapper(final EntityType targetType)
    {
        if (EntityType.PERSON == targetType)
        {
            return personMapper;
        }
        if (EntityType.GROUP == targetType)
        {
            return groupMapper;
        }

        throw new IllegalArgumentException("GetFollowersAction supports Person and DomainGroup targets only");
    }
}
TOP

Related Classes of org.eurekastreams.server.action.execution.profile.GetFollowersExecution

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.