Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to create the a new base Java backend class to render the page for site users. #61

Open
computate opened this issue Oct 19, 2021 · 0 comments

Comments

@computate
Copy link
Member

Mission to create the a new base Java backend class to render the page for site users.

Mission

Your mission, should you choose to accept it, is to create the a new base Java backend class to render the page for site users. .

The day of the operation is October 26th, 2021

You will have 4 hours to complete your mission.

An open science project to uncover the planetary virome, freely and openly

You will be joining a team of the boldest

of all Red Hatters for this mission, who will be at your service developing the code and automating the deployment to OpenShift.

  • Should you, or any of your force be taken or compromised that day, you must report back as many changes as you have made, and any knowledge of your actions, by pull request or comments on the issue on the board.

How to get started:

Follow the instructions here to setup the project: https://github.com/team19hackathon2021/serratus-api/

The details of your mission:

  • The Java package name will be: io.serratus.api.enus.user
  • The Java class name will be: SiteUserGenPage
  • The path to the Java class will be: /usr/local/src/serratus-api/src/main/java/io/serratus/api/enus/user/SiteUserGenPage.java
  • The Java class will extend: SiteUserGenPageGen<BaseModelPage>
package io.serratus.api.enus.user;

import java.util.List;
import java.lang.Long;
import java.lang.String;
import io.serratus.api.enus.base.BaseModelPage;
import io.serratus.api.enus.request.SiteRequestEnUS;
import io.serratus.api.enus.user.SiteUser;
import java.io.IOException;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.serratus.api.enus.search.SearchList;
import io.serratus.api.enus.wrap.Wrap;
import io.serratus.api.enus.page.PageLayout;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.api.service.ServiceRequest;
import io.vertx.core.json.JsonArray;
import java.net.URLDecoder;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import org.apache.solr.common.util.SimpleOrderedMap;
import java.util.stream.Collectors;
import java.util.Arrays;
import org.apache.solr.client.solrj.response.QueryResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.math.MathContext;
import org.apache.commons.collections.CollectionUtils;
import java.util.Objects;
import org.apache.solr.client.solrj.SolrQuery.SortClause;
import io.vertx.core.Promise;
import io.serratus.api.enus.config.ConfigKeys;

/**
 **/

public class SiteUserGenPage extends SiteUserGenPageGen<BaseModelPage> {
}

Create the init method for the searchListSiteUser_ field inside the SiteUserGenPage class

Create an initialization method that starts with an underscore ()searchListSiteUser to define the searchListSiteUser_ field

Here is a suggestion on what the code might look like:

	protected void _searchListSiteUser_(Wrap<SearchList<SiteUser>> w) {
	}

Create the init method for the listSiteUser field inside the SiteUserGenPage class

Create an initialization method that starts with an underscore (_)listSiteUser to define the listSiteUser field

Here is a suggestion on what the code might look like:

protected void _listSiteUser(JsonArray l) {
		Optional.ofNullable(searchListSiteUser_).map(o -> o.getList()).orElse(Arrays.asList()).stream().map(o -> JsonObject.mapFrom(o)).forEach(o -> l.add(o));
	}

Create the init method for the siteUserCount field inside the SiteUserGenPage class

Create an initialization method that starts with an underscore (_)siteUserCount to define the siteUserCount field

Here is a suggestion on what the code might look like:

	protected void _siteUserCount(Wrap<Integer> w) {
		w.o(searchListSiteUser_ == null ? 0 : searchListSiteUser_.size());
	}

Create the init method for the siteUser_ field inside the SiteUserGenPage class

Create an initialization method that starts with an underscore ()siteUser to define the siteUser_ field

Here is a suggestion on what the code might look like:

	protected void _siteUser_(Wrap<SiteUser> w) {
		if(siteUserCount == 1)
			w.o(searchListSiteUser_.get(0));
	}

Create the init method for the pk field inside the SiteUserGenPage class

Create an initialization method that starts with an underscore (_)pk to define the pk field

Here is a suggestion on what the code might look like:

	protected void _pk(Wrap<Long> w) {
		if(siteUserCount == 1)
			w.o(siteUser_.getPk());
	}

Create the _promiseBefore method inside the SiteUserGenPage class

	protected void _promiseBefore(Promise<Void> promise) {
		promise.complete();
	}

Create the _classSimpleName method inside the SiteUserGenPage class

	protected void _classSimpleName(Wrap<String> w) {
		w.o("SiteUser");
	}

Create the _pageTitle method inside the SiteUserGenPage class

	protected void _pageTitle(Wrap<String> c) {
		if(siteUser_ != null && siteUser_.getObjectTitle() != null)
			c.o(siteUser_.getObjectTitle());
		else if(siteUser_ != null)
			c.o("site users");
		else if(searchListSiteUser_ == null || siteUserCount == 0)
			c.o("no site user found");
		else
			c.o("site users");
	}

Create the _pageUri method inside the SiteUserGenPage class

	protected void _pageUri(Wrap<String> c) {
		c.o("/api/user");
	}

Create the _roles method inside the SiteUserGenPage class

	protected void _roles(List<String> l) {
		if(siteRequest_ != null) {
			l.addAll(Stream.concat(siteRequest_.getUserResourceRoles().stream(), siteRequest_.getUserRealmRoles().stream()).distinct().collect(Collectors.toList()));
		}
	}

Create the _rolesRequired method inside the SiteUserGenPage class

	protected void _rolesRequired(List<String> l) {
		l.addAll(Optional.ofNullable(siteRequest_.getConfig().getJsonArray(ConfigKeys.AUTH_ROLES_REQUIRED + "_SiteUser")).orElse(new JsonArray()).stream().map(o -> o.toString()).collect(Collectors.toList()));
	}

Create the _pagination method inside the SiteUserGenPage class

	protected void _pagination(JsonObject pagination) {
		JsonArray pages = new JsonArray();
		Long start = searchListSiteUser_.getStart().longValue();
		Long rows = searchListSiteUser_.getRows().longValue();
		Long foundNum = searchListSiteUser_.getQueryResponse().getResults().getNumFound();
		Long startNum = start + 1L;
		Long endNum = start + rows;
		Long floorMod = Math.floorMod(foundNum, rows);
		Long last = Math.floorDiv(foundNum, rows) - (floorMod.equals(0L) ? 1L : 0L) * rows;
		endNum = endNum < foundNum ? endNum : foundNum;
		startNum = foundNum == 0L ? 0L : startNum;
		Long paginationStart = start - 10L * rows;
		if(paginationStart < 0L)
			paginationStart = 0L;
		Long paginationEnd = start + 10L * rows;
		if(paginationEnd > foundNum)
			paginationEnd = foundNum;

		pagination.put("1L", 1L);
		pagination.put("0L", 0L);
		pagination.put("start", start);
		pagination.put("rows", rows);
		pagination.put("rowsPrevious", rows / 2);
		pagination.put("rowsNext", rows * 2);
		pagination.put("startNum", startNum);
		pagination.put("endNum", endNum);
		pagination.put("foundNum", foundNum);
		pagination.put("pageStart", new JsonObject().put("start", 0L).put("pageNumber", 1L));
		if(start > 0L)
			pagination.put("pagePrevious", new JsonObject().put("start", start - rows).put("pageNumber", start - rows + 1L));
		if(start + rows < foundNum)
			pagination.put("pageNext", new JsonObject().put("start", start + rows).put("pageNumber", start + rows + 1L));
		pagination.put("pageEnd", new JsonObject().put("start", last).put("pageNumber", last + 1L));
		pagination.put("pages", pages);
		for(Long i = paginationStart; i < paginationEnd; i += rows) {
			Long pageNumber = Math.floorDiv(i, rows) + 1L;
			JsonObject page = new JsonObject();
			page.put("pageNumber", pageNumber);
			page.put("currentPage", start.equals(i));
			page.put("start", i);
			pages.add(page);
		}
	}

Create the _query method inside the SiteUserGenPage class

	protected void _query(JsonObject query) {
		ServiceRequest serviceRequest = siteRequest_.getServiceRequest();
		JsonObject params = serviceRequest.getParams();

		JsonObject queryParams = Optional.ofNullable(serviceRequest).map(ServiceRequest::getParams).map(or -> or.getJsonObject("query")).orElse(new JsonObject());
		Long num = searchListSiteUser_.getQueryResponse().getResults().getNumFound();
		String q = "*:*";
		String q1 = "objectText";
		String q2 = "";
		for(String paramName : queryParams.fieldNames()) {
			String entityVar = null;
			String valueIndexed = null;
			Object paramObjectValues = queryParams.getValue(paramName);
			JsonArray paramObjects = paramObjectValues instanceof JsonArray ? (JsonArray)paramObjectValues : new JsonArray().add(paramObjectValues);

			try {
				for(Object paramObject : paramObjects) {
					switch(paramName) {
					case "q":
						q = (String)paramObject;
						entityVar = StringUtils.trim(StringUtils.substringBefore((String)paramObject, ":"));
						valueIndexed = URLDecoder.decode(StringUtils.trim(StringUtils.substringAfter((String)paramObject, ":")), "UTF-8");
						q1 = entityVar.equals("*") ? q1 : entityVar;
						q2 = valueIndexed;
						q = q1 + ":" + q2;
					}
				}
			} catch(Exception e) {
				ExceptionUtils.rethrow(e);
			}
		}
		query.put("q", q);

		Integer rows1 = Optional.ofNullable(searchListSiteUser_).map(l -> l.getRows()).orElse(10);
		Integer start1 = Optional.ofNullable(searchListSiteUser_).map(l -> l.getStart()).orElse(1);
		Integer start2 = start1 - rows1;
		Integer start3 = start1 + rows1;
		Integer rows2 = rows1 / 2;
		Integer rows3 = rows1 * 2;
		start2 = start2 < 0 ? 0 : start2;
		JsonArray fqs = new JsonArray();
		for(String fq : Optional.ofNullable(searchListSiteUser_).map(l -> l.getFilterQueries()).orElse(new String[0])) {
			if(!StringUtils.contains(fq, "(")) {
				String fq1 = StringUtils.substringBefore(fq, "_");
				String fq2 = StringUtils.substringAfter(fq, ":");
				if(!StringUtils.startsWithAny(fq, "classCanonicalNames_", "archived_", "deleted_", "sessionId", "userKeys"))
					fqs.add(new JsonObject().put("var", fq1).put("val", fq2));
				}
			}
		query.put("fq", fqs);

		JsonArray sorts = new JsonArray();
		for(SortClause sort : Optional.ofNullable(searchListSiteUser_).map(l -> l.getSorts()).orElse(Arrays.asList())) {
			sorts.add(new JsonObject().put("var", StringUtils.substringBefore(sort.getItem(), "_")).put("order", sort.getOrder().name()));
		}
		query.put("sort", sorts);
	}

Create the _promiseAfter method inside the SiteUserGenPage class

	protected void _promiseAfter(Promise<Void> promise) {
		promise.complete();
	}

Create the _pageImageUri method inside the SiteUserGenPage class

	protected void _pageImageUri(Wrap<String> c) {
			c.o("/png/api/user-999.png");
	}

Create the _contextIconGroup method inside the SiteUserGenPage class

	protected void _contextIconGroup(Wrap<String> c) {
			c.o("regular");
	}

Create the _contextIconName method inside the SiteUserGenPage class

	protected void _contextIconName(Wrap<String> c) {
			c.o("user-cog");
	}

This message will not self-destruct.

This message will not self destruct, because this project is open source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant