Skip to content

Commit

Permalink
fixing save, NPE for create new value, small cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
katkav committed May 24, 2019
1 parent 4a73387 commit 7c46cab
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 212 deletions.
Expand Up @@ -21,6 +21,7 @@

import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.impl.prism.ItemVisibilityHandler;
import com.evolveum.midpoint.gui.impl.prism.ObjectWrapper;
import com.evolveum.midpoint.gui.impl.prism.PrismContainerValueWrapper;
import com.evolveum.midpoint.gui.impl.prism.PrismValueWrapper;
import com.evolveum.midpoint.prism.Item;
Expand All @@ -32,6 +33,7 @@
import com.evolveum.midpoint.util.DebugDumpable;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;

/**
* @author katka
Expand Down Expand Up @@ -76,5 +78,10 @@ public interface ItemWrapper<V extends PrismValue, I extends Item<V, ID>, ID ext
<D extends ItemDelta<V, ID>> void applyDelta(D delta) throws SchemaException;

<D extends ItemDelta<V, ID>> Collection<D> getDelta() throws SchemaException;

<O extends ObjectType> ItemStatus findObjectStatus();

<OW extends PrismObjectWrapper<O>, O extends ObjectType> OW findObjectWrapper(ItemWrapper parent);


}
Expand Up @@ -16,17 +16,34 @@
package com.evolveum.midpoint.gui.api.util;

import org.apache.commons.lang.StringUtils;
import org.apache.wicket.ajax.AjaxRequestTarget;

import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.prism.ItemWrapper;
import com.evolveum.midpoint.gui.api.prism.PrismContainerWrapper;
import com.evolveum.midpoint.gui.impl.factory.WrapperContext;
import com.evolveum.midpoint.gui.impl.prism.PrismValueWrapper;
import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.PrismValue;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
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.prism.ValueStatus;

/**
* @author katka
*
*/
public class WebPrismUtil {

private static final transient Trace LOGGER = TraceManager.getTrace(WebPrismUtil.class);

private static final String DOT_CLASS = WebPrismUtil.class.getName() + ".";
private static final String OPERATION_CREATE_NEW_VALUE = DOT_CLASS + "createNewValue";

public static <ID extends ItemDefinition<I>, I extends Item<V, ID>, V extends PrismValue> String getHelpText(ID def) {
String doc = def.getHelp();
if (StringUtils.isEmpty(doc)) {
Expand All @@ -38,5 +55,39 @@ public static <ID extends ItemDefinition<I>, I extends Item<V, ID>, V extends Pr

return doc.replaceAll("\\s{2,}", " ").trim();
}


public static <IW extends ItemWrapper, PV extends PrismValue, VW extends PrismValueWrapper> VW createNewValueWrapper(IW itemWrapper, PV newValue, PageBase pageBase, AjaxRequestTarget target) {
LOGGER.debug("Adding value to {}", itemWrapper);

OperationResult result = new OperationResult(OPERATION_CREATE_NEW_VALUE);

VW newValueWrapper = null;
try {

if (!(itemWrapper instanceof PrismContainerWrapper)) {
itemWrapper.getItem().add(newValue);
}

Task task = pageBase.createSimpleTask(OPERATION_CREATE_NEW_VALUE);

WrapperContext context = new WrapperContext(task, result);
context.setObjectStatus(itemWrapper.findObjectStatus());
context.setShowEmpty(true);
context.setCreateIfEmpty(true);

newValueWrapper = pageBase.createValueWrapper(itemWrapper, newValue, ValueStatus.ADDED, context);
itemWrapper.getValues().add(newValueWrapper);
result.recordSuccess();

return newValueWrapper;
} catch (SchemaException e) {
LOGGER.error("Cannot create new value for {}", itemWrapper, e);
result.recordFatalError("Cannot create value wrapper for " + newValue + ". Reason: " + e.getMessage(), e);
target.add(pageBase.getFeedbackPanel());
}

return newValueWrapper;
}

}
Expand Up @@ -21,6 +21,7 @@
import java.util.stream.Collectors;

import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.gui.api.util.WebPrismUtil;
import com.evolveum.midpoint.gui.impl.component.icon.CompositedIconBuilder;
import com.evolveum.midpoint.gui.impl.component.icon.IconCssStyle;
import com.evolveum.midpoint.model.api.AssignmentObjectRelation;
Expand Down Expand Up @@ -58,6 +59,7 @@
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.query.ObjectPaging;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
Expand Down Expand Up @@ -87,9 +89,13 @@ public abstract class MultivalueContainerListPanel<C extends Containerable, S ex

private static final long serialVersionUID = 1L;

private static final String DOT_CLASS = MultivalueContainerListPanel.class.getName() + ".";
private static final String OPERATION_CREATE_NEW_VALUE = DOT_CLASS + "createNewValue";

public static final String ID_ITEMS = "items";
private static final String ID_ITEMS_TABLE = "itemsTable";
public static final String ID_SEARCH_ITEM_PANEL = "search";


private static final Trace LOGGER = TraceManager.getTrace(MultivalueContainerListPanel.class);

Expand Down Expand Up @@ -343,35 +349,6 @@ protected boolean isSearchEnabled(){
}

private void searchPerformed(ObjectQuery query, AjaxRequestTarget target) {

// MultivalueContainerListDataProvider<C> provider = getDataProvider();

// ObjectQuery finalQuery = null;
//
// ObjectQuery searchQuery = getQuery();
//
// ObjectQuery customQuery = createQuery();
//
// if (query != null && query.getFilter() != null) {
// if (customQuery != null && customQuery.getFilter() != null) {
// finalQuery = ObjectQuery.createObjectQuery(AndFilter.createAnd(customQuery.getFilter(), query.getFilter()));
// }
// finalQuery = query;
//
// } else {
// finalQuery = customQuery;
// }
//
// provider.setQuery(finalQuery);
// String storageKey = getStorageKey();
// if (StringUtils.isNotEmpty(storageKey)) {
// PageStorage storage = getPageStorage(storageKey);
// if (storage != null) {
// storage.setSearch(searchModel.getObject());
// storage.setPaging(null);
// }
// }
//
Table table = getItemTable();
table.setCurrentPage(null);
target.add((Component) table);
Expand All @@ -382,7 +359,6 @@ private void searchPerformed(ObjectQuery query, AjaxRequestTarget target) {
private ObjectQuery getQuery() {
Search search = searchModel.getObject();
ObjectQuery query = search.createObjectQuery(getPageBase().getPrismContext());
// query = addFilterToContentQuery(query);
return query;
}

Expand Down Expand Up @@ -481,18 +457,10 @@ public List<PrismContainerValueWrapper<C>> getPerformedSelectedItems(IModel<Pris
//TODO generalize for properites
public PrismContainerValueWrapper<C> createNewItemContainerValueWrapper(
PrismContainerValue<C> newItem,
PrismContainerWrapper<C> model) {

WrapperContext context = new WrapperContext(null, null);
context.setCreateIfEmpty(true);
PrismValueWrapper<?,?> valueWrapper = null;
try {
valueWrapper = getPageBase().createValueWrapper(model, newItem, ValueStatus.ADDED, context);
} catch (SchemaException e) {
//TODO error handling
}
model.getValues().add((PrismContainerValueWrapper) valueWrapper);
return (PrismContainerValueWrapper<C>) valueWrapper;
PrismContainerWrapper<C> model, AjaxRequestTarget target) {

return WebPrismUtil.createNewValueWrapper(model, newItem, getPageBase(), target);

}

public ColumnMenuAction<PrismContainerValueWrapper<C>> createDeleteColumnAction() {
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
package com.evolveum.midpoint.gui.impl.factory;

import com.evolveum.midpoint.gui.api.prism.ItemStatus;
import com.evolveum.midpoint.schema.ResourceShadowDiscriminator;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
Expand All @@ -37,6 +38,7 @@ public class WrapperContext {
private boolean showEmpty;
private boolean forceCreate;

private ItemStatus objectStatus;

//Shadow related attributes
private ResourceType resource;
Expand Down Expand Up @@ -114,4 +116,13 @@ public boolean isForceCreate() {
public void setForceCreate(boolean forceCreate) {
this.forceCreate = forceCreate;
}


public ItemStatus getObjectStatus() {
return objectStatus;
}

public void setObjectStatus(ItemStatus objectStatus) {
this.objectStatus = objectStatus;
}
}
Expand Up @@ -159,10 +159,9 @@ private List<PrismContainerValueWrapper<GlobalPolicyRuleType>> getObjects() {
}

protected void newGlobalPolicuRuleClickPerformed(AjaxRequestTarget target) {
//TODO maybe change to itemFactory.createContainerValue()???
PrismContainerValue<GlobalPolicyRuleType> newObjectPolicy = getModelObject().getItem().createNewValue();
PrismContainerValueWrapper<GlobalPolicyRuleType> newObjectPolicyWrapper = getMultivalueContainerListPanel().createNewItemContainerValueWrapper(newObjectPolicy, getModelObject());
// newObjectPolicyWrapper.setShowEmpty(true, true);
// newObjectPolicyWrapper.computeStripes();
PrismContainerValueWrapper<GlobalPolicyRuleType> newObjectPolicyWrapper = getMultivalueContainerListPanel().createNewItemContainerValueWrapper(newObjectPolicy, getModelObject(), target);
getMultivalueContainerListPanel().itemDetailsPerformed(target, Arrays.asList(newObjectPolicyWrapper));
}

Expand Down Expand Up @@ -211,7 +210,7 @@ protected void onClick(AjaxRequestTarget target, IModel<PrismContainerValueWrapp
getMultivalueContainerListPanel().itemDetailsPerformed(target, rowModel);
}

protected DisplayType getIconDisplayType(IModel<ContainerValueWrapper<GlobalPolicyRuleType>> rowModel) {
protected DisplayType getIconDisplayType(IModel<PrismContainerValueWrapper<GlobalPolicyRuleType>> rowModel) {
return WebComponentUtil.createDisplayType(WebComponentUtil.createDefaultBlackIcon(SystemConfigurationType.COMPLEX_TYPE));

}
Expand All @@ -221,18 +220,17 @@ protected DisplayType getIconDisplayType(IModel<ContainerValueWrapper<GlobalPoli

columns.add(linkColumn);

//TODO wrapper form wrapper model
columns.add(new PrismContainerWrapperColumn<GlobalPolicyRuleType>(getModel(), GlobalPolicyRuleType.F_POLICY_CONSTRAINTS, getPageBase()));

columns.add(new PrismContainerWrapperColumn<GlobalPolicyRuleType>(getModel(),GlobalPolicyRuleType.F_POLICY_ACTIONS, getPageBase()));

//todo property from container model

columns.add(new PrismPropertyWrapperColumn<GlobalPolicyRuleType, String>(getModel(), GlobalPolicyRuleType.F_POLICY_SITUATION, ColumnType.STRING, getPageBase()));

List<InlineMenuItem> menuActionsList = getMultivalueContainerListPanel().getDefaultMenuActions();
columns.add(new InlineMenuButtonColumn(menuActionsList, getPageBase()) {

private static final long serialVersionUID = 1L;

@Override
public String getCssClass() {
return " col-md-1 ";
Expand Down
Expand Up @@ -167,7 +167,7 @@ protected List<PrismContainerValueWrapper<ClassLoggerConfigurationType>> postSea
@Override
protected void newItemPerformed(AjaxRequestTarget target) {
PrismContainerValue<ClassLoggerConfigurationType> newLogger = loggerModel.getObject().getItem().createNewValue();
PrismContainerValueWrapper<ClassLoggerConfigurationType> newLoggerWrapper = getLoggersMultivalueContainerListPanel().createNewItemContainerValueWrapper(newLogger, loggerModel.getObject());
PrismContainerValueWrapper<ClassLoggerConfigurationType> newLoggerWrapper = getLoggersMultivalueContainerListPanel().createNewItemContainerValueWrapper(newLogger, loggerModel.getObject(), target);
loggerEditPerformed(target, Model.of(newLoggerWrapper), null);
}

Expand Down Expand Up @@ -396,7 +396,7 @@ protected void newAppendersClickPerformed(AjaxRequestTarget target) {
newObjectPolicy.setParent(appenders.getModelObject().getItem());
newObjectPolicy.setPrismContext(getPageBase().getPrismContext());

PrismContainerValueWrapper<AppenderConfigurationType> newAppenderContainerWrapper = getAppendersMultivalueContainerListPanel().createNewItemContainerValueWrapper(newObjectPolicy, appenders.getModelObject());
PrismContainerValueWrapper<AppenderConfigurationType> newAppenderContainerWrapper = getAppendersMultivalueContainerListPanel().createNewItemContainerValueWrapper(newObjectPolicy, appenders.getModelObject(), target);
getAppendersMultivalueContainerListPanel().itemDetailsPerformed(target, Arrays.asList(newAppenderContainerWrapper));
}

Expand Down
Expand Up @@ -50,6 +50,7 @@
import com.evolveum.midpoint.gui.api.component.password.PasswordPanel;
import com.evolveum.midpoint.gui.api.prism.PrismContainerWrapper;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.gui.api.util.WebPrismUtil;
import com.evolveum.midpoint.gui.impl.component.data.column.EditableColumn;
import com.evolveum.midpoint.gui.impl.component.form.TriStateFormGroup;
import com.evolveum.midpoint.gui.impl.factory.ItemRealValueModel;
Expand All @@ -62,6 +63,8 @@
import com.evolveum.midpoint.prism.PrismPropertyValue;
import com.evolveum.midpoint.prism.query.ObjectPaging;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
Expand Down Expand Up @@ -106,6 +109,9 @@ public class NotificationConfigTabPanel extends BasePanel<PrismContainerWrapper<
private static final long serialVersionUID = 1L;

private static final Trace LOGGER = TraceManager.getTrace(NotificationConfigTabPanel.class);

private static final String DOT_CLASS = NotificationConfigTabPanel.class.getName() + ".";
private static final String OPERATION_CREATE_NEW_VALUE = DOT_CLASS + "createNewValue";

private static final String ID_MAIL_CONFIG_HEADER = "mailConfigurationHeader";
private static final String ID_DEFAULT_FROM = "defaultFrom";
Expand Down Expand Up @@ -147,22 +153,8 @@ private void initPaging() {
protected void initLayout() {

PrismPropertyWrapperModel<NotificationConfigurationType, MailConfigurationType> mailConfig = PrismPropertyWrapperModel.fromContainerWrapper(getModel(), NotificationConfigurationType.F_MAIL);
// IModel<PrismPropertyWrapper<MailConfigurationType>> mailConfig = Model.of(getModel().getObject().findProperty(NotificationConfigurationType.F_MAIL));


add(createHeader(ID_MAIL_CONFIG_HEADER, mailConfig)); //mailConfig.getObject().getTypeName().getLocalPart() + ".details"));

Form form = new Form<>("form");


// if(mailConfig != null) {
// mailConfigType = new PropertyModel<MailConfigurationType>(mailConfig, "values[0].value.value").getObject();
// }
//
// if(mailConfigType == null) {
// mailConfigType = new MailConfigurationType();
// ((PrismPropertyValue<MailConfigurationType>)((List<PrismPropertyValueWrapper<MailConfigurationType>>)mailConfig.getObject().getValues()).get(0)).setValue(mailConfigType);
// }
add(createHeader(ID_MAIL_CONFIG_HEADER, mailConfig));

PropertyModel<MailConfigurationType> mailConfigType = new ItemRealValueModel<>(new PropertyModel<>(mailConfig, "values[0]"));

Expand Down Expand Up @@ -255,20 +247,14 @@ public boolean isVisible() {

@Override
public void onClick(AjaxRequestTarget target) {

PrismPropertyWrapper<FileConfigurationType> propertyWrapper = fileConfig.getObject();
try {
PrismPropertyValue<FileConfigurationType> newValue = getPrismContext().itemFactory().createPropertyValue();
propertyWrapper.getItem().add(newValue);
WrapperContext context = new WrapperContext(null, null);
PrismPropertyValueWrapper<FileConfigurationType> newValueWrapper =
getPageBase().createValueWrapper(propertyWrapper, newValue, ValueStatus.ADDED, context);
newValueWrapper.setRealValue(new FileConfigurationType());
propertyWrapper.getValues().add(newValueWrapper);
} catch (SchemaException e) {
LOGGER.error("Cannot create new value for {}", propertyWrapper, e);
getSession().error("Cannot create new value for " + propertyWrapper + ". Reason: " + e.getMessage());
target.add(getPageBase().getFeedbackPanel());
}
PrismPropertyValue<FileConfigurationType> newValue = getPrismContext().itemFactory().createPropertyValue();

PrismPropertyValueWrapper<FileConfigurationType> newValueWrapper = WebPrismUtil.createNewValueWrapper(propertyWrapper, newValue, getPageBase(), target);
//TODO: do we really need to set real value?? why??
newValueWrapper.setRealValue(new FileConfigurationType());

target.add(files);
}
};
Expand Down Expand Up @@ -340,14 +326,7 @@ public void onClick(AjaxRequestTarget target) {
}

private <T> Panel createHeader(String id, IModel<PrismPropertyWrapper<T>> model) {
// if (StringUtils.isEmpty(displayName)) {
// displayName = "displayName.not.set";
// }

PrismPropertyHeaderPanel<T> header = new PrismPropertyHeaderPanel<>(id, model);

// StringResourceModel headerLabelModel = createStringResource(new PropertyModel<>(model, "displayName"));
// Label header = new Label(id, headerLabelModel);
header.add(AttributeAppender.prepend("class", "prism-title pull-left"));
return header;
}
Expand Down
Expand Up @@ -163,9 +163,7 @@ private List<PrismContainerValueWrapper<ObjectPolicyConfigurationType>> getObjec

protected void newObjectPolicyClickPerformed(AjaxRequestTarget target) {
PrismContainerValue<ObjectPolicyConfigurationType> newObjectPolicy = getModel().getObject().getItem().createNewValue();
PrismContainerValueWrapper<ObjectPolicyConfigurationType> newObjectPolicyWrapper = getMultivalueContainerListPanel().createNewItemContainerValueWrapper(newObjectPolicy, getModelObject());
// newObjectPolicyWrapper.setShowEmpty(true, false);
// newObjectPolicyWrapper.computeStripes();
PrismContainerValueWrapper<ObjectPolicyConfigurationType> newObjectPolicyWrapper = getMultivalueContainerListPanel().createNewItemContainerValueWrapper(newObjectPolicy, getModelObject(), target);
getMultivalueContainerListPanel().itemDetailsPerformed(target, Arrays.asList(newObjectPolicyWrapper));
}

Expand Down

0 comments on commit 7c46cab

Please sign in to comment.