All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.computate.smartvillage.enus.page.PageLayout Maven / Gradle / Ivy

Go to download

Red Hat Global Social Innovation Program is partnering with Boston University and Smarta Byar in order to collaborate on creating a global and open research platform allowing researchers to study what social sustainability means by using a digital twin of Veberöd, Sweden as the test village, supported by Smarta Byar. The goal of this collaboration is to build the open source technological infrastructure so that researchers can collaborate on this platform effectively to study mobility, public health, sustainability among other study areas to ultimately help define better the link between well being and eco-smart cities.

There is a newer version: 2.0.2
Show newest version
/*
 * Copyright (c) 2018-2022 Computate Limited Liability Company in Utah, USA,
 * and the Smarta Byar Smart Village FIWARE IHub. 
 *
 * This program and the accompanying materials are made available under the
 * terms of the GNU GENERAL PUBLIC LICENSE Version 3 which is available at
 * 
 * https://www.gnu.org/licenses/gpl-3.0.en.html
 * 
 * You may not propagate or modify a covered work except as expressly provided 
 * under this License. Any attempt otherwise to propagate or modify it is void, 
 * and will automatically terminate your rights under this License (including 
 * any patent licenses granted under the third paragraph of section 11).
 */
package org.computate.smartvillage.enus.page;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.computate.search.response.solr.SolrResponse;
import org.computate.search.tool.SearchTool;
import org.computate.search.wrap.Wrap;
import org.computate.smartvillage.enus.config.ConfigKeys;
import org.computate.smartvillage.enus.page.PageLayoutGen;
import org.computate.smartvillage.enus.request.SiteRequestEnUS;
import org.computate.vertx.config.ComputateConfigKeys;

import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.api.service.ServiceRequest;


/**
 * Keyword: classSimpleNamePageLayout
 * Description: Defines variables to be used when rendering Handlebars template pages
 * Promise: true
 **/
public class PageLayout extends PageLayoutGen {

	public static DateTimeFormatter FORMATDateTimeShort = DateTimeFormatter.ofPattern("MMM d yyyy h:mm a", Locale.US);

	public static DateTimeFormatter FORMATDateShort = DateTimeFormatter.ofPattern("EEE MMM d yyyy", Locale.US);

	public static DateTimeFormatter FORMATMonthYear = DateTimeFormatter.ofPattern("MMM yyyy", Locale.US);

	public static DateTimeFormatter FORMATDateDisplay = DateTimeFormatter.ofPattern("EEEE MMMM d yyyy", Locale.US);

	public static DateTimeFormatter FORMATDateTimeDisplay = DateTimeFormatter.ofPattern("EEEE MMMM d yyyy h:mm a:ss.SSS", Locale.US);

	public static DateTimeFormatter FORMATZonedDateTimeDisplay = DateTimeFormatter.ofPattern("EEEE MMMM d yyyy H:mm:ss.SSS zz VV", Locale.US);

	public static DateTimeFormatter FORMATTimeDisplay = DateTimeFormatter.ofPattern("h:mm a", Locale.US);

	/**
	 * Ignore: true
	 * Description: The current request object
	**/
	protected void _siteRequest_(Wrap w) {
	}

	/**
	 * Description: The current request language
	 */
	protected void _lang(Wrap w) {
		w.o(siteRequest_.getLang());
	}

	/**
	 * Description: The current request vars
	 */
	protected void _requestVars(Wrap> w) {
		w.o(siteRequest_.getRequestVars());
	}

	/**
	 * Description: The current Vert.x service request
	 */
	protected void _serviceRequest(Wrap w) {
		w.o(siteRequest_.getServiceRequest());
	}

	protected void _staticBaseUrl(Wrap w) {
		w.o(siteRequest_.getConfig().getString(ConfigKeys.STATIC_BASE_URL));
	}

	protected void _siteBaseUrl(Wrap w) {
		w.o(siteRequest_.getConfig().getString(ConfigKeys.SITE_BASE_URL));
	}

	protected void _siteAuthUrl(Wrap w) {
		w.o(siteRequest_.getConfig().getString(ConfigKeys.AUTH_URL));
	}

	protected void _siteAuthRealm(Wrap w) {
		w.o(siteRequest_.getConfig().getString(ConfigKeys.AUTH_REALM));
	}

	protected void _fontAwesomeKit(Wrap w) {
		w.o(siteRequest_.getConfig().getString(ConfigKeys.FONTAWESOME_KIT));
	}

	protected void _facebookGraphVersion(Wrap w) {
		w.o(siteRequest_.getConfig().getString(ConfigKeys.FACEBOOK_GRAPH_VERSION));
	}

	protected void _facebookAppId(Wrap w) {
		w.o(siteRequest_.getConfig().getString(ConfigKeys.FACEBOOK_APP_ID));
	}

	/**
	 * Description: The current request URI
	 */
	protected void _pageUri(Wrap w) {
		w.o(Optional.ofNullable(serviceRequest).map(r -> r.getExtra()).map(e -> e.getString("uri")).orElse(null));
	}

	/**
	 * Description: The current page name
	 */
	protected void _pageId(Wrap w) {
		if(pageUri != null)
			w.o(StringUtils.substringAfterLast(pageUri, "/"));
	}

	/**
	 * Description: The API request URI
	 */
	protected void _apiUri(Wrap w) {
	}

	/**
	 * Description: The current request HTTP method
	 */
	protected void _pageMethod(Wrap w) {
		w.o(Optional.ofNullable(serviceRequest).map(r -> r.getExtra()).map(e -> e.getString("method")).orElse(null));
	}

	/**
	 * Description: The current request parameters
	 */
	protected void _params(Wrap w) {
		w.o(Optional.ofNullable(serviceRequest).map(r -> r.getParams()).orElse(new JsonObject()));
	}

	/**
	 * Description: The current user's primary key
	 */
	protected void _userKey(Wrap w) {
		w.o(siteRequest_.getUserKey());
	}

	/**
	 * Description: The current user's full name
	 */
	protected void _userFullName(Wrap w) {
		w.o(siteRequest_.getUserFullName());
	}

	/**
	 * Description: The current user's username
	 */
	protected void _userName(Wrap w) {
		w.o(siteRequest_.getUserName());
	}

	/**
	 * Description: The current user's email
	 */
	protected void _userEmail(Wrap w) {
		w.o(siteRequest_.getUserEmail());
	}

	/**
	 * Description: The logout URL
	 */
	protected void _logoutUrl(Wrap w) {
		JsonObject config = siteRequest_.getConfig();
		try {
			w.o(config.getString(ConfigKeys.AUTH_URL) + "/realms/" + config.getString(ConfigKeys.AUTH_REALM) + "/protocol/openid-connect/logout?redirect_uri=" + URLEncoder.encode(config.getString(ConfigKeys.SITE_BASE_URL) + "/logout", "UTF-8"));
		} catch (UnsupportedEncodingException ex) {
			ExceptionUtils.rethrow(ex);
		}
	}

	/**
	 * Description: A helper field for the long number 0
	 */
	protected void _long0(Wrap w) {
		w.o(0L);
	}

	/**
	 * Description: A helper field for the long number 1
	 */
	protected void _long1(Wrap w) {
		w.o(1L);
	}

	/**
	 * Description: A helper field for the integer number 0
	 */
	protected void _int0(Wrap w) {
		w.o(0);
	}

	/**
	 * Description: A helper field for the integer number 1
	 */
	protected void _int1(Wrap w) {
		w.o(1);
	}

	/**
	 * Ignore: true
	 * Description: A method that can be overridden at the start of the request that makes this main template be initialized with a Vert.x promise for reactive initialization
	**/
	protected void _promiseBefore(Promise promise) {
		promise.complete();
	}

	/**
	 * Description: The simple name of this Java class
	 */
	protected void _classSimpleName(Wrap w) {
	}

	/**
	 * Description: The page title to override
	 */
	protected void _pageTitle(Wrap w) {
	}

	/**
	 * Description: The user's roles
	 */
	protected void _roles(List l) {
		if(siteRequest_ != null) {
			l.addAll(Stream.concat(siteRequest_.getUserResourceRoles().stream(), siteRequest_.getUserResourceRoles().stream()).collect(Collectors.toList()));
		}
	}

	/**
	 * Description: The required roles to access this page
	 */
	protected void _roleForWrite(List l) {
		l.add(siteRequest_.getConfig().getString(ConfigKeys.AUTH_ROLE_ADMIN));
	}

	/**
	 * Description: The required roles to access this page
	 */
	protected void _roleForRead(List l) {
	}

	/**
	 * Description: The admin roles required to access this page
	 */
	protected void _authRoleAdmin(List l) {
		l.add(siteRequest_.getConfig().getString(ConfigKeys.AUTH_ROLE_ADMIN));
	}

	/**
	 * Description: The admin roles required to access this page
	 */
	protected void _authRoleSuperAdmin(List l) {
		l.add(siteRequest_.getConfig().getString(ConfigKeys.AUTH_ROLE_SUPER_ADMIN));
	}

	protected void _stats(Wrap w) {
	}

	protected void _facetCounts(Wrap w) {
	}

	/**
	 * Description: The pagination data about this request
	 */
	protected void _pagination(JsonObject pagination) {
	}

	protected void _defaultFieldListVars(List l) {
	}

	protected void _defaultStatsVars(List l) {
	}

	protected void _defaultPivotVars(List l) {
	}

	protected void _varsQ(JsonObject vars) {
	}

	protected void _varsFq(JsonObject vars) {
	}

	protected void _varsRange(JsonObject vars) {
	}

	/**
	 * Description: The query data about this request
	 */
	protected void _query(JsonObject query) {
	}

	protected void _pageResponse(Wrap w) {
	}

	protected void _defaultZoneId(Wrap w) {
		w.o(Optional.ofNullable(siteRequest_.getRequestVars().get(VAR_defaultZoneId)).orElse(siteRequest_.getConfig().getString(ComputateConfigKeys.SITE_ZONE)));
	}

	/**
	 * Ignore: true
	 **/
	protected void _defaultTimeZone(Wrap w) {
		w.o(ZoneId.of(defaultZoneId));
	}

	protected void _defaultLocaleId(Wrap w) {
		w.o(Optional.ofNullable(siteRequest_.getRequestHeaders().get("Accept-Language")).map(acceptLanguage -> StringUtils.substringBefore(acceptLanguage, ",")).orElse(siteRequest_.getConfig().getString(ComputateConfigKeys.SITE_LOCALE)));
	}

	protected void _rows(Wrap w) {
	}

	protected void _start(Wrap w) {
	}

	/**
	 * Ignore: true
	 **/
	protected void _defaultLocale(Wrap w) {
		w.o(Locale.forLanguageTag(defaultLocaleId));
	}

	protected void _rangeGap(Wrap w) {
	}

	protected void _rangeEnd(Wrap w) {
	}

	protected void _rangeStart(Wrap w) {
	}

	protected void _defaultRangeStats(Wrap w) {
	}

	protected void _defaultRangeGap(Wrap w) {
	}

	protected void _defaultRangeEnd(Wrap w) {
	}

	protected void _defaultRangeStart(Wrap w) {
	}

	protected void _defaultRangeVar(Wrap w) {
	}

	protected void _defaultFacetSort(Wrap w) {
	}

	protected void _defaultFacetLimit(Wrap w) {
	}

	protected void _defaultFacetMinCount(Wrap w) {
	}

	protected void _defaultPivotMinCount(Wrap w) {
	}

	protected void _DEFAULT_MAP_LOCATION(Wrap w) {
	}

	protected void _DEFAULT_MAP_ZOOM(Wrap w) {
	}

	/**
	 * Description: The query String for this request
	 */
	protected void _queryStr(Wrap w) {
		ServiceRequest serviceRequest = siteRequest_.getServiceRequest();
		JsonObject params = serviceRequest.getParams();
		List paramList = new ArrayList<>();

		JsonObject queryParams = Optional.ofNullable(serviceRequest).map(ServiceRequest::getParams).map(or -> or.getJsonObject("query")).orElse(new JsonObject());
		for(String paramName : queryParams.fieldNames()) {
			Object paramObjectValues = queryParams.getValue(paramName);
			JsonArray paramObjects = paramObjectValues instanceof JsonArray ? (JsonArray)paramObjectValues : new JsonArray().add(paramObjectValues);

			try {
				for(Object paramObject : paramObjects) {
					if(paramObject != null)
						paramList.add(String.format("%s=%s", paramName, SearchTool.urlEncode(paramObject.toString())));
				}
			} catch(Exception e) {
				ExceptionUtils.rethrow(e);
			}
		}
		if(params.size() == 0)
			w.o("");
		else
			w.o(String.format("?%s", StringUtils.join(paramList, "&")));
	}

	/**
	 * Ignore: true
	 * Description: A method that can be overridden at the end of the request that makes this main template be initialized with a Vert.x promise for reactive initialization
	**/
	protected void _promiseAfter(Promise promise) {
		promise.complete();
	}

	/**
	 * Description: The image URI for this page
	 */
	protected void _pageImageUri(Wrap w) {
	}

	/**
	 * Description: The image width
	 */
	protected void _pageImageWidth(Wrap c) {
	}

	/**
	 * Description: The image height
	 */
	protected void _pageImageHeight(Wrap c) {
	}

	/**
	 * Description: The video ID for this page
	 */
	protected void _pageVideoId(Wrap c) {
	}

	/**
	 * Description: The icon group for this page
	 */
	protected void _classIconGroup(Wrap w) {
	}

	/**
	 * Description: The icon name for this page
	 */
	protected void _classIconName(Wrap w) {
	}

	/**
	 * Description: The icon CSS classes for this page
	 */
	protected void _classIconCssClasses(Wrap w) {
		if(classIconGroup != null && classIconName != null) {
			w.o(String.format("fa-%s fa-%s ", classIconGroup, classIconName));
		}
	}

	/**
	 * Description: An optional description field for the page
	 */
	protected void _pageDescription(Wrap w) {
	}
}