001/*
002 * (C) Copyright 2016 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 *     Gabriel Barata
018 *     Yannis JULIENNE
019 */
020package org.nuxeo.functionaltests.pages.tabs;
021
022import java.util.List;
023
024import org.apache.commons.lang.StringUtils;
025import org.nuxeo.functionaltests.AjaxRequestManager;
026import org.nuxeo.functionaltests.pages.AbstractPage;
027import org.openqa.selenium.By;
028import org.openqa.selenium.NoSuchElementException;
029import org.openqa.selenium.WebDriver;
030import org.openqa.selenium.WebElement;
031import org.openqa.selenium.support.FindBy;
032
033/**
034 * @since 8.3
035 */
036public class PublishTabSubPage extends AbstractPage {
037
038    @FindBy(xpath = "//select[@id='publishTreeForm:publishSelectTreeName']")
039    public WebElement selectTree;
040
041    @FindBy(xpath = "//select[contains(@id, 'publishTreeForm:j')]")
042    public WebElement selectRendition;
043
044    @FindBy(xpath = "//div[@id='publishTreeForm:publishingInfoList']/table/tbody/tr")
045    List<WebElement> publishingInfos;
046
047    public PublishTabSubPage(WebDriver driver) {
048        super(driver);
049    }
050
051    public PublishTabSubPage publish(String sectionTree, String rendtion, String sectionName) {
052        selectItemInDropDownMenu(selectTree, sectionTree);
053        selectItemInDropDownMenu(selectRendition, rendtion);
054
055        int index = findTreeNodeIndex(sectionName);
056        publishIemInPublishTreeForm(index);
057
058        return asPage(PublishTabSubPage.class);
059    }
060
061    public List<WebElement> getPublishingInfos() {
062        return publishingInfos;
063    }
064
065    public PublishTabSubPage unpublish(String sectionName, String version) {
066
067        for (WebElement publishRow : getPublishingInfos()) {
068            if ((StringUtils.isBlank(sectionName) || matchSectionName(publishRow, sectionName))
069                    && (StringUtils.isBlank(version) || matchVersion(publishRow, version))) {
070                publishRow.findElement(By.linkText("Unpublish")).click();
071                return asPage(PublishTabSubPage.class);
072            }
073        }
074
075        return this;
076    }
077
078    private boolean matchSectionName(WebElement publishRow, String sectionName) {
079        String sectionNameXpath = String.format("./td/a[contains(text(),'%s')]", sectionName);
080        try {
081            return publishRow.findElement(By.xpath(sectionNameXpath)) != null;
082        } catch (NoSuchElementException e) {
083            return false;
084        }
085    }
086
087    private boolean matchVersion(WebElement publishRow, String version) {
088        String versionXpath = String.format("./td[text()='%s']", version);
089        try {
090            return publishRow.findElement(By.xpath(versionXpath)) != null;
091        } catch (NoSuchElementException e) {
092            return false;
093        }
094    }
095
096    private int findTreeNodeIndex(String itemName) {
097        List<WebElement> elements = getTreeNode();
098        int index = 0;
099        for (WebElement sub : elements) {
100            if (sub.getText().equals(itemName)) {
101                return index - 1; // skip index for "Sections"
102            }
103            index++;
104        }
105        List<WebElement> expanders = getItemExpanderInPublishTreeForm();
106        if (expanders.size() == 0) {
107            return -1;
108        } else {
109            for (WebElement expander : expanders) {
110                expander.click();
111            }
112            return findTreeNodeIndex(itemName);
113        }
114    }
115
116    private List<WebElement> getTreeNode() {
117        return findElementsWithTimeout(By.xpath(
118                "//div[@id='publishTreeForm:sectionTree'] //div[@class='rf-trn'] //span[@class='tipsyShow tipsyGravityS']"));
119    }
120
121    private List<WebElement> getItemExpanderInPublishTreeForm() {
122        return findElementsWithTimeout(
123                By.xpath("//div[@id='publishTreeForm:sectionTree'] //span[@class='rf-trn-hnd-colps rf-trn-hnd']"));
124    }
125
126    private void publishIemInPublishTreeForm(int index) {
127        AjaxRequestManager arm = new AjaxRequestManager(driver);
128        arm.begin();
129        findElementWaitUntilEnabledAndClick(
130                By.xpath("//div[@id='publishTreeForm:sectionTree'] //a[contains(@id, 'publishRecursiveAdaptor." + index
131                        + ":publishCommandLink')]"));
132        arm.end();
133    }
134}