Skip to content

Commit

Permalink
Model detaching for Info Dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
katkav committed Mar 15, 2022
1 parent f200665 commit 0bba55d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1633,7 +1633,7 @@ public void redirectBackToBreadcrumb(Breadcrumb breadcrumb) {
setResponsePage(page);
}

protected void setTimeZone(PageBase page) {
protected void setTimeZone() {
String timeZone = null;
GuiProfiledPrincipal principal = AuthUtil.getPrincipalUser();
if (principal != null && principal.getCompiledGuiProfile() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import javax.xml.namespace.QName;

import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;

import com.evolveum.midpoint.gui.api.GuiStyleConstants;
Expand All @@ -29,11 +30,16 @@
public abstract class PageDashboard extends PageAdminHome {
private static final long serialVersionUID = 1L;

private final Model<PrismObject<? extends FocusType>> principalModel = new Model<>();
private final LoadableDetachableModel<PrismObject<? extends FocusType>> principalModel;

public PageDashboard() {
principalModel.setObject(loadFocusSelf());
setTimeZone(PageDashboard.this);
principalModel = new LoadableDetachableModel<>() {
@Override
protected PrismObject<? extends FocusType> load() {
return loadFocusSelf();
}
};
setTimeZone();
}

@Override
Expand Down Expand Up @@ -155,38 +161,10 @@ protected <F extends FocusType> void customizationFocusInfoBoxType(InfoBoxType i
String icon, String keyPrefix, OperationResult result, Task task) {
}

protected String formatPercentage(int totalItems, int actualItems) {
float percentage = (totalItems==0 ? 0 : actualItems*100.0f/totalItems);
String format = "%.0f";

if(percentage < 100.0f && percentage % 10 != 0 && ((percentage % 10) % 1) != 0) {
format = "%.1f";
}
return String.format(format, percentage);
}

protected void customizationPercentageInfoBoxTypeModel(InfoBoxType infoBoxType, String bgColor, String icon, String keyPrefix,
int totalItems, int actualItems, boolean zeroIsGood) {
}

protected Model<InfoBoxType> getPercentageInfoBoxTypeModel(String bgColor, String icon, String keyPrefix,
int totalItems, int actualItems, boolean zeroIsGood) {
InfoBoxType infoBoxType = new InfoBoxType(bgColor, icon,
getString(keyPrefix +".label"));

infoBoxType.setNumber(formatPercentage(totalItems, actualItems) + " %" + " " + getString(keyPrefix + ".number"));

int progress = 0;
if (totalItems != 0) {
progress = actualItems * 100 / totalItems;
}
infoBoxType.setProgress(progress);

StringBuilder descSb = new StringBuilder();
descSb.append(totalItems).append(" ").append(getString(keyPrefix + ".total"));
infoBoxType.setDescription(descSb.toString());
customizationPercentageInfoBoxTypeModel(infoBoxType, bgColor, icon, keyPrefix, totalItems, actualItems, zeroIsGood);

return new Model<>(infoBoxType);
@Override
protected void onDetach() {
principalModel.detach();
super.onDetach();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;

import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;
Expand Down Expand Up @@ -56,7 +57,7 @@ protected void onInitialize() {

@Override
public IModel<PersonalInfoDto> createModel() {
return new LoadableModel<PersonalInfoDto>(false) {
return new LoadableDetachableModel<>() {

private static final long serialVersionUID = 1L;

Expand Down Expand Up @@ -99,7 +100,7 @@ private PersonalInfoDto loadPersonalInfo() {
}

protected void initLayout() {
DateLabelComponent lastLoginDate = new DateLabelComponent(ID_LAST_LOGIN_DATE, new IModel<Date>() {
DateLabelComponent lastLoginDate = new DateLabelComponent(ID_LAST_LOGIN_DATE, new IModel<>() {

private static final long serialVersionUID = 1L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public class PageSelfDashboard extends PageSelf {
private static final String OPERATION_LOAD_ASSIGNMENTS = DOT_CLASS + "loadAssignments";

public PageSelfDashboard() {
setTimeZone(PageSelfDashboard.this);
setTimeZone();
initLayout();
}

Expand Down

0 comments on commit 0bba55d

Please sign in to comment.