Add unit test for vfc-nfvo-wfengine
[vfc/nfvo/wfengine.git] / activiti-extension / src / main / java / org / activiti / rest / conf / init / InitBaseProcessDefinition.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.activiti.rest.conf.init;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileNotFoundException;
21 import java.util.List;
22
23 import javax.annotation.PostConstruct;
24
25 import org.activiti.engine.RepositoryService;
26 import org.activiti.engine.repository.DeploymentBuilder;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.context.annotation.Configuration;
31 import org.springframework.core.env.Environment;
32
33 @Configuration
34 public class InitBaseProcessDefinition {
35
36   protected static final Logger LOGGER = LoggerFactory.getLogger(InitBaseProcessDefinition.class);
37
38   @Autowired
39   protected RepositoryService repositoryService;
40
41   @Autowired
42   protected Environment environment;
43
44   @PostConstruct
45   public void init() {
46
47     if (Boolean.valueOf(this.environment.getProperty("init.process.definitions", "true"))
48         .booleanValue()) {
49       LOGGER.info("Initializing base process definitions");
50       initBaseProcessDefinitions();
51     }
52   }
53
54   protected void initBaseProcessDefinitions() {
55     String deploymentName = "base definitions";
56     List deploymentList =
57         this.repositoryService.createDeploymentQuery().deploymentName(deploymentName).list();
58
59     if ((deploymentList == null) || (deploymentList.isEmpty())) {
60
61       String classPath = this.getClass().getClassLoader().getResource("/").getPath();
62       classPath = classPath.replaceAll("WEB-INF/classes", "baseProccessDefinition");
63
64       File files = new File(classPath);
65       DeploymentBuilder builder = this.repositoryService.createDeployment().name(deploymentName);
66       File flist[] = files.listFiles();
67       if (flist != null && flist.length != 0) {
68         for (File f : flist) {
69
70           String fileName = f.getName();
71           if (fileName != null && fileName.endsWith("bpmn20.xml")) {
72             try {
73               builder.addInputStream(fileName, new FileInputStream(f));
74               LOGGER.info("deploy success: {}", fileName);
75             } catch (FileNotFoundException e) {
76               LOGGER.info("deploy failed: {}", fileName);
77             }
78           }
79         }
80         builder.deploy();
81       }
82     }
83   }
84 }