summaryrefslogtreecommitdiffstats
path: root/deprecated-workflow-designer/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/writer/BpmnPlanArtefactWriter.java
blob: 4b369ff362983fb8f0034c3e0ce98bd10c55b39d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
 * Copyright (c) 2017 ZTE Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the Apache License, Version 2.0
 * and the Eclipse Public License v1.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     ZTE - initial API and implementation and/or initial documentation
 */
package org.onap.sdc.workflowdesigner.writer;

import java.io.StringWriter;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.onap.sdc.workflowdesigner.config.Config;
import org.onap.sdc.workflowdesigner.model.Process;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BpmnPlanArtefactWriter {

    private Process process;

    private final static String TEMPLATE_PATH = Config.PROPERTIES.getProperty(Config.TEMPLATE_PATH);

    private static Logger log = LoggerFactory.getLogger(BpmnPlanArtefactWriter.class);

    public BpmnPlanArtefactWriter(Process process) throws Exception {
        this.process = process;
        Velocity.init();
    }

    public String completePlanTemplate() throws ResourceNotFoundException, ParseErrorException, Exception {
        log.debug("Completing BPMN process template...");

        VelocityContext context = new VelocityContext();

        VelocityEngine ve = new VelocityEngine();
        ve.setProperty("resource.loader", "class");
        ve.setProperty("class.resource.loader.class",
                "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Template planTemplate = ve.getTemplate(TEMPLATE_PATH + "bpmn_template.xml");

        context.put("process", process);
        context.put("templatePath", TEMPLATE_PATH);
        StringWriter planWriter = new StringWriter();
        planTemplate.merge(context, planWriter);

        String bpmnProcessContent = planWriter.toString();

        log.debug("Completed BPMN process template" + bpmnProcessContent);

        return bpmnProcessContent;

    }

}