Skip to content

Commit

Permalink
MID-7502 improving breadcrumbs, removed transiency for icon/label mod…
Browse files Browse the repository at this point in the history
…els (wrong) and started caching values
  • Loading branch information
1azyman committed Jan 24, 2022
1 parent cad0588 commit c5b6e13
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@

package com.evolveum.midpoint.web.component.breadcrumbs;

import java.io.Serializable;
import java.util.Arrays;
import com.evolveum.midpoint.util.DebugDumpable;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;

import org.apache.wicket.RestartResponseException;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.IModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import com.evolveum.midpoint.util.DebugDumpable;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import java.io.Serializable;
import java.util.Arrays;

/**
* Breadcrumb object that is stored in the session. It represents the way "back" to the main menu.
Expand All @@ -37,10 +37,9 @@ public class Breadcrumb implements Serializable, DebugDumpable {

private static final Trace LOG = TraceManager.getTrace(Breadcrumb.class);

private transient IModel<String> labelModel;
private String label;
private transient IModel<String> iconModel;
private String icon;
private CachedModel labelModel;
private CachedModel iconModel;

private boolean useLink = false;
private boolean visible = true;

Expand All @@ -51,96 +50,39 @@ public Breadcrumb(IModel<String> labelModel) {
this(labelModel, null);
}

public Breadcrumb(IModel<String> labelModel, IModel<String> icon) {
setLabel(labelModel);
setIcon(icon);
public Breadcrumb(IModel<String> labelModel, IModel<String> iconModel) {
setLabel(new CachedModel(labelModel));
setIcon(new CachedModel(iconModel));
}

public PageParameters getParameters() {
return null;
}

public IModel<String> getLabel() {
if (labelModel == null && label != null) {
labelModel = new IModel<String>() {
private static final long serialVersionUID = 1L;

@Override
public String getObject() {
return label;
}
};
}
return labelModel;
}

public void setLabel(final IModel<String> label) {
if (label == null) {
this.labelModel = null;
this.labelModel = new CachedModel((String) null);
return;
}

this.labelModel = new IModel<String>() {
private static final long serialVersionUID = 1L;

@Override
public String getObject() {
try {
return label.getObject();
} catch (Exception ex) {
LOG.warn("Couldn't load breadcrumb model value", ex);
return null;
}
}

@Override
public void detach() {
label.detach();
}

};
this.labelModel = new CachedModel(label);
}

public IModel<String> getIcon() {
if (iconModel == null && icon != null) {
iconModel = new IModel<String>() {
private static final long serialVersionUID = 1L;

@Override
public String getObject() {
return icon;
}
};
}
return iconModel;
}

public void setIcon(final IModel<String> icon) {
if (icon == null) {
this.iconModel = null;
this.iconModel = new CachedModel((String) null);
return;
}

this.iconModel = new IModel<String>() {
private static final long serialVersionUID = 1L;

@Override
public String getObject() {
try {
return icon.getObject();
} catch (Exception ex) {
LOG.warn("Couldn't load breadcrumb model value", ex);
return null;
}
}

@Override
public void detach() {
Breadcrumb.this.icon = icon.getObject();
Breadcrumb.this.iconModel = null;
}

};
this.iconModel = new CachedModel(icon);
}

public boolean isUseLink() {
Expand All @@ -167,35 +109,10 @@ public RestartResponseException getRestartResponseException() {
throw new UnsupportedOperationException("Should be implemented in a subclass");
}

private <T extends Serializable> IModel<T> wrapModel(final IModel<T> model) {
if (model == null) {
return null;
}

return new IModel<T>() {

@Override
public T getObject() {
try {
return model.getObject();
} catch (Exception ex) {
LOG.warn("Couldn't load breadcrumb model value", ex);
return null;
}
}

@Override
public void detach() {
model.getObject();
}

};
}

@Override
public boolean equals(Object o) {
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }
if (this == o) {return true;}
if (o == null || getClass() != o.getClass()) {return false;}

//we don't compare label/icon models, we would need to compare models values
return true;
Expand All @@ -218,9 +135,7 @@ public String debugDump(int indent) {
sb.append(this.getClass().getSimpleName());
sb.append("\n");
DebugUtil.debugDumpWithLabelLn(sb, "labelModel", labelModel == null ? "" : labelModel.toString(), indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "label", label, indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "iconModel", iconModel == null ? "" : iconModel.toString(), indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "icon", icon, indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "useLink", useLink, indent + 1);
DebugUtil.debugDumpWithLabel(sb, "visible", visible, indent + 1);
extendsDebugDump(sb, indent);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2010-2022 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.evolveum.midpoint.web.component.breadcrumbs;

import org.apache.wicket.model.IModel;

/**
* Breadcrums text is not changing over time, we'll try to use this behavior to cache string value created by underlying IModel object.
* When value is cached, underlying model is forgotten to save space - we don't want to get value when creating cached model (too soon).
*
* Created by Viliam Repan (lazyman).
*/
public class CachedModel implements IModel<String> {

private String cachedValue;

private IModel<String> valueModel;

public CachedModel(String value) {
this.cachedValue = value;
}

public CachedModel(IModel<String> valueModel) {
this.valueModel = valueModel;
}

@Override
public String getObject() {
if (cachedValue != null) {
return cachedValue;
}

if (valueModel != null) {
cachedValue = valueModel.getObject();
valueModel = null;
}

return cachedValue;
}
}

0 comments on commit c5b6e13

Please sign in to comment.