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