/*******************************************************************************
 * (c) Copyright 2017 EntIT Software LLC, a Micro Focus company
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a 
 * copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including without 
 * limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to 
 * whom the Software is furnished to do so, subject to the following 
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY 
 * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE.
 ******************************************************************************/
package com.fortify.bugtracker.src.fod.processor;

import com.fortify.bugtracker.common.src.json.preprocessor.JSONMapEnrichWithVulnState;
import com.fortify.bugtracker.common.src.processor.AbstractSourceVulnerabilityProcessor;
import com.fortify.bugtracker.src.fod.cli.ICLIOptionsFoD;
import com.fortify.bugtracker.src.fod.config.FoDSourceVulnerabilitiesConfiguration;
import com.fortify.bugtracker.src.fod.connection.FoDConnectionFactory;
import com.fortify.bugtracker.src.fod.json.preprocessor.enrich.FoDJSONMapEnrichWithOnDemandBugLinkFromComment;
import com.fortify.client.fod.api.FoDVulnerabilityAPI;
import com.fortify.client.fod.api.query.builder.FoDReleaseVulnerabilitiesQueryBuilder;
import com.fortify.processrunner.cli.CLIOptionDefinition;
import com.fortify.processrunner.cli.CLIOptionDefinitions;
import com.fortify.processrunner.context.Context;
import com.fortify.processrunner.processor.IProcessor;

/**
 * This abstract {@link IProcessor} implementation provides some common functionality used by 
 * {@link FoDSourceProcessorSubmitVulnsToTarget} and {@link FoDSourceProcessorUpdateVulnsOnTarget}, like storing 
 * the {@link FoDSourceVulnerabilitiesConfiguration}, checking the {@link Context}, and providing 
 * FoD connection-related {@link CLIOptionDefinition} instances.
 * 
 * @author Ruud Senden
 *
 */
public abstract class AbstractFoDSourceVulnerabilityProcessor extends AbstractSourceVulnerabilityProcessor<FoDSourceVulnerabilitiesConfiguration> {

	/**
	 * Add FoD connection property definitions
	 */
	@Override
	public final void addCLIOptionDefinitions(CLIOptionDefinitions cliOptionDefinitions) {
		FoDConnectionFactory.addCLIOptionDefinitions(cliOptionDefinitions);
		cliOptionDefinitions.add(ICLIOptionsFoD.CLI_FOD_RELEASE_ID);
		if ( getVulnerabilityProcessor() != null ) {
			getVulnerabilityProcessor().addCLIOptionDefinitions(cliOptionDefinitions);
		}
	}
	
	@Override
	protected IProcessor createProcessorRetrieveAndProcessVulnerabilities(Context context) {
		return new FoDSourceProcessorRetrieveVulnerabilities(getVulnerabilityQuery(context), getVulnerabilityProcessor(), getPurpose());
	}
	
	@Override
	protected String getVulnBugLinkFieldName() {
		return "bugLink";
	}
	
	@Override
	protected String getVulnBugIdFieldName() {
		return "bugId";
	}
	
	protected FoDReleaseVulnerabilitiesQueryBuilder createFoDVulnerabilityBaseQueryBuilder(Context context) {
		FoDReleaseVulnerabilitiesQueryBuilder result = FoDConnectionFactory.getConnection(context).api(FoDVulnerabilityAPI.class)
				.queryVulnerabilities(ICLIOptionsFoD.CLI_FOD_RELEASE_ID.getValue(context))
				.onDemandSummary() // Required to get bug link from comments, for user convenience we always make summary available
				.preProcessor(new JSONMapEnrichWithVulnState(getConfiguration().getIsVulnerabilityOpenExpression()));
		if ( getConfiguration().isAddBugDataAsComment() ) {
			result.preProcessor(new FoDJSONMapEnrichWithOnDemandBugLinkFromComment(getConfiguration().getTargetIssueLocatorCommentHelper(getTargetName())));
		}
		return result;
	}
}

@@ -1761,16 +1761,75 @@
 actory;%0A
+import com.fortify.bugtracker.src.fod.context.IContextFoD;%0A
 import c
@@ -2294,16 +2294,94 @@
 ocessor;
+%0Aimport com.fortify.util.rest.json.preprocessor.enrich.JSONMapEnrichWithValue;
 %0A%0A/**%0A *
@@ -4061,16 +4061,117 @@
 ntext))%0A
+%09%09%09%09.preProcessor(new JSONMapEnrichWithValue(%22release%22, context.as(IContextFoD.class).getRelease()))%0A
 %09%09%09%09.onD

Add release object to every vulnerability for easy access in vulnerability expressions