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

io.spotnext.sample.endpoints.HomePageEndpoint Maven / Gradle / Ivy

package io.spotnext.sample.endpoints;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.spotnext.cms.rendering.transformers.ThymeleafRendererResponseTransformer;
import io.spotnext.core.infrastructure.http.ModelAndView;
import io.spotnext.core.infrastructure.service.ModelService;
import io.spotnext.core.infrastructure.service.UserService;
import io.spotnext.core.infrastructure.support.MimeType;
import io.spotnext.core.management.annotation.Handler;
import io.spotnext.core.management.annotation.RemoteEndpoint;
import io.spotnext.core.persistence.query.JpqlQuery;
import io.spotnext.core.persistence.query.QueryResult;
import io.spotnext.core.persistence.service.QueryService;
import io.spotnext.core.security.service.AuthenticationService;
import io.spotnext.itemtype.core.user.User;
import io.spotnext.itemtype.core.user.UserGroup;
import io.spotnext.sample.filters.IsAdminFilter;
import io.spotnext.sample.types.itemtypes.Party;
import spark.Request;
import spark.Response;
import spark.route.HttpMethod;

@RemoteEndpoint(pathMapping = "/")
public class HomePageEndpoint {

	@Resource
	private ModelService modelService;

	@Resource
	private QueryService queryService;

	@Resource
	private AuthenticationService authenticationService;

	@Resource
	private UserService userService;

	@Handler(responseTransformer = ThymeleafRendererResponseTransformer.class, mimeType = MimeType.HTML)
	public ModelAndView getHomepage(final Request request, final Response response) {
		final Map model = new HashMap<>();

		model.put("pageTitle", "Party service sample page");
		model.put("parties", getAllParties());

		return ModelAndView.ok("homepage").withPayload(model);
	}

	@Handler(responseTransformer = ThymeleafRendererResponseTransformer.class, pathMapping = "/login", mimeType = MimeType.HTML, method = HttpMethod.post)
	public ModelAndView postLogin(final Request request, final Response response) {
		String username = request.queryParams("username");
		String password = request.queryParams("password");

		if (username != null && password != null) {
			User user = authenticationService.getAuthenticatedUser(username, password, false);

			if (user != null) {
				userService.setCurrentUser(user);
				response.redirect("/manage");
				return null;
			}
		}

		response.redirect("/");
		return null;
	}

	@Handler(responseTransformer = ThymeleafRendererResponseTransformer.class, pathMapping = "/logout", mimeType = MimeType.HTML, method = HttpMethod.post)
	public ModelAndView postLogout(final Request request, final Response response) {
		userService.setCurrentUser(null);
		response.redirect("/");
		return null;
	}

	@Handler(responseTransformer = ThymeleafRendererResponseTransformer.class, pathMapping = "/manage", mimeType = MimeType.HTML, authenticationFilter = IsAdminFilter.class)
	public ModelAndView getManage(final Request request, final Response response) {
		final Map model = new HashMap<>();
		model.put("pageTitle", "Party service sample page");
		model.put("parties", getAllParties());

		model.put("isLoggedIn", true);

		return ModelAndView.ok("homepage").withPayload(model);
	}

	@SuppressFBWarnings("DM_BOXED_PRIMITIVE_FOR_PARSING")
	@Handler(responseTransformer = ThymeleafRendererResponseTransformer.class, pathMapping = "/cancel", mimeType = MimeType.HTML, method = HttpMethod.post, authenticationFilter = IsAdminFilter.class)
	public ModelAndView postCancelParty(final Request request, final Response response) {
		String partyPk = request.queryParams("partyPk");

		if (partyPk != null) {
			modelService.remove(Party.class, Long.valueOf(partyPk));
		}

		response.redirect("/manage");
		return null;
	}

	protected List getAllParties() {
		String query = "SELECT p FROM Party p";
		JpqlQuery partyQuery = new JpqlQuery<>(query, Party.class);
		QueryResult result = queryService.query(partyQuery);

		return result.getResultList();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy