use of org.alfresco.service.cmr.workflow.WorkflowInstance in project acs-community-packaging by Alfresco.
the class UINodeWorkflowInfo method renderAdvancedWorkflowInfo.
/**
* Renders the advanced workflow details for the given node.
*
* @param context Faces context
* @param node The node
* @param nodeService The NodeService instance
* @param ddService The Data Dictionary instance
* @param workflowService The WorkflowService instance
* @param out The response writer
* @param bundle Message bundle to get strings from
*/
protected void renderAdvancedWorkflowInfo(FacesContext context, Node node, NodeService nodeService, DictionaryService ddService, WorkflowService workflowService, ResponseWriter out, ResourceBundle bundle) throws IOException {
boolean isContent = true;
QName type = nodeService.getType(node.getNodeRef());
if (ddService.isSubClass(type, ContentModel.TYPE_FOLDER)) {
isContent = false;
}
// anything for other types
if (isContent) {
// Render HTML for advanved workflow
out.write("<div class=\"nodeWorkflowInfoTitle\">");
out.write(bundle.getString("advanced_workflows"));
out.write("</div><div class=\"nodeWorkflowInfoText\">");
List<WorkflowInstance> workflows = workflowService.getWorkflowsForContent(node.getNodeRef(), true);
if (workflows != null && workflows.size() > 0) {
// list out all the workflows the document is part of
if (isContent) {
out.write(bundle.getString("doc_part_of_advanced_workflows"));
} else {
out.write(bundle.getString("space_part_of_advanced_workflows"));
}
out.write(":<br/><ul>");
for (WorkflowInstance wi : workflows) {
out.write("<li>");
out.write(wi.definition.title);
if (wi.description != null && wi.description.length() > 0) {
out.write(" (");
out.write(Utils.encode(wi.description));
out.write(")");
}
out.write(" ");
if (wi.startDate != null) {
out.write(bundle.getString("started_on").toLowerCase());
out.write(" ");
out.write(Utils.getDateFormat(context).format(wi.startDate));
out.write(" ");
}
if (wi.initiator != null) {
out.write(bundle.getString("by"));
out.write(" ");
out.write(Utils.encode(User.getFullName(nodeService, wi.initiator)));
out.write(".");
}
out.write("</li>");
}
out.write("</ul>");
} else {
if (isContent) {
out.write(bundle.getString("doc_not_in_advanced_workflow"));
} else {
out.write(bundle.getString("space_not_in_advanced_workflow"));
}
}
out.write("</div>");
}
}
use of org.alfresco.service.cmr.workflow.WorkflowInstance in project alfresco-remote-api by Alfresco.
the class WorkflowInstanceGet method buildModel.
@Override
protected Map<String, Object> buildModel(WorkflowModelBuilder modelBuilder, WebScriptRequest req, Status status, Cache cache) {
Map<String, String> params = req.getServiceMatch().getTemplateVars();
// getting workflow instance id from request parameters
String workflowInstanceId = params.get("workflow_instance_id");
boolean includeTasks = getIncludeTasks(req);
WorkflowInstance workflowInstance = workflowService.getWorkflowById(workflowInstanceId);
// task was not found -> return 404
if (workflowInstance == null) {
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find workflow instance with id: " + workflowInstanceId);
}
Map<String, Object> model = new HashMap<String, Object>();
// build the model for ftl
model.put("workflowInstance", modelBuilder.buildDetailed(workflowInstance, includeTasks));
return model;
}
use of org.alfresco.service.cmr.workflow.WorkflowInstance in project alfresco-remote-api by Alfresco.
the class WorkflowInstancesGet method buildModel.
@Override
protected Map<String, Object> buildModel(WorkflowModelBuilder modelBuilder, WebScriptRequest req, Status status, Cache cache) {
WorkflowInstanceQuery workflowInstanceQuery = new WorkflowInstanceQuery();
Map<String, String> params = req.getServiceMatch().getTemplateVars();
// state is not included into filters list as it will be taken into account before filtering
WorkflowState state = getState(req);
// get filter param values
Map<QName, Object> filters = new HashMap<QName, Object>(9);
if (req.getParameter(PARAM_INITIATOR) != null)
filters.put(QNAME_INITIATOR, personService.getPerson(req.getParameter(PARAM_INITIATOR)));
if (req.getParameter(PARAM_PRIORITY) != null)
filters.put(WorkflowModel.PROP_WORKFLOW_PRIORITY, req.getParameter(PARAM_PRIORITY));
String excludeParam = req.getParameter(PARAM_EXCLUDE);
if (excludeParam != null && excludeParam.length() > 0) {
workflowInstanceQuery.setExcludedDefinitions(Arrays.asList(StringUtils.tokenizeToStringArray(excludeParam, ",")));
}
// process all the date related parameters
Map<DatePosition, Date> dateParams = new HashMap<DatePosition, Date>();
Date dueBefore = getDateFromRequest(req, PARAM_DUE_BEFORE);
if (dueBefore != null) {
dateParams.put(DatePosition.BEFORE, dueBefore);
}
Date dueAfter = getDateFromRequest(req, PARAM_DUE_AFTER);
if (dueAfter != null) {
dateParams.put(DatePosition.AFTER, dueAfter);
}
if (dateParams.isEmpty()) {
if (req.getParameter(PARAM_DUE_BEFORE) != null || req.getParameter(PARAM_DUE_AFTER) != null) {
filters.put(WorkflowModel.PROP_WORKFLOW_DUE_DATE, null);
}
} else {
filters.put(WorkflowModel.PROP_WORKFLOW_DUE_DATE, dateParams);
}
workflowInstanceQuery.setStartBefore(getDateFromRequest(req, PARAM_STARTED_BEFORE));
workflowInstanceQuery.setStartAfter(getDateFromRequest(req, PARAM_STARTED_AFTER));
workflowInstanceQuery.setEndBefore(getDateFromRequest(req, PARAM_COMPLETED_BEFORE));
workflowInstanceQuery.setEndAfter(getDateFromRequest(req, PARAM_COMPLETED_AFTER));
// determine if there is a definition id to filter by
String workflowDefinitionId = params.get(VAR_DEFINITION_ID);
if (workflowDefinitionId == null) {
workflowDefinitionId = req.getParameter(PARAM_DEFINITION_ID);
}
// default workflow state to ACTIVE if not supplied
if (state == null) {
state = WorkflowState.ACTIVE;
}
workflowInstanceQuery.setActive(state == WorkflowState.ACTIVE);
workflowInstanceQuery.setCustomProps(filters);
List<WorkflowInstance> workflows = new ArrayList<WorkflowInstance>();
int total = 0;
// MNT-9074 My Tasks fails to render if tasks quantity is excessive
int maxItems = getIntParameter(req, PARAM_MAX_ITEMS, DEFAULT_MAX_ITEMS);
int skipCount = getIntParameter(req, PARAM_SKIP_COUNT, DEFAULT_SKIP_COUNT);
if (workflowDefinitionId == null && req.getParameter(PARAM_DEFINITION_NAME) != null) {
/**
* If we are searching by workflow definition name then there may be many workflow definition instances.
*/
int workingSkipCount = skipCount;
/**
* Yes there could be multiple process definitions with that definition name
*/
String definitionName = req.getParameter(PARAM_DEFINITION_NAME);
List<WorkflowDefinition> defs = workflowService.getAllDefinitionsByName(definitionName);
int itemsToQuery = maxItems;
for (WorkflowDefinition def : defs) {
workflowDefinitionId = def.getId();
workflowInstanceQuery.setWorkflowDefinitionId(workflowDefinitionId);
if (maxItems < 0 || itemsToQuery > 0) {
workflows.addAll(workflowService.getWorkflows(workflowInstanceQuery, itemsToQuery, workingSkipCount));
}
if (maxItems > 0) {
itemsToQuery = maxItems - workflows.size();
}
total += (int) workflowService.countWorkflows(workflowInstanceQuery);
if (workingSkipCount > 0) {
workingSkipCount = skipCount - total;
if (workingSkipCount < 0) {
workingSkipCount = 0;
}
}
}
} else {
/**
* This is the old single task implementation
*/
if (workflowDefinitionId != null) {
workflowInstanceQuery.setWorkflowDefinitionId(workflowDefinitionId);
}
workflows.addAll(workflowService.getWorkflows(workflowInstanceQuery, maxItems, skipCount));
total = (int) workflowService.countWorkflows(workflowInstanceQuery);
}
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>(total);
// init empty list
results.addAll(Arrays.asList((Map<String, Object>[]) new Map[total]));
for (WorkflowInstance workflow : workflows) {
// set to special index
results.set(skipCount, modelBuilder.buildSimple(workflow));
skipCount++;
}
// create and return results, paginated if necessary
return createResultModel(req, "workflowInstances", results);
}
use of org.alfresco.service.cmr.workflow.WorkflowInstance in project alfresco-remote-api by Alfresco.
the class AbstractWorkflowRestApiTest method testWorkflowInstancesGet.
public void testWorkflowInstancesGet() throws Exception {
// Should work even with definitions hidden.
wfTestHelper.setVisible(false);
// Start workflow as USER1 and assign task to USER2.
personManager.setUser(USER1);
WorkflowDefinition adhocDef = workflowService.getDefinitionByName(getAdhocWorkflowDefinitionName());
Map<QName, Serializable> params = new HashMap<QName, Serializable>();
params.put(WorkflowModel.ASSOC_ASSIGNEE, personManager.get(USER2));
Date dueDate = new Date();
params.put(WorkflowModel.PROP_WORKFLOW_DUE_DATE, dueDate);
params.put(WorkflowModel.PROP_WORKFLOW_PRIORITY, 1);
params.put(WorkflowModel.ASSOC_PACKAGE, packageRef);
params.put(WorkflowModel.PROP_CONTEXT, packageRef);
WorkflowPath adhocPath = workflowService.startWorkflow(adhocDef.getId(), params);
String WorkflowId = adhocPath.getInstance().getId();
workflows.add(WorkflowId);
WorkflowTask startTask = workflowService.getTasksForWorkflowPath(adhocPath.getId()).get(0);
WorkflowInstance adhocInstance = startTask.getPath().getInstance();
workflowService.endTask(startTask.getId(), null);
// Get Workflow Instance Collection
Response response = sendRequest(new GetRequest(URL_WORKFLOW_INSTANCES), 200);
assertEquals(Status.STATUS_OK, response.getStatus());
String jsonStr = response.getContentAsString();
JSONObject json = new JSONObject(jsonStr);
JSONArray result = json.getJSONArray("data");
assertNotNull(result);
int totalItems = result.length();
for (int i = 0; i < result.length(); i++) {
checkSimpleWorkflowInstanceResponse(result.getJSONObject(i));
}
Response forDefinitionResponse = sendRequest(new GetRequest(MessageFormat.format(URL_WORKFLOW_INSTANCES_FOR_DEFINITION, adhocDef.getId())), 200);
assertEquals(Status.STATUS_OK, forDefinitionResponse.getStatus());
String forDefinitionJsonStr = forDefinitionResponse.getContentAsString();
JSONObject forDefinitionJson = new JSONObject(forDefinitionJsonStr);
JSONArray forDefinitionResult = forDefinitionJson.getJSONArray("data");
assertNotNull(forDefinitionResult);
for (int i = 0; i < forDefinitionResult.length(); i++) {
checkSimpleWorkflowInstanceResponse(forDefinitionResult.getJSONObject(i));
}
// create a date an hour ago to test filtering
Calendar hourAgoCal = Calendar.getInstance();
hourAgoCal.setTime(dueDate);
hourAgoCal.add(Calendar.HOUR_OF_DAY, -1);
Date anHourAgo = hourAgoCal.getTime();
Calendar hourLater = Calendar.getInstance();
hourLater.setTime(dueDate);
hourLater.add(Calendar.HOUR_OF_DAY, 1);
Date anHourLater = hourLater.getTime();
// filter by initiator
checkFiltering(URL_WORKFLOW_INSTANCES + "?initiator=" + USER1);
// filter by startedAfter
checkFiltering(URL_WORKFLOW_INSTANCES + "?startedAfter=" + ISO8601DateFormat.format(anHourAgo));
// filter by startedBefore
checkFiltering(URL_WORKFLOW_INSTANCES + "?startedBefore=" + ISO8601DateFormat.format(anHourLater));
// filter by dueAfter
checkFiltering(URL_WORKFLOW_INSTANCES + "?dueAfter=" + ISO8601DateFormat.format(anHourAgo));
// filter by dueBefore
checkFiltering(URL_WORKFLOW_INSTANCES + "?dueBefore=" + ISO8601DateFormat.format(anHourLater));
if (adhocInstance.getEndDate() != null) {
// filter by completedAfter
checkFiltering(URL_WORKFLOW_INSTANCES + "?completedAfter=" + ISO8601DateFormat.format(adhocInstance.getEndDate()));
// filter by completedBefore
checkFiltering(URL_WORKFLOW_INSTANCES + "?completedBefore=" + ISO8601DateFormat.format(adhocInstance.getEndDate()));
}
// filter by priority
checkFiltering(URL_WORKFLOW_INSTANCES + "?priority=1");
// filter by state
checkFiltering(URL_WORKFLOW_INSTANCES + "?state=active");
// filter by definition name
checkFiltering(URL_WORKFLOW_INSTANCES + "?definitionName=" + getAdhocWorkflowDefinitionName());
// paging
int maxItems = 3;
for (int skipCount = 0; skipCount < totalItems; skipCount += maxItems) {
checkPaging(URL_WORKFLOW_INSTANCES + "?maxItems=" + maxItems + "&skipCount=" + skipCount, totalItems, maxItems, skipCount);
}
// check the exclude filtering
String exclude = getAdhocWorkflowDefinitionName();
response = sendRequest(new GetRequest(URL_WORKFLOW_INSTANCES + "?exclude=" + exclude), 200);
assertEquals(Status.STATUS_OK, response.getStatus());
jsonStr = response.getContentAsString();
json = new JSONObject(jsonStr);
JSONArray results = json.getJSONArray("data");
assertNotNull(results);
boolean adhocWorkflowPresent = false;
for (int i = 0; i < results.length(); i++) {
JSONObject workflowInstanceJSON = results.getJSONObject(i);
String type = workflowInstanceJSON.getString("name");
if (exclude.equals(type)) {
adhocWorkflowPresent = true;
break;
}
}
assertFalse("Found adhoc workflows when they were supposed to be excluded", adhocWorkflowPresent);
}
use of org.alfresco.service.cmr.workflow.WorkflowInstance in project alfresco-remote-api by Alfresco.
the class AbstractWorkflowRestApiTest method testWorkflowInstanceGet.
public void testWorkflowInstanceGet() throws Exception {
// Start workflow as USER1 and assign task to USER2.
personManager.setUser(USER1);
WorkflowDefinition adhocDef = workflowService.getDefinitionByName(getAdhocWorkflowDefinitionName());
Map<QName, Serializable> params = new HashMap<QName, Serializable>();
params.put(WorkflowModel.ASSOC_ASSIGNEE, personManager.get(USER2));
Date dueDate = new Date();
params.put(WorkflowModel.PROP_WORKFLOW_DUE_DATE, dueDate);
params.put(WorkflowModel.PROP_WORKFLOW_PRIORITY, 1);
params.put(WorkflowModel.ASSOC_PACKAGE, packageRef);
params.put(WorkflowModel.PROP_CONTEXT, packageRef);
WorkflowPath adhocPath = workflowService.startWorkflow(adhocDef.getId(), params);
String WorkflowId = adhocPath.getInstance().getId();
workflows.add(WorkflowId);
// End start task.
WorkflowTask startTask = workflowService.getTasksForWorkflowPath(adhocPath.getId()).get(0);
startTask = workflowService.endTask(startTask.getId(), null);
WorkflowInstance adhocInstance = startTask.getPath().getInstance();
Response response = sendRequest(new GetRequest(URL_WORKFLOW_INSTANCES + "/" + adhocInstance.getId() + "?includeTasks=true"), 200);
assertEquals(Status.STATUS_OK, response.getStatus());
String jsonStr = response.getContentAsString();
JSONObject json = new JSONObject(jsonStr);
JSONObject result = json.getJSONObject("data");
assertNotNull(result);
assertEquals(adhocInstance.getId(), result.getString("id"));
assertTrue(result.opt("message").equals(JSONObject.NULL));
assertEquals(adhocInstance.getDefinition().getName(), result.getString("name"));
assertEquals(adhocInstance.getDefinition().getTitle(), result.getString("title"));
assertEquals(adhocInstance.getDefinition().getDescription(), result.getString("description"));
assertEquals(adhocInstance.isActive(), result.getBoolean("isActive"));
assertEquals(org.springframework.extensions.surf.util.ISO8601DateFormat.format(adhocInstance.getStartDate()), result.getString("startDate"));
assertNotNull(result.get("dueDate"));
assertNotNull(result.get("endDate"));
assertEquals(1, result.getInt("priority"));
JSONObject initiator = result.getJSONObject("initiator");
assertEquals(USER1, initiator.getString("userName"));
assertEquals(personManager.getFirstName(USER1), initiator.getString("firstName"));
assertEquals(personManager.getLastName(USER1), initiator.getString("lastName"));
assertEquals(adhocInstance.getContext().toString(), result.getString("context"));
assertEquals(adhocInstance.getWorkflowPackage().toString(), result.getString("package"));
assertNotNull(result.getString("startTaskInstanceId"));
JSONObject jsonDefinition = result.getJSONObject("definition");
WorkflowDefinition adhocDefinition = adhocInstance.getDefinition();
assertNotNull(jsonDefinition);
assertEquals(adhocDefinition.getId(), jsonDefinition.getString("id"));
assertEquals(adhocDefinition.getName(), jsonDefinition.getString("name"));
assertEquals(adhocDefinition.getTitle(), jsonDefinition.getString("title"));
assertEquals(adhocDefinition.getDescription(), jsonDefinition.getString("description"));
assertEquals(adhocDefinition.getVersion(), jsonDefinition.getString("version"));
assertEquals(adhocDefinition.getStartTaskDefinition().getMetadata().getName().toPrefixString(namespaceService), jsonDefinition.getString("startTaskDefinitionType"));
assertTrue(jsonDefinition.has("taskDefinitions"));
JSONArray tasks = result.getJSONArray("tasks");
assertTrue(tasks.length() > 1);
}
Aggregations