package org.apache.maven.continuum.web.view;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import com.opensymphony.webwork.views.util.UrlHelper;
import com.opensymphony.xwork.ActionContext;
import org.apache.maven.continuum.web.model.ProjectSummary;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.security.authorization.AuthorizationException;
import org.codehaus.plexus.security.system.SecuritySession;
import org.codehaus.plexus.security.system.SecuritySystem;
import org.codehaus.plexus.security.system.SecuritySystemConstants;
import org.codehaus.plexus.xwork.PlexusLifecycleListener;
import org.extremecomponents.table.bean.Column;
import org.extremecomponents.table.cell.DisplayCell;
import org.extremecomponents.table.core.TableModel;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
import java.util.HashMap;

/**
 * Used in Summary view
 *
 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
 * @version $Id$
 * @deprecated use of cells is discouraged due to lack of i18n and design in java code.
 *             Use jsp:include instead.
 */
public class BuildCell
    extends DisplayCell
{
    protected String getCellValue( TableModel tableModel, Column column )
    {
        ProjectSummary project = (ProjectSummary) tableModel.getCurrentRowBean();
        String contextPath = tableModel.getContext().getContextPath();

        int buildNumber = project.getBuildNumber();

        String result = "<div align=\"center\">";

        if ( project.isInBuildingQueue() )
        {
            result +=
                "<img src=\"" + contextPath + "/images/inqueue.gif\" alt=\"In Queue\" title=\"In Queue\" border=\"0\">";
        }
        else if ( project.isInCheckoutQueue() )
        {
            result += "<img src=\"" + contextPath +
                "/images/checkingout.gif\" alt=\"Checking Out sources\" title=\"Checking Out sources\" border=\"0\">";
        }
        else
        {
            if ( project.getState() == 1 || project.getState() == 10 || project.getState() == 2 ||
                project.getState() == 3 || project.getState() == 4 )
            {
                if ( project.getBuildNumber() > 0 )
                {
                    HashMap params = new HashMap();

                    params.put( "projectId", new Integer( project.getId() ) );

                    params.put( "projectName", project.getName() );

                    params.put( "buildId", new Integer( buildNumber ) );

                    params.put( "projectGroupId", new Integer( project.getProjectGroupId() ) );

                    PageContext pageContext = (PageContext) tableModel.getContext().getContextObject();

                    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();

                    HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();

                    String url = UrlHelper.buildUrl( "/buildResult.action", request, response, params );

                    if ( isAuthorized( project ) )
                    {
                        // we are authzd so act normally
                        result += "<a href=\"" + url + "\">" + project.getBuildNumber() + "</a>";
                    }
                    else
                    {
                        result += project.getBuildNumber();
                    }
                }
                else
                {
                    result += "&nbsp;";
                }
            }
            else if ( project.getState() == 6 )
            {
                result += "<img src=\"" + contextPath +
                    "/images/building.gif\" alt=\"Building\" title=\"Building\" border=\"0\">";
            }
            else if ( project.getState() == 7 )
            {
                result += "<img src=\"" + contextPath +
                    "/images/checkingout.gif\" alt=\"Checking Out sources\" title=\"Checking Out sources\" border=\"0\">";
            }
            else if ( project.getState() == 8 )
            {
                result += "<img src=\"" + contextPath +
                    "/images/checkingout.gif\" alt=\"Updating sources\" title=\"Updating sources\" border=\"0\">";
            }
            else
            {
                result += "<img src=\"" + contextPath +
                    "/images/inqueue.gif\" alt=\"In Queue\" title=\"In Queue\" border=\"0\">";
            }
        }

        return result + "</div>";
    }

    private boolean isAuthorized( ProjectSummary project )
    {
        // do the authz bit
        ActionContext context = ActionContext.getContext();

        PlexusContainer container = (PlexusContainer) context.getApplication().get( PlexusLifecycleListener.KEY );
        SecuritySession securitySession =
            (SecuritySession) context.getSession().get( SecuritySystemConstants.SECURITY_SESSION_KEY );

        try
        {
            SecuritySystem securitySystem = (SecuritySystem) container.lookup( SecuritySystem.ROLE );

            if ( !securitySystem.isAuthorized( securitySession, "continuum-build-group",
                                               project.getProjectGroupName() ) )
            {
                return false;
            }
        }
        catch ( ComponentLookupException cle )
        {
            return false;
        }
        catch ( AuthorizationException ae )
        {
            return false;
        }

        return true;
    }
}

@@ -3073,28 +3073,17 @@
    if ( 
-project.getB
+b
 uildNumb
@@ -3084,18 +3084,16 @@
 ldNumber
-()
  %3E 0 )%0A 
@@ -3367,27 +3367,45 @@
 nteger( 
-buildNumber
+project.getBuildInSuccessId()
  ) );%0A%0A 
@@ -4102,28 +4102,17 @@
 %22%5C%22%3E%22 + 
-project.getB
+b
 uildNumb
@@ -4113,18 +4113,16 @@
 ldNumber
-()
  + %22%3C/a%3E
@@ -4227,28 +4227,17 @@
 sult += 
-project.getB
+b
 uildNumb
@@ -4238,18 +4238,16 @@
 ldNumber
-()
 ;%0A      

Fix buildNumber link