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

com.celum.dbplugin.resource.VelocityDecoratorResource Maven / Gradle / Ivy

There is a newer version: 1.2
Show newest version
/*****************************************************************************
 * Copyright 2012 celum Slovakia s r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************/
package com.celum.dbplugin.resource;

import com.celum.dbplugin.installer.SqlScript;
import com.celum.dbplugin.installer.SqlScriptsResource;
import com.celum.dbplugin.installer.StringSqlScript;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import java.io.StringWriter;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * This decorator is responsible for replacing template values in SQL scripts
 * by values from plugin configuration, from 'templateContext'.
 *
 * @author Zdenko Vrabel ([email protected])
 */
public class VelocityDecoratorResource extends SqlScriptsResource
{
	//-------------------------------------------------------------------------------------------------------------------
	// members & constants
	//-------------------------------------------------------------------------------------------------------------------

	/** referring to real sql script resource */
	private final SqlScriptsResource decoratedResource;

	/** contains key-value pairs for templates */
	private final Map values;


	//-------------------------------------------------------------------------------------------------------------------
	// methods
	//-------------------------------------------------------------------------------------------------------------------

	/**
	 * Constructor
	 *
	 * @param decoratedResource
	 * @param values
	 */
	public VelocityDecoratorResource(SqlScriptsResource decoratedResource, Map values)
	{
		this.decoratedResource = decoratedResource;
		this.values = values;
	}


	/**
	 * gets scripts from decorated resource and does template evaluation
	 * {@inheritDoc}
	 */
	@Override
	public Collection getSortedScripts()
	{
		try {
			Collection templateScripts = decoratedResource.getSortedScripts();
			Collection evaluatedScripts = doTemplating(templateScripts);
			return evaluatedScripts;
		} catch (Exception e) {
			throw new IllegalStateException(e);
		}
	}


	/**
	 * This is main method which goes thought all template scripts
	 * and evaluate them to final result.
	 *
	 * @param templateScripts
	 * @return
	 * @throws Exception
	 */
	private Collection doTemplating(Collection templateScripts) throws Exception
	{
		//initialize velocity
		VelocityEngine ve = new VelocityEngine();
		ve.init();

		//create context
		VelocityContext vc = new VelocityContext();
		for (Map.Entry e : values.entrySet()) {
			vc.put(e.getKey(), e.getValue());
		}

		//do templating job
		List finalScripts = new LinkedList();
		for (SqlScript template : templateScripts) {
			StringWriter sw = new StringWriter();
			ve.evaluate(vc, sw, template.getId(), template.asText());
			sw.flush();
			finalScripts.add(new StringSqlScript(template.getId(), sw.toString()));
		}

		return finalScripts;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy