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

step.plugins.screentemplating.ScreenTemplateService Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (C) 2020, exense GmbH
 *  
 * This file is part of STEP
 *  
 * STEP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *  
 * STEP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *  
 * You should have received a copy of the GNU Affero General Public License
 * along with STEP.  If not, see .
 ******************************************************************************/
package step.plugins.screentemplating;

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

import jakarta.annotation.PostConstruct;
import jakarta.inject.Singleton;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.UriInfo;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.bson.types.ObjectId;

import step.core.GlobalContext;
import step.core.deployment.ControllerServiceException;
import step.framework.server.access.AuthorizationManager;
import step.core.access.Role;
import step.core.access.User;
import step.core.accessors.AbstractOrganizableObject;
import step.core.deployment.AbstractStepServices;
import step.framework.server.security.Secured;
import step.framework.server.Session;
import step.core.objectenricher.ObjectPredicate;
import step.core.objectenricher.ObjectPredicateFactory;

@Singleton
@Path("screens")
@Tag(name = "Screens")
public class ScreenTemplateService extends AbstractStepServices {
	
	protected AuthorizationManager authorizationManager;
	protected ScreenTemplateManager screenTemplateManager;
	protected ScreenInputAccessor screenInputAccessor;
	protected ObjectPredicateFactory objectPredicateFactory;
	private final Set screens = new HashSet<>() {
		{
			add(ScreenTemplatePlugin.FUNCTION_SCREEN_ID);
			add(ScreenTemplatePlugin.EXECUTION_PARAMETERS);
			add(ScreenTemplatePlugin.PLAN_SCREEN_ID);
		}
	};

	@PostConstruct
	public void init() throws Exception {
		super.init();
		GlobalContext context = getContext();
		authorizationManager = context.get(AuthorizationManager.class);
		screenInputAccessor = context.get(ScreenInputAccessor.class);
		screenTemplateManager = context.get(ScreenTemplateManager.class);
		objectPredicateFactory = context.get(ObjectPredicateFactory.class);
	}
	
	@GET
	@Secured
	@Produces(MediaType.APPLICATION_JSON)
	public Set getScreens() {
		return screens;
	}
	
	@GET
	@Secured
	@Path("/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	public List getInputsForScreenGet(@PathParam("id") String screenId, @Context UriInfo uriInfo) {
		Map contextBindings = getContextBindings(uriInfo);
		ObjectPredicate objectPredicate = objectPredicateFactory.getObjectPredicate(getSession());
		return screenTemplateManager.getInputsForScreen(screenId, contextBindings, objectPredicate);
	}
	
	@SuppressWarnings("unchecked")
	@POST
	@Secured
	@Path("/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public List getInputsForScreenPost(@PathParam("id") String screenId, Object params) {
		ObjectPredicate objectPredicate = objectPredicateFactory.getObjectPredicate(getSession());
		Map contextBindings = getContextBindings(null);
		if(params instanceof Map) {
			contextBindings.putAll((Map) params);
		}
		return screenTemplateManager.getInputsForScreen(screenId, contextBindings, objectPredicate);
	}

	@GET
	@Secured
	@Path("/{id}/screen-inputs")
	@Produces(MediaType.APPLICATION_JSON)
	public List getScreenInputsForScreenGet(@PathParam("id") String screenId, @Context UriInfo uriInfo) {
		Map contextBindings = getContextBindings(uriInfo);
		ObjectPredicate objectPredicate = objectPredicateFactory.getObjectPredicate(getSession());
		return screenTemplateManager.getScreenInputsForScreen(screenId, contextBindings, objectPredicate);
	}

	@SuppressWarnings("unchecked")
	@POST
	@Secured
	@Path("/{id}/screen-inputs")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public List getScreenInputsForScreenPost(@PathParam("id") String screenId, Object params) {
		ObjectPredicate objectPredicate = objectPredicateFactory.getObjectPredicate(getSession());
		Map contextBindings = getContextBindings(null);
		if(params instanceof Map) {
			contextBindings.putAll((Map) params);
		}
		return screenTemplateManager.getScreenInputsForScreen(screenId, contextBindings, objectPredicate);
	}
	
	@GET
	@Secured
	@Path("/{screenid}/{inputid}")
	@Produces(MediaType.APPLICATION_JSON)
	public Input getInputForScreen(@PathParam("screenid") String screenId, @PathParam("inputid") String inputId, @Context UriInfo uriInfo) {		
		return getInputsForScreenGet(screenId, uriInfo).stream().filter(i->i.getId().equals(inputId)).findFirst().orElse(null);
	}
	
	@GET
	@Secured
	@Path("/input/byscreen/{screenid}")
	@Produces(MediaType.APPLICATION_JSON)
	public List getScreenInputsByScreenId(@PathParam("screenid") String screenId) {		
		return screenInputAccessor.getScreenInputsByScreenId(screenId);
	}
	
	@GET
	@Secured
	@Path("/input/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	public ScreenInput getInput(@PathParam("id") String id) {
		return screenInputAccessor.get(new ObjectId(id));
	}
	
	@POST
	@Secured(right="screenInputs-write")
	@Path("/input/{id}/move")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public void moveInput(@PathParam("id") String id, int offset) {
		screenTemplateManager.moveInput(id, offset);
	}
	
	@DELETE
	@Secured(right="screenInputs-delete")
	@Path("/input/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	public void deleteInput(@PathParam("id") String id) {
		ScreenInput screenInput = screenInputAccessor.get(id);
		if (screenInput.getImmutable()) {
			throw new ControllerServiceException("This screen input is immutable.");
		}
		screenInputAccessor.remove(new ObjectId(id));
		screenTemplateManager.notifyChange();
	}
	
	@POST
	@Secured(right="screenInputs-write")
	@Path("/input")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public void saveInput(ScreenInput screenInput) {
		ScreenInput screenInputOrig = screenInputAccessor.get(screenInput.getId());
		if (screenInputOrig != null && screenInputOrig.getImmutable()) {
			throw new ControllerServiceException("This screen input is immutable.");
		}
		screenInputAccessor.save(screenInput);
		screenTemplateManager.notifyChange();
	}

	private Map getContextBindings(UriInfo uriInfo) {
		Map contextBindings = new HashMap<>();

		Session session = getSession();
		if(session!=null) {
			contextBindings.put("user", session.getUser().getUsername());
			Role roleInContext = authorizationManager.getRoleInContext(session);
			if(roleInContext!= null) {
				String roleName = roleInContext.getAttributes().get(AbstractOrganizableObject.NAME);
				contextBindings.put("role", roleName);
			}
		}
		
		if(uriInfo != null) {
			for(String key:uriInfo.getQueryParameters().keySet()) {
				contextBindings.put(key, uriInfo.getQueryParameters().getFirst(key));
			}
		}
		return contextBindings;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy