Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Evolveum/midpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-lizner committed Aug 31, 2020
2 parents 010d7b2 + 3d96746 commit 9122b06
Show file tree
Hide file tree
Showing 22 changed files with 385 additions and 206 deletions.
Expand Up @@ -12,6 +12,8 @@
import com.evolveum.midpoint.gui.api.prism.wrapper.ItemWrapper;
import com.evolveum.midpoint.gui.impl.factory.panel.ItemPanelContext;

import org.apache.wicket.feedback.ComponentFeedbackMessageFilter;

public interface GuiComponentFactory<T extends ItemPanelContext>{

<IW extends ItemWrapper> boolean match(IW wrapper);
Expand All @@ -21,6 +23,7 @@ public interface GuiComponentFactory<T extends ItemPanelContext>{
Integer getOrder();

default void configure(T panelCtx, Component component) {
panelCtx.getFeedback().setFilter(new ComponentFeedbackMessageFilter(component));
}

}
Expand Up @@ -9,11 +9,22 @@
import com.evolveum.midpoint.gui.api.prism.wrapper.PrismObjectWrapper;
import com.evolveum.midpoint.gui.api.prism.wrapper.PrismPropertyWrapper;
import com.evolveum.midpoint.gui.api.prism.wrapper.PrismReferenceWrapper;
import com.evolveum.midpoint.gui.impl.Channel;
import com.evolveum.midpoint.model.api.authentication.GuiProfiledPrincipal;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.path.ItemPath;

import com.evolveum.midpoint.schema.util.ObjectTypeUtil;
import com.evolveum.midpoint.util.logging.LoggingUtils;

import com.evolveum.midpoint.web.security.MidPointApplication;
import com.evolveum.midpoint.web.security.util.SecurityUtils;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ProvenanceAcquisitionType;

import com.evolveum.midpoint.xml.ns._public.common.common_3.ProvenanceMetadataType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ValueMetadataType;

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

Expand All @@ -29,6 +40,10 @@
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.component.prism.ValueStatus;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
* @author katka
*
Expand Down Expand Up @@ -157,4 +172,102 @@ public static <T> PrismPropertyValue<T> findSinglePropertyValue(ItemWrapper<?,?>
return null;
}
}

public static <C extends Containerable> void cleanupEmptyContainers(PrismContainer<C> container) {
List<PrismContainerValue<C>> values = container.getValues();
Iterator<PrismContainerValue<C>> valueIterator = values.iterator();
while (valueIterator.hasNext()) {
PrismContainerValue<C> value = valueIterator.next();

PrismContainerValue<C> valueAfter = cleanupEmptyContainerValue(value);
if (valueAfter == null || valueAfter.isIdOnly() || valueAfter.isEmpty()) {
valueIterator.remove();
}
}
}

public static <C extends Containerable> PrismContainerValue<C> cleanupEmptyContainerValue(PrismContainerValue<C> value) {
Collection<Item<?, ?>> items = value.getItems();

if (items != null) {
Iterator<Item<?, ?>> iterator = items.iterator();
while (iterator.hasNext()) {
Item<?, ?> item = iterator.next();

cleanupEmptyValues(item);
if (item.isEmpty()) {
iterator.remove();
}
}
}

if (value.getItems() == null || value.getItems().isEmpty()) {
return null;
}

return value;
}


private static <T> void cleanupEmptyValues(Item item) {
if (item instanceof PrismContainer) {
cleanupEmptyContainers((PrismContainer) item);
}

if (item instanceof PrismProperty) {
PrismProperty<T> property = (PrismProperty) item;
List<PrismPropertyValue<T>> pVals = property.getValues();
if (pVals == null || pVals.isEmpty()) {
return;
}

Iterator<PrismPropertyValue<T>> iterator = pVals.iterator();
while (iterator.hasNext()) {
PrismPropertyValue<T> pVal = iterator.next();
if (pVal == null || pVal.isEmpty() || pVal.getRealValue() == null) {
iterator.remove();
}
}
}

if (item instanceof PrismReference) {
PrismReference ref = (PrismReference) item;
List<PrismReferenceValue> values = ref.getValues();
if (values == null || values.isEmpty()) {
return;
}

Iterator<PrismReferenceValue> iterator = values.iterator();
while (iterator.hasNext()) {
PrismReferenceValue rVal = iterator.next();
if (rVal == null || rVal.isEmpty()) {
iterator.remove();
}
}
}
}

//TODO find better place
public static PrismContainerValue<ValueMetadataType> getNewYieldValue() throws SchemaException {
MidPointApplication app = MidPointApplication.get();
ProvenanceMetadataType provenanceMetadataType = new ProvenanceMetadataType(app.getPrismContext()).acquisition(WebPrismUtil.createAcquition());
ValueMetadataType valueMetadataType = new ValueMetadataType(app.getPrismContext()).provenance(provenanceMetadataType);
return valueMetadataType.asPrismContainerValue();

}

public static ProvenanceAcquisitionType createAcquition() {
MidPointApplication app = MidPointApplication.get();
ProvenanceAcquisitionType acquisitionType = new ProvenanceAcquisitionType(app.getPrismContext());
GuiProfiledPrincipal principal = SecurityUtils.getPrincipalUser();
if (principal != null) {
FocusType focus = principal.getFocus();
if (focus != null) {
acquisitionType.setActorRef(ObjectTypeUtil.createObjectRef(focus, app.getPrismContext()));
}
}
acquisitionType.setChannel(Channel.USER.getChannel());
acquisitionType.setTimestamp(app.getClock().currentTimeXMLGregorianCalendar());
return acquisitionType;
}
}
Expand Up @@ -13,6 +13,7 @@
import com.evolveum.midpoint.gui.impl.prism.panel.DefaultContainerablePanel;
import com.evolveum.midpoint.prism.Containerable;

import org.apache.wicket.feedback.ComponentFeedbackMessageFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -38,6 +39,14 @@ public org.apache.wicket.Component createPanel(PrismContainerPanelContext<C> pan
return new DefaultContainerablePanel<>(panelCtx.getComponentId(), panelCtx.getValueWrapper(), panelCtx.getSettings());
}

// @Override
// public void configure(PrismContainerPanelContext<C> panelCtx, org.apache.wicket.Component component) {
// if (!(component instanceof DefaultContainerablePanel)) {
// return;
// }
// panelCtx.getFeedback().setFilter(new ComponentFeedbackMessageFilter(component));
// }

@Override
public Integer getOrder() {
return Integer.MAX_VALUE;
Expand Down
Expand Up @@ -21,6 +21,8 @@
import com.evolveum.midpoint.prism.path.ItemName;
import com.evolveum.midpoint.task.api.TaskManager;

import com.evolveum.midpoint.xml.ns._public.common.common_3.*;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

Expand All @@ -38,9 +40,6 @@
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.component.prism.ValueStatus;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.VirtualContainerItemSpecificationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.VirtualContainersSpecificationType;
import com.evolveum.prism.xml.ns._public.types_3.ItemPathType;

/**
Expand Down Expand Up @@ -121,6 +120,9 @@ public IW createWrapper(PrismContainerValueWrapper<?> parent, Item childItem, It
}

private boolean skipCreateWrapper(ItemDefinition<?> def, ItemStatus status, WrapperContext context, boolean isEmptyValue) {
if (def == null) {
return true;
}
if (QNameUtil.match(FocusType.F_LINK_REF, def.getItemName()) || QNameUtil.match(FocusType.F_PERSONA_REF, def.getItemName())) {
LOGGER.trace("Skip creating wrapper for {}, it is not supported", def);
return true;
Expand Down Expand Up @@ -178,7 +180,7 @@ protected List<VW> createValuesWrapper(IW itemWrapper, I item, WrapperContext co
if (shouldCreateEmptyValue(item, context)) {
PV prismValue = createNewValue(item);
VW valueWrapper = createValueWrapper(itemWrapper, prismValue, ValueStatus.ADDED, context);
// setupMetadata(valueWrapper, context);
setupMetadata(itemWrapper, valueWrapper, context);
pvWrappers.add(valueWrapper);
}
return pvWrappers;
Expand All @@ -187,7 +189,7 @@ protected List<VW> createValuesWrapper(IW itemWrapper, I item, WrapperContext co
for (PV pcv : values) {
if(canCreateValueWrapper(pcv)){
VW valueWrapper = createValueWrapper(itemWrapper, pcv, ValueStatus.NOT_CHANGED, context);
setupMetadata(valueWrapper, context);
setupMetadata(itemWrapper, valueWrapper, context);
pvWrappers.add(valueWrapper);
}
}
Expand All @@ -196,24 +198,91 @@ protected List<VW> createValuesWrapper(IW itemWrapper, I item, WrapperContext co

}

protected <VW extends PrismValueWrapper> void setupMetadata(VW valueWrapper, WrapperContext ctx) throws SchemaException {
protected <VW extends PrismValueWrapper> void setupMetadata(IW itemWrapper, VW valueWrapper, WrapperContext ctx) throws SchemaException {
if (itemWrapper.isMetadata()) {
return;
}
PrismValue oldValue = valueWrapper.getNewValue();
PrismContainer<Containerable> metadataContainer = oldValue.getValueMetadataAsContainer();
// Optional<ValueMetadata> metadata = oldValue.valueMetadata(); // TODO
// return;
// // TODO adapt this code
// if (!metadata.isPresent()) {
// LOGGER.trace("Skipping creating metadata");
// return;
// }
//
// ValueMetadata valueMetadata = metadata.get();

if (canContainLegacyMetadata(oldValue)) {
PrismContainer<MetadataType> oldMetadata = ((PrismContainerValue) oldValue).findContainer(ObjectType.F_METADATA);
if (oldMetadata != null && oldMetadata.getValue() != null) {
PrismContainerValue<Containerable> newMetadataValue = metadataContainer.createNewValue();
transformStorageMetadata(newMetadataValue, oldMetadata);
transformProcessMetadata(newMetadataValue, oldMetadata);
}
}

ValueMetadataWrapperFactoryImpl valueMetadataWrapperFactory = new ValueMetadataWrapperFactoryImpl(getRegistry());
PrismContainerWrapper<Containerable> valueMetadataWrapper = valueMetadataWrapperFactory.createWrapper(null, metadataContainer, ItemStatus.NOT_CHANGED.NOT_CHANGED, ctx);
PrismContainerWrapper<Containerable> valueMetadataWrapper = valueMetadataWrapperFactory.createWrapper(null, metadataContainer, ItemStatus.NOT_CHANGED, ctx);
valueWrapper.setValueMetadata(new ValueMetadataWrapperImpl(valueMetadataWrapper));
}

private <T, PV extends PrismValue> boolean canContainLegacyMetadata(PV value) {
if (value instanceof PrismObjectValue) {
return true;
}

if (!(value instanceof PrismContainerValue)) {
return false;
}

PrismContainerDefinition containerDef = ((PrismContainerValue) value).getDefinition();
if (containerDef == null || containerDef.isRuntimeSchema()) {
return false;
}

T realValue = value.getRealValue();
if (realValue == null) {
return false;
}

if (PasswordType.class.isAssignableFrom(realValue.getClass())) {
return true;
}

if (AssignmentType.class.isAssignableFrom(realValue.getClass())) {
return true;
}

return false;
}

private void transformStorageMetadata(PrismContainerValue<Containerable> metadataValue, PrismContainer<MetadataType> oldMetadata) throws SchemaException {
PrismContainer<StorageMetadataType> storagetMetadata = metadataValue.findOrCreateContainer(ValueMetadataType.F_STORAGE);

MetadataType oldMetadataType = oldMetadata.getRealValue();
StorageMetadataType storageMetadataType = new StorageMetadataType(prismContext);
storageMetadataType.setCreateChannel(oldMetadataType.getCreateChannel());
storageMetadataType.setCreateTaskRef(oldMetadataType.getCreateTaskRef());
storageMetadataType.setCreateTimestamp(oldMetadataType.getCreateTimestamp());
storageMetadataType.setCreatorRef(oldMetadataType.getCreatorRef());
storageMetadataType.setModifierRef(oldMetadataType.getModifierRef());
storageMetadataType.setModifyChannel(oldMetadataType.getModifyChannel());
storageMetadataType.setModifyTaskRef(oldMetadataType.getModifyTaskRef());
storageMetadataType.setModifyTimestamp(oldMetadataType.getModifyTimestamp());

storagetMetadata.setRealValue(storageMetadataType);

}

private void transformProcessMetadata(PrismContainerValue<Containerable> metadataValue, PrismContainer<MetadataType> oldContainer) throws SchemaException {
PrismContainer<ProcessMetadataType> processMetadata = metadataValue.findOrCreateContainer(ValueMetadataType.F_PROCESS);

MetadataType oldMetadata = oldContainer.getRealValue();
ProcessMetadataType processMetadataType = new ProcessMetadataType(prismContext);
processMetadataType.setCertificationFinishedTimestamp(oldMetadata.getCertificationFinishedTimestamp());
processMetadataType.setCertificationOutcome(oldMetadata.getCertificationOutcome());
processMetadataType.setCreateApprovalTimestamp(oldMetadata.getCreateApprovalTimestamp());
processMetadataType.setModifyApprovalTimestamp(oldMetadata.getModifyApprovalTimestamp());
processMetadataType.setRequestorComment(oldMetadata.getRequestorComment());
processMetadataType.setRequestorRef(oldMetadata.getRequestorRef());
processMetadataType.setRequestTimestamp(oldMetadata.getRequestTimestamp());

processMetadata.setRealValue(processMetadataType);
}

protected List<PV> getValues(I item) {
return item.getValues();
}
Expand Down
Expand Up @@ -48,16 +48,17 @@ public void registerWrapperPanel(PrismContainerWrapper<C> wrapper) {
getRegistry().registerWrapperPanel(wrapper.getTypeName(), MetadataContainerPanel.class);
}

@Override
protected boolean shouldCreateEmptyValue(PrismContainer<C> item, WrapperContext context) {
return false;
}

@Override
public PrismContainerWrapper<C> createWrapper(PrismContainerValueWrapper<?> parent, ItemDefinition<?> def, WrapperContext context) throws SchemaException {
// Boolean readonly = context.getReadOnly() == null ? null : Boolean.valueOf(context.getReadOnly());
// context.setReadOnly(true);
// context.setMetadata(true);
WrapperContext ctx = context.clone();
ctx.setReadOnly(true);
ctx.setMetadata(true);
PrismContainerWrapper<C> wrapper = super.createWrapper(parent, def, ctx);
// context.setReadOnly(readonly);
return wrapper;
}
}
Expand Up @@ -70,7 +70,7 @@ public PrismObjectWrapper<O> createObjectWrapper(PrismObject<O> object, ItemStat
context.setShowEmpty(ItemStatus.ADDED == status);
objectWrapper.setExpanded(true);
PrismContainerValueWrapper<O> valueWrapper = createValueWrapper(objectWrapper, object.getValue(), ItemStatus.ADDED == status ? ValueStatus.ADDED : ValueStatus.NOT_CHANGED, context);
setupMetadata(valueWrapper, context);
setupMetadata(objectWrapper, valueWrapper, context);
objectWrapper.getValues().add(valueWrapper);
registerWrapperPanel(objectWrapper);

Expand Down Expand Up @@ -98,7 +98,7 @@ public void updateWrapper(PrismObjectWrapper<O> wrapper, WrapperContext context)
wrapper.getValue().getItems().clear();

PrismContainerValueWrapper<O> valueWrapper = createValueWrapper(wrapper, wrapper.getObject().getValue(), ItemStatus.ADDED == wrapper.getStatus() ? ValueStatus.ADDED : ValueStatus.NOT_CHANGED, context);
setupMetadata(valueWrapper, context);
setupMetadata(wrapper, valueWrapper, context);
wrapper.getValues().clear();
wrapper.getValues().add(valueWrapper);

Expand Down Expand Up @@ -136,6 +136,7 @@ public PrismContainerValueWrapper<O> createValueWrapper(PrismContainerWrapper<O>
def.setMaxOccurs(1);
def.setDisplayName(WebComponentUtil.getOrigStringFromPoly(display.getLabel()));
def.setDynamic(true);
def.setRuntimeSchema(true);

ItemWrapperFactory<?, ?, ?> factory = getRegistry().findWrapperFactory(def, null);
if (factory == null) {
Expand Down

0 comments on commit 9122b06

Please sign in to comment.