Skip to content

Commit

Permalink
- initial commit for form manager services
Browse files Browse the repository at this point in the history
  • Loading branch information
salaboy committed Apr 9, 2015
1 parent c472bf6 commit 1212f9b
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2015 JBoss by Red Hat.
*
* 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 org.jbpm.kie.services.impl;

import java.util.Map;

/**
*
* @author salaboy
*/
public interface FormManagerService {
void registerForm(String deploymentId, String key, String formContent);
Map<String, String> getAllFormsByDeployment(String deploymentId);
Map<String, String> getAllForms();
String getFormByKey(String deploymentId, String key);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2015 JBoss by Red Hat.
*
* 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 org.jbpm.kie.services.impl;

import java.util.HashMap;
import java.util.Map;

/**
*
* @author salaboy
*/
public class FormManagerServiceImpl implements FormManagerService {
private Map<String, Map<String, String>> formsRegistry = new HashMap<String, Map<String, String>>();


@Override
public void registerForm(String deploymentId, String key, String formContent) {
if(formsRegistry.get(deploymentId) == null){
formsRegistry.put(deploymentId, new HashMap<String, String>());
}
formsRegistry.get(deploymentId).put(key, formContent);
}

@Override
public Map<String, String> getAllFormsByDeployment(String deploymentId) {
return formsRegistry.get(deploymentId);
}

@Override
public Map<String, String> getAllForms() {
Map<String, String> allForms = new HashMap<String, String>();
for(Map<String, String> formsByDep : formsRegistry.values()){
allForms.putAll(formsByDep);
}
return allForms;
}

@Override
public String getFormByKey(String deploymentId, String key) {
if(deploymentId != null && formsRegistry != null && formsRegistry.containsKey(deploymentId)){
return formsRegistry.get(deploymentId).get(key);
}
return null;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ public class KModuleDeploymentService extends AbstractDeploymentService {
private DefinitionService bpmn2Service;

private DeploymentDescriptorMerger merger = new DeploymentDescriptorMerger();

private FormManagerService formManagerService;



public void onInit() {
EntityManagerFactoryManager.get().addEntityManagerFactory("org.jbpm.domain", getEmf());
}
Expand Down Expand Up @@ -121,10 +124,10 @@ public void deploy(DeploymentUnit unit) {

KieBase kbase = kieContainer.getKieBase(kbaseName);

Map<String, String> formsData = new HashMap<String, String>();
//Map<String, String> formsData = new HashMap<String, String>();
Collection<String> files = module.getFileNames();

processResources(module, formsData, files, kieContainer, kmoduleUnit, deployedUnit, releaseId);
processResources(module, files, kieContainer, kmoduleUnit, deployedUnit, releaseId);

if (module.getKieDependencies() != null) {
Collection<InternalKieModule> dependencies = module.getKieDependencies().values();
Expand All @@ -133,7 +136,7 @@ public void deploy(DeploymentUnit unit) {
logger.debug("Processing dependency module " + depModule.getReleaseId());
files = depModule.getFileNames();

processResources(depModule, formsData, files, kieContainer, kmoduleUnit, deployedUnit, depModule.getReleaseId());
processResources(depModule, files, kieContainer, kmoduleUnit, deployedUnit, depModule.getReleaseId());
}
}
if (module.getJarDependencies() != null && !module.getJarDependencies().isEmpty()) {
Expand Down Expand Up @@ -275,7 +278,7 @@ protected Object getInstanceFromModel(ObjectModel model, KieContainer kieContain
return resolver.getInstance(model, kieContainer.getClassLoader(), contaxtParams);
}

protected void processResources(InternalKieModule module, Map<String, String> formsData, Collection<String> files,
protected void processResources(InternalKieModule module, Collection<String> files,
KieContainer kieContainer, DeploymentUnit unit, DeployedUnitImpl deployedUnit, ReleaseId releaseId) {
for (String fileName : files) {
if(fileName.matches(".+bpmn[2]?$")) {
Expand All @@ -288,7 +291,7 @@ protected void processResources(InternalKieModule module, Map<String, String> fo
}
process.setEncodedProcessSource(Base64.encodeBase64String(processString.getBytes()));
process.setDeploymentId(unit.getIdentifier());
process.setForms(formsData);
// process.setForms(formsData);
deployedUnit.addAssetLocation(process.getId(), process);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("Unsupported encoding while processing process " + fileName);
Expand All @@ -302,7 +305,8 @@ protected void processResources(InternalKieModule module, Map<String, String> fo
while (m.find()) {
key = m.group(2);
}
formsData.put(key, formContent);
// formsData.put(key, formContent);
formManagerService.registerForm(unit.getIdentifier(), key, formContent);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("Unsupported encoding while processing form " + fileName);
}
Expand All @@ -315,7 +319,8 @@ protected void processResources(InternalKieModule module, Map<String, String> fo
while (m.find()) {
key = m.group(2);
}
formsData.put(key+".form", formContent);
// formsData.put(key+".form", formContent);
formManagerService.registerForm(unit.getIdentifier(), key+".form", formContent);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("Unsupported encoding while processing form " + fileName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.jbpm.kie.services.impl.form;

import java.util.Map;
import org.jbpm.kie.services.impl.FormManagerService;

import org.jbpm.services.api.model.ProcessDefinition;
import org.kie.api.task.model.Task;
Expand All @@ -28,4 +29,6 @@ public interface FormProvider {
String render(String name, ProcessDefinition process, Map<String, Object> renderContext);

String render(String name, Task task, ProcessDefinition process, Map<String, Object> renderContext);

void setFormManagerService(FormManagerService formManagerService);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2015 JBoss by Red Hat.
*
* 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 org.jbpm.kie.services.impl.form.provider;

import org.jbpm.kie.services.impl.FormManagerService;
import org.jbpm.kie.services.impl.form.FormProvider;

/**
*
* @author salaboy
*/
public abstract class AbstractFormProvider implements FormProvider{
protected FormManagerService formManagerService;
public void setFormManagerService(FormManagerService formManagerService){
this.formManagerService = formManagerService;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import java.io.StringWriter;
import java.util.Map;

import org.jbpm.kie.services.impl.form.FormProvider;

import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Template;

public abstract class FreemakerFormProvider implements FormProvider {

public abstract class FreemakerFormProvider extends AbstractFormProvider {



protected String render(String name, InputStream src, Map<String, Object> renderContext) {

String str = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,81 +13,97 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jbpm.kie.services.impl.form.provider;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Map;

import org.jbpm.kie.services.impl.model.ProcessAssetDesc;
import org.jbpm.services.api.model.ProcessDefinition;
import org.kie.api.task.model.Task;
import org.kie.internal.task.api.model.InternalTask;


public class InMemoryFormProvider extends FreemakerFormProvider {

private static final String DEFAULT_PROCESS = "DefaultProcess";
private static final String DEFAULT_TASK = "DefaultTask";

@Override
public String render(String name, ProcessDefinition process, Map<String, Object> renderContext) {
ProcessAssetDesc asset = null;
if (!(process instanceof ProcessAssetDesc)) {
return null;
}
asset = (ProcessAssetDesc) process;

InputStream template = null;
if (asset.getForms().containsKey(process.getId())) {
template = new ByteArrayInputStream(asset.getForms().get(process.getId()).getBytes());
} else if (asset.getForms().containsKey(process.getId() + "-taskform")) {
template = new ByteArrayInputStream(asset.getForms().get(process.getId() + "-taskform").getBytes());
} else if (asset.getForms().containsKey(DEFAULT_PROCESS)) {
template = new ByteArrayInputStream(asset.getForms().get(DEFAULT_PROCESS).getBytes());
ProcessAssetDesc asset = null;
if (!(process instanceof ProcessAssetDesc)) {
return null;
}
asset = (ProcessAssetDesc) process;
String templateString = formManagerService.getFormByKey(process.getDeploymentId(), process.getId());
if (templateString == null) {
templateString = formManagerService.getFormByKey(process.getDeploymentId(), process.getId() + "-taskform");
}

if (template == null) return null;
if (templateString == null || templateString.isEmpty()) {
return null;
} else {
return render(name, new ByteArrayInputStream(templateString.getBytes()), renderContext);
}

return render(name, template, renderContext);
// if (asset.getForms().containsKey(process.getId())) {
// //template = new ByteArrayInputStream(asset.getForms().get(process.getId()).getBytes());
// template = new ByteArrayInputStream(formManagerService.getFormByKey(process.getDeploymentId(), process.getId()).getBytes());
// } else if (asset.getForms().containsKey(process.getId() + "-taskform")) {
// //template = new ByteArrayInputStream(asset.getForms().get(process.getId() + "-taskform").getBytes());
// template = new ByteArrayInputStream(formManagerService.getFormByKey(process.getDeploymentId(), process.getId()+"-taskform").getBytes());
// } else if (asset.getForms().containsKey(DEFAULT_PROCESS)) {
// template = new ByteArrayInputStream(asset.getForms().get(DEFAULT_PROCESS).getBytes());
// }
}

@Override
public String render(String name, Task task, ProcessDefinition process, Map<String, Object> renderContext) {
ProcessAssetDesc asset = null;
if (!(process instanceof ProcessAssetDesc)) {
return null;
}
asset = (ProcessAssetDesc) process;
InputStream template = null;
if(task != null && process != null){
String lookupName = "";
String formName = ((InternalTask)task).getFormName();
if(formName != null && !formName.equals("")){
ProcessAssetDesc asset = null;
if (!(process instanceof ProcessAssetDesc)) {
return null;
}
asset = (ProcessAssetDesc) process;

String lookupName = "";
if (task != null && process != null) {

String formName = ((InternalTask) task).getFormName();
if (formName != null && !formName.equals("")) {
lookupName = formName;
}else{
} else {
lookupName = task.getNames().get(0).getText();

}
if (asset.getForms().containsKey(lookupName)) {
template = new ByteArrayInputStream(asset.getForms().get(lookupName).getBytes());
} else if (asset.getForms().containsKey(lookupName.replace(" ", "")+ "-taskform")) {
template = new ByteArrayInputStream(asset.getForms().get(lookupName.replace(" ", "") + "-taskform").getBytes());
} else if (asset.getForms().containsKey(DEFAULT_TASK)) {
template = new ByteArrayInputStream(asset.getForms().get(DEFAULT_TASK).getBytes());

}

}
String templateString = formManagerService.getFormByKey(asset.getDeploymentId(), lookupName);
if (templateString == null) {
templateString = formManagerService.getFormByKey(asset.getDeploymentId(), lookupName.replace(" ", "") + "-taskform");
}

if (template == null) return null;
if (templateString == null || templateString.isEmpty()) {
return null;
} else {
return render(name, new ByteArrayInputStream(templateString.getBytes()), renderContext);
}

return render(name, template, renderContext);
// if (asset.getForms().containsKey(lookupName)) {
// template = new ByteArrayInputStream(asset.getForms().get(lookupName).getBytes());
// } else if (asset.getForms().containsKey(lookupName.replace(" ", "")+ "-taskform")) {
// template = new ByteArrayInputStream(asset.getForms().get(lookupName.replace(" ", "") + "-taskform").getBytes());
// } else if (asset.getForms().containsKey(DEFAULT_TASK)) {
// template = new ByteArrayInputStream(asset.getForms().get(DEFAULT_TASK).getBytes());
// }
// }
// if (template == null) return null;
//
// return render(name, template, renderContext);
}

@Override
public int getPriority() {
return 3;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@
*/
package org.jbpm.services.cdi.impl.form.providers;

import javax.inject.Inject;
import org.jbpm.kie.services.impl.FormManagerService;
import org.jbpm.kie.services.impl.form.provider.InMemoryFormProvider;

public class InMemoryFormProviderCDI extends InMemoryFormProvider {

@Inject
@Override
public void setFormManagerService(FormManagerService formManagerService){
super.setFormManagerService(formManagerService);
}
}

0 comments on commit 1212f9b

Please sign in to comment.