com.epam.jdi.light.elements.init.entities.collection.EntitiesCollection Maven / Gradle / Ivy
package com.epam.jdi.light.elements.init.entities.collection;
import com.epam.jdi.light.elements.common.UIElement;
import com.epam.jdi.light.elements.composite.WebPage;
import com.epam.jdi.light.elements.interfaces.base.IBaseElement;
import com.epam.jdi.light.elements.interfaces.base.ICoreElement;
import com.epam.jdi.tools.LinqUtils;
import com.epam.jdi.tools.map.MapArray;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static com.epam.jdi.light.common.Exceptions.exception;
import static com.epam.jdi.light.elements.composite.WebPage.getCurrentPage;
import static com.epam.jdi.light.elements.init.UIFactory.$;
import static com.epam.jdi.tools.JsonUtils.getMapFromJson;
import static com.epam.jdi.tools.JsonUtils.scanFolder;
import static com.epam.jdi.tools.LinqUtils.first;
import static com.epam.jdi.tools.PrintUtils.print;
import static com.epam.jdi.tools.PropertyReader.getProperty;
import static com.epam.jdi.tools.ReflectionUtils.isClass;
import static com.epam.jdi.tools.ReflectionUtils.isInterface;
/**
* Created by Roman Iovlev on 26.09.2019
* Email: [email protected]; Skype: roman.iovlev
*/
public class EntitiesCollection {
public static MapArray PAGES = new MapArray<>();
public static MapArray> ELEMENTS = new MapArray<>();
static MapArray jsonPages;
static MapArray jsonElements;
private EntitiesCollection() { }
public static void addPage(WebPage page) {
PAGES.update(page.getName(), page);
}
public static void updatePage(WebPage page) {
String className = page.getClass().getSimpleName();
if (PAGES.keys().contains(className))
PAGES.removeByKey(className);
PAGES.update(page.getName(), page);
}
static void readPagesFromJson() {
jsonPages = getMapFromJson("pages", "json.page.objects");
}
public static WebPage getPage(String pageName) {
WebPage page = PAGES.get(pageName);
if (page == null)
page = PAGES.get(pageName + " Page");
if (page == null) {
if (jsonPages == null)
readPagesFromJson();
if (jsonPages == null) {
throw exception("Can't find page with name %s. Available pages: %s", pageName,
print(PAGES.keys()));
} else page = new WebPage(jsonPages.get(pageName));
}
return page;
}
public static void addElement(Object jdi) {
if (isInterface(jdi.getClass(), ICoreElement.class)) {
ICoreElement element = (ICoreElement) jdi;
if (ELEMENTS.has(element.getName()))
ELEMENTS.get(element.getName()).add(jdi);
else {
List