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.lang3.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    /**
080     * @since 9.2
081     */
082    public List<WebElement> getTreeNode() {
083        return findElementsWithTimeout(By.xpath(
084                "//div[@id='publishTreeForm:sectionTree'] //div[@class='rf-trn'] //span[@class='tipsyShow tipsyGravityS']"));
085    }
086
087    /**
088     * @since 9.2
089     */
090    public void expandAll() {
091        List<WebElement> expanders = getItemExpanderInPublishTreeForm();
092        if (expanders.isEmpty()) {
093            return;
094        } else {
095            for (WebElement expander : expanders) {
096                Locator.waitUntilEnabledAndClick(expander);
097            }
098        }
099    }
100
101    /**
102     * @since 9.2
103     */
104    public void refreshPublicationTree() {
105        AjaxRequestManager arm = new AjaxRequestManager(driver);
106        arm.begin();
107        findElementWaitUntilEnabledAndClick(By.xpath("//a[contains(@id, 'publishTreeForm:j')]"));
108        arm.end();
109    }
110
111    private boolean matchSectionName(WebElement publishRow, String sectionName) {
112        String sectionNameXpath = String.format("./td/a[contains(text(),'%s')]", sectionName);
113        try {
114            return publishRow.findElement(By.xpath(sectionNameXpath)) != null;
115        } catch (NoSuchElementException e) {
116            return false;
117        }
118    }
119
120    private boolean matchVersion(WebElement publishRow, String version) {
121        String versionXpath = String.format("./td[text()='%s']", version);
122        try {
123            return publishRow.findElement(By.xpath(versionXpath)) != null;
124        } catch (NoSuchElementException e) {
125            return false;
126        }
127    }
128
129    private int findTreeNodeIndex(String itemName) {
130        List<WebElement> elements = getTreeNode();
131        int index = 0;
132        for (WebElement sub : elements) {
133            if (sub.getText().equals(itemName)) {
134                return index - 1; // skip index for "Sections"
135            }
136            index++;
137        }
138        List<WebElement> expanders = getItemExpanderInPublishTreeForm();
139        if (expanders.size() == 0) {
140            return -1;
141        } else {
142            for (WebElement expander : expanders) {
143                Locator.waitUntilEnabledAndClick(expander);
144            }
145            return findTreeNodeIndex(itemName);
146        }
147    }
148
149    private List<WebElement> getItemExpanderInPublishTreeForm() {
150        return findElementsWithTimeout(
151                By.xpath("//div[@id='publishTreeForm:sectionTree'] //span[@class='rf-trn-hnd-colps rf-trn-hnd']"));
152    }
153
154    private void publishIemInPublishTreeForm(int index) {
155        AjaxRequestManager arm = new AjaxRequestManager(driver);
156        arm.begin();
157        findElementWaitUntilEnabledAndClick(
158                By.xpath("//div[@id='publishTreeForm:sectionTree'] //a[contains(@id, 'publishRecursiveAdaptor." + index
159                        + ":publishCommandLink')]"));
160        arm.end();
161    }
162}