Skip to content

Commit

Permalink
PageWorkItems/PageWorkItem now works with searchContainers method. Im…
Browse files Browse the repository at this point in the history
…plementing factoring out search filters.
  • Loading branch information
mederly committed Mar 4, 2016
1 parent 8e43c3b commit 9b59664
Show file tree
Hide file tree
Showing 14 changed files with 469 additions and 76 deletions.
Expand Up @@ -22,9 +22,11 @@
import com.evolveum.midpoint.model.api.ModelService;
import com.evolveum.midpoint.model.api.TaskService;
import com.evolveum.midpoint.model.api.WorkflowService;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.query.ObjectPaging;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.prism.query.OrderDirection;
import com.evolveum.midpoint.repo.api.RepositoryService;
import com.evolveum.midpoint.schema.SchemaConstantsGenerated;
import com.evolveum.midpoint.task.api.TaskManager;
import com.evolveum.midpoint.util.logging.Trace;
Expand Down Expand Up @@ -83,11 +85,21 @@ protected ModelService getModel() {
return application.getModel();
}

protected RepositoryService getRepositoryService() {
MidPointApplication application = (MidPointApplication) MidPointApplication.get();
return application.getRepositoryService();
}

protected TaskManager getTaskManager() {
MidPointApplication application = (MidPointApplication) MidPointApplication.get();
return application.getTaskManager();
}

protected PrismContext getPrismContext() {
MidPointApplication application = (MidPointApplication) MidPointApplication.get();
return application.getPrismContext();
}

protected TaskService getTaskService() {
MidPointApplication application = (MidPointApplication) MidPointApplication.get();
return application.getTaskService();
Expand Down
Expand Up @@ -17,24 +17,35 @@
package com.evolveum.midpoint.web.page.admin.workflow.dto;

import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.model.api.PolicyViolationException;
import com.evolveum.midpoint.model.api.WorkflowService;
import com.evolveum.midpoint.prism.query.ObjectPaging;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.prism.query.OrderDirection;
import com.evolveum.midpoint.prism.query.builder.QueryBuilder;
import com.evolveum.midpoint.prism.query.builder.S_AtomicFilterExit;
import com.evolveum.midpoint.prism.query.builder.S_FilterEntryOrEmpty;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.security.api.MidPointPrincipal;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SystemException;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.exception.*;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.component.data.BaseSortableDataProvider;
import com.evolveum.midpoint.web.security.MidPointApplication;
import com.evolveum.midpoint.web.security.SecurityUtils;
import com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemNewType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import org.apache.wicket.Component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static com.evolveum.midpoint.gui.api.util.WebComponentUtil.*;
import static com.evolveum.midpoint.prism.query.OrderDirection.DESCENDING;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemNewType.F_WORK_ITEM_CREATED_TIMESTAMP;

/**
* @author lazyman
*/
Expand All @@ -47,7 +58,7 @@ public class WorkItemDtoNewProvider extends BaseSortableDataProvider<WorkItemNew

boolean assigned;

public static String currentUser() {
public String currentUser() {
MidPointPrincipal principal = SecurityUtils.getPrincipalUser();
if (principal == null) {
return "Unknown";
Expand All @@ -65,12 +76,12 @@ public WorkItemDtoNewProvider(Component component, boolean assigned) {
public Iterator<? extends WorkItemNewDto> internalIterator(long first, long count) {
getAvailableData().clear();

Task task = getTaskManager().createTaskInstance();
OperationResult result = new OperationResult(OPERATION_LIST_ITEMS);

try {
WorkflowService wfm = getWorkflowService();
List<WorkItemNewType> items = wfm.listWorkItemsNewRelatedToUser(currentUser(), assigned,
WebComponentUtil.safeLongToInteger(first), WebComponentUtil.safeLongToInteger(count), result);
ObjectQuery query = createQuery(first, count);
List<WorkItemNewType> items = getModel().searchContainers(WorkItemNewType.class, query, null, task, result);

for (WorkItemNewType item : items) {
try {
Expand All @@ -81,7 +92,7 @@ public Iterator<? extends WorkItemNewDto> internalIterator(long first, long coun
}
}

} catch (Exception ex) {
} catch (SchemaException|ObjectNotFoundException|SecurityViolationException|ConfigurationException|RuntimeException ex) {
LoggingUtils.logUnexpectedException(LOGGER, "Unhandled exception when listing work items", ex);
result.recordFatalError("Couldn't list work items.", ex);
}
Expand All @@ -93,14 +104,31 @@ public Iterator<? extends WorkItemNewDto> internalIterator(long first, long coun
return getAvailableData().iterator();
}

private ObjectQuery createQuery(long first, long count) throws SchemaException {
ObjectQuery query = createQuery();
query.setPaging(ObjectPaging.createPaging(safeLongToInteger(first), safeLongToInteger(count), F_WORK_ITEM_CREATED_TIMESTAMP, DESCENDING));
return query;
}

private ObjectQuery createQuery() throws SchemaException {
if (assigned) {
return QueryBuilder.queryFor(WorkItemNewType.class, getPrismContext())
.item(WorkItemNewType.F_ASSIGNEE_REF).ref(currentUser())
.build();
} else {
throw new UnsupportedOperationException("search by more than one ref is not supported");
}
}

@Override
protected int internalSize() {
int count = 0;
Task task = getTaskManager().createTaskInstance();
OperationResult result = new OperationResult(OPERATION_COUNT_ITEMS);
WorkflowService workflowService = getWorkflowService();
try {
count = workflowService.countWorkItemsRelatedToUser(currentUser(), assigned, result);
} catch (SchemaException|ObjectNotFoundException e) {
ObjectQuery query = createQuery();
count = getModel().countContainers(WorkItemNewType.class, query, null, task, result);
} catch (SchemaException|RuntimeException e) {
throw new SystemException("Couldn't count work items: " + e.getMessage(), e);
}

Expand All @@ -114,4 +142,32 @@ protected int internalSize() {

return count;
}

// TODO - fix this temporary implementation (perhaps by storing 'groups' in user context on logon)
public List<String> getGroupsForUser(String oid, OperationResult result) throws SchemaException, ObjectNotFoundException {
List<String> retval = new ArrayList<>();
UserType userType = getRepositoryService().getObject(UserType.class, oid, null, result).asObjectable();
for (AssignmentType assignmentType : userType.getAssignment()) {
ObjectReferenceType ref = assignmentType.getTargetRef();
if (ref != null) {
String groupName = objectReferenceToGroupName(ref);
if (groupName != null) { // if the reference represents a group name (i.e. it is not e.g. an account ref)
retval.add(groupName);
}
}
}
return retval;
}

private String objectReferenceToGroupName(ObjectReferenceType ref) {
if (RoleType.COMPLEX_TYPE.equals(ref.getType())) {
return "role:" + ref.getOid();
} else if (OrgType.COMPLEX_TYPE.equals(ref.getType())) {
return "org:" + ref.getOid();
} else {
return null;
}
}


}
Expand Up @@ -33,6 +33,7 @@
import java.util.Properties;
import java.util.Set;

import com.evolveum.midpoint.repo.api.RepositoryService;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.IOUtils;
import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
Expand Down Expand Up @@ -189,6 +190,8 @@ public class MidPointApplication extends AuthenticatedWebApplication {
transient PrismContext prismContext;
@Autowired
transient TaskManager taskManager;
@Autowired
transient private RepositoryService repositoryService; // temporary
@Autowired
transient private WorkflowService workflowService;
@Autowired
Expand Down Expand Up @@ -413,6 +416,10 @@ public TaskManager getTaskManager() {
return taskManager;
}

public RepositoryService getRepositoryService() {
return repositoryService;
}

public TaskService getTaskService() {
return taskService;
}
Expand Down
Expand Up @@ -413,6 +413,15 @@ public static QName asSingleName(ItemPath path) {
return path != null ? path.asSingleName() : null;
}

public static ItemPath[] asPathArray(QName... names) {
ItemPath[] paths = new ItemPath[names.length];
int i = 0;
for (QName name : names) {
paths[i++] = new ItemPath(name);
}
return paths;
}

public enum CompareResult {
EQUIVALENT,
SUPERPATH,
Expand Down
Expand Up @@ -186,4 +186,26 @@ public void addFilter(ObjectFilter objectFilter) {
setFilter(AndFilter.createAnd(objectFilter, filter));
}
}

// use when offset/maxSize is expected
public Integer getOffset() {
if (paging == null) {
return null;
}
if (paging.getCookie() != null) {
throw new UnsupportedOperationException("Paging cookie is not supported here.");
}
return paging.getOffset();
}

// use when offset/maxSize is expected
public Integer getMaxSize() {
if (paging == null) {
return null;
}
if (paging.getCookie() != null) {
throw new UnsupportedOperationException("Paging cookie is not supported here.");
}
return paging.getMaxSize();
}
}

0 comments on commit 9b59664

Please sign in to comment.