001/*
002 * (C) Copyright 2014 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 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
018 */
019package org.nuxeo.functionaltests.pages.search;
020
021import java.util.ArrayList;
022import java.util.List;
023import java.util.Map;
024
025import org.nuxeo.functionaltests.AjaxRequestManager;
026import org.nuxeo.functionaltests.Locator;
027import org.nuxeo.functionaltests.Required;
028import org.nuxeo.functionaltests.forms.Select2WidgetElement;
029import org.nuxeo.functionaltests.pages.search.aggregates.CheckBoxAggregateElements;
030import org.nuxeo.functionaltests.pages.search.aggregates.Select2AggregateElement;
031import org.openqa.selenium.By;
032import org.openqa.selenium.NoSuchElementException;
033import org.openqa.selenium.WebDriver;
034import org.openqa.selenium.WebElement;
035import org.openqa.selenium.support.FindBy;
036
037import com.google.common.base.Function;
038
039/**
040 * @since 6.0
041 */
042public class DefaultSearchSubPage extends AbstractSearchSubPage {
043
044    private static final String S2_COLLECTION_XPATH = "//*[@id='s2id_nxl_gridSearchLayout:nxw_searchLayout_form:nxl_default_search_layout:nxw_visible_collection_select2']";
045
046    public static final String TREE_PATH_ID = "nxl_gridSearchLayout:nxw_searchLayout_form:nxl_default_search_layout:nxw_ecm_path_treeId";
047
048    @FindBy(id = "s2id_nxl_gridSearchLayout:nxw_searchLayout_form:nxl_default_search_layout:nxw_dc_creator_agg")
049    protected WebElement authorAggregate;
050
051    @FindBy(id = "nxl_gridSearchLayout:nxw_searchLayout_form:nxl_default_search_layout:nxw_dc_coverage_agg")
052    protected WebElement coverageAggregate;
053
054    @FindBy(id = "nxl_gridSearchLayout:nxw_searchLayout_form:nxl_default_search_layout:nxw_dc_created_agg")
055    protected WebElement createdAggregate;
056
057    @FindBy(id = "nxl_gridSearchLayout:nxw_searchLayout_form:nxl_default_search_layout:nxw_dc_modified_agg")
058    protected WebElement modifiedAggregate;
059
060    @FindBy(id = "nxl_gridSearchLayout:nxw_searchLayout_form:nxl_default_search_layout:nxw_common_size_agg")
061    protected WebElement sizeAggregate;
062
063    @FindBy(id = "nxl_gridSearchLayout:nxw_searchLayout_form:nxl_default_search_layout:nxw_dc_subjects_agg")
064    protected WebElement subjectsAggregate;
065
066    @FindBy(id = "nxw_ecm_path_openPopup")
067    @Required
068    protected WebElement openPathPopupButton;
069
070    @FindBy(id = "nxw_ecm_path")
071    @Required
072    protected WebElement selectPathDiv;
073
074    public static final String PATH_REGEX = "(.*) \\((.*)\\)";
075
076    protected static final String EXPAND_XPATH = "ancestor::div[@class='rf-trn']/span[contains(@class,'rf-trn-hnd')]";
077
078    public DefaultSearchSubPage(WebDriver driver) {
079        super(driver);
080    }
081
082    public Map<String, Integer> getAvailableCoverageAggregate() {
083        return new CheckBoxAggregateElements(coverageAggregate).getAggregates();
084    }
085
086    public Map<String, Integer> getAvailableCreatedAggregate() {
087        return new CheckBoxAggregateElements(createdAggregate).getAggregates();
088    }
089
090    public Map<String, Integer> getAvailableModifiedAggregate() {
091        return new CheckBoxAggregateElements(modifiedAggregate).getAggregates();
092    }
093
094    public Map<String, Integer> getAvailableSizeAggregate() {
095        return new CheckBoxAggregateElements(sizeAggregate).getAggregates();
096    }
097
098    public Map<String, Integer> getAvailableSubjectsAggregate() {
099        return new CheckBoxAggregateElements(subjectsAggregate).getAggregates();
100    }
101
102    public WebElement getCoverageAggregate() {
103        return coverageAggregate;
104    }
105
106    public WebElement getCreatedAggregate() {
107        return createdAggregate;
108    }
109
110    public WebElement getModifiedAggregate() {
111        return modifiedAggregate;
112    }
113
114    public WebElement getSizeAggregate() {
115        return sizeAggregate;
116    }
117
118    public WebElement getSubjectsAggregate() {
119        return subjectsAggregate;
120    }
121
122    /**
123     * @since 7.4
124     */
125    public Map<String, Integer> getAvailableAuthorAggregate() {
126        Select2AggregateElement s2AuthorAggregate =  new Select2AggregateElement(driver, authorAggregate, true);
127        return s2AuthorAggregate.getAggregates();
128    }
129
130    public SearchPage selectCoverageAggregate(String label) {
131        new CheckBoxAggregateElements(driver, coverageAggregate).selectOrUnselect(label);
132        return asPage(SearchPage.class);
133    }
134
135    public SearchPage selectCreatedAggregate(String label) {
136        new CheckBoxAggregateElements(driver, createdAggregate).selectOrUnselect(label);
137        return asPage(SearchPage.class);
138    }
139
140    public SearchPage selectModifiedAggregate(String label) {
141        new CheckBoxAggregateElements(driver, modifiedAggregate).selectOrUnselect(label);
142        return asPage(SearchPage.class);
143    }
144
145    public SearchPage selectSizeAggregate(String label) {
146        new CheckBoxAggregateElements(driver, sizeAggregate).selectOrUnselect(label);
147        return asPage(SearchPage.class);
148    }
149
150    public SearchPage selectSubjectsAggregate(String label) {
151        new CheckBoxAggregateElements(driver, subjectsAggregate).selectOrUnselect(label);
152        return asPage(SearchPage.class);
153    }
154
155    public void selectPath(String path) {
156        assert (path != null && !path.isEmpty() && path.charAt(0) == '/');
157        openPathPopupButton.click();
158        Locator.waitUntilGivenFunction(new Function<WebDriver, Boolean>() {
159            @Override
160            public Boolean apply(WebDriver driver) {
161                try {
162                    WebElement tree = driver.findElement(By.id(TREE_PATH_ID));
163                    return tree.isDisplayed();
164                } catch (NoSuchElementException e) {
165                    return false;
166                }
167            }
168        });
169        if (path.length() == 1) {
170            AjaxRequestManager a = new AjaxRequestManager(driver);
171            a.watchAjaxRequests();
172            driver.findElement(By.id(TREE_PATH_ID)).findElement(By.linkText("/")).click();
173            a.waitForAjaxRequests();
174            return;
175        } else {
176            AjaxRequestManager a = new AjaxRequestManager(driver);
177            a.watchAjaxRequests();
178            driver.findElement(By.id(TREE_PATH_ID)).findElement(By.linkText("/")).findElement(By.xpath(EXPAND_XPATH)).click();
179            a.waitForAjaxRequests();
180        }
181        String[] pathArray = path.substring(1).split("/");
182        int i = 0;
183        for (; i < pathArray.length - 1; i++) {
184            AjaxRequestManager a = new AjaxRequestManager(driver);
185            a.watchAjaxRequests();
186            driver.findElement(By.id(TREE_PATH_ID)).findElement(By.linkText(pathArray[i])).findElement(
187                    By.xpath(EXPAND_XPATH)).click();
188            a.waitForAjaxRequests();
189        }
190        AjaxRequestManager a = new AjaxRequestManager(driver);
191        a.watchAjaxRequests();
192        driver.findElement(By.id(TREE_PATH_ID)).findElement(By.linkText(pathArray[i])).click();
193        a.waitForAjaxRequests();
194        driver.findElement(By.id("fancybox-close")).click();
195        Locator.waitUntilGivenFunction(new Function<WebDriver, Boolean>() {
196            @Override
197            public Boolean apply(WebDriver driver) {
198                try {
199                    WebElement tree = driver.findElement(By.id(TREE_PATH_ID));
200                    return !tree.isDisplayed();
201                } catch (NoSuchElementException e) {
202                    return false;
203                }
204            }
205        });
206    }
207
208    public void deselectPath(String path) {
209        assert (path != null && !path.isEmpty());
210        int lastPartIndex = path.lastIndexOf('/');
211        String folderName = path.substring(lastPartIndex + 1);
212        WebElement e = selectPathDiv.findElement(By.xpath("descendant::label[contains(text(),'" + folderName
213                + "')]/ancestor::span[@class='sticker']/a"));
214        AjaxRequestManager a = new AjaxRequestManager(driver);
215        a.watchAjaxRequests();
216        e.click();
217        a.waitForAjaxRequests();
218    }
219
220    /**
221     * @since 7.3
222     */
223    public void selectCollections(final String[] collections) {
224        Select2WidgetElement collectionsWidget = new Select2WidgetElement(
225                driver,
226                driver.findElement(By.xpath(S2_COLLECTION_XPATH)),
227                true);
228        collectionsWidget.selectValues(collections);
229    }
230
231    /**
232     * @since 7.3
233     */
234    public List<String> getSelectedCollections() {
235        Select2WidgetElement collectionsWidget = new Select2WidgetElement(
236                driver,
237                driver.findElement(By.xpath(S2_COLLECTION_XPATH)),
238                true);
239        List<String> result = new ArrayList<String>();
240        for (WebElement el : collectionsWidget.getSelectedValues()) {
241            result.add(el.getText());
242        }
243        return result;
244    }
245}