Skip to content

Commit

Permalink
improving the code for new (archetype) object creation
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaHonchar committed Feb 20, 2019
1 parent 5c8c23e commit 5a824d1
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 82 deletions.
Expand Up @@ -73,6 +73,8 @@
import com.evolveum.midpoint.web.component.menu.cog.InlineMenuItem;
import com.evolveum.midpoint.web.component.menu.cog.InlineMenuItemAction;
import com.evolveum.midpoint.web.component.prism.*;
import com.evolveum.midpoint.web.page.admin.resources.PageResourceWizard;
import com.evolveum.midpoint.web.page.admin.valuePolicy.PageValuePolicy;
import com.evolveum.midpoint.web.util.ObjectTypeGuiDescriptor;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType;
Expand Down Expand Up @@ -192,6 +194,7 @@ public final class WebComponentUtil {
private static RelationRegistry staticallyProvidedRelationRegistry;

private static Map<Class<?>, Class<? extends PageBase>> objectDetailsPageMap;
private static Map<Class<?>, Class<? extends PageBase>> createNewObjectPageMap;

static {
objectDetailsPageMap = new HashMap<>();
Expand All @@ -202,6 +205,12 @@ public final class WebComponentUtil {
objectDetailsPageMap.put(ResourceType.class, PageResource.class);
objectDetailsPageMap.put(TaskType.class, PageTaskEdit.class);
objectDetailsPageMap.put(ReportType.class, PageReport.class);
objectDetailsPageMap.put(ValuePolicyType.class, PageValuePolicy.class);
}

static{
createNewObjectPageMap = new HashMap<>();
createNewObjectPageMap.put(ResourceType.class, PageResourceWizard.class);
}

// only pages that support 'advanced search' are currently listed here (TODO: generalize)
Expand Down Expand Up @@ -1970,16 +1979,36 @@ public static String createShadowIcon(PrismObject<ShadowType> object) {
return GuiStyleConstants.CLASS_SHADOW_ICON_UNKNOWN;
}

public static <AHT extends AssignmentHolderType> AHT createNewObjectWithCollectionRef(Class<AHT> type, PrismContext context,
ObjectReferenceType collectionRef){
if (UserType.class.equals(type) && collectionRef != null && ArchetypeType.COMPLEX_TYPE.equals(collectionRef.getType())){
UserType user = new UserType(context);
AssignmentType assignment = new AssignmentType();
assignment.setTargetRef(collectionRef.clone());
user.getAssignment().add(assignment);
return (AHT) user;
public static <AHT extends AssignmentHolderType, O extends ObjectType> void initNewObjectWithReference(PageBase pageBase, O targetObject, QName type, Collection<QName> relations) throws SchemaException {
List<ObjectReferenceType> newReferences = new ArrayList<>();
for (QName relation : relations) {
newReferences.add(ObjectTypeUtil.createObjectRef(targetObject, relation));
}
return null;
initNewObjectWithReference(pageBase, type, newReferences);
}

public static <AHT extends AssignmentHolderType> void initNewObjectWithReference(PageBase pageBase, QName type, List<ObjectReferenceType> newReferences) throws SchemaException {
PrismContext prismContext = pageBase.getPrismContext();
PrismObjectDefinition<AHT> def = prismContext.getSchemaRegistry().findObjectDefinitionByType(type);
PrismObject<AHT> obj = def.instantiate();
AHT assignmentHolder = obj.asObjectable();
if (newReferences != null) {
newReferences.stream().forEach(ref -> {
AssignmentType assignment = new AssignmentType();
assignment.setTargetRef(ref);
assignmentHolder.getAssignment().add(assignment);

// Set parentOrgRef in any case. This is not strictly correct.
// The parentOrgRef should be added by the projector. But
// this is needed to successfully pass through security
// TODO: fix MID-3234
if (ref.getType() != null && OrgType.COMPLEX_TYPE.equals(ref.getType())) {
assignmentHolder.getParentOrgRef().add(ref.clone());
}
});
}

WebComponentUtil.dispatchToObjectDetailsPage(obj, true, pageBase);
}

public static String createUserIconTitle(PrismObject<UserType> object) {
Expand Down Expand Up @@ -2304,7 +2333,7 @@ public static void dispatchToObjectDetailsPage(PrismObject obj, Component compon

// shows the actual object that is passed via parameter (not its state in repository)
public static void dispatchToObjectDetailsPage(PrismObject obj, boolean isNewObject, Component component) {
Class newObjectPageClass = getObjectDetailsPage(obj.getCompileTimeClass());
Class newObjectPageClass = isNewObject ? getNewlyCreatedObjectPage(obj.getCompileTimeClass()) : getObjectDetailsPage(obj.getCompileTimeClass());
if (newObjectPageClass == null) {
throw new IllegalArgumentException("Cannot determine details page for "+obj.getCompileTimeClass());
}
Expand Down Expand Up @@ -2378,6 +2407,10 @@ public static Class<? extends PageBase> getObjectDetailsPage(Class<? extends Obj
return objectDetailsPageMap.get(type);
}

public static Class<? extends PageBase> getNewlyCreatedObjectPage(Class<? extends ObjectType> type) {
return createNewObjectPageMap.get(type);
}

public static Class<? extends PageBase> getObjectListPage(Class<? extends ObjectType> type) {
return objectListPageMap.get(type);
}
Expand Down
Expand Up @@ -109,8 +109,9 @@ public boolean isVisible() {
@Override
protected IModel<String> getDefaltParentOrgModel() {
return new ReadOnlyModel<String>(() -> {
List<OrgType> parentOrgs = FocusSummaryPanel.this.getModel().getObject().getParentOrg();
if (parentOrgs.isEmpty()) {
O focusObject = FocusSummaryPanel.this.getModel().getObject();
List<OrgType> parentOrgs = focusObject != null ? focusObject.getParentOrg() : null;
if (parentOrgs == null || parentOrgs.isEmpty()) {
return "";
}
// Kinda hack now .. "functional" orgType always has preference
Expand Down Expand Up @@ -140,6 +141,9 @@ protected IModel<AbstractResource> getPhotoModel() {
public AbstractResource getObject() {
byte[] jpegPhoto = null;
O object = getModel().getObject();
if (object == null){
return null;
}
if (object instanceof FocusType) {
jpegPhoto = ((FocusType) object).getJpegPhoto();
}
Expand Down
Expand Up @@ -25,6 +25,7 @@
import com.evolveum.midpoint.prism.util.PolyStringUtils;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.component.menu.cog.InlineMenuItem;
Expand Down Expand Up @@ -183,7 +184,21 @@ protected String getStorageKey() {

protected void objectDetailsPerformed(AjaxRequestTarget target, O object){}

protected void newObjectActionPerformed(AjaxRequestTarget target, CompiledObjectCollectionView collectionView){}
protected void newObjectActionPerformed(AjaxRequestTarget target, CompiledObjectCollectionView collectionView){
if (collectionView == null){
collectionView = getCollectionViewObject();
}
ObjectReferenceType collectionViewReference = collectionView != null && collectionView.getCollection() != null ?
collectionView.getCollection().getCollectionRef() : null;
try {
WebComponentUtil.initNewObjectWithReference(PageAdminObjectList.this,
WebComponentUtil.classToQName(getPrismContext(), getType()),
collectionViewReference != null ? Arrays.asList(collectionViewReference) : null);
} catch (SchemaException ex){
getFeedbackPanel().getFeedbackMessages().error(PageAdminObjectList.this, ex.getUserFriendlyMessage());
target.add(getFeedbackPanel());
}
}

protected ObjectFilter getArchetypeViewFilter(){
CompiledObjectCollectionView view = getCollectionViewObject();
Expand Down
Expand Up @@ -151,12 +151,6 @@ protected void objectDetailsPerformed(AjaxRequestTarget target, ResourceType obj

}

@Override
protected void newObjectActionPerformed(AjaxRequestTarget target, CompiledObjectCollectionView collectionView) {
navigateToNext(PageResourceWizard.class);

}

@Override
protected Class<ResourceType> getType(){
return ResourceType.class;
Expand Down
Expand Up @@ -566,8 +566,9 @@ protected void okPerformed(QName type, Collection<QName> relations, AjaxRequestT
target.add(getPageBase().getFeedbackPanel());
return;
}
AbstractRoleMemberPanel.this.getPageBase().hideMainPopup(target);
try {
MemberOperationsHelper.initObjectForAdd(AbstractRoleMemberPanel.this.getPageBase(), AbstractRoleMemberPanel.this.getModelObject(), type, relations, target);
WebComponentUtil.initNewObjectWithReference(AbstractRoleMemberPanel.this.getPageBase(), AbstractRoleMemberPanel.this.getModelObject(), type, relations);
} catch (SchemaException e) {
throw new SystemException(e.getMessage(), e);
}
Expand Down
Expand Up @@ -124,37 +124,7 @@ protected OrgType getAssignmentTargetRefObject(){
browser.setOutputMarkupId(true);
pageBase.showMainPopup(browser, target);
}

public static <O extends ObjectType, R extends AbstractRoleType> void initObjectForAdd(PageBase pageBase, R targetObject, QName type, Collection<QName> relations, AjaxRequestTarget target) throws SchemaException {
pageBase.hideMainPopup(target);
PrismContext prismContext = pageBase.getPrismContext();
PrismObjectDefinition<O> def = prismContext.getSchemaRegistry().findObjectDefinitionByType(type);
PrismObject<O> obj = def.instantiate();
List<ObjectReferenceType> newReferences = new ArrayList<>();
for (QName relation : relations) {
newReferences.add(createReference(targetObject, relation));
}
ObjectType objType = (ObjectType) obj.asObjectable();
if (FocusType.class.isAssignableFrom(obj.getCompileTimeClass())) {
newReferences.stream().forEach(ref -> {
AssignmentType assignment = new AssignmentType();
assignment.setTargetRef(ref);
((FocusType) objType).getAssignment().add(assignment);

// Set parentOrgRef in any case. This is not strictly correct.
// The parentOrgRef should be added by the projector. But
// this is needed to successfully pass through security
// TODO: fix MID-3234
if (ref.getType() != null && OrgType.COMPLEX_TYPE.equals(ref.getType())) {
objType.getParentOrgRef().add(ref.clone());
}
});

}

WebComponentUtil.dispatchToObjectDetailsPage(obj, true, pageBase);
}

public static <R extends AbstractRoleType> ObjectQuery createDirectMemberQuery(R targetObject, QName objectType, Collection<QName> relations, ObjectViewDto<OrgType> tenant, ObjectViewDto<OrgType> project, PrismContext prismContext) {
// We assume tenantRef.relation and orgRef.relation are always default ones (see also MID-3581)
S_FilterEntry q0;
Expand Down
Expand Up @@ -92,11 +92,6 @@ protected void objectDetailsPerformed(AjaxRequestTarget target, RoleType object)
PageRoles.this.roleDetailsPerformed(target, object.getOid());
}

@Override
protected void newObjectActionPerformed(AjaxRequestTarget target, CompiledObjectCollectionView collectionView) {
navigateToNext(PageRole.class);
}

@Override
protected Class<RoleType> getType(){
return RoleType.class;
Expand Down
Expand Up @@ -99,11 +99,6 @@ protected List<InlineMenuItem> createRowActions() {
return listInlineMenuHelper.createRowActions();
}

@Override
protected void newObjectActionPerformed(AjaxRequestTarget target, CompiledObjectCollectionView collectionView) {
navigateToNext(PageService.class);
}

@Override
protected Class<ServiceType> getType(){
return ServiceType.class;
Expand Down
Expand Up @@ -141,7 +141,8 @@ protected List<AssignmentInfoDto> load() {

@Override
protected FocusSummaryPanel<UserType> createSummaryPanel() {
return new UserSummaryPanel(ID_SUMMARY_PANEL, Model.of(getObjectModel().getObject().getObject().asObjectable()), this);
return new UserSummaryPanel(ID_SUMMARY_PANEL, isEditingFocus() ?
Model.of(getObjectModel().getObject().getObject().asObjectable()) : Model.of(), this);
}

protected void cancelPerformed(AjaxRequestTarget target) {
Expand Down
Expand Up @@ -17,19 +17,18 @@
package com.evolveum.midpoint.web.page.admin.users;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import com.evolveum.midpoint.gui.api.component.ObjectBrowserPanel;
import com.evolveum.midpoint.model.api.authentication.CompiledObjectCollectionView;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.query.ObjectFilter;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.web.application.Url;
import com.evolveum.midpoint.web.component.data.column.*;
import com.evolveum.midpoint.web.component.dialog.HelpInfoPanel;
import com.evolveum.midpoint.web.component.menu.cog.ButtonInlineMenuItem;
import com.evolveum.midpoint.web.component.menu.cog.InlineMenuItemAction;
import com.evolveum.midpoint.web.component.search.SearchFactory;
Expand All @@ -47,7 +46,6 @@
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.export.AbstractExportableColumn;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
Expand All @@ -61,8 +59,6 @@
import com.evolveum.midpoint.model.api.ModelExecuteOptions;
import com.evolveum.midpoint.prism.delta.ChangeType;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.security.api.AuthorizationConstants;
import com.evolveum.midpoint.task.api.Task;
Expand Down Expand Up @@ -399,17 +395,6 @@ protected void objectDetailsPerformed(AjaxRequestTarget target, UserType user) {
navigateToNext(PageUser.class, parameters);
}

@Override
protected void newObjectActionPerformed(AjaxRequestTarget target, CompiledObjectCollectionView collectionView){
if (collectionView == null){
collectionView = getCollectionViewObject();
}
UserType newUser = WebComponentUtil.createNewObjectWithCollectionRef(UserType.class, getPrismContext(),
collectionView != null && collectionView.getCollection() != null ?
collectionView.getCollection().getCollectionRef() : null);
navigateToNext(new PageUser(newUser != null ? newUser.asPrismObject() : null, true));
}

@Override
protected Class getType(){
return UserType.class;
Expand Down
Expand Up @@ -74,11 +74,6 @@ protected void objectDetailsPerformed(AjaxRequestTarget target, ValuePolicyType
PageValuePolicies.this.valuePolicyDetailsPerformed(target, valuePolicy);
}

@Override
protected void newObjectActionPerformed(AjaxRequestTarget target, CompiledObjectCollectionView collectionView) {
navigateToNext(PageValuePolicy.class);
}

@Override
protected List<IColumn<SelectableBean<ValuePolicyType>, String>> initColumns() {
return PageValuePolicies.this.initValuePoliciesColumns();
Expand Down

0 comments on commit 5a824d1

Please sign in to comment.