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

step.core.plans.PlanPlugin Maven / Gradle / Ivy

/*******************************************************************************
 * 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.core.plans;

import org.bson.types.ObjectId;
import step.core.GlobalContext;
import step.core.artefacts.AbstractArtefact;
import step.core.collections.Collection;
import step.core.collections.Filters;
import step.core.entities.EntityManager;
import step.core.export.ExportContext;
import step.core.plans.builder.PlanBuilder;
import step.core.plugins.AbstractControllerPlugin;
import step.core.plugins.Plugin;
import step.framework.server.tables.Table;
import step.framework.server.tables.TableRegistry;
import step.plugins.screentemplating.*;
import step.plugins.table.settings.*;

import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;

@Plugin(dependencies= {ScreenTemplatePlugin.class, TableSettingsPlugin.class})
public class PlanPlugin extends AbstractControllerPlugin {

	@Override
	public void serverStart(GlobalContext context) throws Exception {
		PlanTypeRegistry planTypeRegistry = new PlanTypeRegistry();
		planTypeRegistry.register(new PlanType<>() {

			@Override
			public Class getPlanClass() {
				return Plan.class;
			}

			@Override
			public PlanCompiler getPlanCompiler() {
				return plan -> plan;
			}

			@Override
			public Plan newPlan(String template) throws Exception {
				AbstractArtefact artefact = context.getArtefactHandlerRegistry().getArtefactTypeInstance(template);
				return PlanBuilder.create().startBlock(artefact).endBlock().build();
			}

			@Override
			public Plan clonePlan(Plan plan, boolean updateVisibility) {
				plan.setId(new ObjectId());
				if (updateVisibility) {
					plan.setCustomFields(null);
					plan.setVisible(true);
					if (plan.getRoot() != null) {
						// delete all custom attributes for all children to clean up attributes like "source" cloned from original plan
						plan.getRoot().deepCleanupAllCustomAttributes();
					}
				}
				return plan;
			}

			@Override
			public void onBeforeSave(Plan plan) {

			}
		});
		context.put(PlanTypeRegistry.class, planTypeRegistry);
		context.getServiceRegistrationCallback().registerService(PlanServices.class);
		Collection collection = context.getCollectionFactory().getCollection(EntityManager.plans, Plan.class);
		context.get(TableRegistry.class).register(EntityManager.plans, new Table<>(collection, "plan-read", true)
				.withTableFiltersFactory(e-> Filters.equals("visible", true)));

		context.getEntityManager().registerExportHook(new ModifyPlanAtExportHook());
		context.getEntityManager().registerExportAllFilters(EntityManager.plans, Filters.equals("visible", true));
	}

	@Override
	public void initializeData(GlobalContext context) throws Exception {
		createScreenInputAndTableDefinitionsIfNecessary(context);
	}

	protected void createScreenInputAndTableDefinitionsIfNecessary(GlobalContext context) {
		// Init Plan model and Table settings
		ScreenInputAccessor screenInputAccessor = context.get(ScreenInputAccessor.class);
		List screenInputsByScreenId = screenInputAccessor.getScreenInputsByScreenId(ScreenTemplatePlugin.PLAN_SCREEN_ID);
		// Search name input
		Optional nameInputOrig = screenInputsByScreenId.stream().filter(i -> i.getInput().getId().equals("attributes.name")).findFirst();
		// Create it if not existing
		if(nameInputOrig.isEmpty()) {
			Input nameInput = new Input(InputType.TEXT, "attributes.name", "Name", null, null);
			nameInput.setCustomUIComponents(List.of("planLink"));
			ScreenInput savedInput = screenInputAccessor.save(new ScreenInput(0, ScreenTemplatePlugin.PLAN_SCREEN_ID, nameInput, true));
			createTableSettingsIfNecessary(context, savedInput);
		} else {
			createTableSettingsIfNecessary(context, nameInputOrig.get());
		}
	}

	private void createTableSettingsIfNecessary(GlobalContext context, ScreenInput nameInput) {
		TableSettingsAccessor tableSettingsAccessor = context.get(TableSettingsAccessor.class);
		if (tableSettingsAccessor.findSystemTableSettings(EntityManager.plans).isEmpty()) {
			TableSettings setting = TableSettingsBuilder.builder().withSettingId(EntityManager.plans)
					.addColumn("bulkSelection", true)
					.addColumn("attributes.project", true)
					.addColumn("entityLock", true)
					.addColumn("attributes.name", true, nameInput)
					.addColumn("type", true)
					.addColumn("automationPackage", true)
					.addColumn("actions", true)
					.build();
			tableSettingsAccessor.save(setting);
		}
	}

	public static class ModifyPlanAtExportHook implements BiConsumer {

		@Override
		public void accept(Object object_, ExportContext exportContext) {
			if (object_ instanceof Plan) {
				Plan plan = (Plan) object_;
				//change visibility of plans
				if (!plan.isVisible()) {
					plan.setVisible(true);
				}
			}
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy