4b369ff362983fb8f0034c3e0ce98bd10c55b39d
[sdc/sdc-workflow-designer.git] /
1 /**
2  * Copyright (c) 2017 ZTE Corporation.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the Apache License, Version 2.0
5  * and the Eclipse Public License v1.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     ZTE - initial API and implementation and/or initial documentation
11  */
12 package org.onap.sdc.workflowdesigner.writer;
13
14 import java.io.StringWriter;
15
16 import org.apache.velocity.Template;
17 import org.apache.velocity.VelocityContext;
18 import org.apache.velocity.app.Velocity;
19 import org.apache.velocity.app.VelocityEngine;
20 import org.apache.velocity.exception.ParseErrorException;
21 import org.apache.velocity.exception.ResourceNotFoundException;
22 import org.onap.sdc.workflowdesigner.config.Config;
23 import org.onap.sdc.workflowdesigner.model.Process;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class BpmnPlanArtefactWriter {
28
29     private Process process;
30
31     private final static String TEMPLATE_PATH = Config.PROPERTIES.getProperty(Config.TEMPLATE_PATH);
32
33     private static Logger log = LoggerFactory.getLogger(BpmnPlanArtefactWriter.class);
34
35     public BpmnPlanArtefactWriter(Process process) throws Exception {
36         this.process = process;
37         Velocity.init();
38     }
39
40     public String completePlanTemplate() throws ResourceNotFoundException, ParseErrorException, Exception {
41         log.debug("Completing BPMN process template...");
42
43         VelocityContext context = new VelocityContext();
44
45         VelocityEngine ve = new VelocityEngine();
46         ve.setProperty("resource.loader", "class");
47         ve.setProperty("class.resource.loader.class",
48                 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
49         Template planTemplate = ve.getTemplate(TEMPLATE_PATH + "bpmn_template.xml");
50
51         context.put("process", process);
52         context.put("templatePath", TEMPLATE_PATH);
53         StringWriter planWriter = new StringWriter();
54         planTemplate.merge(context, planWriter);
55
56         String bpmnProcessContent = planWriter.toString();
57
58         log.debug("Completed BPMN process template" + bpmnProcessContent);
59
60         return bpmnProcessContent;
61
62     }
63
64 }