/**
 * Copyright 2012 Houghton Associates
 * 
 * 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 com.houghtonassociates.bamboo.plugins.view;

import java.util.List;

import org.apache.log4j.Logger;

import com.atlassian.bamboo.plan.PlanHelper;
import com.atlassian.bamboo.repository.Repository;
import com.atlassian.bamboo.resultsummary.vcs.RepositoryChangeset;
import com.atlassian.bamboo.ww2.actions.chains.ViewChainResult;
import com.atlassian.bamboo.ww2.aware.permissions.PlanReadSecurityAware;
import com.houghtonassociates.bamboo.plugins.GerritRepositoryAdapter;
import com.houghtonassociates.bamboo.plugins.dao.GerritChangeVO;
import com.houghtonassociates.bamboo.plugins.dao.GerritService;

/**
 * @author Jason Huntley
 * 
 */
public class ViewGerritChainResultsAction extends ViewChainResult implements
    PlanReadSecurityAware {

    private static final long serialVersionUID = 1L;
    private GerritChangeVO changeVO = null;
    private GerritService gerritService = null;
    private GerritRepositoryAdapter repository = null;
    private static final Logger log = Logger
        .getLogger(ViewGerritChainResultsAction.class);

    public ViewGerritChainResultsAction() {
        super();

        changeVO = new GerritChangeVO();
    }

    public GerritRepositoryAdapter getRepository() {
        if (repository == null) {
            Repository repo = PlanHelper.getDefaultRepository(this.getPlan());

            if (repo instanceof GerritRepositoryAdapter) {
                repository = (GerritRepositoryAdapter) repo;
            }
        }

        return repository;
    }

    public GerritService getGerritService() {
        if (gerritService == null) {
            Repository repo = PlanHelper.getDefaultRepository(this.getPlan());

            if (repo instanceof GerritRepositoryAdapter) {
                GerritRepositoryAdapter gra = getRepository();
                gerritService =
                    new GerritService(gra.getHostname(), gra.getPort(),
                        gra.getGerritAuthentication());
            }
        }

        return gerritService;
    }

    @Override
    public String doExecute() throws Exception {
        GerritChangeVO c =
            getGerritService().getChangeByRevision(this.getRevision());

        if (c == null) {
            log.error(this.getTextProvider().getText(
                "repository.gerrit.messages.error.retrieve"));

            changeVO = new GerritChangeVO();
        } else {
            changeVO = c;
        }
        return super.doExecute();
    }

    public String getHTTPHost() {
        return getRepository().getHostname();
    }

    public GerritChangeVO getChange() {
        return changeVO;
    }

    public String getChangeID() {
        return changeVO.getId();
    }

    public String getRevision() {
        List<RepositoryChangeset> changeset =
            this.getResultsSummary().getRepositoryChangesets();
        RepositoryChangeset rcs = changeset.get(0);
        String revision = rcs.getChangesetId();
        return revision;
    }
}

@@ -1649,16 +1649,143 @@
 .class);
+%0A    private static final String GERRIT_REPOSITORY_PLUGIN_KEY =%0A        %22com.houghtonassociates.bamboo.plugins.gReview:gerrit%22;
 %0A%0A    pu
@@ -2808,24 +2808,181 @@
 n %7B%0A        
+final String revision = this.getRevision();%0A%0A        if (revision == null) %7B%0A            changeVO = new GerritChangeVO();%0A        %7D else %7B%0A            final 
 GerritChange
@@ -2985,19 +2985,28 @@
 angeVO c
- =%0A
+hange =%0A    
         
@@ -3048,34 +3048,24 @@
 evision(
-this.getR
+r
 evision
-()
 );%0A%0A    
@@ -3072,13 +3072,22 @@
     
+    
 if (c
+hange
  == 
@@ -3086,32 +3086,36 @@
 ange == null) %7B%0A
+    
             log.
@@ -3168,16 +3168,20 @@
         
+ 
+   
 %22reposit
@@ -3215,25 +3215,27 @@
 etrieve%22));%0A
-%0A
+   
             
@@ -3226,32 +3226,33 @@
 %0A               
+ 
 changeVO = new G
@@ -3268,32 +3268,36 @@
 geVO();%0A        
+    
 %7D else %7B%0A       
@@ -3293,32 +3293,36 @@
 e %7B%0A            
+  
+  
 changeVO = c;%0A  
@@ -3317,17 +3317,36 @@
 geVO = c
-;
+hange;%0A            %7D
 %0A       
@@ -3664,16 +3664,22 @@
         
+final 
 List%3CRep
@@ -3705,16 +3705,17 @@
 hangeset
+s
  =%0A     
@@ -3773,32 +3773,37 @@
 sets();%0A        
+for (
 RepositoryChange
@@ -3810,13 +3810,19 @@
 set 
-rcs =
+changeset :
  cha
@@ -3831,16 +3831,12 @@
 eset
-.get(0);
+s) %7B
 %0A   
@@ -3844,29 +3844,148 @@
     
-String revision = rcs
+    if (changeset.getRepositoryData().getPluginKey()%0A                .equals(GERRIT_REPOSITORY_PLUGIN_KEY))%0A                return changeset
 .get
@@ -4011,23 +4011,29 @@
     
-return revision
+%7D%0A        return null
 ;%0A  

Fix Gerrit Chain Results screen to show only Gerrit changes.