Skip to content

Commit

Permalink
Added a couple of features to model client sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed May 8, 2014
1 parent 6386c2a commit b1feb02
Showing 1 changed file with 112 additions and 10 deletions.
Expand Up @@ -16,35 +16,45 @@
package com.evolveum.midpoint.testing.model.client.sample;

import com.evolveum.midpoint.model.client.ModelClientUtil;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.GetOperationOptionsType;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.ObjectDeltaListType;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.ObjectDeltaOperationListType;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.ObjectListType;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.ObjectSelectorType;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.RetrieveOptionType;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.SelectorQualifiedGetOptionType;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.SelectorQualifiedGetOptionsType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ModelExecuteOptionsType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OperationResultType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SystemConfigurationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SystemObjectsType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import com.evolveum.midpoint.xml.ns._public.common.fault_3.FaultMessage;
import com.evolveum.midpoint.xml.ns._public.model.model_3.ModelPortType;
import com.evolveum.midpoint.xml.ns._public.model.model_3.ModelService;
import com.evolveum.prism.xml.ns._public.query_3.OrderDirectionType;
import com.evolveum.prism.xml.ns._public.query_3.PagingType;
import com.evolveum.prism.xml.ns._public.query_3.QueryType;
import com.evolveum.prism.xml.ns._public.query_3.SearchFilterType;
import com.evolveum.prism.xml.ns._public.types_3.ChangeTypeType;
import com.evolveum.prism.xml.ns._public.types_3.ItemDeltaType;
import com.evolveum.prism.xml.ns._public.types_3.ModificationTypeType;
import com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;
import org.apache.commons.io.IOUtils;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import javax.xml.bind.JAXBContext;
Expand Down Expand Up @@ -101,11 +111,23 @@ public static void main(String[] args) {
System.out.println("Got Sailor role");
System.out.println(sailorRole);

Collection<ResourceType> resouces = listResources(modelPort);
Collection<ResourceType> resources = listResources(modelPort);
System.out.println("Resources");
System.out.println(resouces);

String userGuybrushoid = createUserGuybrush(modelPort, sailorRole);
dump(resources);

Collection<UserType> users = listUsers(modelPort);
System.out.println("Users");
dump(users);

Collection<TaskType> tasks = listTasks(modelPort);
System.out.println("Tasks");
dump(tasks);
System.out.println("Next scheduled times: ");
for (TaskType taskType : tasks) {
System.out.println(" - " + getOrig(taskType.getName()) + ": " + taskType.getNextRunStartTimestamp());
}

String userGuybrushoid = createUserGuybrush(modelPort, sailorRole);
System.out.println("Created user guybrush, OID: "+userGuybrushoid);

String userLeChuckOid = createUserFromSystemResource(modelPort, "user-lechuck.xml");
Expand All @@ -131,15 +153,46 @@ public static void main(String[] args) {
// ... because deleting the user will delete also all the traces (except logs and audit of course).
deleteUser(modelPort, userGuybrushoid);
deleteUser(modelPort, userLeChuckOid);
System.out.println("Deleted user");
System.out.println("Deleted user(s)");

} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}

private static SystemConfigurationType getConfiguration(ModelPortType modelPort) throws FaultMessage {
// TODO move to ModelClientUtil
private static String getOrig(PolyStringType polyStringType) {
if (polyStringType == null) {
return null;
}
StringBuilder sb = new StringBuilder();
for (Object o : polyStringType.getContent()) {
if (o instanceof String) {
sb.append(o);
} else if (o instanceof Element) {
Element e = (Element) o;
if ("orig".equals(e.getLocalName())) {
return e.getTextContent();
}
} else if (o instanceof JAXBElement) {
JAXBElement je = (JAXBElement) o;
if ("orig".equals(je.getName().getLocalPart())) {
return (String) je.getValue();
}
}
}
return sb.toString();
}

private static void dump(Collection<? extends ObjectType> objects) {
System.out.println("Objects returned: " + objects.size());
for (ObjectType objectType : objects) {
System.out.println(" - " + getOrig(objectType.getName()) + ": " + objectType);
}
}

private static SystemConfigurationType getConfiguration(ModelPortType modelPort) throws FaultMessage {

Holder<ObjectType> objectHolder = new Holder<ObjectType>();
Holder<OperationResultType> resultHolder = new Holder<OperationResultType>();
Expand All @@ -162,7 +215,53 @@ private static Collection<ResourceType> listResources(ModelPortType modelPort) t
return (Collection) objectList.getObject();
}

private static String createUserGuybrush(ModelPortType modelPort, RoleType role) throws FaultMessage {
private static Collection<UserType> listUsers(ModelPortType modelPort) throws SAXException, IOException, FaultMessage {
SelectorQualifiedGetOptionsType options = new SelectorQualifiedGetOptionsType();
Holder<ObjectListType> objectListHolder = new Holder<ObjectListType>();
Holder<OperationResultType> resultHolder = new Holder<OperationResultType>();

// let's say we want to get first 3 users, sorted alphabetically by user name
QueryType queryType = new QueryType(); // holds search query + paging options
PagingType pagingType = new PagingType();
pagingType.setMaxSize(3);
pagingType.setOrderBy(ModelClientUtil.createItemPathType("name"));
pagingType.setOrderDirection(OrderDirectionType.ASCENDING);
queryType.setPaging(pagingType);

modelPort.searchObjects(ModelClientUtil.getTypeQName(UserType.class), queryType, options, objectListHolder, resultHolder);

ObjectListType objectList = objectListHolder.value;
return (Collection) objectList.getObject();
}

private static Collection<TaskType> listTasks(ModelPortType modelPort) throws SAXException, IOException, FaultMessage {
SelectorQualifiedGetOptionsType operationOptions = new SelectorQualifiedGetOptionsType();

// Let's say we want to retrieve tasks' next scheduled time (because this may be a costly operation if
// JDBC based quartz scheduler is used, the fetching of this attribute has to be explicitly requested)
SelectorQualifiedGetOptionType getNextScheduledTimeOption = new SelectorQualifiedGetOptionType();

// prepare a selector (described by path) + options (saying to retrieve that attribute)
ObjectSelectorType selector = new ObjectSelectorType();
selector.setPath(ModelClientUtil.createItemPathType("nextRunStartTimestamp"));
getNextScheduledTimeOption.setSelector(selector);
GetOperationOptionsType selectorOptions = new GetOperationOptionsType();
selectorOptions.setRetrieve(RetrieveOptionType.INCLUDE);
getNextScheduledTimeOption.setOptions(selectorOptions);

// add newly created option to the list of operation options
operationOptions.getOption().add(getNextScheduledTimeOption);

Holder<ObjectListType> objectListHolder = new Holder<ObjectListType>();
Holder<OperationResultType> resultHolder = new Holder<OperationResultType>();

modelPort.searchObjects(ModelClientUtil.getTypeQName(TaskType.class), null, operationOptions, objectListHolder, resultHolder);

ObjectListType objectList = objectListHolder.value;
return (Collection) objectList.getObject();
}

private static String createUserGuybrush(ModelPortType modelPort, RoleType role) throws FaultMessage {
Document doc = ModelClientUtil.getDocumnent();

UserType user = new UserType();
Expand Down Expand Up @@ -394,15 +493,18 @@ private static Collection<RoleType> listRequestableRoles(ModelPortType modelPort
return (Collection) objectList.getObject();
}

private static void deleteUser(ModelPortType modelPort, String userGuybrushoid) throws FaultMessage {
private static void deleteUser(ModelPortType modelPort, String oid) throws FaultMessage {
ObjectDeltaType deltaType = new ObjectDeltaType();
deltaType.setObjectType(ModelClientUtil.getTypeQName(UserType.class));
deltaType.setChangeType(ChangeTypeType.DELETE);
deltaType.setOid(userGuybrushoid);
deltaType.setOid(oid);

ObjectDeltaListType deltaListType = new ObjectDeltaListType();
deltaListType.getDelta().add(deltaType);
modelPort.executeChanges(deltaListType, null);

ModelExecuteOptionsType executeOptionsType = new ModelExecuteOptionsType();
executeOptionsType.setRaw(true);
modelPort.executeChanges(deltaListType, executeOptionsType);
}

public static ModelPortType createModelPort(String[] args) {
Expand Down

0 comments on commit b1feb02

Please sign in to comment.