com.centurylink.mdw.hub.context.Page Maven / Gradle / Ivy
package com.centurylink.mdw.hub.context;
import com.centurylink.mdw.model.asset.api.AssetInfo;
import com.centurylink.mdw.model.workflow.PackageMeta;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Represents a custom UI page to be loaded.
*/
public class Page {
private final Mdw mdw;
public Mdw getMdw() { return mdw; }
private final String path;
public String getPath() { return path; }
public String getRelPath() {
return path.substring(1);
}
private AssetInfo asset;
public AssetInfo getAsset() {
if (asset == null) {
File file = new File(mdw.getAssetRoot() + "/" + path);
asset = new AssetInfo(file.getName(), file, 0, "0");
}
return asset;
}
/**
* Accessed in react default index.html substitution.
*/
@SuppressWarnings("unused")
public String getAssetPath() {
String relPath = getRelPath();
return relPath.substring(0, relPath.length() - getAsset().getName().length() - 1).replace('/', '.')
+ "/" + getAsset().getName();
}
private String template;
public String getTemplate() { return template; }
public void setTemplate(String template) { this.template = template; }
private AssetInfo templateAsset;
public AssetInfo getTemplateAsset() {
if (templateAsset == null) {
File file = new File(mdw.getAssetRoot() + "/" + template);
templateAsset = new AssetInfo(file.getName(), file, 0, "0");
}
return templateAsset;
}
public boolean exists() {
return getAsset().getFile().exists();
}
public File getFile() {
return new File(mdw.getAssetRoot() + "/" + path);
}
public String getExt() {
return getAsset().getExtension();
}
/**
* Embedded means inside MDWHub tabs/frame.
*/
private boolean embedded;
public boolean isEmbedded() { return embedded; }
public void setEmbedded(boolean embedded) { this.embedded = embedded; }
/**
* path begins with /
*/
public Page(Mdw mdw, String path) {
this.mdw = mdw;
this.path = path;
}
/**
* Locate closest page in ancestor package hierarchy (if any).
*/
public Page findAncestor(String name) {
Path rootPath = Paths.get(mdw.getAssetRoot().getPath()).normalize();
Path pkgPath = Paths.get(getAsset().getFile().getPath()).normalize();
while (pkgPath != null && pkgPath.startsWith(rootPath)) {
if (new File(pkgPath + "/" + PackageMeta.PACKAGE_YAML_PATH).exists() ) {
Page p = new Page(mdw, "/" + rootPath.relativize(pkgPath).toString().replace('\\', '/') + "/" + name);
if (p.exists())
return p;
}
pkgPath = pkgPath.getParent();
}
return null;
}
}