Skip to content

Commit b252c08

Browse files
committed
fixed ModelController (Issue #27)
1 parent 9a59579 commit b252c08

File tree

3 files changed

+152
-153
lines changed

3 files changed

+152
-153
lines changed

src/main/java/org/imixs/application/model/ModelController.java

Lines changed: 148 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import java.text.ParseException;
3535
import java.util.ArrayList;
3636
import java.util.Collections;
37-
import java.util.LinkedHashSet;
3837
import java.util.List;
3938
import java.util.Set;
4039
import java.util.logging.Level;
@@ -44,19 +43,21 @@
4443

4544
import org.imixs.workflow.FileData;
4645
import org.imixs.workflow.ItemCollection;
46+
import org.imixs.workflow.WorkflowKernel;
4747
import org.imixs.workflow.bpmn.BPMNUtil;
4848
import org.imixs.workflow.engine.ModelService;
4949
import org.imixs.workflow.engine.WorkflowService;
5050
import org.imixs.workflow.exceptions.AccessDeniedException;
5151
import org.imixs.workflow.exceptions.ModelException;
52-
import org.imixs.workflow.faces.data.WorkflowController;
52+
import org.imixs.workflow.exceptions.PluginException;
53+
import org.imixs.workflow.faces.fileupload.FileUploadController;
5354
import org.openbpmn.bpmn.BPMNModel;
5455
import org.openbpmn.bpmn.exceptions.BPMNModelException;
5556
import org.openbpmn.bpmn.util.BPMNModelFactory;
5657
import org.xml.sax.SAXException;
5758

58-
import jakarta.ejb.EJB;
59-
import jakarta.enterprise.context.RequestScoped;
59+
import jakarta.annotation.PostConstruct;
60+
import jakarta.enterprise.context.ConversationScoped;
6061
import jakarta.faces.event.ActionEvent;
6162
import jakarta.inject.Inject;
6263
import jakarta.inject.Named;
@@ -75,45 +76,63 @@
7576
*
7677
*/
7778
@Named
78-
@RequestScoped
79+
// @RequestScoped
80+
@ConversationScoped
7981
public class ModelController implements Serializable {
8082

8183
private static final long serialVersionUID = 1L;
84+
private static Logger logger = Logger.getLogger(ModelController.class.getName());
8285

83-
@Inject
84-
protected ModelUploadHandler modelUploadHandler;
86+
protected ItemCollection modelUploads = null;
87+
// @Inject
88+
// protected ModelUploadHandler modelUploadHandler;
8589

8690
@Inject
87-
WorkflowController workflowController;
88-
89-
@EJB
9091
protected ModelService modelService;
9192

92-
@EJB
93+
@Inject
9394
protected WorkflowService workflowService;
9495

95-
private static final Logger logger = Logger.getLogger(ModelController.class.getName());
96+
@Inject
97+
FileUploadController fileUploadController;
98+
99+
/**
100+
* PostConstruct event - init model uploads
101+
*/
102+
@PostConstruct
103+
void init() {
104+
modelUploads = new ItemCollection();
105+
}
106+
107+
public ItemCollection getModelUploads() {
108+
return modelUploads;
109+
}
110+
111+
public void setModelUploads(ItemCollection modelUploads) {
112+
this.modelUploads = modelUploads;
113+
}
96114

97115
/**
98116
* returns all groups for a version
99117
*
100118
* @param version
101119
* @return
102120
*/
103-
public Set<String> getGroups(String version) {
121+
public List<String> getGroups(String version) {
104122
try {
105-
BPMNModel model;
106-
model = modelService.getModelManager().getModel(version);
107-
return modelService.getModelManager().findAllGroupsByModel(model);
123+
BPMNModel model = modelService.getModelManager().getModel(version);
124+
Set<String> groups = modelService.getModelManager().findAllGroupsByModel(model);
125+
// Convert the Set to a List
126+
return new ArrayList<>(groups);
108127
} catch (ModelException e) {
109-
logger.log(Level.WARNING, "Unable to load groups:{0}", e.getMessage());
128+
logger.warning("Unable to load groups:" + e.getMessage());
110129
}
111130
// return empty result
112-
return new LinkedHashSet<>();
131+
return new ArrayList<String>();
113132
}
114133

115134
/**
116-
* Returns a sorted list of all WorkflowGroups from all models.
135+
* Returns a String list of all WorkflowGroup names.
117136
*
118137
* Workflow groups of the system model will be skipped.
119138
*
@@ -126,7 +145,6 @@ public Set<String> getGroups(String version) {
126145
* @throws ModelException
127146
*/
128147
public List<String> getWorkflowGroups() throws ModelException {
129-
130148
List<String> result = new ArrayList<>();
131149
for (BPMNModel model : modelService.getModelManager().getAllModels()) {
132150
String version = BPMNUtil.getVersion(model);
@@ -143,66 +161,57 @@ public List<String> getWorkflowGroups() throws ModelException {
143161
result.add(groupName);
144162
}
145163
}
146-
147164
Collections.sort(result);
148165
return result;
149-
150166
}
151167

152168
/**
153-
* Finds the first matching model version for a group name
169+
* Returns the ModelVersion of a given group name
154170
*
155171
* @param group
156172
* @return
157173
* @throws ModelException
158174
*/
159-
public String findVersionByGroup(String group) throws ModelException {
175+
public String getVersionByGroup(String group) throws ModelException {
160176
return modelService.getModelManager().findVersionByGroup(group);
161177
}
162178

163179
/**
164-
* Loads the first start task in the corresponding workflow group
180+
* Returns a list of all Imixs Tasks for a given group
165181
*
166-
* @param group
167182
* @return
168-
* @throws ModelException
169183
*/
170-
public ItemCollection findStartTaskByGroup(String version, String group) throws ModelException {
171-
ItemCollection result = null;
172-
BPMNModel model = modelService.getModelManager().getModel(version);
173-
List<ItemCollection> startTasks;
174-
175-
startTasks = modelService.getModelManager().findStartTasks(model, group);
176-
if (startTasks.size() > 0) {
177-
result = startTasks.get(0);
184+
public List<ItemCollection> findAllTasksByGroup(String group) {
185+
List<ItemCollection> result = new ArrayList<>();
186+
if (group == null || group.isEmpty()) {
187+
return result;
178188
}
179-
180-
if (result == null) {
181-
logger.warning("No Model found for Group '" + group + "'");
189+
try {
190+
String version = modelService.getModelManager().findVersionByGroup(group);
191+
BPMNModel model = modelService.getModelManager().getModel(version);
192+
result = modelService.getModelManager().findTasks(model, group);
193+
} catch (ModelException e) {
194+
logger.warning("Failed to call findAllTasksByGroup for '" + group + "'");
182195
}
183196

184197
return result;
185-
186198
}
187199

188200
/**
189-
* Creates a new process instance based on a given start task in the
190-
* corresponding workflow model
191-
* group
201+
* Returns a list of all Imixs Start Tasks for a given group
192202
*
193-
* @param version - model version
194-
* @param startTask - initial task
195-
* @return new process instance
196-
* @throws ModelException
203+
* @return
197204
*/
198-
public ItemCollection startWorkflowByTask(String version, ItemCollection startTask) throws ModelException {
199-
ItemCollection result = null;
200-
workflowController.create(version, startTask.getItemValueInteger("numprocessid"), null);
201-
if (result == null) {
202-
logger.warning("No Model found for model version '" + version + "'");
205+
public List<ItemCollection> findAllStartTasksByGroup(String version, String group) {
206+
List<ItemCollection> result = new ArrayList<>();
207+
try {
208+
BPMNModel model = modelService.getModelManager().getModel(version);
209+
return modelService.getModelManager().findStartTasks(model, group);
210+
} catch (ModelException e) {
211+
logger.severe(
212+
"Failed to find start tasks for workflow group '" + group + "' : " + e.getMessage());
203213
}
204214
return result;
205-
206215
}
207216

208217
/**
@@ -241,7 +250,14 @@ public ItemCollection getModelEntity(String version) {
241250
*/
242251
public void doUploadModel(ActionEvent event)
243252
throws ModelException {
244-
List<FileData> fileList = modelUploadHandler.getModelUploads().getFileData();
253+
254+
try {
255+
fileUploadController.attacheFiles(modelUploads);
256+
} catch (PluginException e) {
257+
e.printStackTrace();
258+
}
259+
260+
List<FileData> fileList = getModelUploads().getFileData();
245261

246262
if (fileList == null) {
247263
return;
@@ -265,19 +281,96 @@ public void doUploadModel(ActionEvent event)
265281
logger.log(Level.WARNING, "Invalid Model Type. Model {0} can't be imported!", file.getName());
266282
}
267283
}
268-
modelUploadHandler.reset();
284+
modelUploads = new ItemCollection();
269285
}
270286

271287
/**
272288
* This Method deletes the given model from the database and the internal model
273289
* cache.
274290
*
275-
* @param modelversion
276291
* @throws AccessDeniedException
277292
* @throws ModelException
278293
*/
279294
public void deleteModel(String modelversion) throws AccessDeniedException, ModelException {
280295
modelService.deleteModel(modelversion);
281296
}
282297

298+
/**
299+
* This method returns a process entity for a given ModelVersion or null if no
300+
* entity exists.
301+
*
302+
*
303+
* @param modelVersion
304+
* - version for the model to search the process entity
305+
* @param processid
306+
* - id of the process entity
307+
* @return an instance of the matching process entity
308+
* @throws ModelException
309+
*/
310+
public ItemCollection getProcessEntity(int processid, String modelversion) {
311+
try {
312+
BPMNModel model = modelService.getModelManager().getModel(modelversion);
313+
return modelService.getModelManager().findTaskByID(model, processid);
314+
} catch (ModelException e) {
315+
logger.warning("Unable to load task " + processid + " in model version '" + modelversion + "' - "
316+
+ e.getMessage());
317+
}
318+
return null;
319+
}
320+
321+
/**
322+
* This method return the 'rtfdescription' of a processentity and applies the
323+
* dynamic Text replacement function from the jee plugin
324+
*
325+
* @param processid
326+
* @param modelversion
327+
* @return
328+
* @throws ModelException
329+
*/
330+
public String getProcessDescription(int processid, String modelversion, ItemCollection documentContext) {
331+
ItemCollection pe = null;
332+
try {
333+
BPMNModel model = modelService.getModelManager().getModel(modelversion);
334+
pe = modelService.getModelManager().findTaskByID(model, processid);
335+
} catch (ModelException e1) {
336+
logger.warning("Unable to load task " + processid + " in model version '" + modelversion + "' - "
337+
+ e1.getMessage());
338+
}
339+
if (pe == null) {
340+
return "";
341+
}
342+
// String desc = pe.getItemValueString("rtfdescription");
343+
String desc = pe.getItemValueString(BPMNUtil.TASK_ITEM_DOCUMENTATION);
344+
try {
345+
desc = workflowService.adaptText(desc, documentContext);
346+
} catch (PluginException e) {
347+
logger.warning("Unable to update processDescription: " + e.getMessage());
348+
}
349+
return desc;
350+
}
351+
352+
/**
353+
* This method return the 'rtfdescription' of the initial task and resolves
354+
* textblock entries.
355+
* <p>
356+
* The method creates a dummy workitem to resolve the correct textblock
357+
*
358+
* @param processid
359+
* @param modelversion
360+
* @return
361+
* @throws ModelException
362+
*/
363+
public String getProcessDescriptionByInitialTask(ItemCollection initialTask, String modelVersion,
364+
String workflowGroup) {
365+
String result = "";
366+
if (initialTask != null) {
367+
// Dummy Workitem
368+
ItemCollection dummy = new ItemCollection();
369+
dummy.setItemValue(WorkflowKernel.WORKFLOWSTATUS, initialTask.getItemValueString("name"));
370+
dummy.setItemValue(WorkflowKernel.WORKFLOWGROUP, workflowGroup);
371+
result = getProcessDescription(initialTask.getItemValueInteger("taskid"), modelVersion, dummy);
372+
}
373+
return result;
374+
}
375+
283376
}

src/main/java/org/imixs/application/model/ModelUploadHandler.java

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)